]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/voting/mod.rs
Merge pull request #2593 from LemmyNet/refactor-notifications
[lemmy.git] / crates / apub / src / activities / voting / mod.rs
1 use crate::{
2   activities::community::send_activity_in_community,
3   activity_lists::AnnouncableActivities,
4   fetcher::post_or_comment::PostOrComment,
5   objects::{comment::ApubComment, person::ApubPerson, post::ApubPost},
6   protocol::activities::voting::{
7     undo_vote::UndoVote,
8     vote::{Vote, VoteType},
9   },
10   SendActivity,
11 };
12 use activitypub_federation::core::object_id::ObjectId;
13 use lemmy_api_common::{
14   comment::{CommentResponse, CreateCommentLike},
15   context::LemmyContext,
16   post::{CreatePostLike, PostResponse},
17   sensitive::Sensitive,
18   utils::get_local_user_view_from_jwt,
19   websocket::{
20     send::{send_comment_ws_message_simple, send_post_ws_message},
21     UserOperation,
22   },
23 };
24 use lemmy_db_schema::{
25   newtypes::CommunityId,
26   source::{
27     comment::{CommentLike, CommentLikeForm},
28     community::Community,
29     person::Person,
30     post::{PostLike, PostLikeForm},
31   },
32   traits::{Crud, Likeable},
33 };
34 use lemmy_utils::error::LemmyError;
35
36 pub mod undo_vote;
37 pub mod vote;
38
39 #[async_trait::async_trait(?Send)]
40 impl SendActivity for CreatePostLike {
41   type Response = PostResponse;
42
43   async fn send_activity(
44     request: &Self,
45     response: &Self::Response,
46     context: &LemmyContext,
47   ) -> Result<(), LemmyError> {
48     let object_id = ObjectId::new(response.post_view.post.ap_id.clone());
49     let community_id = response.post_view.community.id;
50     send_activity(
51       object_id,
52       community_id,
53       request.score,
54       &request.auth,
55       context,
56     )
57     .await
58   }
59 }
60
61 #[async_trait::async_trait(?Send)]
62 impl SendActivity for CreateCommentLike {
63   type Response = CommentResponse;
64
65   async fn send_activity(
66     request: &Self,
67     response: &Self::Response,
68     context: &LemmyContext,
69   ) -> Result<(), LemmyError> {
70     let object_id = ObjectId::new(response.comment_view.comment.ap_id.clone());
71     let community_id = response.comment_view.community.id;
72     send_activity(
73       object_id,
74       community_id,
75       request.score,
76       &request.auth,
77       context,
78     )
79     .await
80   }
81 }
82
83 async fn send_activity(
84   object_id: ObjectId<PostOrComment>,
85   community_id: CommunityId,
86   score: i16,
87   jwt: &Sensitive<String>,
88   context: &LemmyContext,
89 ) -> Result<(), LemmyError> {
90   let community = Community::read(context.pool(), community_id).await?.into();
91   let local_user_view = get_local_user_view_from_jwt(jwt, context.pool(), context.secret()).await?;
92   let actor = Person::read(context.pool(), local_user_view.person.id)
93     .await?
94     .into();
95
96   // score of 1 means upvote, -1 downvote, 0 undo a previous vote
97   if score != 0 {
98     let vote = Vote::new(object_id, &actor, &community, score.try_into()?, context)?;
99     let activity = AnnouncableActivities::Vote(vote);
100     send_activity_in_community(activity, &actor, &community, vec![], false, context).await
101   } else {
102     // Lemmy API doesnt distinguish between Undo/Like and Undo/Dislike, so we hardcode it here.
103     let vote = Vote::new(object_id, &actor, &community, VoteType::Like, context)?;
104     let undo_vote = UndoVote::new(vote, &actor, &community, context)?;
105     let activity = AnnouncableActivities::UndoVote(undo_vote);
106     send_activity_in_community(activity, &actor, &community, vec![], false, context).await
107   }
108 }
109
110 #[tracing::instrument(skip_all)]
111 async fn vote_comment(
112   vote_type: &VoteType,
113   actor: ApubPerson,
114   comment: &ApubComment,
115   context: &LemmyContext,
116 ) -> Result<(), LemmyError> {
117   let comment_id = comment.id;
118   let like_form = CommentLikeForm {
119     comment_id,
120     post_id: comment.post_id,
121     person_id: actor.id,
122     score: vote_type.into(),
123   };
124   let person_id = actor.id;
125   CommentLike::remove(context.pool(), person_id, comment_id).await?;
126   CommentLike::like(context.pool(), &like_form).await?;
127
128   send_comment_ws_message_simple(comment_id, UserOperation::CreateCommentLike, context).await?;
129   Ok(())
130 }
131
132 #[tracing::instrument(skip_all)]
133 async fn vote_post(
134   vote_type: &VoteType,
135   actor: ApubPerson,
136   post: &ApubPost,
137   context: &LemmyContext,
138 ) -> Result<(), LemmyError> {
139   let post_id = post.id;
140   let like_form = PostLikeForm {
141     post_id: post.id,
142     person_id: actor.id,
143     score: vote_type.into(),
144   };
145   let person_id = actor.id;
146   PostLike::remove(context.pool(), person_id, post_id).await?;
147   PostLike::like(context.pool(), &like_form).await?;
148
149   send_post_ws_message(post.id, UserOperation::CreatePostLike, None, None, context).await?;
150   Ok(())
151 }
152
153 #[tracing::instrument(skip_all)]
154 async fn undo_vote_comment(
155   actor: ApubPerson,
156   comment: &ApubComment,
157   context: &LemmyContext,
158 ) -> Result<(), LemmyError> {
159   let comment_id = comment.id;
160   let person_id = actor.id;
161   CommentLike::remove(context.pool(), person_id, comment_id).await?;
162
163   send_comment_ws_message_simple(comment_id, UserOperation::CreateCommentLike, context).await?;
164   Ok(())
165 }
166
167 #[tracing::instrument(skip_all)]
168 async fn undo_vote_post(
169   actor: ApubPerson,
170   post: &ApubPost,
171   context: &LemmyContext,
172 ) -> Result<(), LemmyError> {
173   let post_id = post.id;
174   let person_id = actor.id;
175   PostLike::remove(context.pool(), person_id, post_id).await?;
176
177   send_post_ws_message(post_id, UserOperation::CreatePostLike, None, None, context).await?;
178   Ok(())
179 }