1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
6 get_local_user_view_from_jwt,
10 use lemmy_apub::ApubObjectType;
11 use lemmy_db_queries::{source::post::Post_, Crud};
12 use lemmy_db_schema::source::{moderator::*, post::*};
13 use lemmy_db_views::post_view::PostView;
14 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
15 use lemmy_websocket::{messages::SendPost, LemmyContext, UserOperationCrud};
17 #[async_trait::async_trait(?Send)]
18 impl PerformCrud for DeletePost {
19 type Response = PostResponse;
23 context: &Data<LemmyContext>,
24 websocket_id: Option<ConnectionId>,
25 ) -> Result<PostResponse, LemmyError> {
26 let data: &DeletePost = &self;
27 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
29 let post_id = data.post_id;
30 let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
33 local_user_view.person.id,
34 orig_post.community_id,
39 // Verify that only the creator can delete
40 if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
41 return Err(ApiError::err("no_post_edit_allowed").into());
45 let post_id = data.post_id;
46 let deleted = data.deleted;
47 let updated_post = blocking(context.pool(), move |conn| {
48 Post::update_deleted(conn, post_id, deleted)
55 .send_delete(&local_user_view.person, context)
59 .send_undo_delete(&local_user_view.person, context)
64 let post_id = data.post_id;
65 let post_view = blocking(context.pool(), move |conn| {
66 PostView::read(conn, post_id, Some(local_user_view.person.id))
70 let res = PostResponse { post_view };
72 context.chat_server().do_send(SendPost {
73 op: UserOperationCrud::DeletePost,
82 #[async_trait::async_trait(?Send)]
83 impl PerformCrud for RemovePost {
84 type Response = PostResponse;
88 context: &Data<LemmyContext>,
89 websocket_id: Option<ConnectionId>,
90 ) -> Result<PostResponse, LemmyError> {
91 let data: &RemovePost = &self;
92 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
94 let post_id = data.post_id;
95 let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
98 local_user_view.person.id,
99 orig_post.community_id,
104 // Verify that only the mods can remove
107 local_user_view.person.id,
108 orig_post.community_id,
113 let post_id = data.post_id;
114 let removed = data.removed;
115 let updated_post = blocking(context.pool(), move |conn| {
116 Post::update_removed(conn, post_id, removed)
121 let form = ModRemovePostForm {
122 mod_person_id: local_user_view.person.id,
123 post_id: data.post_id,
124 removed: Some(removed),
125 reason: data.reason.to_owned(),
127 blocking(context.pool(), move |conn| {
128 ModRemovePost::create(conn, &form)
135 .send_remove(&local_user_view.person, context)
139 .send_undo_remove(&local_user_view.person, context)
144 let post_id = data.post_id;
145 let person_id = local_user_view.person.id;
146 let post_view = blocking(context.pool(), move |conn| {
147 PostView::read(conn, post_id, Some(person_id))
151 let res = PostResponse { post_view };
153 context.chat_server().do_send(SendPost {
154 op: UserOperationCrud::RemovePost,