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;
28 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
30 let comment_id = data.comment_id;
31 let orig_comment = blocking(context.pool(), move |conn| {
32 CommentView::read(conn, comment_id, None)
37 local_user_view.person.id,
38 orig_comment.community.id,
43 // Verify that only the creator can delete
44 if local_user_view.person.id != orig_comment.creator.id {
45 return Err(ApiError::err("no_comment_edit_allowed").into());
49 let deleted = data.deleted;
50 let updated_comment = blocking(context.pool(), move |conn| {
51 Comment::update_deleted(conn, comment_id, deleted)
54 .map_err(|_| ApiError::err("couldnt_update_comment"))?;
56 // Send the apub message
57 let community = blocking(context.pool(), move |conn| {
58 Community::read(conn, orig_comment.post.community_id)
62 &local_user_view.person,
64 updated_comment.ap_id.clone().into(),
70 let post_id = updated_comment.post_id;
71 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
72 let recipient_ids = send_local_notifs(
75 local_user_view.person.clone(),
82 send_comment_ws_message(
84 UserOperationCrud::DeleteComment,
86 None, // TODO a comment delete might clear forms?
87 Some(local_user_view.person.id),
95 #[async_trait::async_trait(?Send)]
96 impl PerformCrud for RemoveComment {
97 type Response = CommentResponse;
101 context: &Data<LemmyContext>,
102 websocket_id: Option<ConnectionId>,
103 ) -> Result<CommentResponse, LemmyError> {
104 let data: &RemoveComment = self;
105 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
107 let comment_id = data.comment_id;
108 let orig_comment = blocking(context.pool(), move |conn| {
109 CommentView::read(conn, comment_id, None)
114 local_user_view.person.id,
115 orig_comment.community.id,
120 // Verify that only a mod or admin can remove
123 local_user_view.person.id,
124 orig_comment.community.id,
129 let removed = data.removed;
130 let updated_comment = blocking(context.pool(), move |conn| {
131 Comment::update_removed(conn, comment_id, removed)
134 .map_err(|_| ApiError::err("couldnt_update_comment"))?;
137 let form = ModRemoveCommentForm {
138 mod_person_id: local_user_view.person.id,
139 comment_id: data.comment_id,
140 removed: Some(removed),
141 reason: data.reason.to_owned(),
143 blocking(context.pool(), move |conn| {
144 ModRemoveComment::create(conn, &form)
148 // Send the apub message
149 let community = blocking(context.pool(), move |conn| {
150 Community::read(conn, orig_comment.post.community_id)
154 &local_user_view.person,
156 updated_comment.ap_id.clone().into(),
157 data.reason.clone().unwrap_or_else(|| "".to_string()),
163 let post_id = updated_comment.post_id;
164 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
165 let recipient_ids = send_local_notifs(
168 local_user_view.person.clone(),
175 send_comment_ws_message(
177 UserOperationCrud::RemoveComment,
179 None, // TODO maybe this might clear other forms
180 Some(local_user_view.person.id),