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::{
26 base::{AnyBase, AsBase},
30 use actix_web::{client::Client, HttpResponse};
33 comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
34 comment_view::CommentView,
35 community::{Community, CommunityForm},
36 community_view::CommunityView,
38 post::{Post, PostForm, PostLike, PostLikeForm},
44 pub async fn receive_undo(
48 chat_server: ChatServerParam,
49 ) -> Result<HttpResponse, LemmyError> {
50 let undo = Undo::from_any_base(activity)?.unwrap();
51 match undo.object().as_single_kind_str() {
52 Some("Delete") => receive_undo_delete(undo, client, pool, chat_server).await,
53 Some("Remove") => receive_undo_remove(undo, client, pool, chat_server).await,
54 Some("Like") => receive_undo_like(undo, client, pool, chat_server).await,
55 Some("Dislike") => receive_undo_dislike(undo, client, pool, chat_server).await,
56 _ => receive_unhandled_activity(undo),
60 fn check_is_undo_valid<T, A>(outer_activity: &Undo, inner_activity: &T) -> Result<(), LemmyError>
62 T: AsBase<A> + ActorAndObjectRef,
64 let outer_actor = outer_activity.actor()?;
65 let outer_actor_uri = outer_actor.as_single_xsd_any_uri().unwrap();
67 let inner_actor = inner_activity.actor()?;
68 let inner_actor_uri = inner_actor.as_single_xsd_any_uri().unwrap();
70 if outer_actor_uri.domain() != inner_actor_uri.domain() {
71 Err(anyhow!("Cant undo activities from a different instance").into())
77 async fn receive_undo_delete(
81 chat_server: ChatServerParam,
82 ) -> Result<HttpResponse, LemmyError> {
83 let delete = Delete::from_any_base(undo.object().to_owned().one().unwrap())?.unwrap();
84 check_is_undo_valid(&undo, &delete)?;
85 let type_ = delete.object().as_single_kind_str().unwrap();
87 "Note" => receive_undo_delete_comment(undo, &delete, client, pool, chat_server).await,
88 "Page" => receive_undo_delete_post(undo, &delete, client, pool, chat_server).await,
89 "Group" => receive_undo_delete_community(undo, &delete, client, pool, chat_server).await,
90 d => Err(anyhow!("Undo Delete type {} not supported", d).into()),
94 async fn receive_undo_remove(
98 chat_server: ChatServerParam,
99 ) -> Result<HttpResponse, LemmyError> {
100 let remove = Remove::from_any_base(undo.object().to_owned().one().unwrap())?.unwrap();
101 check_is_undo_valid(&undo, &remove)?;
103 let type_ = remove.object().as_single_kind_str().unwrap();
105 "Note" => receive_undo_remove_comment(undo, &remove, client, pool, chat_server).await,
106 "Page" => receive_undo_remove_post(undo, &remove, client, pool, chat_server).await,
107 "Group" => receive_undo_remove_community(undo, &remove, client, pool, chat_server).await,
108 d => Err(anyhow!("Undo Delete type {} not supported", d).into()),
112 async fn receive_undo_like(
116 chat_server: ChatServerParam,
117 ) -> Result<HttpResponse, LemmyError> {
118 let like = Like::from_any_base(undo.object().to_owned().one().unwrap())?.unwrap();
119 check_is_undo_valid(&undo, &like)?;
121 let type_ = like.object().as_single_kind_str().unwrap();
123 "Note" => receive_undo_like_comment(undo, &like, client, pool, chat_server).await,
124 "Page" => receive_undo_like_post(undo, &like, client, pool, chat_server).await,
125 d => Err(anyhow!("Undo Delete type {} not supported", d).into()),
129 async fn receive_undo_dislike(
133 _chat_server: ChatServerParam,
134 ) -> Result<HttpResponse, LemmyError> {
135 let dislike = Dislike::from_any_base(undo.object().to_owned().one().unwrap())?.unwrap();
136 check_is_undo_valid(&undo, &dislike)?;
138 // TODO: need to implement Undo<Dislike>
140 let type_ = dislike.object().as_single_kind_str().unwrap();
141 Err(anyhow!("Undo Delete type {} not supported", type_).into())
144 async fn receive_undo_delete_comment(
149 chat_server: ChatServerParam,
150 ) -> Result<HttpResponse, LemmyError> {
151 let user = get_user_from_activity(delete, client, pool).await?;
152 let note = Note::from_any_base(delete.object().to_owned().one().unwrap())?.unwrap();
154 let comment_ap_id = CommentForm::from_apub(¬e, client, pool, Some(user.actor_id()?))
158 let comment = get_or_fetch_and_insert_comment(&comment_ap_id, client, pool).await?;
160 let comment_form = CommentForm {
161 content: comment.content.to_owned(),
162 parent_id: comment.parent_id,
163 post_id: comment.post_id,
164 creator_id: comment.creator_id,
166 deleted: Some(false),
169 updated: Some(naive_now()),
170 ap_id: comment.ap_id,
171 local: comment.local,
173 let comment_id = comment.id;
174 blocking(pool, move |conn| {
175 Comment::update(conn, comment_id, &comment_form)
180 let comment_id = comment.id;
182 blocking(pool, move |conn| CommentView::read(conn, comment_id, None)).await??;
184 // TODO get those recipient actor ids from somewhere
185 let recipient_ids = vec![];
186 let res = CommentResponse {
187 comment: comment_view,
192 chat_server.do_send(SendComment {
193 op: UserOperation::EditComment,
198 announce_if_community_is_local(undo, &user, client, pool).await?;
199 Ok(HttpResponse::Ok().finish())
202 async fn receive_undo_remove_comment(
207 chat_server: ChatServerParam,
208 ) -> Result<HttpResponse, LemmyError> {
209 let mod_ = get_user_from_activity(remove, client, pool).await?;
210 let note = Note::from_any_base(remove.object().to_owned().one().unwrap())?.unwrap();
212 let comment_ap_id = CommentForm::from_apub(¬e, client, pool, None)
216 let comment = get_or_fetch_and_insert_comment(&comment_ap_id, client, pool).await?;
218 let comment_form = CommentForm {
219 content: comment.content.to_owned(),
220 parent_id: comment.parent_id,
221 post_id: comment.post_id,
222 creator_id: comment.creator_id,
223 removed: Some(false),
227 updated: Some(naive_now()),
228 ap_id: comment.ap_id,
229 local: comment.local,
231 let comment_id = comment.id;
232 blocking(pool, move |conn| {
233 Comment::update(conn, comment_id, &comment_form)
238 let comment_id = comment.id;
240 blocking(pool, move |conn| CommentView::read(conn, comment_id, None)).await??;
242 // TODO get those recipient actor ids from somewhere
243 let recipient_ids = vec![];
244 let res = CommentResponse {
245 comment: comment_view,
250 chat_server.do_send(SendComment {
251 op: UserOperation::EditComment,
256 announce_if_community_is_local(undo, &mod_, client, pool).await?;
257 Ok(HttpResponse::Ok().finish())
260 async fn receive_undo_delete_post(
265 chat_server: ChatServerParam,
266 ) -> Result<HttpResponse, LemmyError> {
267 let user = get_user_from_activity(delete, client, pool).await?;
268 let page = PageExt::from_any_base(delete.object().to_owned().one().unwrap())?.unwrap();
270 let post_ap_id = PostForm::from_apub(&page, client, pool, Some(user.actor_id()?))
274 let post = get_or_fetch_and_insert_post(&post_ap_id, client, pool).await?;
276 let post_form = PostForm {
277 name: post.name.to_owned(),
278 url: post.url.to_owned(),
279 body: post.body.to_owned(),
280 creator_id: post.creator_id.to_owned(),
281 community_id: post.community_id,
283 deleted: Some(false),
287 updated: Some(naive_now()),
288 embed_title: post.embed_title,
289 embed_description: post.embed_description,
290 embed_html: post.embed_html,
291 thumbnail_url: post.thumbnail_url,
296 let post_id = post.id;
297 blocking(pool, move |conn| Post::update(conn, post_id, &post_form)).await??;
300 let post_id = post.id;
301 let post_view = blocking(pool, move |conn| PostView::read(conn, post_id, None)).await??;
303 let res = PostResponse { post: post_view };
305 chat_server.do_send(SendPost {
306 op: UserOperation::EditPost,
311 announce_if_community_is_local(undo, &user, client, pool).await?;
312 Ok(HttpResponse::Ok().finish())
315 async fn receive_undo_remove_post(
320 chat_server: ChatServerParam,
321 ) -> Result<HttpResponse, LemmyError> {
322 let mod_ = get_user_from_activity(remove, client, pool).await?;
323 let page = PageExt::from_any_base(remove.object().to_owned().one().unwrap())?.unwrap();
325 let post_ap_id = PostForm::from_apub(&page, client, pool, None)
329 let post = get_or_fetch_and_insert_post(&post_ap_id, client, pool).await?;
331 let post_form = PostForm {
332 name: post.name.to_owned(),
333 url: post.url.to_owned(),
334 body: post.body.to_owned(),
335 creator_id: post.creator_id.to_owned(),
336 community_id: post.community_id,
337 removed: Some(false),
342 updated: Some(naive_now()),
343 embed_title: post.embed_title,
344 embed_description: post.embed_description,
345 embed_html: post.embed_html,
346 thumbnail_url: post.thumbnail_url,
351 let post_id = post.id;
352 blocking(pool, move |conn| Post::update(conn, post_id, &post_form)).await??;
355 let post_id = post.id;
356 let post_view = blocking(pool, move |conn| PostView::read(conn, post_id, None)).await??;
358 let res = PostResponse { post: post_view };
360 chat_server.do_send(SendPost {
361 op: UserOperation::EditPost,
366 announce_if_community_is_local(undo, &mod_, client, pool).await?;
367 Ok(HttpResponse::Ok().finish())
370 async fn receive_undo_delete_community(
375 chat_server: ChatServerParam,
376 ) -> Result<HttpResponse, LemmyError> {
377 let user = get_user_from_activity(delete, client, pool).await?;
378 let group = GroupExt::from_any_base(delete.object().to_owned().one().unwrap())?.unwrap();
380 let community_actor_id = CommunityForm::from_apub(&group, client, pool, Some(user.actor_id()?))
384 let community = blocking(pool, move |conn| {
385 Community::read_from_actor_id(conn, &community_actor_id)
389 let community_form = CommunityForm {
390 name: community.name.to_owned(),
391 title: community.title.to_owned(),
392 description: community.description.to_owned(),
393 category_id: community.category_id, // Note: need to keep this due to foreign key constraint
394 creator_id: community.creator_id, // Note: need to keep this due to foreign key constraint
397 updated: Some(naive_now()),
398 deleted: Some(false),
399 nsfw: community.nsfw,
400 actor_id: community.actor_id,
401 local: community.local,
402 private_key: community.private_key,
403 public_key: community.public_key,
404 last_refreshed_at: None,
405 icon: Some(community.icon.to_owned()),
406 banner: Some(community.banner.to_owned()),
409 let community_id = community.id;
410 blocking(pool, move |conn| {
411 Community::update(conn, community_id, &community_form)
415 let community_id = community.id;
416 let res = CommunityResponse {
417 community: blocking(pool, move |conn| {
418 CommunityView::read(conn, community_id, None)
423 let community_id = res.community.id;
425 chat_server.do_send(SendCommunityRoomMessage {
426 op: UserOperation::EditCommunity,
432 announce_if_community_is_local(undo, &user, client, pool).await?;
433 Ok(HttpResponse::Ok().finish())
436 async fn receive_undo_remove_community(
441 chat_server: ChatServerParam,
442 ) -> Result<HttpResponse, LemmyError> {
443 let mod_ = get_user_from_activity(remove, client, pool).await?;
444 let group = GroupExt::from_any_base(remove.object().to_owned().one().unwrap())?.unwrap();
446 let community_actor_id = CommunityForm::from_apub(&group, client, pool, Some(mod_.actor_id()?))
450 let community = blocking(pool, move |conn| {
451 Community::read_from_actor_id(conn, &community_actor_id)
455 let community_form = CommunityForm {
456 name: community.name.to_owned(),
457 title: community.title.to_owned(),
458 description: community.description.to_owned(),
459 category_id: community.category_id, // Note: need to keep this due to foreign key constraint
460 creator_id: community.creator_id, // Note: need to keep this due to foreign key constraint
461 removed: Some(false),
463 updated: Some(naive_now()),
465 nsfw: community.nsfw,
466 actor_id: community.actor_id,
467 local: community.local,
468 private_key: community.private_key,
469 public_key: community.public_key,
470 last_refreshed_at: None,
471 icon: Some(community.icon.to_owned()),
472 banner: Some(community.banner.to_owned()),
475 let community_id = community.id;
476 blocking(pool, move |conn| {
477 Community::update(conn, community_id, &community_form)
481 let community_id = community.id;
482 let res = CommunityResponse {
483 community: blocking(pool, move |conn| {
484 CommunityView::read(conn, community_id, None)
489 let community_id = res.community.id;
491 chat_server.do_send(SendCommunityRoomMessage {
492 op: UserOperation::EditCommunity,
498 announce_if_community_is_local(undo, &mod_, client, pool).await?;
499 Ok(HttpResponse::Ok().finish())
502 async fn receive_undo_like_comment(
507 chat_server: ChatServerParam,
508 ) -> Result<HttpResponse, LemmyError> {
509 let user = get_user_from_activity(like, client, pool).await?;
510 let note = Note::from_any_base(like.object().to_owned().one().unwrap())?.unwrap();
512 let comment = CommentForm::from_apub(¬e, client, pool, None).await?;
514 let comment_id = get_or_fetch_and_insert_comment(&comment.get_ap_id()?, client, pool)
518 let like_form = CommentLikeForm {
520 post_id: comment.post_id,
524 blocking(pool, move |conn| CommentLike::remove(conn, &like_form)).await??;
528 blocking(pool, move |conn| CommentView::read(conn, comment_id, None)).await??;
530 // TODO get those recipient actor ids from somewhere
531 let recipient_ids = vec![];
532 let res = CommentResponse {
533 comment: comment_view,
538 chat_server.do_send(SendComment {
539 op: UserOperation::CreateCommentLike,
544 announce_if_community_is_local(undo, &user, client, pool).await?;
545 Ok(HttpResponse::Ok().finish())
548 async fn receive_undo_like_post(
553 chat_server: ChatServerParam,
554 ) -> Result<HttpResponse, LemmyError> {
555 let user = get_user_from_activity(like, client, pool).await?;
556 let page = PageExt::from_any_base(like.object().to_owned().one().unwrap())?.unwrap();
558 let post = PostForm::from_apub(&page, client, pool, None).await?;
560 let post_id = get_or_fetch_and_insert_post(&post.get_ap_id()?, client, pool)
564 let like_form = PostLikeForm {
569 blocking(pool, move |conn| PostLike::remove(conn, &like_form)).await??;
572 let post_view = blocking(pool, move |conn| PostView::read(conn, post_id, None)).await??;
574 let res = PostResponse { post: post_view };
576 chat_server.do_send(SendPost {
577 op: UserOperation::CreatePostLike,
582 announce_if_community_is_local(undo, &user, client, pool).await?;
583 Ok(HttpResponse::Ok().finish())