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)
47 // Dont delete it if its already been deleted.
48 if orig_comment.comment.deleted == data.deleted {
49 return Err(ApiError::err_plain("couldnt_update_comment").into());
53 local_user_view.person.id,
54 orig_comment.community.id,
59 // Verify that only the creator can delete
60 if local_user_view.person.id != orig_comment.creator.id {
61 return Err(ApiError::err_plain("no_comment_edit_allowed").into());
65 let deleted = data.deleted;
66 let updated_comment = blocking(context.pool(), move |conn| {
67 Comment::update_deleted(conn, comment_id, deleted)
70 .map_err(|e| ApiError::err("couldnt_update_comment", e))?;
72 // Send the apub message
73 let community = blocking(context.pool(), move |conn| {
74 Community::read(conn, orig_comment.post.community_id)
78 &local_user_view.person.clone().into(),
79 &community.clone().into(),
80 updated_comment.ap_id.clone().into(),
86 let post_id = updated_comment.post_id;
87 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
88 let recipient_ids = send_local_notifs(
91 &local_user_view.person,
98 send_comment_ws_message(
100 UserOperationCrud::DeleteComment,
102 None, // TODO a comment delete might clear forms?
103 Some(local_user_view.person.id),
111 #[async_trait::async_trait(?Send)]
112 impl PerformCrud for RemoveComment {
113 type Response = CommentResponse;
117 context: &Data<LemmyContext>,
118 websocket_id: Option<ConnectionId>,
119 ) -> Result<CommentResponse, LemmyError> {
120 let data: &RemoveComment = self;
121 let local_user_view =
122 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
124 let comment_id = data.comment_id;
125 let orig_comment = blocking(context.pool(), move |conn| {
126 CommentView::read(conn, comment_id, None)
131 local_user_view.person.id,
132 orig_comment.community.id,
137 // Verify that only a mod or admin can remove
140 local_user_view.person.id,
141 orig_comment.community.id,
146 let removed = data.removed;
147 let updated_comment = blocking(context.pool(), move |conn| {
148 Comment::update_removed(conn, comment_id, removed)
151 .map_err(|e| ApiError::err("couldnt_update_comment", e))?;
154 let form = ModRemoveCommentForm {
155 mod_person_id: local_user_view.person.id,
156 comment_id: data.comment_id,
157 removed: Some(removed),
158 reason: data.reason.to_owned(),
160 blocking(context.pool(), move |conn| {
161 ModRemoveComment::create(conn, &form)
165 // Send the apub message
166 let community = blocking(context.pool(), move |conn| {
167 Community::read(conn, orig_comment.post.community_id)
171 &local_user_view.person.clone().into(),
173 updated_comment.ap_id.clone().into(),
174 data.reason.clone().unwrap_or_else(|| "".to_string()),
180 let post_id = updated_comment.post_id;
181 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
182 let recipient_ids = send_local_notifs(
185 &local_user_view.person.clone(),
192 send_comment_ws_message(
194 UserOperationCrud::RemoveComment,
196 None, // TODO maybe this might clear other forms
197 Some(local_user_view.person.id),