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_queries::{source::comment::Comment_, Crud};
13 use lemmy_db_schema::source::{comment::*, community::Community, moderator::*, post::Post};
14 use lemmy_db_views::comment_view::CommentView;
15 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
16 use lemmy_websocket::{send::send_comment_ws_message, LemmyContext, UserOperationCrud};
18 #[async_trait::async_trait(?Send)]
19 impl PerformCrud for DeleteComment {
20 type Response = CommentResponse;
24 context: &Data<LemmyContext>,
25 websocket_id: Option<ConnectionId>,
26 ) -> Result<CommentResponse, LemmyError> {
27 let data: &DeleteComment = self;
29 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
31 let comment_id = data.comment_id;
32 let orig_comment = blocking(context.pool(), move |conn| {
33 CommentView::read(conn, comment_id, None)
38 local_user_view.person.id,
39 orig_comment.community.id,
44 // Verify that only the creator can delete
45 if local_user_view.person.id != orig_comment.creator.id {
46 return Err(ApiError::err_plain("no_comment_edit_allowed").into());
50 let deleted = data.deleted;
51 let updated_comment = blocking(context.pool(), move |conn| {
52 Comment::update_deleted(conn, comment_id, deleted)
55 .map_err(|e| ApiError::err("couldnt_update_comment", e))?;
57 // Send the apub message
58 let community = blocking(context.pool(), move |conn| {
59 Community::read(conn, orig_comment.post.community_id)
63 &local_user_view.person,
65 updated_comment.ap_id.clone().into(),
71 let post_id = updated_comment.post_id;
72 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
73 let recipient_ids = send_local_notifs(
76 local_user_view.person.clone(),
84 send_comment_ws_message(
86 UserOperationCrud::DeleteComment,
88 None, // TODO a comment delete might clear forms?
89 Some(local_user_view.person.id),
97 #[async_trait::async_trait(?Send)]
98 impl PerformCrud for RemoveComment {
99 type Response = CommentResponse;
103 context: &Data<LemmyContext>,
104 websocket_id: Option<ConnectionId>,
105 ) -> Result<CommentResponse, LemmyError> {
106 let data: &RemoveComment = self;
107 let local_user_view =
108 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
110 let comment_id = data.comment_id;
111 let orig_comment = blocking(context.pool(), move |conn| {
112 CommentView::read(conn, comment_id, None)
117 local_user_view.person.id,
118 orig_comment.community.id,
123 // Verify that only a mod or admin can remove
126 local_user_view.person.id,
127 orig_comment.community.id,
132 let removed = data.removed;
133 let updated_comment = blocking(context.pool(), move |conn| {
134 Comment::update_removed(conn, comment_id, removed)
137 .map_err(|e| ApiError::err("couldnt_update_comment", e))?;
140 let form = ModRemoveCommentForm {
141 mod_person_id: local_user_view.person.id,
142 comment_id: data.comment_id,
143 removed: Some(removed),
144 reason: data.reason.to_owned(),
146 blocking(context.pool(), move |conn| {
147 ModRemoveComment::create(conn, &form)
151 // Send the apub message
152 let community = blocking(context.pool(), move |conn| {
153 Community::read(conn, orig_comment.post.community_id)
157 &local_user_view.person,
159 updated_comment.ap_id.clone().into(),
160 data.reason.clone().unwrap_or_else(|| "".to_string()),
166 let post_id = updated_comment.post_id;
167 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
168 let recipient_ids = send_local_notifs(
171 local_user_view.person.clone(),
179 send_comment_ws_message(
181 UserOperationCrud::RemoveComment,
183 None, // TODO maybe this might clear other forms
184 Some(local_user_view.person.id),