]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/voting/mod.rs
Rewrite voting (#1685)
[lemmy.git] / crates / apub / src / activities / voting / mod.rs
1 use crate::activities::{
2   comment::send_websocket_message as send_comment_message,
3   post::send_websocket_message as send_post_message,
4   voting::vote::VoteType,
5 };
6 use lemmy_api_common::blocking;
7 use lemmy_db_queries::Likeable;
8 use lemmy_db_schema::source::{
9   comment::{Comment, CommentLike, CommentLikeForm},
10   person::Person,
11   post::{Post, PostLike, PostLikeForm},
12 };
13 use lemmy_utils::LemmyError;
14 use lemmy_websocket::{LemmyContext, UserOperation};
15
16 pub mod undo_vote;
17 pub mod vote;
18
19 async fn vote_comment(
20   vote_type: &VoteType,
21   actor: Person,
22   comment: &Comment,
23   context: &LemmyContext,
24 ) -> Result<(), LemmyError> {
25   let comment_id = comment.id;
26   let like_form = CommentLikeForm {
27     comment_id,
28     post_id: comment.post_id,
29     person_id: actor.id,
30     score: vote_type.into(),
31   };
32   let person_id = actor.id;
33   blocking(context.pool(), move |conn| {
34     CommentLike::remove(conn, person_id, comment_id)?;
35     CommentLike::like(conn, &like_form)
36   })
37   .await??;
38
39   send_comment_message(
40     comment_id,
41     vec![],
42     UserOperation::CreateCommentLike,
43     context,
44   )
45   .await
46 }
47
48 async fn vote_post(
49   vote_type: &VoteType,
50   actor: Person,
51   post: &Post,
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_message(post.id, UserOperation::CreatePostLike, context).await
68 }
69
70 async fn undo_vote_comment(
71   actor: Person,
72   comment: &Comment,
73   context: &LemmyContext,
74 ) -> Result<(), LemmyError> {
75   let comment_id = comment.id;
76   let person_id = actor.id;
77   blocking(context.pool(), move |conn| {
78     CommentLike::remove(conn, person_id, comment_id)
79   })
80   .await??;
81
82   send_comment_message(
83     comment.id,
84     vec![],
85     UserOperation::CreateCommentLike,
86     context,
87   )
88   .await
89 }
90
91 async fn undo_vote_post(
92   actor: Person,
93   post: &Post,
94   context: &LemmyContext,
95 ) -> Result<(), LemmyError> {
96   let post_id = post.id;
97   let person_id = actor.id;
98   blocking(context.pool(), move |conn| {
99     PostLike::remove(conn, person_id, post_id)
100   })
101   .await??;
102   send_post_message(post.id, UserOperation::CreatePostLike, context).await
103 }