2022-07-14 08:11:21 +00:00
|
|
|
use tokio_postgres::GenericClient;
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
use crate::activitypub::{
|
|
|
|
activity::{create_activity, Activity},
|
2022-07-23 21:37:21 +00:00
|
|
|
actors::types::Actor,
|
2022-07-14 08:11:21 +00:00
|
|
|
constants::AP_PUBLIC,
|
|
|
|
deliverer::OutgoingActivity,
|
2022-07-15 17:31:02 +00:00
|
|
|
identifiers::local_object_id,
|
2022-07-14 08:11:21 +00:00
|
|
|
vocabulary::LIKE,
|
|
|
|
};
|
|
|
|
use crate::config::Instance;
|
|
|
|
use crate::errors::DatabaseError;
|
2022-07-20 21:53:40 +00:00
|
|
|
use crate::models::posts::types::{Post, Visibility};
|
2022-07-14 08:11:21 +00:00
|
|
|
use crate::models::profiles::types::DbActorProfile;
|
|
|
|
use crate::models::users::types::User;
|
|
|
|
|
|
|
|
fn build_like_note(
|
|
|
|
instance_url: &str,
|
|
|
|
actor_profile: &DbActorProfile,
|
|
|
|
note_id: &str,
|
|
|
|
reaction_id: &Uuid,
|
|
|
|
recipient_id: &str,
|
2022-07-20 21:53:40 +00:00
|
|
|
is_public: bool,
|
2022-07-14 08:11:21 +00:00
|
|
|
) -> Activity {
|
2022-07-15 17:31:02 +00:00
|
|
|
let activity_id = local_object_id(instance_url, reaction_id);
|
2022-07-20 21:53:40 +00:00
|
|
|
let mut primary_audience = vec![recipient_id.to_string()];
|
|
|
|
if is_public {
|
|
|
|
primary_audience.push(AP_PUBLIC.to_string());
|
|
|
|
};
|
2022-07-14 08:11:21 +00:00
|
|
|
let activity = create_activity(
|
|
|
|
instance_url,
|
|
|
|
&actor_profile.username,
|
|
|
|
LIKE,
|
|
|
|
activity_id,
|
|
|
|
note_id,
|
2022-07-20 21:53:40 +00:00
|
|
|
primary_audience,
|
2022-07-14 08:11:21 +00:00
|
|
|
vec![],
|
|
|
|
);
|
|
|
|
activity
|
|
|
|
}
|
|
|
|
|
2022-07-14 12:02:01 +00:00
|
|
|
pub async fn get_like_note_recipients(
|
|
|
|
_db_client: &impl GenericClient,
|
|
|
|
instance_url: &str,
|
|
|
|
post: &Post,
|
|
|
|
) -> Result<(Vec<Actor>, String), DatabaseError> {
|
|
|
|
let mut recipients: Vec<Actor> = Vec::new();
|
|
|
|
let primary_recipient = post.author.actor_id(instance_url);
|
|
|
|
if let Some(remote_actor) = post.author.actor_json.as_ref() {
|
|
|
|
recipients.push(remote_actor.clone());
|
|
|
|
};
|
|
|
|
Ok((recipients, primary_recipient))
|
|
|
|
}
|
|
|
|
|
2022-07-14 08:11:21 +00:00
|
|
|
pub async fn prepare_like_note(
|
|
|
|
db_client: &impl GenericClient,
|
|
|
|
instance: Instance,
|
|
|
|
user: &User,
|
|
|
|
post: &Post,
|
|
|
|
reaction_id: &Uuid,
|
2022-07-13 23:40:33 +00:00
|
|
|
) -> Result<OutgoingActivity<Activity>, DatabaseError> {
|
2022-07-14 12:02:01 +00:00
|
|
|
let (recipients, primary_recipient) = get_like_note_recipients(
|
|
|
|
db_client,
|
|
|
|
&instance.url(),
|
|
|
|
post,
|
|
|
|
).await?;
|
2022-07-14 08:11:21 +00:00
|
|
|
let note_id = post.get_object_id(&instance.url());
|
|
|
|
let activity = build_like_note(
|
|
|
|
&instance.url(),
|
|
|
|
&user.profile,
|
|
|
|
¬e_id,
|
|
|
|
reaction_id,
|
|
|
|
&primary_recipient,
|
2022-07-20 21:53:40 +00:00
|
|
|
matches!(post.visibility, Visibility::Public),
|
2022-07-14 08:11:21 +00:00
|
|
|
);
|
|
|
|
Ok(OutgoingActivity {
|
|
|
|
instance,
|
|
|
|
sender: user.clone(),
|
|
|
|
activity,
|
|
|
|
recipients,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use serde_json::json;
|
|
|
|
use crate::utils::id::new_uuid;
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
const INSTANCE_URL: &str = "https://example.com";
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_build_like_note() {
|
|
|
|
let author = DbActorProfile::default();
|
|
|
|
let note_id = "https://example.com/objects/123";
|
|
|
|
let note_author_id = "https://example.com/users/test";
|
|
|
|
let reaction_id = new_uuid();
|
|
|
|
let activity = build_like_note(
|
|
|
|
INSTANCE_URL,
|
|
|
|
&author,
|
|
|
|
note_id,
|
|
|
|
&reaction_id,
|
|
|
|
note_author_id,
|
2022-07-20 21:53:40 +00:00
|
|
|
true,
|
2022-07-14 08:11:21 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
activity.id,
|
|
|
|
format!("{}/objects/{}", INSTANCE_URL, reaction_id),
|
|
|
|
);
|
|
|
|
assert_eq!(activity.object, json!(note_id));
|
2022-07-20 21:53:40 +00:00
|
|
|
assert_eq!(activity.to.unwrap(), json!([note_author_id, AP_PUBLIC]));
|
2022-07-14 08:11:21 +00:00
|
|
|
}
|
|
|
|
}
|