]> Untitled Git - lemmy.git/blob - crates/api/src/site/mod_log.rs
2ca725150b3312c2614da1314b2de6fb5810b6a1
[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, is_admin, is_mod_or_admin, local_user_view_from_jwt_opt},
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;
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))]
39   async fn perform(&self, context: &Data<LemmyContext>) -> Result<GetModlogResponse, LemmyError> {
40     let data: &GetModlog = self;
41
42     let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), context).await;
43     let local_site = LocalSite::read(context.pool()).await?;
44
45     check_private_instance(&local_user_view, &local_site)?;
46
47     let type_ = data.type_.unwrap_or(All);
48     let community_id = data.community_id;
49
50     let (local_person_id, is_admin) = match local_user_view {
51       Some(s) => (s.person.id, is_admin(&s).is_ok()),
52       None => (PersonId(-1), false),
53     };
54     let community_id_value = match community_id {
55       Some(s) => s,
56       None => CommunityId(-1),
57     };
58     let is_mod_of_community = data.community_id.is_some()
59       && is_mod_or_admin(context.pool(), local_person_id, community_id_value)
60         .await
61         .is_ok();
62     let hide_modlog_names = local_site.hide_modlog_mod_names && !is_mod_of_community && !is_admin;
63
64     let mod_person_id = if hide_modlog_names {
65       None
66     } else {
67       data.mod_person_id
68     };
69     let other_person_id = data.other_person_id;
70     let params = ModlogListParams {
71       community_id,
72       mod_person_id,
73       other_person_id,
74       page: data.page,
75       limit: data.limit,
76       hide_modlog_names,
77     };
78     let removed_posts = match type_ {
79       All | ModRemovePost => ModRemovePostView::list(context.pool(), params).await?,
80       _ => Default::default(),
81     };
82
83     let locked_posts = match type_ {
84       All | ModLockPost => ModLockPostView::list(context.pool(), params).await?,
85       _ => Default::default(),
86     };
87
88     let featured_posts = match type_ {
89       All | ModFeaturePost => ModFeaturePostView::list(context.pool(), params).await?,
90       _ => Default::default(),
91     };
92
93     let removed_comments = match type_ {
94       All | ModRemoveComment => ModRemoveCommentView::list(context.pool(), params).await?,
95       _ => Default::default(),
96     };
97
98     let banned_from_community = match type_ {
99       All | ModBanFromCommunity => ModBanFromCommunityView::list(context.pool(), params).await?,
100       _ => Default::default(),
101     };
102
103     let added_to_community = match type_ {
104       All | ModAddCommunity => ModAddCommunityView::list(context.pool(), params).await?,
105       _ => Default::default(),
106     };
107
108     let transferred_to_community = match type_ {
109       All | ModTransferCommunity => ModTransferCommunityView::list(context.pool(), params).await?,
110       _ => Default::default(),
111     };
112
113     let hidden_communities = match type_ {
114       All | ModHideCommunity if other_person_id.is_none() => {
115         ModHideCommunityView::list(context.pool(), params).await?
116       }
117       _ => Default::default(),
118     };
119
120     // These arrays are only for the full modlog, when a community isn't given
121     let (
122       banned,
123       added,
124       removed_communities,
125       admin_purged_persons,
126       admin_purged_communities,
127       admin_purged_posts,
128       admin_purged_comments,
129     ) = if data.community_id.is_none() {
130       (
131         match type_ {
132           All | ModBan => ModBanView::list(context.pool(), params).await?,
133           _ => Default::default(),
134         },
135         match type_ {
136           All | ModAdd => ModAddView::list(context.pool(), params).await?,
137           _ => Default::default(),
138         },
139         match type_ {
140           All | ModRemoveCommunity if other_person_id.is_none() => {
141             ModRemoveCommunityView::list(context.pool(), params).await?
142           }
143           _ => Default::default(),
144         },
145         match type_ {
146           All | AdminPurgePerson if other_person_id.is_none() => {
147             AdminPurgePersonView::list(context.pool(), params).await?
148           }
149           _ => Default::default(),
150         },
151         match type_ {
152           All | AdminPurgeCommunity if other_person_id.is_none() => {
153             AdminPurgeCommunityView::list(context.pool(), params).await?
154           }
155           _ => Default::default(),
156         },
157         match type_ {
158           All | AdminPurgePost if other_person_id.is_none() => {
159             AdminPurgePostView::list(context.pool(), params).await?
160           }
161           _ => Default::default(),
162         },
163         match type_ {
164           All | AdminPurgeComment if other_person_id.is_none() => {
165             AdminPurgeCommentView::list(context.pool(), params).await?
166           }
167           _ => Default::default(),
168         },
169       )
170     } else {
171       Default::default()
172     };
173
174     // Return the jwt
175     Ok(GetModlogResponse {
176       removed_posts,
177       locked_posts,
178       featured_posts,
179       removed_comments,
180       removed_communities,
181       banned_from_community,
182       banned,
183       added_to_community,
184       added,
185       transferred_to_community,
186       admin_purged_persons,
187       admin_purged_communities,
188       admin_purged_posts,
189       admin_purged_comments,
190       hidden_communities,
191     })
192   }
193 }