Create dedicated types for building Follow(), Accept(Follow) and Undo(Follow) activities
This commit is contained in:
parent
ff745cfe64
commit
51318046a8
3 changed files with 89 additions and 47 deletions
|
@ -1,11 +1,12 @@
|
|||
use serde::Serialize;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::activitypub::{
|
||||
activity::{create_activity, Activity, Object},
|
||||
activity::Object,
|
||||
actors::types::Actor,
|
||||
constants::AP_CONTEXT,
|
||||
deliverer::OutgoingActivity,
|
||||
identifiers::local_object_id,
|
||||
identifiers::{local_actor_id, local_object_id},
|
||||
vocabulary::{ACCEPT, FOLLOW},
|
||||
};
|
||||
use crate::config::Instance;
|
||||
|
@ -13,12 +14,27 @@ use crate::models::profiles::types::DbActorProfile;
|
|||
use crate::models::users::types::User;
|
||||
use crate::utils::id::new_uuid;
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct AcceptFollow {
|
||||
#[serde(rename = "@context")]
|
||||
context: String,
|
||||
|
||||
#[serde(rename = "type")]
|
||||
activity_type: String,
|
||||
|
||||
id: String,
|
||||
actor: String,
|
||||
object: Object,
|
||||
|
||||
to: Vec<String>,
|
||||
}
|
||||
|
||||
fn build_accept_follow(
|
||||
instance_url: &str,
|
||||
actor_profile: &DbActorProfile,
|
||||
source_actor_id: &str,
|
||||
follow_activity_id: &str,
|
||||
) -> Activity {
|
||||
) -> AcceptFollow {
|
||||
let object = Object {
|
||||
context: Some(json!(AP_CONTEXT)),
|
||||
id: follow_activity_id.to_string(),
|
||||
|
@ -27,16 +43,15 @@ fn build_accept_follow(
|
|||
};
|
||||
// Accept(Follow) is idempotent so its ID can be random
|
||||
let activity_id = local_object_id(instance_url, &new_uuid());
|
||||
let activity = create_activity(
|
||||
instance_url,
|
||||
&actor_profile.username,
|
||||
ACCEPT,
|
||||
activity_id,
|
||||
object,
|
||||
vec![source_actor_id.to_string()],
|
||||
vec![],
|
||||
);
|
||||
activity
|
||||
let actor_id = local_actor_id(instance_url, &actor_profile.username);
|
||||
AcceptFollow {
|
||||
context: AP_CONTEXT.to_string(),
|
||||
activity_type: ACCEPT.to_string(),
|
||||
id: activity_id,
|
||||
actor: actor_id,
|
||||
object: object,
|
||||
to: vec![source_actor_id.to_string()],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn prepare_accept_follow(
|
||||
|
@ -83,7 +98,7 @@ mod tests {
|
|||
|
||||
assert_eq!(activity.id.starts_with(INSTANCE_URL), true);
|
||||
assert_eq!(activity.activity_type, "Accept");
|
||||
assert_eq!(activity.object["id"], follow_activity_id);
|
||||
assert_eq!(activity.to.unwrap(), json!([follower_id]));
|
||||
assert_eq!(activity.object.id, follow_activity_id);
|
||||
assert_eq!(activity.to, vec![follower_id]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,34 +1,48 @@
|
|||
use serde::Serialize;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::activitypub::{
|
||||
activity::{create_activity, Activity},
|
||||
actors::types::Actor,
|
||||
constants::AP_CONTEXT,
|
||||
deliverer::OutgoingActivity,
|
||||
identifiers::local_object_id,
|
||||
identifiers::{local_actor_id, local_object_id},
|
||||
vocabulary::FOLLOW,
|
||||
};
|
||||
use crate::config::Instance;
|
||||
use crate::models::profiles::types::DbActorProfile;
|
||||
use crate::models::users::types::User;
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct Follow {
|
||||
#[serde(rename = "@context")]
|
||||
context: String,
|
||||
|
||||
#[serde(rename = "type")]
|
||||
activity_type: String,
|
||||
|
||||
id: String,
|
||||
actor: String,
|
||||
object: String,
|
||||
|
||||
to: Vec<String>,
|
||||
}
|
||||
|
||||
fn build_follow(
|
||||
instance_url: &str,
|
||||
actor_profile: &DbActorProfile,
|
||||
target_actor_id: &str,
|
||||
follow_request_id: &Uuid,
|
||||
) -> Activity {
|
||||
let object = target_actor_id.to_string();
|
||||
) -> Follow {
|
||||
let activity_id = local_object_id(instance_url, follow_request_id);
|
||||
let activity = create_activity(
|
||||
instance_url,
|
||||
&actor_profile.username,
|
||||
FOLLOW,
|
||||
activity_id,
|
||||
object,
|
||||
vec![target_actor_id.to_string()],
|
||||
vec![],
|
||||
);
|
||||
activity
|
||||
let actor_id = local_actor_id(instance_url, &actor_profile.username);
|
||||
Follow {
|
||||
context: AP_CONTEXT.to_string(),
|
||||
activity_type: FOLLOW.to_string(),
|
||||
id: activity_id,
|
||||
actor: actor_id,
|
||||
object: target_actor_id.to_string(),
|
||||
to: vec![target_actor_id.to_string()],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn prepare_follow(
|
||||
|
@ -54,7 +68,6 @@ pub fn prepare_follow(
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde_json::json;
|
||||
use crate::utils::id::new_uuid;
|
||||
use super::*;
|
||||
|
||||
|
@ -85,7 +98,6 @@ mod tests {
|
|||
format!("{}/users/{}", INSTANCE_URL, follower.username),
|
||||
);
|
||||
assert_eq!(activity.object, target_actor_id);
|
||||
assert_eq!(activity.to.unwrap(), json!([target_actor_id]));
|
||||
assert_eq!(activity.cc.unwrap(), json!([]));
|
||||
assert_eq!(activity.to, vec![target_actor_id]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
use serde::Serialize;
|
||||
use serde_json::json;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::activitypub::{
|
||||
activity::{create_activity, Activity, Object},
|
||||
activity::Object,
|
||||
actors::types::Actor,
|
||||
constants::AP_CONTEXT,
|
||||
deliverer::OutgoingActivity,
|
||||
|
@ -13,12 +14,27 @@ use crate::config::Instance;
|
|||
use crate::models::profiles::types::DbActorProfile;
|
||||
use crate::models::users::types::User;
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct UndoFollow {
|
||||
#[serde(rename = "@context")]
|
||||
context: String,
|
||||
|
||||
#[serde(rename = "type")]
|
||||
activity_type: String,
|
||||
|
||||
id: String,
|
||||
actor: String,
|
||||
object: Object,
|
||||
|
||||
to: Vec<String>,
|
||||
}
|
||||
|
||||
fn build_undo_follow(
|
||||
instance_url: &str,
|
||||
actor_profile: &DbActorProfile,
|
||||
target_actor_id: &str,
|
||||
follow_request_id: &Uuid,
|
||||
) -> Activity {
|
||||
) -> UndoFollow {
|
||||
let follow_activity_id = local_object_id(
|
||||
instance_url,
|
||||
follow_request_id,
|
||||
|
@ -36,16 +52,15 @@ fn build_undo_follow(
|
|||
..Default::default()
|
||||
};
|
||||
let activity_id = format!("{}/undo", object.id);
|
||||
let activity = create_activity(
|
||||
instance_url,
|
||||
&actor_profile.username,
|
||||
UNDO,
|
||||
activity_id,
|
||||
object,
|
||||
vec![target_actor_id.to_string()],
|
||||
vec![],
|
||||
);
|
||||
activity
|
||||
let actor_id = local_actor_id(instance_url, &actor_profile.username);
|
||||
UndoFollow {
|
||||
context: AP_CONTEXT.to_string(),
|
||||
activity_type: UNDO.to_string(),
|
||||
id: activity_id,
|
||||
actor: actor_id,
|
||||
object: object,
|
||||
to: vec![target_actor_id.to_string()],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn prepare_undo_follow(
|
||||
|
@ -97,10 +112,10 @@ mod tests {
|
|||
);
|
||||
assert_eq!(activity.activity_type, "Undo");
|
||||
assert_eq!(
|
||||
activity.object["id"],
|
||||
activity.object.id,
|
||||
format!("{}/objects/{}", INSTANCE_URL, follow_request_id),
|
||||
);
|
||||
assert_eq!(activity.object["object"], target_actor_id);
|
||||
assert_eq!(activity.to.unwrap(), json!([target_actor_id]));
|
||||
assert_eq!(activity.object.object.unwrap(), target_actor_id);
|
||||
assert_eq!(activity.to, vec![target_actor_id]);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue