2 api::{comment::CommentResponse, community::CommunityResponse, post::PostResponse},
4 fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post},
6 announce_if_community_is_local,
7 get_user_from_activity,
8 receive_unhandled_activity,
15 routes::ChatServerParam,
17 server::{SendComment, SendCommunityRoomMessage, SendPost},
23 use activitystreams::{activity::Remove, base::AnyBase, object::Note, prelude::*};
24 use actix_web::{client::Client, HttpResponse};
26 comment::{Comment, CommentForm},
27 comment_view::CommentView,
28 community::{Community, CommunityForm},
29 community_view::CommunityView,
31 post::{Post, PostForm},
36 pub async fn receive_remove(
40 chat_server: ChatServerParam,
41 ) -> Result<HttpResponse, LemmyError> {
42 let remove = Remove::from_any_base(activity)?.unwrap();
43 match remove.object().as_single_kind_str() {
44 Some("Page") => receive_remove_post(remove, client, pool, chat_server).await,
45 Some("Note") => receive_remove_comment(remove, client, pool, chat_server).await,
46 Some("Group") => receive_remove_community(remove, client, pool, chat_server).await,
47 _ => receive_unhandled_activity(remove),
51 async fn receive_remove_post(
55 chat_server: ChatServerParam,
56 ) -> Result<HttpResponse, LemmyError> {
57 let mod_ = get_user_from_activity(&remove, client, pool).await?;
58 let page = PageExt::from_any_base(remove.object().to_owned().one().unwrap())?.unwrap();
60 let post_ap_id = PostForm::from_apub(&page, client, pool)
64 let post = get_or_fetch_and_insert_post(&post_ap_id, client, pool).await?;
66 let post_form = PostForm {
67 name: post.name.to_owned(),
68 url: post.url.to_owned(),
69 body: post.body.to_owned(),
70 creator_id: post.creator_id.to_owned(),
71 community_id: post.community_id,
77 updated: Some(naive_now()),
78 embed_title: post.embed_title,
79 embed_description: post.embed_description,
80 embed_html: post.embed_html,
81 thumbnail_url: post.thumbnail_url,
86 let post_id = post.id;
87 blocking(pool, move |conn| Post::update(conn, post_id, &post_form)).await??;
90 let post_id = post.id;
91 let post_view = blocking(pool, move |conn| PostView::read(conn, post_id, None)).await??;
93 let res = PostResponse { post: post_view };
95 chat_server.do_send(SendPost {
96 op: UserOperation::EditPost,
101 announce_if_community_is_local(remove, &mod_, client, pool).await?;
102 Ok(HttpResponse::Ok().finish())
105 async fn receive_remove_comment(
109 chat_server: ChatServerParam,
110 ) -> Result<HttpResponse, LemmyError> {
111 let mod_ = get_user_from_activity(&remove, client, pool).await?;
112 let note = Note::from_any_base(remove.object().to_owned().one().unwrap())?.unwrap();
114 let comment_ap_id = CommentForm::from_apub(¬e, client, pool)
118 let comment = get_or_fetch_and_insert_comment(&comment_ap_id, client, pool).await?;
120 let comment_form = CommentForm {
121 content: comment.content.to_owned(),
122 parent_id: comment.parent_id,
123 post_id: comment.post_id,
124 creator_id: comment.creator_id,
129 updated: Some(naive_now()),
130 ap_id: comment.ap_id,
131 local: comment.local,
133 let comment_id = comment.id;
134 blocking(pool, move |conn| {
135 Comment::update(conn, comment_id, &comment_form)
140 let comment_id = comment.id;
142 blocking(pool, move |conn| CommentView::read(conn, comment_id, None)).await??;
144 // TODO get those recipient actor ids from somewhere
145 let recipient_ids = vec![];
146 let res = CommentResponse {
147 comment: comment_view,
152 chat_server.do_send(SendComment {
153 op: UserOperation::EditComment,
158 announce_if_community_is_local(remove, &mod_, client, pool).await?;
159 Ok(HttpResponse::Ok().finish())
162 async fn receive_remove_community(
166 chat_server: ChatServerParam,
167 ) -> Result<HttpResponse, LemmyError> {
168 let mod_ = get_user_from_activity(&remove, client, pool).await?;
169 let group = GroupExt::from_any_base(remove.object().to_owned().one().unwrap())?.unwrap();
171 let community_actor_id = CommunityForm::from_apub(&group, client, pool)
175 let community = blocking(pool, move |conn| {
176 Community::read_from_actor_id(conn, &community_actor_id)
180 let community_form = CommunityForm {
181 name: community.name.to_owned(),
182 title: community.title.to_owned(),
183 description: community.description.to_owned(),
184 category_id: community.category_id, // Note: need to keep this due to foreign key constraint
185 creator_id: community.creator_id, // Note: need to keep this due to foreign key constraint
188 updated: Some(naive_now()),
190 nsfw: community.nsfw,
191 actor_id: community.actor_id,
192 local: community.local,
193 private_key: community.private_key,
194 public_key: community.public_key,
195 last_refreshed_at: None,
198 let community_id = community.id;
199 blocking(pool, move |conn| {
200 Community::update(conn, community_id, &community_form)
204 let community_id = community.id;
205 let res = CommunityResponse {
206 community: blocking(pool, move |conn| {
207 CommunityView::read(conn, community_id, None)
212 let community_id = res.community.id;
214 chat_server.do_send(SendCommunityRoomMessage {
215 op: UserOperation::EditCommunity,
221 announce_if_community_is_local(remove, &mod_, client, pool).await?;
222 Ok(HttpResponse::Ok().finish())