Use custom type for building Announce(Note) activity
This commit is contained in:
parent
1721eb4a88
commit
fd632b4816
1 changed files with 37 additions and 22 deletions
|
@ -1,41 +1,56 @@
|
||||||
|
use serde::Serialize;
|
||||||
use tokio_postgres::GenericClient;
|
use tokio_postgres::GenericClient;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::activitypub::{
|
use crate::activitypub::{
|
||||||
activity::{create_activity, Activity},
|
|
||||||
actors::types::Actor,
|
actors::types::Actor,
|
||||||
constants::AP_PUBLIC,
|
constants::{AP_CONTEXT, AP_PUBLIC},
|
||||||
deliverer::OutgoingActivity,
|
deliverer::OutgoingActivity,
|
||||||
identifiers::{local_actor_followers, local_object_id},
|
identifiers::{local_actor_followers, local_actor_id, local_object_id},
|
||||||
vocabulary::ANNOUNCE,
|
vocabulary::ANNOUNCE,
|
||||||
};
|
};
|
||||||
use crate::config::Instance;
|
use crate::config::Instance;
|
||||||
use crate::errors::DatabaseError;
|
use crate::errors::DatabaseError;
|
||||||
use crate::models::posts::types::Post;
|
use crate::models::posts::types::Post;
|
||||||
use crate::models::profiles::types::DbActorProfile;
|
|
||||||
use crate::models::relationships::queries::get_followers;
|
use crate::models::relationships::queries::get_followers;
|
||||||
use crate::models::users::types::User;
|
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(
|
fn build_announce_note(
|
||||||
instance_url: &str,
|
instance_url: &str,
|
||||||
actor_profile: &DbActorProfile,
|
sender_username: &str,
|
||||||
post: &Post,
|
post: &Post,
|
||||||
repost_id: &Uuid,
|
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 object_id = post.get_object_id(instance_url);
|
||||||
let activity_id = local_object_id(instance_url, repost_id);
|
let activity_id = local_object_id(instance_url, repost_id);
|
||||||
let recipient_id = post.author.actor_id(instance_url);
|
let recipient_id = post.author.actor_id(instance_url);
|
||||||
let followers = local_actor_followers(instance_url, &actor_profile.username);
|
let followers = local_actor_followers(instance_url, sender_username);
|
||||||
let activity = create_activity(
|
Announce {
|
||||||
instance_url,
|
context: AP_CONTEXT.to_string(),
|
||||||
&actor_profile.username,
|
activity_type: ANNOUNCE.to_string(),
|
||||||
ANNOUNCE,
|
actor: actor_id,
|
||||||
activity_id,
|
id: activity_id,
|
||||||
object_id,
|
object: object_id,
|
||||||
vec![AP_PUBLIC.to_string(), recipient_id],
|
to: vec![AP_PUBLIC.to_string(), recipient_id],
|
||||||
vec![followers],
|
cc: vec![followers],
|
||||||
);
|
}
|
||||||
activity
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_announce_note_recipients(
|
pub async fn get_announce_note_recipients(
|
||||||
|
@ -64,7 +79,7 @@ pub async fn prepare_announce_note(
|
||||||
user: &User,
|
user: &User,
|
||||||
post: &Post,
|
post: &Post,
|
||||||
repost_id: &Uuid,
|
repost_id: &Uuid,
|
||||||
) -> Result<OutgoingActivity<Activity>, DatabaseError> {
|
) -> Result<OutgoingActivity<Announce>, DatabaseError> {
|
||||||
assert_ne!(&post.id, repost_id);
|
assert_ne!(&post.id, repost_id);
|
||||||
let (recipients, _) = get_announce_note_recipients(
|
let (recipients, _) = get_announce_note_recipients(
|
||||||
db_client,
|
db_client,
|
||||||
|
@ -74,7 +89,7 @@ pub async fn prepare_announce_note(
|
||||||
).await?;
|
).await?;
|
||||||
let activity = build_announce_note(
|
let activity = build_announce_note(
|
||||||
&instance.url(),
|
&instance.url(),
|
||||||
&user.profile,
|
&user.profile.username,
|
||||||
post,
|
post,
|
||||||
repost_id,
|
repost_id,
|
||||||
);
|
);
|
||||||
|
@ -88,8 +103,8 @@ pub async fn prepare_announce_note(
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use serde_json::json;
|
|
||||||
use crate::activitypub::actors::types::Actor;
|
use crate::activitypub::actors::types::Actor;
|
||||||
|
use crate::models::profiles::types::DbActorProfile;
|
||||||
use crate::utils::id::new_uuid;
|
use crate::utils::id::new_uuid;
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
@ -116,7 +131,7 @@ mod tests {
|
||||||
let repost_id = new_uuid();
|
let repost_id = new_uuid();
|
||||||
let activity = build_announce_note(
|
let activity = build_announce_note(
|
||||||
INSTANCE_URL,
|
INSTANCE_URL,
|
||||||
&announcer,
|
&announcer.username,
|
||||||
&post,
|
&post,
|
||||||
&repost_id,
|
&repost_id,
|
||||||
);
|
);
|
||||||
|
@ -125,6 +140,6 @@ mod tests {
|
||||||
format!("{}/objects/{}", INSTANCE_URL, repost_id),
|
format!("{}/objects/{}", INSTANCE_URL, repost_id),
|
||||||
);
|
);
|
||||||
assert_eq!(activity.object, post_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]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue