Identify incoming followers-only posts

This commit is contained in:
silverpill 2022-02-13 17:55:35 +00:00
parent 50630c004a
commit 9bc8997fb5
2 changed files with 41 additions and 16 deletions

View file

@ -3,7 +3,6 @@ use crate::models::posts::types::Visibility;
use crate::models::profiles::types::DbActorProfile;
pub fn get_note_visibility(
note_id: &str,
author: &DbActorProfile,
primary_audience: Vec<String>,
secondary_audience: Vec<String>,
@ -12,30 +11,32 @@ pub fn get_note_visibility(
secondary_audience.contains(&AP_PUBLIC.to_string()) {
Visibility::Public
} else {
// Treat all notes that aren't public-addressed as direct messages
log::warn!(
"processing non-public note {} attributed to {}; primary audience {:?}; secondary audience {:?}",
note_id,
author.username,
primary_audience,
secondary_audience,
);
let maybe_followers = author.actor_json.as_ref()
.and_then(|actor| actor.followers.as_ref());
if let Some(followers) = maybe_followers {
if primary_audience.contains(&followers) ||
secondary_audience.contains(&followers) {
Visibility::Followers
} else {
Visibility::Direct
}
} else {
Visibility::Direct
}
}
}
#[cfg(test)]
mod tests {
use crate::activitypub::actor::Actor;
use super::*;
#[test]
fn test_get_note_visibility_public() {
let object_id = "https://example.com/test";
let author = DbActorProfile::default();
let primary_audience = vec![AP_PUBLIC.to_string()];
let secondary_audience = vec![];
let visibility = get_note_visibility(
object_id,
&author,
primary_audience,
secondary_audience,
@ -43,14 +44,32 @@ mod tests {
assert_eq!(visibility, Visibility::Public);
}
#[test]
fn test_get_note_visibility_followers() {
let author_followers = "https://example.com/users/author/followers";
let author = DbActorProfile {
actor_json: Some(Actor {
followers: Some(author_followers.to_string()),
..Default::default()
}),
..Default::default()
};
let primary_audience = vec![author_followers.to_string()];
let secondary_audience = vec![];
let visibility = get_note_visibility(
&author,
primary_audience,
secondary_audience,
);
assert_eq!(visibility, Visibility::Followers);
}
#[test]
fn test_get_note_visibility_direct() {
let object_id = "https://example.com/test";
let author = DbActorProfile::default();
let primary_audience = vec!["https://example.com/users/1".to_string()];
let secondary_audience = vec![];
let visibility = get_note_visibility(
object_id,
&author,
primary_audience,
secondary_audience,

View file

@ -17,7 +17,7 @@ use crate::models::posts::queries::{
delete_post,
};
use crate::models::posts::tags::normalize_tag;
use crate::models::posts::types::{Post, PostCreateData};
use crate::models::posts::types::{Post, PostCreateData, Visibility};
use crate::models::profiles::queries::{
get_profile_by_actor_id,
get_profile_by_acct,
@ -379,11 +379,17 @@ pub async fn process_note(
None => vec![],
};
let visibility = get_note_visibility(
&object.id,
&author,
primary_audience,
secondary_audience,
);
if visibility != Visibility::Public {
log::warn!(
"processing note with visibility {:?} attributed to {}",
visibility,
author.username,
);
};
let post_data = PostCreateData {
content: content_cleaned,
in_reply_to_id,