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,
16 routes::ChatServerParam,
18 server::{SendComment, SendCommunityRoomMessage, SendPost},
24 use activitystreams::{activity::Delete, base::AnyBase, object::Note, prelude::*};
25 use actix_web::{client::Client, HttpResponse};
27 comment::{Comment, CommentForm},
28 comment_view::CommentView,
29 community::{Community, CommunityForm},
30 community_view::CommunityView,
32 post::{Post, PostForm},
37 pub async fn receive_delete(
41 chat_server: ChatServerParam,
42 ) -> Result<HttpResponse, LemmyError> {
43 let delete = Delete::from_any_base(activity)?.unwrap();
44 match delete.object().as_single_kind_str() {
45 Some("Page") => receive_delete_post(delete, client, pool, chat_server).await,
46 Some("Note") => receive_delete_comment(delete, client, pool, chat_server).await,
47 Some("Group") => receive_delete_community(delete, client, pool, chat_server).await,
48 _ => receive_unhandled_activity(delete),
52 async fn receive_delete_post(
56 chat_server: ChatServerParam,
57 ) -> Result<HttpResponse, LemmyError> {
58 let user = get_user_from_activity(&delete, client, pool).await?;
59 let page = PageExt::from_any_base(delete.object().to_owned().one().unwrap())?.unwrap();
61 let post_ap_id = PostForm::from_apub(&page, client, pool, Some(user.actor_id()?))
65 let post = get_or_fetch_and_insert_post(&post_ap_id, client, pool).await?;
67 let post_form = PostForm {
68 name: post.name.to_owned(),
69 url: post.url.to_owned(),
70 body: post.body.to_owned(),
71 creator_id: post.creator_id.to_owned(),
72 community_id: post.community_id,
78 updated: Some(naive_now()),
79 embed_title: post.embed_title,
80 embed_description: post.embed_description,
81 embed_html: post.embed_html,
82 thumbnail_url: post.thumbnail_url,
87 let post_id = post.id;
88 blocking(pool, move |conn| Post::update(conn, post_id, &post_form)).await??;
91 let post_id = post.id;
92 let post_view = blocking(pool, move |conn| PostView::read(conn, post_id, None)).await??;
94 let res = PostResponse { post: post_view };
96 chat_server.do_send(SendPost {
97 op: UserOperation::EditPost,
102 announce_if_community_is_local(delete, &user, client, pool).await?;
103 Ok(HttpResponse::Ok().finish())
106 async fn receive_delete_comment(
110 chat_server: ChatServerParam,
111 ) -> Result<HttpResponse, LemmyError> {
112 let user = get_user_from_activity(&delete, client, pool).await?;
113 let note = Note::from_any_base(delete.object().to_owned().one().unwrap())?.unwrap();
115 let comment_ap_id = CommentForm::from_apub(¬e, client, pool, Some(user.actor_id()?))
119 let comment = get_or_fetch_and_insert_comment(&comment_ap_id, client, pool).await?;
121 let comment_form = CommentForm {
122 content: comment.content.to_owned(),
123 parent_id: comment.parent_id,
124 post_id: comment.post_id,
125 creator_id: comment.creator_id,
130 updated: Some(naive_now()),
131 ap_id: comment.ap_id,
132 local: comment.local,
134 let comment_id = comment.id;
135 blocking(pool, move |conn| {
136 Comment::update(conn, comment_id, &comment_form)
141 let comment_id = comment.id;
143 blocking(pool, move |conn| CommentView::read(conn, comment_id, None)).await??;
145 // TODO get those recipient actor ids from somewhere
146 let recipient_ids = vec![];
147 let res = CommentResponse {
148 comment: comment_view,
153 chat_server.do_send(SendComment {
154 op: UserOperation::EditComment,
159 announce_if_community_is_local(delete, &user, client, pool).await?;
160 Ok(HttpResponse::Ok().finish())
163 async fn receive_delete_community(
167 chat_server: ChatServerParam,
168 ) -> Result<HttpResponse, LemmyError> {
169 let group = GroupExt::from_any_base(delete.object().to_owned().one().unwrap())?.unwrap();
170 let user = get_user_from_activity(&delete, client, pool).await?;
172 let community_actor_id = CommunityForm::from_apub(&group, client, pool, Some(user.actor_id()?))
176 let community = blocking(pool, move |conn| {
177 Community::read_from_actor_id(conn, &community_actor_id)
181 let community_form = CommunityForm {
182 name: community.name.to_owned(),
183 title: community.title.to_owned(),
184 description: community.description.to_owned(),
185 category_id: community.category_id, // Note: need to keep this due to foreign key constraint
186 creator_id: community.creator_id, // Note: need to keep this due to foreign key constraint
189 updated: Some(naive_now()),
191 nsfw: community.nsfw,
192 actor_id: community.actor_id,
193 local: community.local,
194 private_key: community.private_key,
195 public_key: community.public_key,
196 last_refreshed_at: None,
197 icon: Some(community.icon.to_owned()),
198 banner: Some(community.banner.to_owned()),
201 let community_id = community.id;
202 blocking(pool, move |conn| {
203 Community::update(conn, community_id, &community_form)
207 let community_id = community.id;
208 let res = CommunityResponse {
209 community: blocking(pool, move |conn| {
210 CommunityView::read(conn, community_id, None)
215 let community_id = res.community.id;
217 chat_server.do_send(SendCommunityRoomMessage {
218 op: UserOperation::EditCommunity,
224 announce_if_community_is_local(delete, &user, client, pool).await?;
225 Ok(HttpResponse::Ok().finish())