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::*, base::AnyBase, object::Note, prelude::*};
24 use actix_web::{client::Client, HttpResponse};
27 comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
28 comment_view::CommentView,
29 community::{Community, CommunityForm},
30 community_view::CommunityView,
32 post::{Post, PostForm, PostLike, PostLikeForm},
38 pub async fn receive_undo(
42 chat_server: ChatServerParam,
43 ) -> Result<HttpResponse, LemmyError> {
44 let undo = Undo::from_any_base(activity)?.unwrap();
45 match undo.object().as_single_kind_str() {
46 Some("Delete") => receive_undo_delete(undo, client, pool, chat_server).await,
47 Some("Remove") => receive_undo_remove(undo, client, pool, chat_server).await,
48 Some("Like") => receive_undo_like(undo, client, pool, chat_server).await,
49 Some("Dislike") => receive_undo_dislike(undo, client, pool, chat_server).await,
50 // TODO: handle undo_dislike?
51 _ => receive_unhandled_activity(undo),
55 async fn receive_undo_delete(
59 chat_server: ChatServerParam,
60 ) -> Result<HttpResponse, LemmyError> {
61 let delete = Delete::from_any_base(undo.object().to_owned().one().unwrap())?.unwrap();
62 let type_ = delete.object().as_single_kind_str().unwrap();
64 "Note" => receive_undo_delete_comment(undo, &delete, client, pool, chat_server).await,
65 "Page" => receive_undo_delete_post(undo, &delete, client, pool, chat_server).await,
66 "Group" => receive_undo_delete_community(undo, &delete, client, pool, chat_server).await,
67 d => Err(anyhow!("Undo Delete type {} not supported", d).into()),
71 async fn receive_undo_remove(
75 chat_server: ChatServerParam,
76 ) -> Result<HttpResponse, LemmyError> {
77 let remove = Remove::from_any_base(undo.object().to_owned().one().unwrap())?.unwrap();
79 let type_ = remove.object().as_single_kind_str().unwrap();
81 "Note" => receive_undo_remove_comment(undo, &remove, client, pool, chat_server).await,
82 "Page" => receive_undo_remove_post(undo, &remove, client, pool, chat_server).await,
83 "Group" => receive_undo_remove_community(undo, &remove, client, pool, chat_server).await,
84 d => Err(anyhow!("Undo Delete type {} not supported", d).into()),
88 async fn receive_undo_like(
92 chat_server: ChatServerParam,
93 ) -> Result<HttpResponse, LemmyError> {
94 let like = Like::from_any_base(undo.object().to_owned().one().unwrap())?.unwrap();
96 let type_ = like.object().as_single_kind_str().unwrap();
98 "Note" => receive_undo_like_comment(undo, &like, client, pool, chat_server).await,
99 "Page" => receive_undo_like_post(undo, &like, client, pool, chat_server).await,
100 d => Err(anyhow!("Undo Delete type {} not supported", d).into()),
104 async fn receive_undo_dislike(
108 _chat_server: ChatServerParam,
109 ) -> Result<HttpResponse, LemmyError> {
110 let dislike = Dislike::from_any_base(undo.object().to_owned().one().unwrap())?.unwrap();
112 let type_ = dislike.object().as_single_kind_str().unwrap();
113 Err(anyhow!("Undo Delete type {} not supported", type_).into())
116 async fn receive_undo_delete_comment(
121 chat_server: ChatServerParam,
122 ) -> Result<HttpResponse, LemmyError> {
123 let user = get_user_from_activity(delete, client, pool).await?;
124 let note = Note::from_any_base(delete.object().to_owned().one().unwrap())?.unwrap();
126 let comment_ap_id = CommentForm::from_apub(¬e, client, pool)
130 let comment = get_or_fetch_and_insert_comment(&comment_ap_id, client, pool).await?;
132 let comment_form = CommentForm {
133 content: comment.content.to_owned(),
134 parent_id: comment.parent_id,
135 post_id: comment.post_id,
136 creator_id: comment.creator_id,
138 deleted: Some(false),
141 updated: Some(naive_now()),
142 ap_id: comment.ap_id,
143 local: comment.local,
145 let comment_id = comment.id;
146 blocking(pool, move |conn| {
147 Comment::update(conn, comment_id, &comment_form)
152 let comment_id = comment.id;
154 blocking(pool, move |conn| CommentView::read(conn, comment_id, None)).await??;
156 // TODO get those recipient actor ids from somewhere
157 let recipient_ids = vec![];
158 let res = CommentResponse {
159 comment: comment_view,
164 chat_server.do_send(SendComment {
165 op: UserOperation::EditComment,
170 announce_if_community_is_local(undo, &user, client, pool).await?;
171 Ok(HttpResponse::Ok().finish())
174 async fn receive_undo_remove_comment(
179 chat_server: ChatServerParam,
180 ) -> Result<HttpResponse, LemmyError> {
181 let mod_ = get_user_from_activity(remove, client, pool).await?;
182 let note = Note::from_any_base(remove.object().to_owned().one().unwrap())?.unwrap();
184 let comment_ap_id = CommentForm::from_apub(¬e, client, pool)
188 let comment = get_or_fetch_and_insert_comment(&comment_ap_id, client, pool).await?;
190 let comment_form = CommentForm {
191 content: comment.content.to_owned(),
192 parent_id: comment.parent_id,
193 post_id: comment.post_id,
194 creator_id: comment.creator_id,
195 removed: Some(false),
199 updated: Some(naive_now()),
200 ap_id: comment.ap_id,
201 local: comment.local,
203 let comment_id = comment.id;
204 blocking(pool, move |conn| {
205 Comment::update(conn, comment_id, &comment_form)
210 let comment_id = comment.id;
212 blocking(pool, move |conn| CommentView::read(conn, comment_id, None)).await??;
214 // TODO get those recipient actor ids from somewhere
215 let recipient_ids = vec![];
216 let res = CommentResponse {
217 comment: comment_view,
222 chat_server.do_send(SendComment {
223 op: UserOperation::EditComment,
228 announce_if_community_is_local(undo, &mod_, client, pool).await?;
229 Ok(HttpResponse::Ok().finish())
232 async fn receive_undo_delete_post(
237 chat_server: ChatServerParam,
238 ) -> Result<HttpResponse, LemmyError> {
239 let user = get_user_from_activity(delete, client, pool).await?;
240 let page = PageExt::from_any_base(delete.object().to_owned().one().unwrap())?.unwrap();
242 let post_ap_id = PostForm::from_apub(&page, client, pool)
246 let post = get_or_fetch_and_insert_post(&post_ap_id, client, pool).await?;
248 let post_form = PostForm {
249 name: post.name.to_owned(),
250 url: post.url.to_owned(),
251 body: post.body.to_owned(),
252 creator_id: post.creator_id.to_owned(),
253 community_id: post.community_id,
255 deleted: Some(false),
259 updated: Some(naive_now()),
260 embed_title: post.embed_title,
261 embed_description: post.embed_description,
262 embed_html: post.embed_html,
263 thumbnail_url: post.thumbnail_url,
268 let post_id = post.id;
269 blocking(pool, move |conn| Post::update(conn, post_id, &post_form)).await??;
272 let post_id = post.id;
273 let post_view = blocking(pool, move |conn| PostView::read(conn, post_id, None)).await??;
275 let res = PostResponse { post: post_view };
277 chat_server.do_send(SendPost {
278 op: UserOperation::EditPost,
283 announce_if_community_is_local(undo, &user, client, pool).await?;
284 Ok(HttpResponse::Ok().finish())
287 async fn receive_undo_remove_post(
292 chat_server: ChatServerParam,
293 ) -> Result<HttpResponse, LemmyError> {
294 let mod_ = get_user_from_activity(remove, client, pool).await?;
295 let page = PageExt::from_any_base(remove.object().to_owned().one().unwrap())?.unwrap();
297 let post_ap_id = PostForm::from_apub(&page, client, pool)
301 let post = get_or_fetch_and_insert_post(&post_ap_id, client, pool).await?;
303 let post_form = PostForm {
304 name: post.name.to_owned(),
305 url: post.url.to_owned(),
306 body: post.body.to_owned(),
307 creator_id: post.creator_id.to_owned(),
308 community_id: post.community_id,
309 removed: Some(false),
314 updated: Some(naive_now()),
315 embed_title: post.embed_title,
316 embed_description: post.embed_description,
317 embed_html: post.embed_html,
318 thumbnail_url: post.thumbnail_url,
323 let post_id = post.id;
324 blocking(pool, move |conn| Post::update(conn, post_id, &post_form)).await??;
327 let post_id = post.id;
328 let post_view = blocking(pool, move |conn| PostView::read(conn, post_id, None)).await??;
330 let res = PostResponse { post: post_view };
332 chat_server.do_send(SendPost {
333 op: UserOperation::EditPost,
338 announce_if_community_is_local(undo, &mod_, client, pool).await?;
339 Ok(HttpResponse::Ok().finish())
342 async fn receive_undo_delete_community(
347 chat_server: ChatServerParam,
348 ) -> Result<HttpResponse, LemmyError> {
349 let user = get_user_from_activity(delete, client, pool).await?;
350 let group = GroupExt::from_any_base(delete.object().to_owned().one().unwrap())?.unwrap();
352 let community_actor_id = CommunityForm::from_apub(&group, client, pool)
356 let community = blocking(pool, move |conn| {
357 Community::read_from_actor_id(conn, &community_actor_id)
361 let community_form = CommunityForm {
362 name: community.name.to_owned(),
363 title: community.title.to_owned(),
364 description: community.description.to_owned(),
365 category_id: community.category_id, // Note: need to keep this due to foreign key constraint
366 creator_id: community.creator_id, // Note: need to keep this due to foreign key constraint
369 updated: Some(naive_now()),
370 deleted: Some(false),
371 nsfw: community.nsfw,
372 actor_id: community.actor_id,
373 local: community.local,
374 private_key: community.private_key,
375 public_key: community.public_key,
376 last_refreshed_at: None,
377 icon: Some(community.icon.to_owned()),
378 banner: Some(community.banner.to_owned()),
381 let community_id = community.id;
382 blocking(pool, move |conn| {
383 Community::update(conn, community_id, &community_form)
387 let community_id = community.id;
388 let res = CommunityResponse {
389 community: blocking(pool, move |conn| {
390 CommunityView::read(conn, community_id, None)
395 let community_id = res.community.id;
397 chat_server.do_send(SendCommunityRoomMessage {
398 op: UserOperation::EditCommunity,
404 announce_if_community_is_local(undo, &user, client, pool).await?;
405 Ok(HttpResponse::Ok().finish())
408 async fn receive_undo_remove_community(
413 chat_server: ChatServerParam,
414 ) -> Result<HttpResponse, LemmyError> {
415 let mod_ = get_user_from_activity(remove, client, pool).await?;
416 let group = GroupExt::from_any_base(remove.object().to_owned().one().unwrap())?.unwrap();
418 let community_actor_id = CommunityForm::from_apub(&group, client, pool)
422 let community = blocking(pool, move |conn| {
423 Community::read_from_actor_id(conn, &community_actor_id)
427 let community_form = CommunityForm {
428 name: community.name.to_owned(),
429 title: community.title.to_owned(),
430 description: community.description.to_owned(),
431 category_id: community.category_id, // Note: need to keep this due to foreign key constraint
432 creator_id: community.creator_id, // Note: need to keep this due to foreign key constraint
433 removed: Some(false),
435 updated: Some(naive_now()),
437 nsfw: community.nsfw,
438 actor_id: community.actor_id,
439 local: community.local,
440 private_key: community.private_key,
441 public_key: community.public_key,
442 last_refreshed_at: None,
443 icon: Some(community.icon.to_owned()),
444 banner: Some(community.banner.to_owned()),
447 let community_id = community.id;
448 blocking(pool, move |conn| {
449 Community::update(conn, community_id, &community_form)
453 let community_id = community.id;
454 let res = CommunityResponse {
455 community: blocking(pool, move |conn| {
456 CommunityView::read(conn, community_id, None)
461 let community_id = res.community.id;
463 chat_server.do_send(SendCommunityRoomMessage {
464 op: UserOperation::EditCommunity,
470 announce_if_community_is_local(undo, &mod_, client, pool).await?;
471 Ok(HttpResponse::Ok().finish())
474 async fn receive_undo_like_comment(
479 chat_server: ChatServerParam,
480 ) -> Result<HttpResponse, LemmyError> {
481 let user = get_user_from_activity(like, client, pool).await?;
482 let note = Note::from_any_base(like.object().to_owned().one().unwrap())?.unwrap();
484 let comment = CommentForm::from_apub(¬e, client, pool).await?;
486 let comment_id = get_or_fetch_and_insert_comment(&comment.get_ap_id()?, client, pool)
490 let like_form = CommentLikeForm {
492 post_id: comment.post_id,
496 blocking(pool, move |conn| CommentLike::remove(conn, &like_form)).await??;
500 blocking(pool, move |conn| CommentView::read(conn, comment_id, None)).await??;
502 // TODO get those recipient actor ids from somewhere
503 let recipient_ids = vec![];
504 let res = CommentResponse {
505 comment: comment_view,
510 chat_server.do_send(SendComment {
511 op: UserOperation::CreateCommentLike,
516 announce_if_community_is_local(undo, &user, client, pool).await?;
517 Ok(HttpResponse::Ok().finish())
520 async fn receive_undo_like_post(
525 chat_server: ChatServerParam,
526 ) -> Result<HttpResponse, LemmyError> {
527 let user = get_user_from_activity(like, client, pool).await?;
528 let page = PageExt::from_any_base(like.object().to_owned().one().unwrap())?.unwrap();
530 let post = PostForm::from_apub(&page, client, pool).await?;
532 let post_id = get_or_fetch_and_insert_post(&post.get_ap_id()?, client, pool)
536 let like_form = PostLikeForm {
541 blocking(pool, move |conn| PostLike::remove(conn, &like_form)).await??;
544 let post_view = blocking(pool, move |conn| PostView::read(conn, post_id, None)).await??;
546 let res = PostResponse { post: post_view };
548 chat_server.do_send(SendPost {
549 op: UserOperation::CreatePostLike,
554 announce_if_community_is_local(undo, &user, client, pool).await?;
555 Ok(HttpResponse::Ok().finish())