]> Untitled Git - lemmy.git/blob - server/src/apub/inbox/activities/dislike.rs
routes.api: fix get_captcha endpoint (#1135)
[lemmy.git] / server / src / apub / inbox / activities / dislike.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::Dislike, 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_dislike(
32   activity: AnyBase,
33   context: &LemmyContext,
34 ) -> Result<HttpResponse, LemmyError> {
35   let dislike = Dislike::from_any_base(activity)?.context(location_info!())?;
36   match dislike.object().as_single_kind_str() {
37     Some("Page") => receive_dislike_post(dislike, context).await,
38     Some("Note") => receive_dislike_comment(dislike, context).await,
39     _ => receive_unhandled_activity(dislike),
40   }
41 }
42
43 async fn receive_dislike_post(
44   dislike: Dislike,
45   context: &LemmyContext,
46 ) -> Result<HttpResponse, LemmyError> {
47   let user = get_user_from_activity(&dislike, context).await?;
48   let page = PageExt::from_any_base(
49     dislike
50       .object()
51       .to_owned()
52       .one()
53       .context(location_info!())?,
54   )?
55   .context(location_info!())?;
56
57   let post = PostForm::from_apub(&page, context, None).await?;
58
59   let post_id = get_or_fetch_and_insert_post(&post.get_ap_id()?, context)
60     .await?
61     .id;
62
63   let like_form = PostLikeForm {
64     post_id,
65     user_id: user.id,
66     score: -1,
67   };
68   let user_id = user.id;
69   blocking(context.pool(), move |conn| {
70     PostLike::remove(conn, user_id, post_id)?;
71     PostLike::like(conn, &like_form)
72   })
73   .await??;
74
75   // Refetch the view
76   let post_view = blocking(context.pool(), move |conn| {
77     PostView::read(conn, post_id, None)
78   })
79   .await??;
80
81   let res = PostResponse { post: post_view };
82
83   context.chat_server().do_send(SendPost {
84     op: UserOperation::CreatePostLike,
85     post: res,
86     websocket_id: None,
87   });
88
89   announce_if_community_is_local(dislike, &user, context).await?;
90   Ok(HttpResponse::Ok().finish())
91 }
92
93 async fn receive_dislike_comment(
94   dislike: Dislike,
95   context: &LemmyContext,
96 ) -> Result<HttpResponse, LemmyError> {
97   let note = Note::from_any_base(
98     dislike
99       .object()
100       .to_owned()
101       .one()
102       .context(location_info!())?,
103   )?
104   .context(location_info!())?;
105   let user = get_user_from_activity(&dislike, context).await?;
106
107   let comment = CommentForm::from_apub(&note, context, None).await?;
108
109   let comment_id = get_or_fetch_and_insert_comment(&comment.get_ap_id()?, context)
110     .await?
111     .id;
112
113   let like_form = CommentLikeForm {
114     comment_id,
115     post_id: comment.post_id,
116     user_id: user.id,
117     score: -1,
118   };
119   let user_id = user.id;
120   blocking(context.pool(), move |conn| {
121     CommentLike::remove(conn, user_id, comment_id)?;
122     CommentLike::like(conn, &like_form)
123   })
124   .await??;
125
126   // Refetch the view
127   let comment_view = blocking(context.pool(), move |conn| {
128     CommentView::read(conn, comment_id, None)
129   })
130   .await??;
131
132   // TODO get those recipient actor ids from somewhere
133   let recipient_ids = vec![];
134   let res = CommentResponse {
135     comment: comment_view,
136     recipient_ids,
137     form_id: None,
138   };
139
140   context.chat_server().do_send(SendComment {
141     op: UserOperation::CreateCommentLike,
142     comment: res,
143     websocket_id: None,
144   });
145
146   announce_if_community_is_local(dislike, &user, context).await?;
147   Ok(HttpResponse::Ok().finish())
148 }