2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 post::{FeaturePost, PostResponse},
8 check_community_deleted_or_removed,
9 get_local_user_view_from_jwt,
13 websocket::{send::send_post_ws_message, 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;
37 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
39 let post_id = data.post_id;
40 let orig_post = Post::read(context.pool(), post_id).await?;
43 local_user_view.person.id,
44 orig_post.community_id,
48 check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?;
50 if data.feature_type == PostFeatureType::Community {
51 // Verify that only the mods can feature in community
54 local_user_view.person.id,
55 orig_post.community_id,
59 is_admin(&local_user_view)?;
63 let post_id = data.post_id;
64 let new_post: PostUpdateForm = if data.feature_type == PostFeatureType::Community {
65 PostUpdateForm::builder()
66 .featured_community(Some(data.featured))
69 PostUpdateForm::builder()
70 .featured_local(Some(data.featured))
73 Post::update(context.pool(), post_id, &new_post).await?;
76 let form = ModFeaturePostForm {
77 mod_person_id: local_user_view.person.id,
78 post_id: data.post_id,
79 featured: data.featured,
80 is_featured_community: data.feature_type == PostFeatureType::Community,
83 ModFeaturePost::create(context.pool(), &form).await?;
87 UserOperation::FeaturePost,
89 Some(local_user_view.person.id),