]> Untitled Git - lemmy.git/blob - crates/api/src/post/feature.rs
Dont return error in case optional auth is invalid (#2879)
[lemmy.git] / crates / api / src / post / feature.rs
1 use crate::Perform;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   context::LemmyContext,
5   post::{FeaturePost, PostResponse},
6   utils::{
7     check_community_ban,
8     check_community_deleted_or_removed,
9     is_admin,
10     is_mod_or_admin,
11     local_user_view_from_jwt,
12   },
13   websocket::UserOperation,
14 };
15 use lemmy_db_schema::{
16   source::{
17     moderator::{ModFeaturePost, ModFeaturePostForm},
18     post::{Post, PostUpdateForm},
19   },
20   traits::Crud,
21   PostFeatureType,
22 };
23 use lemmy_utils::{error::LemmyError, ConnectionId};
24
25 #[async_trait::async_trait(?Send)]
26 impl Perform for FeaturePost {
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: &FeaturePost = self;
36     let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
37
38     let post_id = data.post_id;
39     let orig_post = Post::read(context.pool(), post_id).await?;
40
41     check_community_ban(
42       local_user_view.person.id,
43       orig_post.community_id,
44       context.pool(),
45     )
46     .await?;
47     check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?;
48
49     if data.feature_type == PostFeatureType::Community {
50       // Verify that only the mods can feature in community
51       is_mod_or_admin(
52         context.pool(),
53         local_user_view.person.id,
54         orig_post.community_id,
55       )
56       .await?;
57     } else {
58       is_admin(&local_user_view)?;
59     }
60
61     // Update the post
62     let post_id = data.post_id;
63     let new_post: PostUpdateForm = if data.feature_type == PostFeatureType::Community {
64       PostUpdateForm::builder()
65         .featured_community(Some(data.featured))
66         .build()
67     } else {
68       PostUpdateForm::builder()
69         .featured_local(Some(data.featured))
70         .build()
71     };
72     Post::update(context.pool(), post_id, &new_post).await?;
73
74     // Mod tables
75     let form = ModFeaturePostForm {
76       mod_person_id: local_user_view.person.id,
77       post_id: data.post_id,
78       featured: data.featured,
79       is_featured_community: data.feature_type == PostFeatureType::Community,
80     };
81
82     ModFeaturePost::create(context.pool(), &form).await?;
83
84     context
85       .send_post_ws_message(
86         &UserOperation::FeaturePost,
87         data.post_id,
88         websocket_id,
89         Some(local_user_view.person.id),
90       )
91       .await
92   }
93 }