]> Untitled Git - lemmy.git/blob - crates/api/src/site/mod_log.rs
Add support for Featured Posts (#2585)
[lemmy.git] / crates / api / src / site / mod_log.rs
1 use crate::Perform;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   context::LemmyContext,
5   site::{GetModlog, GetModlogResponse},
6   utils::{check_private_instance, get_local_user_view_from_jwt_opt, is_admin, is_mod_or_admin},
7 };
8 use lemmy_db_schema::{
9   newtypes::{CommunityId, PersonId},
10   source::local_site::LocalSite,
11   ModlogActionType,
12 };
13 use lemmy_db_views_moderator::structs::{
14   AdminPurgeCommentView,
15   AdminPurgeCommunityView,
16   AdminPurgePersonView,
17   AdminPurgePostView,
18   ModAddCommunityView,
19   ModAddView,
20   ModBanFromCommunityView,
21   ModBanView,
22   ModFeaturePostView,
23   ModHideCommunityView,
24   ModLockPostView,
25   ModRemoveCommentView,
26   ModRemoveCommunityView,
27   ModRemovePostView,
28   ModTransferCommunityView,
29   ModlogListParams,
30 };
31 use lemmy_utils::{error::LemmyError, ConnectionId};
32 use ModlogActionType::*;
33
34 #[async_trait::async_trait(?Send)]
35 impl Perform for GetModlog {
36   type Response = GetModlogResponse;
37
38   #[tracing::instrument(skip(context, _websocket_id))]
39   async fn perform(
40     &self,
41     context: &Data<LemmyContext>,
42     _websocket_id: Option<ConnectionId>,
43   ) -> Result<GetModlogResponse, LemmyError> {
44     let data: &GetModlog = self;
45
46     let local_user_view =
47       get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
48         .await?;
49     let local_site = LocalSite::read(context.pool()).await?;
50
51     check_private_instance(&local_user_view, &local_site)?;
52
53     let type_ = data.type_.unwrap_or(All);
54     let community_id = data.community_id;
55
56     let (local_person_id, is_admin) = match local_user_view {
57       Some(s) => (s.person.id, is_admin(&s).is_ok()),
58       None => (PersonId(-1), false),
59     };
60     let community_id_value = match community_id {
61       Some(s) => s,
62       None => CommunityId(-1),
63     };
64     let is_mod_of_community = data.community_id.is_some()
65       && is_mod_or_admin(context.pool(), local_person_id, community_id_value)
66         .await
67         .is_ok();
68     let hide_modlog_names = local_site.hide_modlog_mod_names && !is_mod_of_community && !is_admin;
69
70     let mod_person_id = if hide_modlog_names {
71       None
72     } else {
73       data.mod_person_id
74     };
75     let other_person_id = data.other_person_id;
76     let params = ModlogListParams {
77       community_id,
78       mod_person_id,
79       other_person_id,
80       page: data.page,
81       limit: data.limit,
82       hide_modlog_names,
83     };
84     let removed_posts = match type_ {
85       All | ModRemovePost => ModRemovePostView::list(context.pool(), params).await?,
86       _ => Default::default(),
87     };
88
89     let locked_posts = match type_ {
90       All | ModLockPost => ModLockPostView::list(context.pool(), params).await?,
91       _ => Default::default(),
92     };
93
94     let featured_posts = match type_ {
95       All | ModFeaturePost => ModFeaturePostView::list(context.pool(), params).await?,
96       _ => Default::default(),
97     };
98
99     let removed_comments = match type_ {
100       All | ModRemoveComment => ModRemoveCommentView::list(context.pool(), params).await?,
101       _ => Default::default(),
102     };
103
104     let banned_from_community = match type_ {
105       All | ModBanFromCommunity => ModBanFromCommunityView::list(context.pool(), params).await?,
106       _ => Default::default(),
107     };
108
109     let added_to_community = match type_ {
110       All | ModAddCommunity => ModAddCommunityView::list(context.pool(), params).await?,
111       _ => Default::default(),
112     };
113
114     let transferred_to_community = match type_ {
115       All | ModTransferCommunity => ModTransferCommunityView::list(context.pool(), params).await?,
116       _ => Default::default(),
117     };
118
119     let hidden_communities = match type_ {
120       All | ModHideCommunity if other_person_id.is_none() => {
121         ModHideCommunityView::list(context.pool(), params).await?
122       }
123       _ => Default::default(),
124     };
125
126     // These arrays are only for the full modlog, when a community isn't given
127     let (
128       banned,
129       added,
130       removed_communities,
131       admin_purged_persons,
132       admin_purged_communities,
133       admin_purged_posts,
134       admin_purged_comments,
135     ) = if data.community_id.is_none() {
136       (
137         match type_ {
138           All | ModBan => ModBanView::list(context.pool(), params).await?,
139           _ => Default::default(),
140         },
141         match type_ {
142           All | ModAdd => ModAddView::list(context.pool(), params).await?,
143           _ => Default::default(),
144         },
145         match type_ {
146           All | ModRemoveCommunity if other_person_id.is_none() => {
147             ModRemoveCommunityView::list(context.pool(), params).await?
148           }
149           _ => Default::default(),
150         },
151         match type_ {
152           All | AdminPurgePerson if other_person_id.is_none() => {
153             AdminPurgePersonView::list(context.pool(), params).await?
154           }
155           _ => Default::default(),
156         },
157         match type_ {
158           All | AdminPurgeCommunity if other_person_id.is_none() => {
159             AdminPurgeCommunityView::list(context.pool(), params).await?
160           }
161           _ => Default::default(),
162         },
163         match type_ {
164           All | AdminPurgePost if other_person_id.is_none() => {
165             AdminPurgePostView::list(context.pool(), params).await?
166           }
167           _ => Default::default(),
168         },
169         match type_ {
170           All | AdminPurgeComment if other_person_id.is_none() => {
171             AdminPurgeCommentView::list(context.pool(), params).await?
172           }
173           _ => Default::default(),
174         },
175       )
176     } else {
177       Default::default()
178     };
179
180     // Return the jwt
181     Ok(GetModlogResponse {
182       removed_posts,
183       locked_posts,
184       featured_posts,
185       removed_comments,
186       removed_communities,
187       banned_from_community,
188       banned,
189       added_to_community,
190       added,
191       transferred_to_community,
192       admin_purged_persons,
193       admin_purged_communities,
194       admin_purged_posts,
195       admin_purged_comments,
196       hidden_communities,
197     })
198   }
199 }