diff options
Diffstat (limited to 'server/src/apub/shared_inbox.rs')
-rw-r--r-- | server/src/apub/shared_inbox.rs | 243 |
1 files changed, 243 insertions, 0 deletions
diff --git a/server/src/apub/shared_inbox.rs b/server/src/apub/shared_inbox.rs index ea03c9e6..a9a61020 100644 --- a/server/src/apub/shared_inbox.rs +++ b/server/src/apub/shared_inbox.rs @@ -8,6 +8,7 @@ pub enum SharedAcceptedObjects { Like(Like), Dislike(Dislike), Delete(Delete), + Undo(Undo), } impl SharedAcceptedObjects { @@ -18,6 +19,7 @@ impl SharedAcceptedObjects { SharedAcceptedObjects::Like(l) => l.like_props.get_object_base_box(), SharedAcceptedObjects::Dislike(d) => d.dislike_props.get_object_base_box(), SharedAcceptedObjects::Delete(d) => d.delete_props.get_object_base_box(), + SharedAcceptedObjects::Undo(d) => d.undo_props.get_object_base_box(), } } } @@ -72,6 +74,9 @@ pub async fn shared_inbox( (SharedAcceptedObjects::Delete(d), Some("Group")) => { receive_delete_community(&d, &request, &conn, chat_server) } + (SharedAcceptedObjects::Undo(u), Some("Delete")) => { + receive_undo_delete(&u, &request, &conn, chat_server) + } _ => Err(format_err!("Unknown incoming activity type.")), } } @@ -721,3 +726,241 @@ fn receive_delete_comment( Ok(HttpResponse::Ok().finish()) } + +fn receive_undo_delete( + undo: &Undo, + request: &HttpRequest, + conn: &PgConnection, + chat_server: ChatServerParam, +) -> Result<HttpResponse, Error> { + let delete = undo + .undo_props + .get_object_base_box() + .to_owned() + .unwrap() + .to_owned() + .into_concrete::<Delete>()?; + + let type_ = delete + .delete_props + .get_object_base_box() + .to_owned() + .unwrap() + .kind() + .unwrap(); + + match type_ { + "Note" => receive_undo_delete_comment(&delete, &request, &conn, chat_server), + "Page" => receive_undo_delete_post(&delete, &request, &conn, chat_server), + "Group" => receive_undo_delete_community(&delete, &request, &conn, chat_server), + d => Err(format_err!("Undo Delete type {} not supported", d)), + } +} + +fn receive_undo_delete_comment( + delete: &Delete, + request: &HttpRequest, + conn: &PgConnection, + chat_server: ChatServerParam, +) -> Result<HttpResponse, Error> { + let user_uri = delete + .delete_props + .get_actor_xsd_any_uri() + .unwrap() + .to_string(); + + let note = delete + .delete_props + .get_object_base_box() + .to_owned() + .unwrap() + .to_owned() + .into_concrete::<Note>()?; + + let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; + verify(request, &user.public_key.unwrap())?; + + // Insert the received activity into the activity table + let activity_form = activity::ActivityForm { + user_id: user.id, + data: serde_json::to_value(&delete)?, + local: false, + updated: None, + }; + activity::Activity::create(&conn, &activity_form)?; + + let comment_ap_id = CommentForm::from_apub(¬e, &conn)?.ap_id; + let comment = Comment::read_from_apub_id(conn, &comment_ap_id)?; + let comment_form = CommentForm { + content: comment.content.to_owned(), + parent_id: comment.parent_id, + post_id: comment.post_id, + creator_id: comment.creator_id, + removed: None, + deleted: Some(false), + read: None, + published: None, + updated: Some(naive_now()), + ap_id: comment.ap_id, + local: comment.local, + }; + Comment::update(&conn, comment.id, &comment_form)?; + + // Refetch the view + let comment_view = CommentView::read(&conn, comment.id, None)?; + + // TODO get those recipient actor ids from somewhere + let recipient_ids = vec![]; + let res = CommentResponse { + comment: comment_view, + recipient_ids, + }; + + chat_server.do_send(SendComment { + op: UserOperation::EditComment, + comment: res, + my_id: None, + }); + + Ok(HttpResponse::Ok().finish()) +} + +fn receive_undo_delete_post( + delete: &Delete, + request: &HttpRequest, + conn: &PgConnection, + chat_server: ChatServerParam, +) -> Result<HttpResponse, Error> { + let user_uri = delete + .delete_props + .get_actor_xsd_any_uri() + .unwrap() + .to_string(); + + let page = delete + .delete_props + .get_object_base_box() + .to_owned() + .unwrap() + .to_owned() + .into_concrete::<Page>()?; + + let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; + verify(request, &user.public_key.unwrap())?; + + // Insert the received activity into the activity table + let activity_form = activity::ActivityForm { + user_id: user.id, + data: serde_json::to_value(&delete)?, + local: false, + updated: None, + }; + activity::Activity::create(&conn, &activity_form)?; + + let post_ap_id = PostForm::from_apub(&page, conn)?.ap_id; + let post = Post::read_from_apub_id(conn, &post_ap_id)?; + + let post_form = PostForm { + name: post.name.to_owned(), + url: post.url.to_owned(), + body: post.body.to_owned(), + creator_id: post.creator_id.to_owned(), + community_id: post.community_id, + removed: None, + deleted: Some(false), + nsfw: post.nsfw, + locked: None, + stickied: None, + updated: Some(naive_now()), + embed_title: post.embed_title, + embed_description: post.embed_description, + embed_html: post.embed_html, + thumbnail_url: post.thumbnail_url, + ap_id: post.ap_id, + local: post.local, + published: None, + }; + Post::update(&conn, post.id, &post_form)?; + + // Refetch the view + let post_view = PostView::read(&conn, post.id, None)?; + + let res = PostResponse { post: post_view }; + + chat_server.do_send(SendPost { + op: UserOperation::EditPost, + post: res, + my_id: None, + }); + + Ok(HttpResponse::Ok().finish()) +} + +fn receive_undo_delete_community( + delete: &Delete, + request: &HttpRequest, + conn: &PgConnection, + chat_server: ChatServerParam, +) -> Result<HttpResponse, Error> { + let user_uri = delete + .delete_props + .get_actor_xsd_any_uri() + .unwrap() + .to_string(); + + let group = delete + .delete_props + .get_object_base_box() + .to_owned() + .unwrap() + .to_owned() + .into_concrete::<GroupExt>()?; + + let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; + verify(request, &user.public_key.unwrap())?; + + // Insert the received activity into the activity table + let activity_form = activity::ActivityForm { + user_id: user.id, + data: serde_json::to_value(&delete)?, + local: false, + updated: None, + }; + activity::Activity::create(&conn, &activity_form)?; + + let community_actor_id = CommunityForm::from_apub(&group, &conn)?.actor_id; + let community = Community::read_from_actor_id(conn, &community_actor_id)?; + + let community_form = CommunityForm { + name: community.name.to_owned(), + title: community.title.to_owned(), + description: community.description.to_owned(), + category_id: community.category_id, // Note: need to keep this due to foreign key constraint + creator_id: community.creator_id, // Note: need to keep this due to foreign key constraint + removed: None, + published: None, + updated: Some(naive_now()), + deleted: Some(false), + nsfw: community.nsfw, + actor_id: community.actor_id, + local: community.local, + private_key: community.private_key, + public_key: community.public_key, + last_refreshed_at: None, + }; + + Community::update(&conn, community.id, &community_form)?; + + let res = CommunityResponse { + community: CommunityView::read(&conn, community.id, None)?, + }; + + chat_server.do_send(SendCommunityRoomMessage { + op: UserOperation::EditCommunity, + response: res, + community_id: community.id, + my_id: None, + }); + + Ok(HttpResponse::Ok().finish()) +} |