]> Untitled Git - lemmy.git/blob - crates/api/src/site/mod_log.rs
Dont return error in case optional auth is invalid (#2879)
[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, 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 = local_user_view_from_jwt_opt(data.auth.as_ref(), context).await;
47     let local_site = LocalSite::read(context.pool()).await?;
48
49     check_private_instance(&local_user_view, &local_site)?;
50
51     let type_ = data.type_.unwrap_or(All);
52     let community_id = data.community_id;
53
54     let (local_person_id, is_admin) = match local_user_view {
55       Some(s) => (s.person.id, is_admin(&s).is_ok()),
56       None => (PersonId(-1), false),
57     };
58     let community_id_value = match community_id {
59       Some(s) => s,
60       None => CommunityId(-1),
61     };
62     let is_mod_of_community = data.community_id.is_some()
63       && is_mod_or_admin(context.pool(), local_person_id, community_id_value)
64         .await
65         .is_ok();
66     let hide_modlog_names = local_site.hide_modlog_mod_names && !is_mod_of_community && !is_admin;
67
68     let mod_person_id = if hide_modlog_names {
69       None
70     } else {
71       data.mod_person_id
72     };
73     let other_person_id = data.other_person_id;
74     let params = ModlogListParams {
75       community_id,
76       mod_person_id,
77       other_person_id,
78       page: data.page,
79       limit: data.limit,
80       hide_modlog_names,
81     };
82     let removed_posts = match type_ {
83       All | ModRemovePost => ModRemovePostView::list(context.pool(), params).await?,
84       _ => Default::default(),
85     };
86
87     let locked_posts = match type_ {
88       All | ModLockPost => ModLockPostView::list(context.pool(), params).await?,
89       _ => Default::default(),
90     };
91
92     let featured_posts = match type_ {
93       All | ModFeaturePost => ModFeaturePostView::list(context.pool(), params).await?,
94       _ => Default::default(),
95     };
96
97     let removed_comments = match type_ {
98       All | ModRemoveComment => ModRemoveCommentView::list(context.pool(), params).await?,
99       _ => Default::default(),
100     };
101
102     let banned_from_community = match type_ {
103       All | ModBanFromCommunity => ModBanFromCommunityView::list(context.pool(), params).await?,
104       _ => Default::default(),
105     };
106
107     let added_to_community = match type_ {
108       All | ModAddCommunity => ModAddCommunityView::list(context.pool(), params).await?,
109       _ => Default::default(),
110     };
111
112     let transferred_to_community = match type_ {
113       All | ModTransferCommunity => ModTransferCommunityView::list(context.pool(), params).await?,
114       _ => Default::default(),
115     };
116
117     let hidden_communities = match type_ {
118       All | ModHideCommunity if other_person_id.is_none() => {
119         ModHideCommunityView::list(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(context.pool(), params).await?,
137           _ => Default::default(),
138         },
139         match type_ {
140           All | ModAdd => ModAddView::list(context.pool(), params).await?,
141           _ => Default::default(),
142         },
143         match type_ {
144           All | ModRemoveCommunity if other_person_id.is_none() => {
145             ModRemoveCommunityView::list(context.pool(), params).await?
146           }
147           _ => Default::default(),
148         },
149         match type_ {
150           All | AdminPurgePerson if other_person_id.is_none() => {
151             AdminPurgePersonView::list(context.pool(), params).await?
152           }
153           _ => Default::default(),
154         },
155         match type_ {
156           All | AdminPurgeCommunity if other_person_id.is_none() => {
157             AdminPurgeCommunityView::list(context.pool(), params).await?
158           }
159           _ => Default::default(),
160         },
161         match type_ {
162           All | AdminPurgePost if other_person_id.is_none() => {
163             AdminPurgePostView::list(context.pool(), params).await?
164           }
165           _ => Default::default(),
166         },
167         match type_ {
168           All | AdminPurgeComment if other_person_id.is_none() => {
169             AdminPurgeCommentView::list(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 }