]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/voting/mod.rs
Check user accepted before sending jwt in password reset (fixes #2591) (#2597)
[lemmy.git] / crates / apub / src / activities / voting / mod.rs
1 use crate::{
2   objects::{comment::ApubComment, person::ApubPerson, post::ApubPost},
3   protocol::activities::voting::vote::VoteType,
4 };
5 use lemmy_db_schema::{
6   source::{
7     comment::{CommentLike, CommentLikeForm},
8     post::{PostLike, PostLikeForm},
9   },
10   traits::Likeable,
11 };
12 use lemmy_utils::error::LemmyError;
13 use lemmy_websocket::{
14   send::{send_comment_ws_message_simple, send_post_ws_message},
15   LemmyContext,
16   UserOperation,
17 };
18
19 pub mod undo_vote;
20 pub mod vote;
21
22 #[tracing::instrument(skip_all)]
23 async fn vote_comment(
24   vote_type: &VoteType,
25   actor: ApubPerson,
26   comment: &ApubComment,
27   context: &LemmyContext,
28 ) -> Result<(), LemmyError> {
29   let comment_id = comment.id;
30   let like_form = CommentLikeForm {
31     comment_id,
32     post_id: comment.post_id,
33     person_id: actor.id,
34     score: vote_type.into(),
35   };
36   let person_id = actor.id;
37   CommentLike::remove(context.pool(), person_id, comment_id).await?;
38   CommentLike::like(context.pool(), &like_form).await?;
39
40   send_comment_ws_message_simple(comment_id, UserOperation::CreateCommentLike, context).await?;
41   Ok(())
42 }
43
44 #[tracing::instrument(skip_all)]
45 async fn vote_post(
46   vote_type: &VoteType,
47   actor: ApubPerson,
48   post: &ApubPost,
49   context: &LemmyContext,
50 ) -> Result<(), LemmyError> {
51   let post_id = post.id;
52   let like_form = PostLikeForm {
53     post_id: post.id,
54     person_id: actor.id,
55     score: vote_type.into(),
56   };
57   let person_id = actor.id;
58   PostLike::remove(context.pool(), person_id, post_id).await?;
59   PostLike::like(context.pool(), &like_form).await?;
60
61   send_post_ws_message(post.id, UserOperation::CreatePostLike, None, None, context).await?;
62   Ok(())
63 }
64
65 #[tracing::instrument(skip_all)]
66 async fn undo_vote_comment(
67   actor: ApubPerson,
68   comment: &ApubComment,
69   context: &LemmyContext,
70 ) -> Result<(), LemmyError> {
71   let comment_id = comment.id;
72   let person_id = actor.id;
73   CommentLike::remove(context.pool(), person_id, comment_id).await?;
74
75   send_comment_ws_message_simple(comment_id, UserOperation::CreateCommentLike, context).await?;
76   Ok(())
77 }
78
79 #[tracing::instrument(skip_all)]
80 async fn undo_vote_post(
81   actor: ApubPerson,
82   post: &ApubPost,
83   context: &LemmyContext,
84 ) -> Result<(), LemmyError> {
85   let post_id = post.id;
86   let person_id = actor.id;
87   PostLike::remove(context.pool(), person_id, post_id).await?;
88
89   send_post_ws_message(post_id, UserOperation::CreatePostLike, None, None, context).await?;
90   Ok(())
91 }