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