1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
7 get_local_user_view_from_jwt,
10 use lemmy_apub::activities::deletion::{send_apub_delete, send_apub_remove};
11 use lemmy_db_schema::{
15 moderator::{ModRemoveComment, ModRemoveCommentForm},
20 use lemmy_db_views::comment_view::CommentView;
21 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
22 use lemmy_websocket::{
23 send::{send_comment_ws_message, send_local_notifs},
28 #[async_trait::async_trait(?Send)]
29 impl PerformCrud for DeleteComment {
30 type Response = CommentResponse;
34 context: &Data<LemmyContext>,
35 websocket_id: Option<ConnectionId>,
36 ) -> Result<CommentResponse, LemmyError> {
37 let data: &DeleteComment = self;
39 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
41 let comment_id = data.comment_id;
42 let orig_comment = blocking(context.pool(), move |conn| {
43 CommentView::read(conn, comment_id, None)
48 local_user_view.person.id,
49 orig_comment.community.id,
54 // Verify that only the creator can delete
55 if local_user_view.person.id != orig_comment.creator.id {
56 return Err(ApiError::err_plain("no_comment_edit_allowed").into());
60 let deleted = data.deleted;
61 let updated_comment = blocking(context.pool(), move |conn| {
62 Comment::update_deleted(conn, comment_id, deleted)
65 .map_err(|e| ApiError::err("couldnt_update_comment", e))?;
67 // Send the apub message
68 let community = blocking(context.pool(), move |conn| {
69 Community::read(conn, orig_comment.post.community_id)
73 &local_user_view.person.clone().into(),
74 &community.clone().into(),
75 updated_comment.ap_id.clone().into(),
81 let post_id = updated_comment.post_id;
82 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
83 let recipient_ids = send_local_notifs(
86 &local_user_view.person,
93 send_comment_ws_message(
95 UserOperationCrud::DeleteComment,
97 None, // TODO a comment delete might clear forms?
98 Some(local_user_view.person.id),
106 #[async_trait::async_trait(?Send)]
107 impl PerformCrud for RemoveComment {
108 type Response = CommentResponse;
112 context: &Data<LemmyContext>,
113 websocket_id: Option<ConnectionId>,
114 ) -> Result<CommentResponse, LemmyError> {
115 let data: &RemoveComment = self;
116 let local_user_view =
117 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
119 let comment_id = data.comment_id;
120 let orig_comment = blocking(context.pool(), move |conn| {
121 CommentView::read(conn, comment_id, None)
126 local_user_view.person.id,
127 orig_comment.community.id,
132 // Verify that only a mod or admin can remove
135 local_user_view.person.id,
136 orig_comment.community.id,
141 let removed = data.removed;
142 let updated_comment = blocking(context.pool(), move |conn| {
143 Comment::update_removed(conn, comment_id, removed)
146 .map_err(|e| ApiError::err("couldnt_update_comment", e))?;
149 let form = ModRemoveCommentForm {
150 mod_person_id: local_user_view.person.id,
151 comment_id: data.comment_id,
152 removed: Some(removed),
153 reason: data.reason.to_owned(),
155 blocking(context.pool(), move |conn| {
156 ModRemoveComment::create(conn, &form)
160 // Send the apub message
161 let community = blocking(context.pool(), move |conn| {
162 Community::read(conn, orig_comment.post.community_id)
166 &local_user_view.person.clone().into(),
168 updated_comment.ap_id.clone().into(),
169 data.reason.clone().unwrap_or_else(|| "".to_string()),
175 let post_id = updated_comment.post_id;
176 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
177 let recipient_ids = send_local_notifs(
180 &local_user_view.person.clone(),
187 send_comment_ws_message(
189 UserOperationCrud::RemoveComment,
191 None, // TODO maybe this might clear other forms
192 Some(local_user_view.person.id),