]> Untitled Git - lemmy.git/blob - src/apub/inbox/activities/like.rs
ce067c87990ae0271119abe923f6c3f544b47146
[lemmy.git] / src / apub / inbox / activities / like.rs
1 use crate::{
2   apub::{
3     fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post},
4     inbox::shared_inbox::{
5       announce_if_community_is_local,
6       get_user_from_activity,
7       receive_unhandled_activity,
8     },
9     FromApub,
10     PageExt,
11   },
12   LemmyContext,
13 };
14 use activitystreams::{activity::Like, base::AnyBase, object::Note, prelude::*};
15 use actix_web::HttpResponse;
16 use anyhow::Context;
17 use lemmy_db::{
18   comment::{CommentForm, CommentLike, CommentLikeForm},
19   comment_view::CommentView,
20   post::{PostForm, PostLike, PostLikeForm},
21   post_view::PostView,
22   Likeable,
23 };
24 use lemmy_structs::{
25   blocking,
26   comment::CommentResponse,
27   post::PostResponse,
28   websocket::{SendComment, SendPost, UserOperation},
29 };
30 use lemmy_utils::{location_info, LemmyError};
31
32 pub async fn receive_like(
33   activity: AnyBase,
34   context: &LemmyContext,
35 ) -> Result<HttpResponse, LemmyError> {
36   let like = Like::from_any_base(activity)?.context(location_info!())?;
37   match like.object().as_single_kind_str() {
38     Some("Page") => receive_like_post(like, context).await,
39     Some("Note") => receive_like_comment(like, context).await,
40     _ => receive_unhandled_activity(like),
41   }
42 }
43
44 async fn receive_like_post(like: Like, context: &LemmyContext) -> Result<HttpResponse, LemmyError> {
45   let user = get_user_from_activity(&like, context).await?;
46   let page = PageExt::from_any_base(like.object().to_owned().one().context(location_info!())?)?
47     .context(location_info!())?;
48
49   let post = PostForm::from_apub(&page, context, None).await?;
50
51   let post_id = get_or_fetch_and_insert_post(&post.get_ap_id()?, context)
52     .await?
53     .id;
54
55   let like_form = PostLikeForm {
56     post_id,
57     user_id: user.id,
58     score: 1,
59   };
60   let user_id = user.id;
61   blocking(context.pool(), move |conn| {
62     PostLike::remove(conn, user_id, post_id)?;
63     PostLike::like(conn, &like_form)
64   })
65   .await??;
66
67   // Refetch the view
68   let post_view = blocking(context.pool(), move |conn| {
69     PostView::read(conn, post_id, None)
70   })
71   .await??;
72
73   let res = PostResponse { post: post_view };
74
75   context.chat_server().do_send(SendPost {
76     op: UserOperation::CreatePostLike,
77     post: res,
78     websocket_id: None,
79   });
80
81   announce_if_community_is_local(like, &user, context).await?;
82   Ok(HttpResponse::Ok().finish())
83 }
84
85 async fn receive_like_comment(
86   like: Like,
87   context: &LemmyContext,
88 ) -> Result<HttpResponse, LemmyError> {
89   let note = Note::from_any_base(like.object().to_owned().one().context(location_info!())?)?
90     .context(location_info!())?;
91   let user = get_user_from_activity(&like, context).await?;
92
93   let comment = CommentForm::from_apub(&note, context, None).await?;
94
95   let comment_id = get_or_fetch_and_insert_comment(&comment.get_ap_id()?, context)
96     .await?
97     .id;
98
99   let like_form = CommentLikeForm {
100     comment_id,
101     post_id: comment.post_id,
102     user_id: user.id,
103     score: 1,
104   };
105   let user_id = user.id;
106   blocking(context.pool(), move |conn| {
107     CommentLike::remove(conn, user_id, comment_id)?;
108     CommentLike::like(conn, &like_form)
109   })
110   .await??;
111
112   // Refetch the view
113   let comment_view = blocking(context.pool(), move |conn| {
114     CommentView::read(conn, comment_id, None)
115   })
116   .await??;
117
118   // TODO get those recipient actor ids from somewhere
119   let recipient_ids = vec![];
120   let res = CommentResponse {
121     comment: comment_view,
122     recipient_ids,
123     form_id: None,
124   };
125
126   context.chat_server().do_send(SendComment {
127     op: UserOperation::CreateCommentLike,
128     comment: res,
129     websocket_id: None,
130   });
131
132   announce_if_community_is_local(like, &user, context).await?;
133   Ok(HttpResponse::Ok().finish())
134 }