3 fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post},
5 announce_if_community_is_local,
6 get_user_from_activity,
7 receive_unhandled_activity,
16 use activitystreams::{activity::Delete, base::AnyBase, object::Note, prelude::*};
17 use actix_web::HttpResponse;
21 comment::CommentResponse,
22 community::CommunityResponse,
24 websocket::{SendComment, SendCommunityRoomMessage, SendPost, UserOperation},
27 comment::{Comment, CommentForm},
28 comment_view::CommentView,
29 community::{Community, CommunityForm},
30 community_view::CommunityView,
32 post::{Post, PostForm},
36 use lemmy_utils::{location_info, LemmyError};
38 pub async fn receive_delete(
40 context: &LemmyContext,
41 ) -> Result<HttpResponse, LemmyError> {
42 let delete = Delete::from_any_base(activity)?.context(location_info!())?;
43 match delete.object().as_single_kind_str() {
44 Some("Page") => receive_delete_post(delete, context).await,
45 Some("Note") => receive_delete_comment(delete, context).await,
46 Some("Group") => receive_delete_community(delete, context).await,
47 _ => receive_unhandled_activity(delete),
51 async fn receive_delete_post(
53 context: &LemmyContext,
54 ) -> Result<HttpResponse, LemmyError> {
55 let user = get_user_from_activity(&delete, context).await?;
56 let page = PageExt::from_any_base(delete.object().to_owned().one().context(location_info!())?)?
57 .context(location_info!())?;
59 let post_ap_id = PostForm::from_apub(&page, context, Some(user.actor_id()?))
63 let post = get_or_fetch_and_insert_post(&post_ap_id, context).await?;
65 let post_form = PostForm {
66 name: post.name.to_owned(),
67 url: post.url.to_owned(),
68 body: post.body.to_owned(),
69 creator_id: post.creator_id.to_owned(),
70 community_id: post.community_id,
76 updated: Some(naive_now()),
77 embed_title: post.embed_title,
78 embed_description: post.embed_description,
79 embed_html: post.embed_html,
80 thumbnail_url: post.thumbnail_url,
81 ap_id: Some(post.ap_id),
85 let post_id = post.id;
86 blocking(context.pool(), move |conn| {
87 Post::update(conn, post_id, &post_form)
92 let post_id = post.id;
93 let post_view = blocking(context.pool(), move |conn| {
94 PostView::read(conn, post_id, None)
98 let res = PostResponse { post: post_view };
100 context.chat_server().do_send(SendPost {
101 op: UserOperation::EditPost,
106 announce_if_community_is_local(delete, &user, context).await?;
107 Ok(HttpResponse::Ok().finish())
110 async fn receive_delete_comment(
112 context: &LemmyContext,
113 ) -> Result<HttpResponse, LemmyError> {
114 let user = get_user_from_activity(&delete, context).await?;
115 let note = Note::from_any_base(delete.object().to_owned().one().context(location_info!())?)?
116 .context(location_info!())?;
118 let comment_ap_id = CommentForm::from_apub(¬e, context, Some(user.actor_id()?))
122 let comment = get_or_fetch_and_insert_comment(&comment_ap_id, context).await?;
124 let comment_form = CommentForm {
125 content: comment.content.to_owned(),
126 parent_id: comment.parent_id,
127 post_id: comment.post_id,
128 creator_id: comment.creator_id,
133 updated: Some(naive_now()),
134 ap_id: Some(comment.ap_id),
135 local: comment.local,
137 let comment_id = comment.id;
138 blocking(context.pool(), move |conn| {
139 Comment::update(conn, comment_id, &comment_form)
144 let comment_id = comment.id;
145 let comment_view = blocking(context.pool(), move |conn| {
146 CommentView::read(conn, comment_id, None)
150 // TODO get those recipient actor ids from somewhere
151 let recipient_ids = vec![];
152 let res = CommentResponse {
153 comment: comment_view,
158 context.chat_server().do_send(SendComment {
159 op: UserOperation::EditComment,
164 announce_if_community_is_local(delete, &user, context).await?;
165 Ok(HttpResponse::Ok().finish())
168 async fn receive_delete_community(
170 context: &LemmyContext,
171 ) -> Result<HttpResponse, LemmyError> {
172 let group = GroupExt::from_any_base(delete.object().to_owned().one().context(location_info!())?)?
173 .context(location_info!())?;
174 let user = get_user_from_activity(&delete, context).await?;
176 let community_actor_id = CommunityForm::from_apub(&group, context, Some(user.actor_id()?))
179 .context(location_info!())?;
181 let community = blocking(context.pool(), move |conn| {
182 Community::read_from_actor_id(conn, &community_actor_id)
186 let community_form = CommunityForm {
187 name: community.name.to_owned(),
188 title: community.title.to_owned(),
189 description: community.description.to_owned(),
190 category_id: community.category_id, // Note: need to keep this due to foreign key constraint
191 creator_id: community.creator_id, // Note: need to keep this due to foreign key constraint
194 updated: Some(naive_now()),
196 nsfw: community.nsfw,
197 actor_id: Some(community.actor_id),
198 local: community.local,
199 private_key: community.private_key,
200 public_key: community.public_key,
201 last_refreshed_at: None,
202 icon: Some(community.icon.to_owned()),
203 banner: Some(community.banner.to_owned()),
206 let community_id = community.id;
207 blocking(context.pool(), move |conn| {
208 Community::update(conn, community_id, &community_form)
212 let community_id = community.id;
213 let res = CommunityResponse {
214 community: blocking(context.pool(), move |conn| {
215 CommunityView::read(conn, community_id, None)
220 let community_id = res.community.id;
222 context.chat_server().do_send(SendCommunityRoomMessage {
223 op: UserOperation::EditCommunity,
229 announce_if_community_is_local(delete, &user, context).await?;
230 Ok(HttpResponse::Ok().finish())