2021-04-09 00:22:17 +00:00
|
|
|
use chrono::{DateTime, Utc};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use serde_json::{json, Value};
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
2022-06-15 11:10:41 +00:00
|
|
|
use crate::models::posts::types::Post;
|
2021-04-09 00:22:17 +00:00
|
|
|
use crate::models::profiles::types::DbActorProfile;
|
2021-12-01 23:26:59 +00:00
|
|
|
use crate::utils::id::new_uuid;
|
2021-04-09 00:22:17 +00:00
|
|
|
use super::constants::{AP_CONTEXT, AP_PUBLIC};
|
2022-01-15 23:21:13 +00:00
|
|
|
use super::views::{
|
|
|
|
get_actor_url,
|
|
|
|
get_followers_url,
|
|
|
|
get_object_url,
|
|
|
|
};
|
2021-04-09 00:22:17 +00:00
|
|
|
use super::vocabulary::*;
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct Attachment {
|
2021-11-18 16:50:42 +00:00
|
|
|
pub name: Option<String>,
|
2021-04-09 00:22:17 +00:00
|
|
|
|
|
|
|
#[serde(rename = "type")]
|
|
|
|
pub attachment_type: String,
|
|
|
|
|
2021-12-29 13:57:57 +00:00
|
|
|
pub media_type: Option<String>,
|
2022-01-08 21:16:12 +00:00
|
|
|
pub url: Option<String>,
|
2021-04-09 00:22:17 +00:00
|
|
|
}
|
|
|
|
|
2022-02-15 23:14:39 +00:00
|
|
|
fn default_tag_type() -> String { HASHTAG.to_string() }
|
|
|
|
|
2021-11-11 21:51:47 +00:00
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct Tag {
|
2022-04-27 09:53:52 +00:00
|
|
|
pub name: Option<String>,
|
2021-11-11 21:51:47 +00:00
|
|
|
|
2022-02-15 23:14:39 +00:00
|
|
|
#[serde(rename = "type", default = "default_tag_type")]
|
2021-11-11 21:51:47 +00:00
|
|
|
pub tag_type: String,
|
|
|
|
|
2021-11-24 19:00:36 +00:00
|
|
|
pub href: Option<String>,
|
2021-11-11 21:51:47 +00:00
|
|
|
}
|
|
|
|
|
2021-10-29 16:33:50 +00:00
|
|
|
#[derive(Default, Deserialize, Serialize)]
|
2021-04-09 00:22:17 +00:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct Object {
|
|
|
|
#[serde(rename = "@context")]
|
|
|
|
pub context: Option<Value>,
|
|
|
|
|
|
|
|
pub id: String,
|
|
|
|
|
|
|
|
#[serde(rename = "type")]
|
|
|
|
pub object_type: String,
|
|
|
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2022-04-29 13:15:16 +00:00
|
|
|
pub name: Option<String>,
|
|
|
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2021-04-09 00:22:17 +00:00
|
|
|
pub actor: Option<String>,
|
|
|
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2022-04-13 22:30:41 +00:00
|
|
|
pub attachment: Option<Value>,
|
2021-04-09 00:22:17 +00:00
|
|
|
|
2022-01-04 20:52:41 +00:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub cc: Option<Value>,
|
|
|
|
|
2021-12-18 23:20:25 +00:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub former_type: Option<String>,
|
|
|
|
|
2021-04-09 00:22:17 +00:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub object: Option<String>,
|
|
|
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub published: Option<DateTime<Utc>>,
|
|
|
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2021-12-24 15:13:25 +00:00
|
|
|
pub attributed_to: Option<Value>,
|
2021-04-09 00:22:17 +00:00
|
|
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub in_reply_to: Option<String>,
|
|
|
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub content: Option<String>,
|
|
|
|
|
2021-11-11 21:51:47 +00:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub tag: Option<Vec<Tag>>,
|
|
|
|
|
2021-04-09 00:22:17 +00:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub to: Option<Value>,
|
2022-05-11 12:50:36 +00:00
|
|
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub updated: Option<DateTime<Utc>>,
|
2021-04-09 00:22:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct Activity {
|
|
|
|
#[serde(rename = "@context")]
|
|
|
|
pub context: Value,
|
|
|
|
|
|
|
|
pub id: String,
|
|
|
|
|
|
|
|
#[serde(rename = "type")]
|
|
|
|
pub activity_type: String,
|
|
|
|
|
|
|
|
pub actor: String,
|
|
|
|
pub object: Value,
|
2021-11-20 21:15:56 +00:00
|
|
|
pub to: Option<Value>,
|
2022-01-04 20:52:41 +00:00
|
|
|
pub cc: Option<Value>,
|
2021-04-09 00:22:17 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 21:39:23 +00:00
|
|
|
pub fn create_activity(
|
2021-04-09 00:22:17 +00:00
|
|
|
instance_url: &str,
|
|
|
|
actor_name: &str,
|
|
|
|
activity_type: &str,
|
2022-04-14 18:05:10 +00:00
|
|
|
activity_id: String,
|
2021-11-12 21:33:19 +00:00
|
|
|
object: impl Serialize,
|
2022-01-04 20:52:41 +00:00
|
|
|
primary_audience: Vec<String>,
|
|
|
|
secondary_audience: Vec<String>,
|
2021-04-09 00:22:17 +00:00
|
|
|
) -> Activity {
|
|
|
|
let actor_id = get_actor_url(
|
|
|
|
instance_url,
|
2021-11-13 17:37:31 +00:00
|
|
|
actor_name,
|
2021-04-09 00:22:17 +00:00
|
|
|
);
|
2021-11-13 17:37:31 +00:00
|
|
|
Activity {
|
2021-04-09 00:22:17 +00:00
|
|
|
context: json!(AP_CONTEXT),
|
|
|
|
id: activity_id,
|
|
|
|
activity_type: activity_type.to_string(),
|
|
|
|
actor: actor_id,
|
2021-11-12 21:33:19 +00:00
|
|
|
object: serde_json::to_value(object).unwrap(),
|
2022-01-04 20:52:41 +00:00
|
|
|
to: Some(json!(primary_audience)),
|
|
|
|
cc: Some(json!(secondary_audience)),
|
2021-11-13 17:37:31 +00:00
|
|
|
}
|
2021-04-09 00:22:17 +00:00
|
|
|
}
|
|
|
|
|
2021-10-29 19:21:26 +00:00
|
|
|
pub fn create_activity_like(
|
|
|
|
instance_url: &str,
|
|
|
|
actor_profile: &DbActorProfile,
|
2022-05-13 01:28:39 +00:00
|
|
|
object_id: &str,
|
2021-12-15 01:00:42 +00:00
|
|
|
reaction_id: &Uuid,
|
2021-12-25 00:05:17 +00:00
|
|
|
recipient_id: &str,
|
2021-10-29 19:21:26 +00:00
|
|
|
) -> Activity {
|
2022-04-14 18:05:10 +00:00
|
|
|
let activity_id = get_object_url(instance_url, reaction_id);
|
2021-10-29 19:21:26 +00:00
|
|
|
let activity = create_activity(
|
|
|
|
instance_url,
|
|
|
|
&actor_profile.username,
|
|
|
|
LIKE,
|
2022-04-14 18:05:10 +00:00
|
|
|
activity_id,
|
2022-05-06 15:35:02 +00:00
|
|
|
object_id,
|
2021-12-25 00:05:17 +00:00
|
|
|
vec![AP_PUBLIC.to_string(), recipient_id.to_string()],
|
2022-01-04 20:52:41 +00:00
|
|
|
vec![],
|
2021-10-29 19:21:26 +00:00
|
|
|
);
|
|
|
|
activity
|
|
|
|
}
|
|
|
|
|
2021-12-15 01:00:42 +00:00
|
|
|
pub fn create_activity_undo_like(
|
|
|
|
instance_url: &str,
|
|
|
|
actor_profile: &DbActorProfile,
|
|
|
|
reaction_id: &Uuid,
|
2021-12-25 00:05:17 +00:00
|
|
|
recipient_id: &str,
|
2021-12-15 01:00:42 +00:00
|
|
|
) -> Activity {
|
|
|
|
let object_id = get_object_url(
|
|
|
|
instance_url,
|
|
|
|
reaction_id,
|
|
|
|
);
|
2022-05-06 15:35:02 +00:00
|
|
|
let activity_id = format!("{}/undo", object_id);
|
2021-12-15 01:00:42 +00:00
|
|
|
create_activity(
|
|
|
|
instance_url,
|
|
|
|
&actor_profile.username,
|
|
|
|
UNDO,
|
2022-04-14 18:05:10 +00:00
|
|
|
activity_id,
|
2021-12-15 01:00:42 +00:00
|
|
|
object_id,
|
2021-12-25 00:05:17 +00:00
|
|
|
vec![AP_PUBLIC.to_string(), recipient_id.to_string()],
|
2022-01-04 20:52:41 +00:00
|
|
|
vec![],
|
2021-12-15 01:00:42 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-11-25 21:28:06 +00:00
|
|
|
pub fn create_activity_announce(
|
|
|
|
instance_url: &str,
|
|
|
|
actor_profile: &DbActorProfile,
|
2021-11-26 22:53:40 +00:00
|
|
|
post: &Post,
|
2021-12-16 17:32:49 +00:00
|
|
|
repost_id: &Uuid,
|
2021-11-25 21:28:06 +00:00
|
|
|
) -> Activity {
|
2021-11-26 22:53:40 +00:00
|
|
|
let object_id = post.get_object_id(instance_url);
|
2022-04-14 18:05:10 +00:00
|
|
|
let activity_id = get_object_url(instance_url, repost_id);
|
2021-12-31 15:29:44 +00:00
|
|
|
let recipient_id = post.author.actor_id(instance_url);
|
2021-11-25 21:28:06 +00:00
|
|
|
let activity = create_activity(
|
|
|
|
instance_url,
|
|
|
|
&actor_profile.username,
|
|
|
|
ANNOUNCE,
|
2022-04-14 18:05:10 +00:00
|
|
|
activity_id,
|
2021-12-12 22:36:20 +00:00
|
|
|
object_id,
|
2021-12-25 00:05:17 +00:00
|
|
|
vec![AP_PUBLIC.to_string(), recipient_id],
|
2022-01-04 21:36:45 +00:00
|
|
|
vec![get_followers_url(instance_url, &actor_profile.username)],
|
2021-11-25 21:28:06 +00:00
|
|
|
);
|
|
|
|
activity
|
|
|
|
}
|
|
|
|
|
2021-12-16 17:32:49 +00:00
|
|
|
pub fn create_activity_undo_announce(
|
|
|
|
instance_url: &str,
|
|
|
|
actor_profile: &DbActorProfile,
|
|
|
|
repost_id: &Uuid,
|
2022-01-04 21:36:45 +00:00
|
|
|
recipient_id: &str,
|
2021-12-16 17:32:49 +00:00
|
|
|
) -> Activity {
|
|
|
|
let object_id = get_object_url(
|
|
|
|
instance_url,
|
|
|
|
repost_id,
|
|
|
|
);
|
2022-05-06 15:35:02 +00:00
|
|
|
let activity_id = format!("{}/undo", object_id);
|
2022-01-04 21:36:45 +00:00
|
|
|
let primary_audience = vec![
|
|
|
|
AP_PUBLIC.to_string(),
|
|
|
|
recipient_id.to_string(),
|
|
|
|
];
|
2021-12-16 17:32:49 +00:00
|
|
|
create_activity(
|
|
|
|
instance_url,
|
|
|
|
&actor_profile.username,
|
|
|
|
UNDO,
|
2022-04-14 18:05:10 +00:00
|
|
|
activity_id,
|
2021-12-16 17:32:49 +00:00
|
|
|
object_id,
|
2022-01-04 21:36:45 +00:00
|
|
|
primary_audience,
|
|
|
|
vec![get_followers_url(instance_url, &actor_profile.username)],
|
2021-12-16 17:32:49 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-04-09 00:22:17 +00:00
|
|
|
pub fn create_activity_accept_follow(
|
2021-10-09 12:53:53 +00:00
|
|
|
instance_url: &str,
|
2021-04-09 00:22:17 +00:00
|
|
|
actor_profile: &DbActorProfile,
|
|
|
|
follow_activity_id: &str,
|
2021-11-18 21:28:50 +00:00
|
|
|
source_actor_id: &str,
|
2021-04-09 00:22:17 +00:00
|
|
|
) -> Activity {
|
|
|
|
let object = Object {
|
|
|
|
context: Some(json!(AP_CONTEXT)),
|
|
|
|
id: follow_activity_id.to_string(),
|
|
|
|
object_type: FOLLOW.to_string(),
|
2021-10-29 16:33:50 +00:00
|
|
|
..Default::default()
|
2021-04-09 00:22:17 +00:00
|
|
|
};
|
2022-05-23 08:21:26 +00:00
|
|
|
// Accept(Follow) is idempotent so its ID can be random
|
|
|
|
let activity_id = get_object_url(instance_url, &new_uuid());
|
2021-04-09 00:22:17 +00:00
|
|
|
let activity = create_activity(
|
2021-10-09 12:53:53 +00:00
|
|
|
instance_url,
|
2021-04-09 00:22:17 +00:00
|
|
|
&actor_profile.username,
|
|
|
|
ACCEPT,
|
2022-04-14 18:05:10 +00:00
|
|
|
activity_id,
|
2021-11-12 21:33:19 +00:00
|
|
|
object,
|
2021-12-05 18:08:06 +00:00
|
|
|
vec![source_actor_id.to_string()],
|
2022-01-04 20:52:41 +00:00
|
|
|
vec![],
|
2021-04-09 00:22:17 +00:00
|
|
|
);
|
|
|
|
activity
|
|
|
|
}
|
|
|
|
|
2021-10-09 12:53:53 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
const INSTANCE_URL: &str = "https://example.com";
|
|
|
|
|
2022-05-13 01:28:39 +00:00
|
|
|
#[test]
|
|
|
|
fn test_create_activity_like() {
|
|
|
|
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 = create_activity_like(
|
|
|
|
INSTANCE_URL,
|
|
|
|
&author,
|
|
|
|
note_id,
|
|
|
|
&reaction_id,
|
|
|
|
note_author_id,
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
activity.id,
|
|
|
|
format!("{}/objects/{}", INSTANCE_URL, reaction_id),
|
|
|
|
);
|
|
|
|
assert_eq!(activity.object, json!(note_id));
|
|
|
|
assert_eq!(activity.to.unwrap(), json!([AP_PUBLIC, note_author_id]));
|
|
|
|
}
|
|
|
|
|
2022-05-23 08:21:26 +00:00
|
|
|
#[test]
|
|
|
|
fn test_create_activity_accept_follow() {
|
|
|
|
let target = DbActorProfile {
|
|
|
|
username: "user".to_string(),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let follow_activity_id = "https://test.remote/objects/999";
|
|
|
|
let follower_id = "https://test.remote/users/123";
|
|
|
|
let activity = create_activity_accept_follow(
|
|
|
|
INSTANCE_URL,
|
|
|
|
&target,
|
|
|
|
follow_activity_id,
|
|
|
|
follower_id,
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(activity.id.starts_with(INSTANCE_URL), true);
|
|
|
|
assert_eq!(activity.object["id"], follow_activity_id);
|
|
|
|
assert_eq!(activity.to.unwrap(), json!([follower_id]));
|
|
|
|
}
|
2021-10-09 12:53:53 +00:00
|
|
|
}
|