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