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