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