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::ApubObjectType;
12 use lemmy_db_queries::{source::comment::Comment_, Crud, DeleteableOrRemoveable};
13 use lemmy_db_schema::source::{comment::*, moderator::*};
14 use lemmy_db_views::comment_view::CommentView;
15 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
16 use lemmy_websocket::{messages::SendComment, 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 mut 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
58 updated_comment = updated_comment.blank_out_deleted_or_removed_info();
60 .send_delete(&local_user_view.person, context)
64 .send_undo_delete(&local_user_view.person, context)
69 let comment_id = data.comment_id;
70 let person_id = local_user_view.person.id;
71 let mut comment_view = blocking(context.pool(), move |conn| {
72 CommentView::read(conn, comment_id, Some(person_id))
76 // Blank out deleted or removed info
78 comment_view.comment = comment_view.comment.blank_out_deleted_or_removed_info();
81 // Build the recipients
82 let comment_view_2 = comment_view.clone();
83 let mentions = vec![];
84 let recipient_ids = send_local_notifs(
87 local_user_view.person.clone(),
94 let res = CommentResponse {
97 form_id: None, // TODO a comment delete might clear forms?
100 context.chat_server().do_send(SendComment {
101 op: UserOperationCrud::DeleteComment,
102 comment: res.clone(),
110 #[async_trait::async_trait(?Send)]
111 impl PerformCrud for RemoveComment {
112 type Response = CommentResponse;
116 context: &Data<LemmyContext>,
117 websocket_id: Option<ConnectionId>,
118 ) -> Result<CommentResponse, LemmyError> {
119 let data: &RemoveComment = self;
120 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
122 let comment_id = data.comment_id;
123 let orig_comment = blocking(context.pool(), move |conn| {
124 CommentView::read(conn, comment_id, None)
129 local_user_view.person.id,
130 orig_comment.community.id,
135 // Verify that only a mod or admin can remove
138 local_user_view.person.id,
139 orig_comment.community.id,
144 let removed = data.removed;
145 let mut updated_comment = blocking(context.pool(), move |conn| {
146 Comment::update_removed(conn, comment_id, removed)
149 .map_err(|_| ApiError::err("couldnt_update_comment"))?;
152 let form = ModRemoveCommentForm {
153 mod_person_id: local_user_view.person.id,
154 comment_id: data.comment_id,
155 removed: Some(removed),
156 reason: data.reason.to_owned(),
158 blocking(context.pool(), move |conn| {
159 ModRemoveComment::create(conn, &form)
163 // Send the apub message
165 updated_comment = updated_comment.blank_out_deleted_or_removed_info();
167 .send_remove(&local_user_view.person, context)
171 .send_undo_remove(&local_user_view.person, context)
176 let comment_id = data.comment_id;
177 let person_id = local_user_view.person.id;
178 let mut comment_view = blocking(context.pool(), move |conn| {
179 CommentView::read(conn, comment_id, Some(person_id))
183 // Blank out deleted or removed info
185 comment_view.comment = comment_view.comment.blank_out_deleted_or_removed_info();
188 // Build the recipients
189 let comment_view_2 = comment_view.clone();
191 let mentions = vec![];
192 let recipient_ids = send_local_notifs(
195 local_user_view.person.clone(),
202 let res = CommentResponse {
205 form_id: None, // TODO maybe this might clear other forms
208 context.chat_server().do_send(SendComment {
209 op: UserOperationCrud::RemoveComment,
210 comment: res.clone(),