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};
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, UserOperation};
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 = match blocking(context.pool(), move |conn| {
51 Comment::update_deleted(conn, comment_id, deleted)
55 Ok(comment) => comment,
56 Err(_e) => return Err(ApiError::err("couldnt_update_comment").into()),
59 // Send the apub message
62 .send_delete(&local_user_view.person, context)
66 .send_undo_delete(&local_user_view.person, context)
71 let comment_id = data.comment_id;
72 let person_id = local_user_view.person.id;
73 let comment_view = blocking(context.pool(), move |conn| {
74 CommentView::read(conn, comment_id, Some(person_id))
78 // Build the recipients
79 let comment_view_2 = comment_view.clone();
80 let mentions = vec![];
81 let recipient_ids = send_local_notifs(
84 local_user_view.person.clone(),
91 let res = CommentResponse {
94 form_id: None, // TODO a comment delete might clear forms?
97 context.chat_server().do_send(SendComment {
98 op: UserOperation::DeleteComment,
107 #[async_trait::async_trait(?Send)]
108 impl PerformCrud for RemoveComment {
109 type Response = CommentResponse;
113 context: &Data<LemmyContext>,
114 websocket_id: Option<ConnectionId>,
115 ) -> Result<CommentResponse, LemmyError> {
116 let data: &RemoveComment = &self;
117 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
119 let comment_id = data.comment_id;
120 let orig_comment = blocking(context.pool(), move |conn| {
121 CommentView::read(&conn, comment_id, None)
126 local_user_view.person.id,
127 orig_comment.community.id,
132 // Verify that only a mod or admin can remove
135 local_user_view.person.id,
136 orig_comment.community.id,
141 let removed = data.removed;
142 let updated_comment = match blocking(context.pool(), move |conn| {
143 Comment::update_removed(conn, comment_id, removed)
147 Ok(comment) => comment,
148 Err(_e) => return Err(ApiError::err("couldnt_update_comment").into()),
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
166 .send_remove(&local_user_view.person, context)
170 .send_undo_remove(&local_user_view.person, context)
175 let comment_id = data.comment_id;
176 let person_id = local_user_view.person.id;
177 let comment_view = blocking(context.pool(), move |conn| {
178 CommentView::read(conn, comment_id, Some(person_id))
182 // Build the recipients
183 let comment_view_2 = comment_view.clone();
185 let mentions = vec![];
186 let recipient_ids = send_local_notifs(
189 local_user_view.person.clone(),
196 let res = CommentResponse {
199 form_id: None, // TODO maybe this might clear other forms
202 context.chat_server().do_send(SendComment {
203 op: UserOperation::RemoveComment,
204 comment: res.clone(),