From 0fb8450e56da430ece7502ecde085d4c3a2e2ff2 Mon Sep 17 00:00:00 2001 From: Felix Date: Thu, 14 May 2020 17:44:01 +0200 Subject: [PATCH] Simplify community_inbox --- server/src/apub/community_inbox.rs | 92 ++++++++++++------------------ 1 file changed, 38 insertions(+), 54 deletions(-) diff --git a/server/src/apub/community_inbox.rs b/server/src/apub/community_inbox.rs index 81cbee81..6c35ce0a 100644 --- a/server/src/apub/community_inbox.rs +++ b/server/src/apub/community_inbox.rs @@ -7,6 +7,22 @@ pub enum CommunityAcceptedObjects { Undo(Undo), } +impl CommunityAcceptedObjects { + fn follow(&self) -> Result { + match self { + CommunityAcceptedObjects::Follow(f) => Ok(f.to_owned()), + CommunityAcceptedObjects::Undo(u) => Ok( + u.undo_props + .get_object_base_box() + .to_owned() + .unwrap() + .to_owned() + .into_concrete::()?, + ), + } + } +} + // TODO Consolidate community and user inboxes into a single shared one /// Handler for all incoming activities to community inboxes. pub async fn community_inbox( @@ -14,7 +30,7 @@ pub async fn community_inbox( input: web::Json, path: web::Path, db: DbPoolParam, - chat_server: ChatServerParam, + _chat_server: ChatServerParam, ) -> Result { let input = input.into_inner(); let community_name = path.into_inner(); @@ -22,31 +38,13 @@ pub async fn community_inbox( "Community {} received activity {:?}", &community_name, &input ); - match input { - CommunityAcceptedObjects::Follow(f) => { - handle_follow(&f, &request, &community_name, db, chat_server) - } - CommunityAcceptedObjects::Undo(u) => { - handle_undo_follow(&u, &request, &community_name, db, chat_server) - } - } -} - -/// Handle a follow request from a remote user, adding it to the local database and returning an -/// Accept activity. -fn handle_follow( - follow: &Follow, - request: &HttpRequest, - community_name: &str, - db: DbPoolParam, - _chat_server: ChatServerParam, -) -> Result { + let follow = input.follow()?; let user_uri = follow .follow_props .get_actor_xsd_any_uri() .unwrap() .to_string(); - let _community_uri = follow + let community_uri = follow .follow_props .get_object_xsd_any_uri() .unwrap() @@ -55,10 +53,24 @@ fn handle_follow( let conn = db.get()?; let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; - let community = Community::read_from_name(&conn, &community_name)?; + let community = get_or_fetch_and_upsert_remote_community(&community_uri, &conn)?; verify(&request, &user)?; + match input { + CommunityAcceptedObjects::Follow(f) => handle_follow(&f, &user, &community, &conn), + CommunityAcceptedObjects::Undo(u) => handle_undo_follow(&u, &user, &community, &conn), + } +} + +/// Handle a follow request from a remote user, adding it to the local database and returning an +/// Accept activity. +fn handle_follow( + follow: &Follow, + user: &User_, + community: &Community, + conn: &PgConnection, +) -> Result { insert_activity(&conn, user.id, &follow, false)?; let community_follower_form = CommunityFollowerForm { @@ -76,39 +88,11 @@ fn handle_follow( fn handle_undo_follow( undo: &Undo, - request: &HttpRequest, - community_name: &str, - db: DbPoolParam, - _chat_server: ChatServerParam, + user: &User_, + community: &Community, + conn: &PgConnection, ) -> Result { - let follow = undo - .undo_props - .get_object_base_box() - .to_owned() - .unwrap() - .to_owned() - .into_concrete::()?; - - let user_uri = follow - .follow_props - .get_actor_xsd_any_uri() - .unwrap() - .to_string(); - - let _community_uri = follow - .follow_props - .get_object_xsd_any_uri() - .unwrap() - .to_string(); - - let conn = db.get()?; - - let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; - let community = Community::read_from_name(&conn, &community_name)?; - - verify(&request, &user)?; - - insert_activity(&conn, user.id, &follow, false)?; + insert_activity(&conn, user.id, &undo, false)?; let community_follower_form = CommunityFollowerForm { community_id: community.id, -- 2.44.1