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,
379 let community_id = community.id;
380 blocking(pool, move |conn| {
381 Community::update(conn, community_id, &community_form)
385 let community_id = community.id;
386 let res = CommunityResponse {
387 community: blocking(pool, move |conn| {
388 CommunityView::read(conn, community_id, None)
393 let community_id = res.community.id;
395 chat_server.do_send(SendCommunityRoomMessage {
396 op: UserOperation::EditCommunity,
402 announce_if_community_is_local(undo, &user, client, pool).await?;
403 Ok(HttpResponse::Ok().finish())
406 async fn receive_undo_remove_community(
411 chat_server: ChatServerParam,
412 ) -> Result<HttpResponse, LemmyError> {
413 let mod_ = get_user_from_activity(remove, client, pool).await?;
414 let group = GroupExt::from_any_base(remove.object().to_owned().one().unwrap())?.unwrap();
416 let community_actor_id = CommunityForm::from_apub(&group, client, pool)
420 let community = blocking(pool, move |conn| {
421 Community::read_from_actor_id(conn, &community_actor_id)
425 let community_form = CommunityForm {
426 name: community.name.to_owned(),
427 title: community.title.to_owned(),
428 description: community.description.to_owned(),
429 category_id: community.category_id, // Note: need to keep this due to foreign key constraint
430 creator_id: community.creator_id, // Note: need to keep this due to foreign key constraint
431 removed: Some(false),
433 updated: Some(naive_now()),
435 nsfw: community.nsfw,
436 actor_id: community.actor_id,
437 local: community.local,
438 private_key: community.private_key,
439 public_key: community.public_key,
440 last_refreshed_at: None,
443 let community_id = community.id;
444 blocking(pool, move |conn| {
445 Community::update(conn, community_id, &community_form)
449 let community_id = community.id;
450 let res = CommunityResponse {
451 community: blocking(pool, move |conn| {
452 CommunityView::read(conn, community_id, None)
457 let community_id = res.community.id;
459 chat_server.do_send(SendCommunityRoomMessage {
460 op: UserOperation::EditCommunity,
466 announce_if_community_is_local(undo, &mod_, client, pool).await?;
467 Ok(HttpResponse::Ok().finish())
470 async fn receive_undo_like_comment(
475 chat_server: ChatServerParam,
476 ) -> Result<HttpResponse, LemmyError> {
477 let user = get_user_from_activity(like, client, pool).await?;
478 let note = Note::from_any_base(like.object().to_owned().one().unwrap())?.unwrap();
480 let comment = CommentForm::from_apub(¬e, client, pool).await?;
482 let comment_id = get_or_fetch_and_insert_comment(&comment.get_ap_id()?, client, pool)
486 let like_form = CommentLikeForm {
488 post_id: comment.post_id,
492 blocking(pool, move |conn| CommentLike::remove(conn, &like_form)).await??;
496 blocking(pool, move |conn| CommentView::read(conn, comment_id, None)).await??;
498 // TODO get those recipient actor ids from somewhere
499 let recipient_ids = vec![];
500 let res = CommentResponse {
501 comment: comment_view,
506 chat_server.do_send(SendComment {
507 op: UserOperation::CreateCommentLike,
512 announce_if_community_is_local(undo, &user, client, pool).await?;
513 Ok(HttpResponse::Ok().finish())
516 async fn receive_undo_like_post(
521 chat_server: ChatServerParam,
522 ) -> Result<HttpResponse, LemmyError> {
523 let user = get_user_from_activity(like, client, pool).await?;
524 let page = PageExt::from_any_base(like.object().to_owned().one().unwrap())?.unwrap();
526 let post = PostForm::from_apub(&page, client, pool).await?;
528 let post_id = get_or_fetch_and_insert_post(&post.get_ap_id()?, client, pool)
532 let like_form = PostLikeForm {
537 blocking(pool, move |conn| PostLike::remove(conn, &like_form)).await??;
540 let post_view = blocking(pool, move |conn| PostView::read(conn, post_id, None)).await??;
542 let res = PostResponse { post: post_view };
544 chat_server.do_send(SendPost {
545 op: UserOperation::CreatePostLike,
550 announce_if_community_is_local(undo, &user, client, pool).await?;
551 Ok(HttpResponse::Ok().finish())