2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 post::{FeaturePost, PostResponse},
8 check_community_deleted_or_removed,
11 local_user_view_from_jwt,
13 websocket::UserOperation,
15 use lemmy_db_schema::{
17 moderator::{ModFeaturePost, ModFeaturePostForm},
18 post::{Post, PostUpdateForm},
23 use lemmy_utils::{error::LemmyError, ConnectionId};
25 #[async_trait::async_trait(?Send)]
26 impl Perform for FeaturePost {
27 type Response = PostResponse;
29 #[tracing::instrument(skip(context, websocket_id))]
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?;
38 let post_id = data.post_id;
39 let orig_post = Post::read(context.pool(), post_id).await?;
42 local_user_view.person.id,
43 orig_post.community_id,
47 check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?;
49 if data.feature_type == PostFeatureType::Community {
50 // Verify that only the mods can feature in community
53 local_user_view.person.id,
54 orig_post.community_id,
58 is_admin(&local_user_view)?;
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))
68 PostUpdateForm::builder()
69 .featured_local(Some(data.featured))
72 Post::update(context.pool(), post_id, &new_post).await?;
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,
82 ModFeaturePost::create(context.pool(), &form).await?;
85 .send_post_ws_message(
86 &UserOperation::FeaturePost,
89 Some(local_user_view.person.id),