2022-12-06 20:31:35 +00:00
|
|
|
use serde::Serialize;
|
2022-07-14 08:11:21 +00:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
2023-04-25 13:49:35 +00:00
|
|
|
use fedimovies_config::Instance;
|
|
|
|
use fedimovies_models::{
|
2023-03-30 20:27:17 +00:00
|
|
|
database::{DatabaseClient, DatabaseError},
|
|
|
|
posts::types::{Post, Visibility},
|
|
|
|
profiles::types::{DbActor, DbActorProfile},
|
|
|
|
users::types::User,
|
|
|
|
};
|
2023-02-18 23:52:48 +00:00
|
|
|
|
2022-07-14 08:11:21 +00:00
|
|
|
use crate::activitypub::{
|
2023-01-28 22:35:46 +00:00
|
|
|
constants::AP_PUBLIC,
|
2022-07-14 08:11:21 +00:00
|
|
|
deliverer::OutgoingActivity,
|
2023-04-24 15:35:32 +00:00
|
|
|
identifiers::{local_actor_id, local_object_id, post_object_id, profile_actor_id},
|
2023-01-28 22:35:46 +00:00
|
|
|
types::{build_default_context, Context},
|
2022-07-14 08:11:21 +00:00
|
|
|
vocabulary::LIKE,
|
|
|
|
};
|
|
|
|
|
2022-12-06 20:31:35 +00:00
|
|
|
#[derive(Serialize)]
|
|
|
|
struct Like {
|
|
|
|
#[serde(rename = "@context")]
|
2023-01-28 22:35:46 +00:00
|
|
|
context: Context,
|
2022-12-06 20:31:35 +00:00
|
|
|
|
|
|
|
#[serde(rename = "type")]
|
|
|
|
activity_type: String,
|
|
|
|
|
|
|
|
id: String,
|
|
|
|
actor: String,
|
|
|
|
object: String,
|
|
|
|
|
|
|
|
to: Vec<String>,
|
|
|
|
cc: Vec<String>,
|
|
|
|
}
|
|
|
|
|
2022-12-06 23:29:59 +00:00
|
|
|
pub fn get_like_audience(
|
2022-07-28 22:27:50 +00:00
|
|
|
note_author_id: &str,
|
|
|
|
note_visibility: &Visibility,
|
|
|
|
) -> (Vec<String>, Vec<String>) {
|
|
|
|
let mut primary_audience = vec![note_author_id.to_string()];
|
|
|
|
if matches!(note_visibility, Visibility::Public) {
|
|
|
|
primary_audience.push(AP_PUBLIC.to_string());
|
|
|
|
};
|
|
|
|
let secondary_audience = vec![];
|
|
|
|
(primary_audience, secondary_audience)
|
|
|
|
}
|
|
|
|
|
2022-12-06 23:29:59 +00:00
|
|
|
fn build_like(
|
2022-07-14 08:11:21 +00:00
|
|
|
instance_url: &str,
|
|
|
|
actor_profile: &DbActorProfile,
|
2022-12-06 23:29:59 +00:00
|
|
|
object_id: &str,
|
2022-07-14 08:11:21 +00:00
|
|
|
reaction_id: &Uuid,
|
2022-12-06 23:29:59 +00:00
|
|
|
post_author_id: &str,
|
|
|
|
post_visibility: &Visibility,
|
2022-12-06 20:31:35 +00:00
|
|
|
) -> Like {
|
2022-07-15 17:31:02 +00:00
|
|
|
let activity_id = local_object_id(instance_url, reaction_id);
|
2022-12-06 20:31:35 +00:00
|
|
|
let actor_id = local_actor_id(instance_url, &actor_profile.username);
|
2023-04-24 15:35:32 +00:00
|
|
|
let (primary_audience, secondary_audience) = get_like_audience(post_author_id, post_visibility);
|
2022-12-06 20:31:35 +00:00
|
|
|
Like {
|
2023-01-28 22:35:46 +00:00
|
|
|
context: build_default_context(),
|
2022-12-06 20:31:35 +00:00
|
|
|
activity_type: LIKE.to_string(),
|
|
|
|
id: activity_id,
|
|
|
|
actor: actor_id,
|
2022-12-06 23:29:59 +00:00
|
|
|
object: object_id.to_string(),
|
2022-12-06 20:31:35 +00:00
|
|
|
to: primary_audience,
|
|
|
|
cc: secondary_audience,
|
|
|
|
}
|
2022-07-14 08:11:21 +00:00
|
|
|
}
|
|
|
|
|
2022-12-06 23:29:59 +00:00
|
|
|
pub async fn get_like_recipients(
|
2023-01-17 23:14:18 +00:00
|
|
|
_db_client: &impl DatabaseClient,
|
2022-07-28 22:27:50 +00:00
|
|
|
_instance_url: &str,
|
2022-07-14 12:02:01 +00:00
|
|
|
post: &Post,
|
2023-03-16 20:09:13 +00:00
|
|
|
) -> Result<Vec<DbActor>, DatabaseError> {
|
|
|
|
let mut recipients = vec![];
|
2022-07-14 12:02:01 +00:00
|
|
|
if let Some(remote_actor) = post.author.actor_json.as_ref() {
|
|
|
|
recipients.push(remote_actor.clone());
|
|
|
|
};
|
2022-07-28 22:27:50 +00:00
|
|
|
Ok(recipients)
|
2022-07-14 12:02:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-06 23:29:59 +00:00
|
|
|
pub async fn prepare_like(
|
2023-01-17 23:14:18 +00:00
|
|
|
db_client: &impl DatabaseClient,
|
2022-10-22 00:15:28 +00:00
|
|
|
instance: &Instance,
|
|
|
|
sender: &User,
|
2022-07-14 08:11:21 +00:00
|
|
|
post: &Post,
|
|
|
|
reaction_id: &Uuid,
|
2022-12-04 22:31:02 +00:00
|
|
|
) -> Result<OutgoingActivity, DatabaseError> {
|
2023-04-24 15:35:32 +00:00
|
|
|
let recipients = get_like_recipients(db_client, &instance.url(), post).await?;
|
2023-03-18 13:20:09 +00:00
|
|
|
let object_id = post_object_id(&instance.url(), post);
|
2023-03-18 17:25:52 +00:00
|
|
|
let post_author_id = profile_actor_id(&instance.url(), &post.author);
|
2022-12-06 23:29:59 +00:00
|
|
|
let activity = build_like(
|
2022-07-14 08:11:21 +00:00
|
|
|
&instance.url(),
|
2022-10-22 00:15:28 +00:00
|
|
|
&sender.profile,
|
2022-12-06 23:29:59 +00:00
|
|
|
&object_id,
|
2022-07-14 08:11:21 +00:00
|
|
|
reaction_id,
|
2022-12-06 23:29:59 +00:00
|
|
|
&post_author_id,
|
2022-07-28 22:27:50 +00:00
|
|
|
&post.visibility,
|
2022-07-14 08:11:21 +00:00
|
|
|
);
|
2022-12-04 22:31:02 +00:00
|
|
|
Ok(OutgoingActivity::new(
|
2023-04-24 15:35:32 +00:00
|
|
|
instance, sender, activity, recipients,
|
2022-12-04 22:31:02 +00:00
|
|
|
))
|
2022-07-14 08:11:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2023-04-25 13:49:35 +00:00
|
|
|
use fedimovies_utils::id::generate_ulid;
|
2022-07-14 08:11:21 +00:00
|
|
|
|
|
|
|
const INSTANCE_URL: &str = "https://example.com";
|
|
|
|
|
|
|
|
#[test]
|
2022-12-06 23:29:59 +00:00
|
|
|
fn test_build_like() {
|
2022-07-14 08:11:21 +00:00
|
|
|
let author = DbActorProfile::default();
|
2022-12-06 23:29:59 +00:00
|
|
|
let post_id = "https://example.com/objects/123";
|
|
|
|
let post_author_id = "https://example.com/users/test";
|
2023-02-12 22:04:31 +00:00
|
|
|
let reaction_id = generate_ulid();
|
2022-12-06 23:29:59 +00:00
|
|
|
let activity = build_like(
|
2022-07-14 08:11:21 +00:00
|
|
|
INSTANCE_URL,
|
|
|
|
&author,
|
2022-12-06 23:29:59 +00:00
|
|
|
post_id,
|
2022-07-14 08:11:21 +00:00
|
|
|
&reaction_id,
|
2022-12-06 23:29:59 +00:00
|
|
|
post_author_id,
|
2022-07-28 22:27:50 +00:00
|
|
|
&Visibility::Public,
|
2022-07-14 08:11:21 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
activity.id,
|
|
|
|
format!("{}/objects/{}", INSTANCE_URL, reaction_id),
|
|
|
|
);
|
2022-12-06 23:29:59 +00:00
|
|
|
assert_eq!(activity.object, post_id);
|
|
|
|
assert_eq!(activity.to, vec![post_author_id, AP_PUBLIC]);
|
2023-04-27 22:01:24 +00:00
|
|
|
assert!(activity.cc.is_empty());
|
2022-07-14 08:11:21 +00:00
|
|
|
}
|
|
|
|
}
|