Use custom type for building Announce(Note) activity

This commit is contained in:
silverpill 2022-08-08 15:24:59 +00:00
parent 1721eb4a88
commit fd632b4816

View file

@ -1,41 +1,56 @@
use serde::Serialize;
use tokio_postgres::GenericClient;
use uuid::Uuid;
use crate::activitypub::{
activity::{create_activity, Activity},
actors::types::Actor,
constants::AP_PUBLIC,
constants::{AP_CONTEXT, AP_PUBLIC},
deliverer::OutgoingActivity,
identifiers::{local_actor_followers, local_object_id},
identifiers::{local_actor_followers, local_actor_id, local_object_id},
vocabulary::ANNOUNCE,
};
use crate::config::Instance;
use crate::errors::DatabaseError;
use crate::models::posts::types::Post;
use crate::models::profiles::types::DbActorProfile;
use crate::models::relationships::queries::get_followers;
use crate::models::users::types::User;
#[derive(Serialize)]
pub struct Announce {
#[serde(rename = "@context")]
context: String,
#[serde(rename = "type")]
activity_type: String,
actor: String,
id: String,
object: String,
to: Vec<String>,
cc: Vec<String>,
}
fn build_announce_note(
instance_url: &str,
actor_profile: &DbActorProfile,
sender_username: &str,
post: &Post,
repost_id: &Uuid,
) -> Activity {
) -> Announce {
let actor_id = local_actor_id(instance_url, sender_username);
let object_id = post.get_object_id(instance_url);
let activity_id = local_object_id(instance_url, repost_id);
let recipient_id = post.author.actor_id(instance_url);
let followers = local_actor_followers(instance_url, &actor_profile.username);
let activity = create_activity(
instance_url,
&actor_profile.username,
ANNOUNCE,
activity_id,
object_id,
vec![AP_PUBLIC.to_string(), recipient_id],
vec![followers],
);
activity
let followers = local_actor_followers(instance_url, sender_username);
Announce {
context: AP_CONTEXT.to_string(),
activity_type: ANNOUNCE.to_string(),
actor: actor_id,
id: activity_id,
object: object_id,
to: vec![AP_PUBLIC.to_string(), recipient_id],
cc: vec![followers],
}
}
pub async fn get_announce_note_recipients(
@ -64,7 +79,7 @@ pub async fn prepare_announce_note(
user: &User,
post: &Post,
repost_id: &Uuid,
) -> Result<OutgoingActivity<Activity>, DatabaseError> {
) -> Result<OutgoingActivity<Announce>, DatabaseError> {
assert_ne!(&post.id, repost_id);
let (recipients, _) = get_announce_note_recipients(
db_client,
@ -74,7 +89,7 @@ pub async fn prepare_announce_note(
).await?;
let activity = build_announce_note(
&instance.url(),
&user.profile,
&user.profile.username,
post,
repost_id,
);
@ -88,8 +103,8 @@ pub async fn prepare_announce_note(
#[cfg(test)]
mod tests {
use serde_json::json;
use crate::activitypub::actors::types::Actor;
use crate::models::profiles::types::DbActorProfile;
use crate::utils::id::new_uuid;
use super::*;
@ -116,7 +131,7 @@ mod tests {
let repost_id = new_uuid();
let activity = build_announce_note(
INSTANCE_URL,
&announcer,
&announcer.username,
&post,
&repost_id,
);
@ -125,6 +140,6 @@ mod tests {
format!("{}/objects/{}", INSTANCE_URL, repost_id),
);
assert_eq!(activity.object, post_id);
assert_eq!(activity.to.unwrap(), json!([AP_PUBLIC, post_author_id]));
assert_eq!(activity.to, vec![AP_PUBLIC, post_author_id]);
}
}