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