Move Follow() builder function to builders::follow module
This commit is contained in:
parent
3bfb7dc9eb
commit
d29e27b114
5 changed files with 113 additions and 70 deletions
|
@ -220,31 +220,6 @@ pub fn create_activity_undo_announce(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_activity_follow(
|
|
||||||
instance_url: &str,
|
|
||||||
actor_profile: &DbActorProfile,
|
|
||||||
follow_request_id: &Uuid,
|
|
||||||
target_actor_id: &str,
|
|
||||||
) -> Activity {
|
|
||||||
let object = Object {
|
|
||||||
context: Some(json!(AP_CONTEXT)),
|
|
||||||
id: target_actor_id.to_owned(),
|
|
||||||
object_type: PERSON.to_string(),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
let activity_id = get_object_url(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
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn create_activity_accept_follow(
|
pub fn create_activity_accept_follow(
|
||||||
instance_url: &str,
|
instance_url: &str,
|
||||||
actor_profile: &DbActorProfile,
|
actor_profile: &DbActorProfile,
|
||||||
|
@ -298,39 +273,6 @@ mod tests {
|
||||||
assert_eq!(activity.to.unwrap(), json!([AP_PUBLIC, note_author_id]));
|
assert_eq!(activity.to.unwrap(), json!([AP_PUBLIC, note_author_id]));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_create_activity_follow() {
|
|
||||||
let follower = DbActorProfile {
|
|
||||||
username: "follower".to_string(),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
let follow_request_id = new_uuid();
|
|
||||||
let target_actor_id = "https://test.remote/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);
|
|
||||||
assert_eq!(activity.to.unwrap(), json!([target_actor_id]));
|
|
||||||
assert_eq!(activity.cc.unwrap(), json!([]));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_create_activity_accept_follow() {
|
fn test_create_activity_accept_follow() {
|
||||||
let target = DbActorProfile {
|
let target = DbActorProfile {
|
||||||
|
|
102
src/activitypub/builders/follow.rs
Normal file
102
src/activitypub/builders/follow.rs
Normal file
|
@ -0,0 +1,102 @@
|
||||||
|
use serde_json::json;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::activitypub::{
|
||||||
|
activity::{create_activity, Activity, Object},
|
||||||
|
actor::Actor,
|
||||||
|
constants::AP_CONTEXT,
|
||||||
|
deliverer::OutgoingActivity,
|
||||||
|
views::get_object_url,
|
||||||
|
vocabulary::{FOLLOW, PERSON},
|
||||||
|
};
|
||||||
|
use crate::config::Instance;
|
||||||
|
use crate::models::profiles::types::DbActorProfile;
|
||||||
|
use crate::models::users::types::User;
|
||||||
|
|
||||||
|
fn build_follow(
|
||||||
|
instance_url: &str,
|
||||||
|
actor_profile: &DbActorProfile,
|
||||||
|
target_actor_id: &str,
|
||||||
|
follow_request_id: &Uuid,
|
||||||
|
) -> Activity {
|
||||||
|
let object = Object {
|
||||||
|
context: Some(json!(AP_CONTEXT)),
|
||||||
|
id: target_actor_id.to_owned(),
|
||||||
|
object_type: PERSON.to_string(),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
let activity_id = get_object_url(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
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn prepare_follow(
|
||||||
|
instance: Instance,
|
||||||
|
user: &User,
|
||||||
|
target_actor: &Actor,
|
||||||
|
follow_request_id: &Uuid,
|
||||||
|
) -> OutgoingActivity {
|
||||||
|
let activity = build_follow(
|
||||||
|
&instance.url(),
|
||||||
|
&user.profile,
|
||||||
|
&target_actor.id,
|
||||||
|
follow_request_id,
|
||||||
|
);
|
||||||
|
let recipients = vec![target_actor.clone()];
|
||||||
|
OutgoingActivity {
|
||||||
|
instance,
|
||||||
|
sender: user.clone(),
|
||||||
|
activity,
|
||||||
|
recipients,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use serde_json::{json, Value};
|
||||||
|
use crate::utils::id::new_uuid;
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
const INSTANCE_URL: &str = "https://example.com";
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_build_follow() {
|
||||||
|
let follower = DbActorProfile {
|
||||||
|
username: "follower".to_string(),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
let follow_request_id = new_uuid();
|
||||||
|
let target_actor_id = "https://test.remote/actor/test";
|
||||||
|
let activity = build_follow(
|
||||||
|
INSTANCE_URL,
|
||||||
|
&follower,
|
||||||
|
target_actor_id,
|
||||||
|
&follow_request_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);
|
||||||
|
assert_eq!(activity.to.unwrap(), json!([target_actor_id]));
|
||||||
|
assert_eq!(activity.cc.unwrap(), json!([]));
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
pub mod create_note;
|
pub mod create_note;
|
||||||
pub mod delete_note;
|
pub mod delete_note;
|
||||||
pub mod delete_person;
|
pub mod delete_person;
|
||||||
|
pub mod follow;
|
||||||
pub mod undo_follow;
|
pub mod undo_follow;
|
||||||
pub mod update_person;
|
pub mod update_person;
|
||||||
|
|
|
@ -16,8 +16,8 @@ use crate::models::users::types::User;
|
||||||
fn build_undo_follow(
|
fn build_undo_follow(
|
||||||
instance_url: &str,
|
instance_url: &str,
|
||||||
actor_profile: &DbActorProfile,
|
actor_profile: &DbActorProfile,
|
||||||
follow_request_id: &Uuid,
|
|
||||||
target_actor_id: &str,
|
target_actor_id: &str,
|
||||||
|
follow_request_id: &Uuid,
|
||||||
) -> Activity {
|
) -> Activity {
|
||||||
let follow_activity_id = get_object_url(
|
let follow_activity_id = get_object_url(
|
||||||
instance_url,
|
instance_url,
|
||||||
|
@ -57,8 +57,8 @@ pub fn prepare_undo_follow(
|
||||||
let activity = build_undo_follow(
|
let activity = build_undo_follow(
|
||||||
&instance.url(),
|
&instance.url(),
|
||||||
&user.profile,
|
&user.profile,
|
||||||
follow_request_id,
|
|
||||||
&target_actor.id,
|
&target_actor.id,
|
||||||
|
follow_request_id,
|
||||||
);
|
);
|
||||||
let recipients = vec![target_actor.clone()];
|
let recipients = vec![target_actor.clone()];
|
||||||
OutgoingActivity {
|
OutgoingActivity {
|
||||||
|
|
|
@ -2,12 +2,11 @@ use actix_web::{get, post, patch, web, HttpResponse, Scope};
|
||||||
use actix_web_httpauth::extractors::bearer::BearerAuth;
|
use actix_web_httpauth::extractors::bearer::BearerAuth;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::activitypub::activity::create_activity_follow;
|
|
||||||
use crate::activitypub::builders::{
|
use crate::activitypub::builders::{
|
||||||
|
follow::prepare_follow,
|
||||||
undo_follow::prepare_undo_follow,
|
undo_follow::prepare_undo_follow,
|
||||||
update_person::prepare_update_person,
|
update_person::prepare_update_person,
|
||||||
};
|
};
|
||||||
use crate::activitypub::deliverer::deliver_activity;
|
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
use crate::database::{Pool, get_database_client};
|
use crate::database::{Pool, get_database_client};
|
||||||
use crate::errors::{DatabaseError, HttpError, ValidationError};
|
use crate::errors::{DatabaseError, HttpError, ValidationError};
|
||||||
|
@ -334,14 +333,13 @@ async fn follow_account(
|
||||||
if let Some(remote_actor) = target.actor_json {
|
if let Some(remote_actor) = target.actor_json {
|
||||||
// Create follow request if target is remote
|
// Create follow request if target is remote
|
||||||
match create_follow_request(db_client, ¤t_user.id, &target.id).await {
|
match create_follow_request(db_client, ¤t_user.id, &target.id).await {
|
||||||
Ok(request) => {
|
Ok(follow_request) => {
|
||||||
let activity = create_activity_follow(
|
prepare_follow(
|
||||||
&config.instance_url(),
|
config.instance(),
|
||||||
¤t_user.profile,
|
¤t_user,
|
||||||
&request.id,
|
&remote_actor,
|
||||||
&remote_actor.id,
|
&follow_request.id,
|
||||||
);
|
).spawn_deliver();
|
||||||
deliver_activity(&config, ¤t_user, activity, vec![remote_actor]);
|
|
||||||
},
|
},
|
||||||
Err(DatabaseError::AlreadyExists(_)) => (), // already following
|
Err(DatabaseError::AlreadyExists(_)) => (), // already following
|
||||||
Err(other_error) => return Err(other_error.into()),
|
Err(other_error) => return Err(other_error.into()),
|
||||||
|
|
Loading…
Reference in a new issue