Don't send direct messages to followers

This commit is contained in:
silverpill 2022-01-15 14:53:42 +00:00
parent 572ea53827
commit eed648f140
2 changed files with 19 additions and 14 deletions

View file

@ -3,22 +3,27 @@ use tokio_postgres::GenericClient;
use crate::activitypub::actor::Actor;
use crate::errors::DatabaseError;
use crate::models::posts::queries::get_post_author;
use crate::models::posts::types::Post;
use crate::models::posts::types::{Post, Visibility};
use crate::models::relationships::queries::get_followers;
use crate::models::users::types::User;
pub async fn get_note_audience(
pub async fn get_note_recipients(
db_client: &impl GenericClient,
current_user: &User,
post: &Post,
) -> Result<Vec<Actor>, DatabaseError> {
let mut audience = get_followers(db_client, &current_user.id, None, None).await?;
let mut audience = vec![];
if matches!(post.visibility, Visibility::Public) {
let followers = get_followers(db_client, &current_user.id, None, None).await?;
audience.extend(followers);
};
if let Some(in_reply_to_id) = post.in_reply_to_id {
// TODO: use post.in_reply_to ?
let in_reply_to_author = get_post_author(db_client, &in_reply_to_id).await?;
audience.push(in_reply_to_author);
};
audience.extend(post.mentions.clone());
let mut recipients: Vec<Actor> = Vec::new();
for profile in audience {
if let Some(remote_actor) = profile.actor_json {
@ -33,7 +38,7 @@ pub struct Audience {
pub primary_recipient: String,
}
pub async fn get_like_audience(
pub async fn get_like_recipients(
_db_client: &impl GenericClient,
instance_url: &str,
post: &Post,
@ -46,7 +51,7 @@ pub async fn get_like_audience(
Ok(Audience { recipients, primary_recipient })
}
pub async fn get_announce_audience(
pub async fn get_announce_recipients(
db_client: &impl GenericClient,
instance_url: &str,
current_user: &User,

View file

@ -44,9 +44,9 @@ use crate::models::reactions::queries::{
delete_reaction,
};
use super::helpers::{
get_announce_audience,
get_like_audience,
get_note_audience,
get_announce_recipients,
get_like_recipients,
get_note_recipients,
Audience,
};
use super::types::{Status, StatusData, TransactionData};
@ -119,7 +119,7 @@ async fn create_status(
&instance.url(),
&post,
);
let recipients = get_note_audience(db_client, &current_user, &post).await?;
let recipients = get_note_recipients(db_client, &current_user, &post).await?;
deliver_activity(&config, &current_user, activity, recipients);
let status = Status::from_post(post, &instance.url());
Ok(HttpResponse::Created().json(status))
@ -172,7 +172,7 @@ async fn delete_status(
&config.instance_url(),
&post,
);
let recipients = get_note_audience(db_client, &current_user, &post).await?;
let recipients = get_note_recipients(db_client, &current_user, &post).await?;
deliver_activity(&config, &current_user, activity, recipients);
Ok(HttpResponse::NoContent().finish())
@ -239,7 +239,7 @@ async fn favourite(
if let Some(reaction) = maybe_reaction_created {
// Federate
let Audience { recipients, primary_recipient } =
get_like_audience(db_client, &config.instance_url(), &post).await?;
get_like_recipients(db_client, &config.instance_url(), &post).await?;
let note_id = post.get_object_id(&config.instance_url());
let activity = create_activity_like(
&config.instance_url(),
@ -281,7 +281,7 @@ async fn unfavourite(
if let Some(reaction_id) = maybe_reaction_deleted {
// Federate
let Audience { recipients, primary_recipient } =
get_like_audience(db_client, &config.instance_url(), &post).await?;
get_like_recipients(db_client, &config.instance_url(), &post).await?;
let activity = create_activity_undo_like(
&config.instance_url(),
&current_user.profile,
@ -319,7 +319,7 @@ async fn reblog(
// Federate
let Audience { recipients, .. } =
get_announce_audience(db_client, &config.instance_url(), &current_user, &post).await?;
get_announce_recipients(db_client, &config.instance_url(), &current_user, &post).await?;
let activity = create_activity_announce(
&config.instance_url(),
&current_user.profile,
@ -351,7 +351,7 @@ async fn unreblog(
// Federate
let Audience { recipients, primary_recipient } =
get_announce_audience(db_client, &config.instance_url(), &current_user, &post).await?;
get_announce_recipients(db_client, &config.instance_url(), &current_user, &post).await?;
let activity = create_activity_undo_announce(
&config.instance_url(),
&current_user.profile,