]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/voting/mod.rs
Don't drop error context when adding a message to errors (#1958)
[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 #[tracing::instrument(skip_all)]
25 async fn vote_comment(
26   vote_type: &VoteType,
27   actor: ApubPerson,
28   comment: &ApubComment,
29   context: &LemmyContext,
30 ) -> Result<(), LemmyError> {
31   let comment_id = comment.id;
32   let like_form = CommentLikeForm {
33     comment_id,
34     post_id: comment.post_id,
35     person_id: actor.id,
36     score: vote_type.into(),
37   };
38   let person_id = actor.id;
39   blocking(context.pool(), move |conn| {
40     CommentLike::remove(conn, person_id, comment_id)?;
41     CommentLike::like(conn, &like_form)
42   })
43   .await??;
44
45   send_comment_ws_message_simple(comment_id, UserOperation::CreateCommentLike, context).await?;
46   Ok(())
47 }
48
49 #[tracing::instrument(skip_all)]
50 async fn vote_post(
51   vote_type: &VoteType,
52   actor: ApubPerson,
53   post: &ApubPost,
54   context: &LemmyContext,
55 ) -> Result<(), LemmyError> {
56   let post_id = post.id;
57   let like_form = PostLikeForm {
58     post_id: post.id,
59     person_id: actor.id,
60     score: vote_type.into(),
61   };
62   let person_id = actor.id;
63   blocking(context.pool(), move |conn| {
64     PostLike::remove(conn, person_id, post_id)?;
65     PostLike::like(conn, &like_form)
66   })
67   .await??;
68
69   send_post_ws_message(post.id, UserOperation::CreatePostLike, None, None, context).await?;
70   Ok(())
71 }
72
73 #[tracing::instrument(skip_all)]
74 async fn undo_vote_comment(
75   actor: ApubPerson,
76   comment: &ApubComment,
77   context: &LemmyContext,
78 ) -> Result<(), LemmyError> {
79   let comment_id = comment.id;
80   let person_id = actor.id;
81   blocking(context.pool(), move |conn| {
82     CommentLike::remove(conn, person_id, comment_id)
83   })
84   .await??;
85
86   send_comment_ws_message_simple(comment_id, UserOperation::CreateCommentLike, context).await?;
87   Ok(())
88 }
89
90 #[tracing::instrument(skip_all)]
91 async fn undo_vote_post(
92   actor: ApubPerson,
93   post: &ApubPost,
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
103   send_post_ws_message(post_id, UserOperation::CreatePostLike, None, None, context).await?;
104   Ok(())
105 }