]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/receive/comment_undo.rs
Merge pull request #1328 from LemmyNet/move_views_to_diesel
[lemmy.git] / crates / apub / src / activities / receive / comment_undo.rs
1 use crate::activities::receive::get_actor_as_user;
2 use activitystreams::activity::{Dislike, Like};
3 use lemmy_db_queries::{source::comment::Comment_, Likeable};
4 use lemmy_db_schema::source::comment::{Comment, CommentLike};
5 use lemmy_db_views::comment_view::CommentView;
6 use lemmy_structs::{blocking, comment::CommentResponse};
7 use lemmy_utils::LemmyError;
8 use lemmy_websocket::{messages::SendComment, LemmyContext, UserOperation};
9
10 pub(crate) async fn receive_undo_like_comment(
11   like: &Like,
12   comment: Comment,
13   context: &LemmyContext,
14   request_counter: &mut i32,
15 ) -> Result<(), LemmyError> {
16   let user = get_actor_as_user(like, context, request_counter).await?;
17
18   let comment_id = comment.id;
19   let user_id = user.id;
20   blocking(context.pool(), move |conn| {
21     CommentLike::remove(conn, user_id, comment_id)
22   })
23   .await??;
24
25   // Refetch the view
26   let comment_view = blocking(context.pool(), move |conn| {
27     CommentView::read(conn, comment_id, None)
28   })
29   .await??;
30
31   // TODO get those recipient actor ids from somewhere
32   let recipient_ids = vec![];
33   let res = CommentResponse {
34     comment_view,
35     recipient_ids,
36     form_id: None,
37   };
38
39   context.chat_server().do_send(SendComment {
40     op: UserOperation::CreateCommentLike,
41     comment: res,
42     websocket_id: None,
43   });
44
45   Ok(())
46 }
47
48 pub(crate) async fn receive_undo_dislike_comment(
49   dislike: &Dislike,
50   comment: Comment,
51   context: &LemmyContext,
52   request_counter: &mut i32,
53 ) -> Result<(), LemmyError> {
54   let user = get_actor_as_user(dislike, context, request_counter).await?;
55
56   let comment_id = comment.id;
57   let user_id = user.id;
58   blocking(context.pool(), move |conn| {
59     CommentLike::remove(conn, user_id, comment_id)
60   })
61   .await??;
62
63   // Refetch the view
64   let comment_view = blocking(context.pool(), move |conn| {
65     CommentView::read(conn, comment_id, None)
66   })
67   .await??;
68
69   // TODO get those recipient actor ids from somewhere
70   let recipient_ids = vec![];
71   let res = CommentResponse {
72     comment_view,
73     recipient_ids,
74     form_id: None,
75   };
76
77   context.chat_server().do_send(SendComment {
78     op: UserOperation::CreateCommentLike,
79     comment: res,
80     websocket_id: None,
81   });
82
83   Ok(())
84 }
85
86 pub(crate) async fn receive_undo_delete_comment(
87   context: &LemmyContext,
88   comment: Comment,
89 ) -> Result<(), LemmyError> {
90   let deleted_comment = blocking(context.pool(), move |conn| {
91     Comment::update_deleted(conn, comment.id, false)
92   })
93   .await??;
94
95   // Refetch the view
96   let comment_id = deleted_comment.id;
97   let comment_view = blocking(context.pool(), move |conn| {
98     CommentView::read(conn, comment_id, None)
99   })
100   .await??;
101
102   // TODO get those recipient actor ids from somewhere
103   let recipient_ids = vec![];
104   let res = CommentResponse {
105     comment_view,
106     recipient_ids,
107     form_id: None,
108   };
109
110   context.chat_server().do_send(SendComment {
111     op: UserOperation::EditComment,
112     comment: res,
113     websocket_id: None,
114   });
115
116   Ok(())
117 }
118
119 pub(crate) async fn receive_undo_remove_comment(
120   context: &LemmyContext,
121   comment: Comment,
122 ) -> Result<(), LemmyError> {
123   let removed_comment = blocking(context.pool(), move |conn| {
124     Comment::update_removed(conn, comment.id, false)
125   })
126   .await??;
127
128   // Refetch the view
129   let comment_id = removed_comment.id;
130   let comment_view = blocking(context.pool(), move |conn| {
131     CommentView::read(conn, comment_id, None)
132   })
133   .await??;
134
135   // TODO get those recipient actor ids from somewhere
136   let recipient_ids = vec![];
137   let res = CommentResponse {
138     comment_view,
139     recipient_ids,
140     form_id: None,
141   };
142
143   context.chat_server().do_send(SendComment {
144     op: UserOperation::EditComment,
145     comment: res,
146     websocket_id: None,
147   });
148
149   Ok(())
150 }