]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/voting/mod.rs
Rewrite delete activities (#1699)
[lemmy.git] / crates / apub / src / activities / voting / mod.rs
1 use crate::activities::voting::vote::VoteType;
2 use lemmy_api_common::blocking;
3 use lemmy_db_queries::Likeable;
4 use lemmy_db_schema::source::{
5   comment::{Comment, CommentLike, CommentLikeForm},
6   person::Person,
7   post::{Post, PostLike, PostLikeForm},
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 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_ws_message_simple(comment_id, UserOperation::CreateCommentLike, context).await?;
40   Ok(())
41 }
42
43 async fn vote_post(
44   vote_type: &VoteType,
45   actor: Person,
46   post: &Post,
47   context: &LemmyContext,
48 ) -> Result<(), LemmyError> {
49   let post_id = post.id;
50   let like_form = PostLikeForm {
51     post_id: post.id,
52     person_id: actor.id,
53     score: vote_type.into(),
54   };
55   let person_id = actor.id;
56   blocking(context.pool(), move |conn| {
57     PostLike::remove(conn, person_id, post_id)?;
58     PostLike::like(conn, &like_form)
59   })
60   .await??;
61
62   send_post_ws_message(post.id, UserOperation::CreatePostLike, None, None, context).await?;
63   Ok(())
64 }
65
66 async fn undo_vote_comment(
67   actor: Person,
68   comment: &Comment,
69   context: &LemmyContext,
70 ) -> Result<(), LemmyError> {
71   let comment_id = comment.id;
72   let person_id = actor.id;
73   blocking(context.pool(), move |conn| {
74     CommentLike::remove(conn, person_id, comment_id)
75   })
76   .await??;
77
78   send_comment_ws_message_simple(comment_id, UserOperation::CreateCommentLike, context).await?;
79   Ok(())
80 }
81
82 async fn undo_vote_post(
83   actor: Person,
84   post: &Post,
85   context: &LemmyContext,
86 ) -> Result<(), LemmyError> {
87   let post_id = post.id;
88   let person_id = actor.id;
89   blocking(context.pool(), move |conn| {
90     PostLike::remove(conn, person_id, post_id)
91   })
92   .await??;
93
94   send_post_ws_message(post_id, UserOperation::CreatePostLike, None, None, context).await?;
95   Ok(())
96 }