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, 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
59 .send_delete(&local_user_view.person, context)
63 .send_undo_delete(&local_user_view.person, context)
68 let comment_id = data.comment_id;
69 let person_id = local_user_view.person.id;
70 let comment_view = blocking(context.pool(), move |conn| {
71 CommentView::read(conn, comment_id, Some(person_id))
75 // Build the recipients
76 let comment_view_2 = comment_view.clone();
77 let mentions = vec![];
78 let recipient_ids = send_local_notifs(
81 local_user_view.person.clone(),
88 let res = CommentResponse {
91 form_id: None, // TODO a comment delete might clear forms?
94 context.chat_server().do_send(SendComment {
95 op: UserOperationCrud::DeleteComment,
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 = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
116 let comment_id = data.comment_id;
117 let orig_comment = blocking(context.pool(), move |conn| {
118 CommentView::read(conn, comment_id, None)
123 local_user_view.person.id,
124 orig_comment.community.id,
129 // Verify that only a mod or admin can remove
132 local_user_view.person.id,
133 orig_comment.community.id,
138 let removed = data.removed;
139 let updated_comment = blocking(context.pool(), move |conn| {
140 Comment::update_removed(conn, comment_id, removed)
143 .map_err(|_| ApiError::err("couldnt_update_comment"))?;
146 let form = ModRemoveCommentForm {
147 mod_person_id: local_user_view.person.id,
148 comment_id: data.comment_id,
149 removed: Some(removed),
150 reason: data.reason.to_owned(),
152 blocking(context.pool(), move |conn| {
153 ModRemoveComment::create(conn, &form)
157 // Send the apub message
160 .send_remove(&local_user_view.person, context)
164 .send_undo_remove(&local_user_view.person, context)
169 let comment_id = data.comment_id;
170 let person_id = local_user_view.person.id;
171 let comment_view = blocking(context.pool(), move |conn| {
172 CommentView::read(conn, comment_id, Some(person_id))
176 // Build the recipients
177 let comment_view_2 = comment_view.clone();
179 let mentions = vec![];
180 let recipient_ids = send_local_notifs(
183 local_user_view.person.clone(),
190 let res = CommentResponse {
193 form_id: None, // TODO maybe this might clear other forms
196 context.chat_server().do_send(SendComment {
197 op: UserOperationCrud::RemoveComment,
198 comment: res.clone(),