2021-04-09 00:22:17 +00:00
|
|
|
use chrono::{DateTime, Utc};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use serde_json::{json, Value};
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
use crate::models::posts::types::Post;
|
|
|
|
use crate::models::profiles::types::DbActorProfile;
|
|
|
|
use crate::utils::files::get_file_url;
|
|
|
|
use super::constants::{AP_CONTEXT, AP_PUBLIC};
|
|
|
|
use super::views::{get_actor_url, get_object_url};
|
|
|
|
use super::vocabulary::*;
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct Attachment {
|
|
|
|
pub name: String,
|
|
|
|
|
|
|
|
#[serde(rename = "type")]
|
|
|
|
pub attachment_type: String,
|
|
|
|
|
|
|
|
pub media_type: String,
|
|
|
|
pub url: String,
|
|
|
|
}
|
|
|
|
|
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")]
|
|
|
|
pub actor: Option<String>,
|
|
|
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub attachment: Option<Vec<Attachment>>,
|
|
|
|
|
|
|
|
#[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")]
|
|
|
|
pub attributed_to: Option<String>,
|
|
|
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub in_reply_to: Option<String>,
|
|
|
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub content: Option<String>,
|
|
|
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub to: Option<Value>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_activity(
|
|
|
|
instance_url: &str,
|
|
|
|
actor_name: &str,
|
|
|
|
activity_type: &str,
|
|
|
|
activity_uuid: Option<Uuid>,
|
|
|
|
object: Value,
|
|
|
|
) -> Activity {
|
|
|
|
let actor_id = get_actor_url(
|
|
|
|
instance_url,
|
|
|
|
&actor_name,
|
|
|
|
);
|
|
|
|
let activity_id = get_object_url(
|
|
|
|
instance_url,
|
|
|
|
&activity_uuid.unwrap_or(Uuid::new_v4()),
|
|
|
|
);
|
|
|
|
let activity = Activity {
|
|
|
|
context: json!(AP_CONTEXT),
|
|
|
|
id: activity_id,
|
|
|
|
activity_type: activity_type.to_string(),
|
|
|
|
actor: actor_id,
|
|
|
|
object: object,
|
|
|
|
};
|
|
|
|
activity
|
|
|
|
}
|
|
|
|
|
2021-10-08 13:56:45 +00:00
|
|
|
pub fn create_note(
|
2021-10-09 12:53:53 +00:00
|
|
|
instance_url: &str,
|
2021-04-09 00:22:17 +00:00
|
|
|
post: &Post,
|
2021-10-08 13:56:45 +00:00
|
|
|
in_reply_to: Option<&Post>,
|
|
|
|
) -> Object {
|
2021-04-09 00:22:17 +00:00
|
|
|
let object_id = get_object_url(
|
2021-10-09 12:53:53 +00:00
|
|
|
instance_url,
|
2021-04-09 00:22:17 +00:00
|
|
|
&post.id,
|
|
|
|
);
|
|
|
|
let actor_id = get_actor_url(
|
2021-10-09 12:53:53 +00:00
|
|
|
instance_url,
|
2021-04-09 00:22:17 +00:00
|
|
|
&post.author.username,
|
|
|
|
);
|
|
|
|
let attachments: Vec<Attachment> = post.attachments.iter().map(|db_item| {
|
2021-10-09 12:53:53 +00:00
|
|
|
let url = get_file_url(instance_url, &db_item.file_name);
|
2021-04-09 00:22:17 +00:00
|
|
|
let media_type = db_item.media_type.clone().unwrap_or("".to_string());
|
|
|
|
Attachment {
|
|
|
|
name: "".to_string(),
|
|
|
|
attachment_type: DOCUMENT.to_string(),
|
|
|
|
media_type,
|
|
|
|
url,
|
|
|
|
}
|
|
|
|
}).collect();
|
2021-11-01 00:23:02 +00:00
|
|
|
let mut recipients = vec![AP_PUBLIC.to_string()];
|
2021-10-08 13:56:45 +00:00
|
|
|
let in_reply_to_object_id = match post.in_reply_to_id {
|
|
|
|
Some(in_reply_to_id) => {
|
|
|
|
let post = in_reply_to.unwrap();
|
|
|
|
assert_eq!(post.id, in_reply_to_id);
|
2021-11-01 00:23:02 +00:00
|
|
|
match post.author.actor_json {
|
|
|
|
Some(ref actor_value) => {
|
|
|
|
// Replying to remote post
|
|
|
|
let remote_actor_id = actor_value["id"].as_str().unwrap();
|
|
|
|
recipients.push(remote_actor_id.to_string());
|
|
|
|
post.object_id.clone()
|
|
|
|
},
|
|
|
|
None => Some(get_object_url(instance_url, &post.id)),
|
2021-10-08 13:56:45 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
Object {
|
2021-04-09 00:22:17 +00:00
|
|
|
context: Some(json!(AP_CONTEXT)),
|
|
|
|
id: object_id,
|
|
|
|
object_type: NOTE.to_string(),
|
|
|
|
actor: None,
|
|
|
|
attachment: Some(attachments),
|
|
|
|
object: None,
|
|
|
|
published: Some(post.created_at),
|
2021-10-09 12:53:53 +00:00
|
|
|
attributed_to: Some(actor_id),
|
2021-10-08 13:56:45 +00:00
|
|
|
in_reply_to: in_reply_to_object_id,
|
2021-04-09 00:22:17 +00:00
|
|
|
content: Some(post.content.clone()),
|
2021-11-01 00:23:02 +00:00
|
|
|
to: Some(json!(recipients)),
|
2021-10-08 13:56:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_activity_note(
|
2021-10-09 12:53:53 +00:00
|
|
|
instance_url: &str,
|
2021-10-08 13:56:45 +00:00
|
|
|
post: &Post,
|
|
|
|
in_reply_to: Option<&Post>,
|
|
|
|
) -> Activity {
|
2021-10-09 12:53:53 +00:00
|
|
|
let object = create_note(instance_url, post, in_reply_to);
|
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
|
|
|
&post.author.username,
|
|
|
|
CREATE,
|
|
|
|
None,
|
|
|
|
serde_json::to_value(object).unwrap(),
|
|
|
|
);
|
|
|
|
activity
|
|
|
|
}
|
|
|
|
|
2021-10-29 19:21:26 +00:00
|
|
|
pub fn create_activity_like(
|
|
|
|
instance_url: &str,
|
|
|
|
actor_profile: &DbActorProfile,
|
|
|
|
object_id: &str,
|
|
|
|
) -> Activity {
|
|
|
|
let object = Object {
|
|
|
|
context: Some(json!(AP_CONTEXT)),
|
|
|
|
id: object_id.to_string(),
|
|
|
|
object_type: NOTE.to_string(),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let activity = create_activity(
|
|
|
|
instance_url,
|
|
|
|
&actor_profile.username,
|
|
|
|
LIKE,
|
|
|
|
None,
|
|
|
|
serde_json::to_value(object).unwrap(),
|
|
|
|
);
|
|
|
|
activity
|
|
|
|
}
|
|
|
|
|
2021-04-09 00:22:17 +00:00
|
|
|
pub fn create_activity_follow(
|
2021-10-09 12:53:53 +00:00
|
|
|
instance_url: &str,
|
2021-04-09 00:22:17 +00:00
|
|
|
actor_profile: &DbActorProfile,
|
|
|
|
follow_request_id: &Uuid,
|
2021-10-29 16:33:50 +00:00
|
|
|
target_actor_id: &str,
|
2021-04-09 00:22:17 +00:00
|
|
|
) -> Activity {
|
|
|
|
let object = Object {
|
|
|
|
context: Some(json!(AP_CONTEXT)),
|
2021-10-29 16:33:50 +00:00
|
|
|
id: target_actor_id.to_owned(),
|
2021-04-09 00:22:17 +00:00
|
|
|
object_type: PERSON.to_string(),
|
2021-10-29 16:33:50 +00:00
|
|
|
..Default::default()
|
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,
|
|
|
|
FOLLOW,
|
|
|
|
Some(*follow_request_id),
|
|
|
|
serde_json::to_value(object).unwrap(),
|
|
|
|
);
|
|
|
|
activity
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
) -> Activity {
|
|
|
|
// TODO: use received activity as object
|
|
|
|
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
|
|
|
};
|
|
|
|
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,
|
|
|
|
None,
|
|
|
|
serde_json::to_value(object).unwrap(),
|
|
|
|
);
|
|
|
|
activity
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_activity_undo_follow(
|
2021-10-09 12:53:53 +00:00
|
|
|
instance_url: &str,
|
2021-04-09 00:22:17 +00:00
|
|
|
actor_profile: &DbActorProfile,
|
|
|
|
follow_request_id: &Uuid,
|
2021-10-29 16:33:50 +00:00
|
|
|
target_actor_id: &str,
|
2021-04-09 00:22:17 +00:00
|
|
|
) -> Activity {
|
|
|
|
// TODO: retrieve 'Follow' activity from database
|
|
|
|
let follow_activity_id = get_object_url(
|
2021-10-09 12:53:53 +00:00
|
|
|
instance_url,
|
2021-04-09 00:22:17 +00:00
|
|
|
follow_request_id,
|
|
|
|
);
|
|
|
|
let follow_actor_id = get_actor_url(
|
2021-10-09 12:53:53 +00:00
|
|
|
instance_url,
|
2021-04-09 00:22:17 +00:00
|
|
|
&actor_profile.username,
|
|
|
|
);
|
|
|
|
let object = Object {
|
|
|
|
context: Some(json!(AP_CONTEXT)),
|
|
|
|
id: follow_activity_id,
|
|
|
|
object_type: FOLLOW.to_string(),
|
|
|
|
actor: Some(follow_actor_id),
|
2021-10-29 16:33:50 +00:00
|
|
|
object: Some(target_actor_id.to_owned()),
|
|
|
|
..Default::default()
|
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,
|
|
|
|
UNDO,
|
|
|
|
None,
|
|
|
|
serde_json::to_value(object).unwrap(),
|
|
|
|
);
|
|
|
|
activity
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct OrderedCollection {
|
|
|
|
#[serde(rename = "@context")]
|
|
|
|
pub context: Value,
|
|
|
|
|
|
|
|
pub id: String,
|
|
|
|
|
|
|
|
#[serde(rename = "type")]
|
|
|
|
pub object_type: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OrderedCollection {
|
|
|
|
pub fn new(collection_url: String) -> Self {
|
|
|
|
Self {
|
|
|
|
context: json!(AP_CONTEXT),
|
|
|
|
id: collection_url,
|
|
|
|
object_type: "OrderedCollection".to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-09 12:53:53 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
const INSTANCE_URL: &str = "https://example.com";
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_create_note() {
|
|
|
|
let author = DbActorProfile {
|
|
|
|
username: "author".to_string(),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let post = Post { author, ..Default::default() };
|
|
|
|
let note = create_note(INSTANCE_URL, &post, None);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
note.id,
|
|
|
|
format!("{}/objects/{}", INSTANCE_URL, post.id),
|
|
|
|
);
|
|
|
|
assert_eq!(note.attachment.unwrap().len(), 0);
|
|
|
|
assert_eq!(
|
|
|
|
note.attributed_to.unwrap(),
|
|
|
|
format!("{}/users/{}", INSTANCE_URL, post.author.username),
|
|
|
|
);
|
|
|
|
assert_eq!(note.in_reply_to.is_none(), true);
|
|
|
|
assert_eq!(note.content.unwrap(), post.content);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_create_note_with_local_parent() {
|
|
|
|
let parent = Post::default();
|
|
|
|
let post = Post {
|
|
|
|
in_reply_to_id: Some(parent.id),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let note = create_note(INSTANCE_URL, &post, Some(&parent));
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
note.in_reply_to.unwrap(),
|
|
|
|
format!("{}/objects/{}", INSTANCE_URL, parent.id),
|
|
|
|
);
|
2021-11-01 00:23:02 +00:00
|
|
|
assert_eq!(note.to.unwrap(), json!([AP_PUBLIC]));
|
2021-10-09 12:53:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_create_note_with_remote_parent() {
|
2021-11-01 00:23:02 +00:00
|
|
|
let parent_author_actor_id = "https://test.net/user/test";
|
2021-10-09 12:53:53 +00:00
|
|
|
let parent_author = DbActorProfile {
|
2021-11-01 00:23:02 +00:00
|
|
|
actor_json: Some(json!({
|
|
|
|
"id": parent_author_actor_id,
|
|
|
|
})),
|
2021-10-09 12:53:53 +00:00
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let parent = Post {
|
|
|
|
author: parent_author,
|
|
|
|
object_id: Some("https://test.net/obj/123".to_string()),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let post = Post {
|
|
|
|
in_reply_to_id: Some(parent.id),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let note = create_note(INSTANCE_URL, &post, Some(&parent));
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
note.in_reply_to.unwrap(),
|
|
|
|
parent.object_id.unwrap(),
|
|
|
|
);
|
2021-11-01 00:23:02 +00:00
|
|
|
assert_eq!(
|
|
|
|
note.to.unwrap(),
|
|
|
|
json!([AP_PUBLIC, parent_author_actor_id]),
|
|
|
|
);
|
2021-10-09 12:53:53 +00:00
|
|
|
}
|
2021-10-29 16:33:50 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_create_activity_follow() {
|
|
|
|
let follower = DbActorProfile {
|
|
|
|
username: "follower".to_string(),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let follow_request_id = Uuid::new_v4();
|
|
|
|
let target_actor_id = "https://example.com/actor/test";
|
|
|
|
let activity = create_activity_follow(
|
|
|
|
INSTANCE_URL,
|
|
|
|
&follower,
|
|
|
|
&follow_request_id,
|
|
|
|
target_actor_id,
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
activity.id,
|
|
|
|
format!("{}/objects/{}", INSTANCE_URL, follow_request_id),
|
|
|
|
);
|
|
|
|
assert_eq!(activity.activity_type, "Follow");
|
|
|
|
assert_eq!(
|
|
|
|
activity.actor,
|
|
|
|
format!("{}/users/{}", INSTANCE_URL, follower.username),
|
|
|
|
);
|
|
|
|
assert_eq!(activity.object["id"], target_actor_id);
|
|
|
|
assert_eq!(activity.object["type"], "Person");
|
|
|
|
assert_eq!(activity.object["actor"], Value::Null);
|
|
|
|
assert_eq!(activity.object["object"], Value::Null);
|
|
|
|
assert_eq!(activity.object["content"], Value::Null);
|
|
|
|
}
|
2021-10-09 12:53:53 +00:00
|
|
|
}
|