From 602e4ec229f0f959ba3f5fcd5ccc84b55be3f2c4 Mon Sep 17 00:00:00 2001 From: silverpill Date: Mon, 20 Sep 2021 21:06:48 +0000 Subject: [PATCH] Add handler for Reject(Follow) activity --- src/activitypub/receiver.rs | 14 ++++++++++++-- src/activitypub/vocabulary.rs | 1 + src/models/relationships/queries.rs | 21 ++++++++++++++++++++- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/activitypub/receiver.rs b/src/activitypub/receiver.rs index e0adaf9..f6cd6ca 100644 --- a/src/activitypub/receiver.rs +++ b/src/activitypub/receiver.rs @@ -14,7 +14,11 @@ use crate::models::profiles::queries::{ update_profile, }; use crate::models::profiles::types::ProfileUpdateData; -use crate::models::relationships::queries::{accept_follow_request, follow, unfollow}; +use crate::models::relationships::queries::{ + follow_request_accepted, + follow_request_rejected, + follow, unfollow, +}; use crate::models::users::queries::get_user_by_id; use super::activity::{Object, Activity, create_activity_accept_follow}; use super::actor::Actor; @@ -64,7 +68,13 @@ pub async fn receive_activity( .map_err(|_| ValidationError("invalid object"))?; // TODO: reject if object ID contains wrong instance URI let follow_request_id = parse_object_id(&object.id)?; - accept_follow_request(db_client, &follow_request_id).await?; + follow_request_accepted(db_client, &follow_request_id).await?; + }, + (REJECT, FOLLOW) => { + let object: Object = serde_json::from_value(activity.object) + .map_err(|_| ValidationError("invalid object"))?; + let follow_request_id = parse_object_id(&object.id)?; + follow_request_rejected(db_client, &follow_request_id).await?; }, (CREATE, NOTE) => { let object: Object = serde_json::from_value(activity.object) diff --git a/src/activitypub/vocabulary.rs b/src/activitypub/vocabulary.rs index f3c68d7..da6f3f6 100644 --- a/src/activitypub/vocabulary.rs +++ b/src/activitypub/vocabulary.rs @@ -2,6 +2,7 @@ pub const ACCEPT: &str = "Accept"; pub const CREATE: &str = "Create"; pub const FOLLOW: &str = "Follow"; +pub const REJECT: &str = "Reject"; pub const UNDO: &str = "Undo"; pub const UPDATE: &str = "Update"; diff --git a/src/models/relationships/queries.rs b/src/models/relationships/queries.rs index 4138839..00bbfb2 100644 --- a/src/models/relationships/queries.rs +++ b/src/models/relationships/queries.rs @@ -165,7 +165,7 @@ pub async fn create_follow_request( Ok(request) } -pub async fn accept_follow_request( +pub async fn follow_request_accepted( db_client: &mut impl GenericClient, request_id: &Uuid, ) -> Result<(), DatabaseError> { @@ -188,6 +188,25 @@ pub async fn accept_follow_request( Ok(()) } +pub async fn follow_request_rejected( + db_client: &impl GenericClient, + request_id: &Uuid, +) -> Result<(), DatabaseError> { + let status_sql: i16 = FollowRequestStatus::Rejected.into(); + let updated_count = db_client.execute( + " + UPDATE follow_request + SET request_status = $1 + WHERE id = $2 + ", + &[&status_sql, &request_id], + ).await?; + if updated_count == 0 { + return Err(DatabaseError::NotFound("follow request")); + } + Ok(()) +} + pub async fn delete_follow_request( db_client: &impl GenericClient, source_id: &Uuid,