1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
7 get_local_user_view_from_jwt,
11 use lemmy_apub::activities::deletion::{send_apub_delete, send_apub_remove};
12 use lemmy_db_schema::{
16 moderator::{ModRemoveComment, ModRemoveCommentForm},
21 use lemmy_db_views::comment_view::CommentView;
22 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
23 use lemmy_websocket::{send::send_comment_ws_message, LemmyContext, UserOperationCrud};
25 #[async_trait::async_trait(?Send)]
26 impl PerformCrud for DeleteComment {
27 type Response = CommentResponse;
31 context: &Data<LemmyContext>,
32 websocket_id: Option<ConnectionId>,
33 ) -> Result<CommentResponse, LemmyError> {
34 let data: &DeleteComment = self;
36 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
38 let comment_id = data.comment_id;
39 let orig_comment = blocking(context.pool(), move |conn| {
40 CommentView::read(conn, comment_id, None)
45 local_user_view.person.id,
46 orig_comment.community.id,
51 // Verify that only the creator can delete
52 if local_user_view.person.id != orig_comment.creator.id {
53 return Err(ApiError::err_plain("no_comment_edit_allowed").into());
57 let deleted = data.deleted;
58 let updated_comment = blocking(context.pool(), move |conn| {
59 Comment::update_deleted(conn, comment_id, deleted)
62 .map_err(|e| ApiError::err("couldnt_update_comment", e))?;
64 // Send the apub message
65 let community = blocking(context.pool(), move |conn| {
66 Community::read(conn, orig_comment.post.community_id)
70 &local_user_view.person,
72 updated_comment.ap_id.clone().into(),
78 let post_id = updated_comment.post_id;
79 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
80 let recipient_ids = send_local_notifs(
83 local_user_view.person.clone(),
91 send_comment_ws_message(
93 UserOperationCrud::DeleteComment,
95 None, // TODO a comment delete might clear forms?
96 Some(local_user_view.person.id),
104 #[async_trait::async_trait(?Send)]
105 impl PerformCrud for RemoveComment {
106 type Response = CommentResponse;
110 context: &Data<LemmyContext>,
111 websocket_id: Option<ConnectionId>,
112 ) -> Result<CommentResponse, LemmyError> {
113 let data: &RemoveComment = self;
114 let local_user_view =
115 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
117 let comment_id = data.comment_id;
118 let orig_comment = blocking(context.pool(), move |conn| {
119 CommentView::read(conn, comment_id, None)
124 local_user_view.person.id,
125 orig_comment.community.id,
130 // Verify that only a mod or admin can remove
133 local_user_view.person.id,
134 orig_comment.community.id,
139 let removed = data.removed;
140 let updated_comment = blocking(context.pool(), move |conn| {
141 Comment::update_removed(conn, comment_id, removed)
144 .map_err(|e| ApiError::err("couldnt_update_comment", e))?;
147 let form = ModRemoveCommentForm {
148 mod_person_id: local_user_view.person.id,
149 comment_id: data.comment_id,
150 removed: Some(removed),
151 reason: data.reason.to_owned(),
153 blocking(context.pool(), move |conn| {
154 ModRemoveComment::create(conn, &form)
158 // Send the apub message
159 let community = blocking(context.pool(), move |conn| {
160 Community::read(conn, orig_comment.post.community_id)
164 &local_user_view.person,
166 updated_comment.ap_id.clone().into(),
167 data.reason.clone().unwrap_or_else(|| "".to_string()),
173 let post_id = updated_comment.post_id;
174 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
175 let recipient_ids = send_local_notifs(
178 local_user_view.person.clone(),
186 send_comment_ws_message(
188 UserOperationCrud::RemoveComment,
190 None, // TODO maybe this might clear other forms
191 Some(local_user_view.person.id),