]> Untitled Git - lemmy.git/blob - crates/api/src/site/mod_log.rs
Make functions work with both connection and pool (#3420)
[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(&mut 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(&mut 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(&mut context.pool(), params).await?,
80       _ => Default::default(),
81     };
82
83     let locked_posts = match type_ {
84       All | ModLockPost => ModLockPostView::list(&mut context.pool(), params).await?,
85       _ => Default::default(),
86     };
87
88     let featured_posts = match type_ {
89       All | ModFeaturePost => ModFeaturePostView::list(&mut context.pool(), params).await?,
90       _ => Default::default(),
91     };
92
93     let removed_comments = match type_ {
94       All | ModRemoveComment => ModRemoveCommentView::list(&mut context.pool(), params).await?,
95       _ => Default::default(),
96     };
97
98     let banned_from_community = match type_ {
99       All | ModBanFromCommunity => {
100         ModBanFromCommunityView::list(&mut context.pool(), params).await?
101       }
102       _ => Default::default(),
103     };
104
105     let added_to_community = match type_ {
106       All | ModAddCommunity => ModAddCommunityView::list(&mut context.pool(), params).await?,
107       _ => Default::default(),
108     };
109
110     let transferred_to_community = match type_ {
111       All | ModTransferCommunity => {
112         ModTransferCommunityView::list(&mut context.pool(), params).await?
113       }
114       _ => Default::default(),
115     };
116
117     let hidden_communities = match type_ {
118       All | ModHideCommunity if other_person_id.is_none() => {
119         ModHideCommunityView::list(&mut context.pool(), params).await?
120       }
121       _ => Default::default(),
122     };
123
124     // These arrays are only for the full modlog, when a community isn't given
125     let (
126       banned,
127       added,
128       removed_communities,
129       admin_purged_persons,
130       admin_purged_communities,
131       admin_purged_posts,
132       admin_purged_comments,
133     ) = if data.community_id.is_none() {
134       (
135         match type_ {
136           All | ModBan => ModBanView::list(&mut context.pool(), params).await?,
137           _ => Default::default(),
138         },
139         match type_ {
140           All | ModAdd => ModAddView::list(&mut context.pool(), params).await?,
141           _ => Default::default(),
142         },
143         match type_ {
144           All | ModRemoveCommunity if other_person_id.is_none() => {
145             ModRemoveCommunityView::list(&mut context.pool(), params).await?
146           }
147           _ => Default::default(),
148         },
149         match type_ {
150           All | AdminPurgePerson if other_person_id.is_none() => {
151             AdminPurgePersonView::list(&mut context.pool(), params).await?
152           }
153           _ => Default::default(),
154         },
155         match type_ {
156           All | AdminPurgeCommunity if other_person_id.is_none() => {
157             AdminPurgeCommunityView::list(&mut context.pool(), params).await?
158           }
159           _ => Default::default(),
160         },
161         match type_ {
162           All | AdminPurgePost if other_person_id.is_none() => {
163             AdminPurgePostView::list(&mut context.pool(), params).await?
164           }
165           _ => Default::default(),
166         },
167         match type_ {
168           All | AdminPurgeComment if other_person_id.is_none() => {
169             AdminPurgeCommentView::list(&mut context.pool(), params).await?
170           }
171           _ => Default::default(),
172         },
173       )
174     } else {
175       Default::default()
176     };
177
178     // Return the jwt
179     Ok(GetModlogResponse {
180       removed_posts,
181       locked_posts,
182       featured_posts,
183       removed_comments,
184       removed_communities,
185       banned_from_community,
186       banned,
187       added_to_community,
188       added,
189       transferred_to_community,
190       admin_purged_persons,
191       admin_purged_communities,
192       admin_purged_posts,
193       admin_purged_comments,
194       hidden_communities,
195     })
196   }
197 }