]> Untitled Git - lemmy.git/blob - crates/api/src/post/sticky.rs
Split apart api files (#2216)
[lemmy.git] / crates / api / src / post / sticky.rs
1 use crate::Perform;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   blocking,
5   check_community_ban,
6   check_community_deleted_or_removed,
7   get_local_user_view_from_jwt,
8   is_mod_or_admin,
9   post::{PostResponse, StickyPost},
10 };
11 use lemmy_apub::{
12   objects::post::ApubPost,
13   protocol::activities::{create_or_update::post::CreateOrUpdatePost, CreateOrUpdateType},
14 };
15 use lemmy_db_schema::{
16   source::{
17     moderator::{ModStickyPost, ModStickyPostForm},
18     post::Post,
19   },
20   traits::Crud,
21 };
22 use lemmy_utils::{ConnectionId, LemmyError};
23 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperation};
24
25 #[async_trait::async_trait(?Send)]
26 impl Perform for StickyPost {
27   type Response = PostResponse;
28
29   #[tracing::instrument(skip(context, websocket_id))]
30   async fn perform(
31     &self,
32     context: &Data<LemmyContext>,
33     websocket_id: Option<ConnectionId>,
34   ) -> Result<PostResponse, LemmyError> {
35     let data: &StickyPost = self;
36     let local_user_view =
37       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
38
39     let post_id = data.post_id;
40     let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
41
42     check_community_ban(
43       local_user_view.person.id,
44       orig_post.community_id,
45       context.pool(),
46     )
47     .await?;
48     check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?;
49
50     // Verify that only the mods can sticky
51     is_mod_or_admin(
52       context.pool(),
53       local_user_view.person.id,
54       orig_post.community_id,
55     )
56     .await?;
57
58     // Update the post
59     let post_id = data.post_id;
60     let stickied = data.stickied;
61     let updated_post: ApubPost = blocking(context.pool(), move |conn| {
62       Post::update_stickied(conn, post_id, stickied)
63     })
64     .await??
65     .into();
66
67     // Mod tables
68     let form = ModStickyPostForm {
69       mod_person_id: local_user_view.person.id,
70       post_id: data.post_id,
71       stickied: Some(stickied),
72     };
73     blocking(context.pool(), move |conn| {
74       ModStickyPost::create(conn, &form)
75     })
76     .await??;
77
78     // Apub updates
79     // TODO stickied should pry work like locked for ease of use
80     CreateOrUpdatePost::send(
81       updated_post,
82       &local_user_view.person.clone().into(),
83       CreateOrUpdateType::Update,
84       context,
85     )
86     .await?;
87
88     send_post_ws_message(
89       data.post_id,
90       UserOperation::StickyPost,
91       websocket_id,
92       Some(local_user_view.person.id),
93       context,
94     )
95     .await
96   }
97 }