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