2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 site::{GetModlog, GetModlogResponse},
6 utils::{check_private_instance, is_admin, is_mod_or_admin, local_user_view_from_jwt_opt},
9 newtypes::{CommunityId, PersonId},
10 source::local_site::LocalSite,
13 use lemmy_db_views_moderator::structs::{
14 AdminPurgeCommentView,
15 AdminPurgeCommunityView,
20 ModBanFromCommunityView,
26 ModRemoveCommunityView,
28 ModTransferCommunityView,
31 use lemmy_utils::error::LemmyError;
32 use ModlogActionType::*;
34 #[async_trait::async_trait(?Send)]
35 impl Perform for GetModlog {
36 type Response = GetModlogResponse;
38 #[tracing::instrument(skip(context))]
39 async fn perform(&self, context: &Data<LemmyContext>) -> Result<GetModlogResponse, LemmyError> {
40 let data: &GetModlog = self;
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?;
45 check_private_instance(&local_user_view, &local_site)?;
47 let type_ = data.type_.unwrap_or(All);
48 let community_id = data.community_id;
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),
54 let community_id_value = match community_id {
56 None => CommunityId(-1),
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)
62 let hide_modlog_names = local_site.hide_modlog_mod_names && !is_mod_of_community && !is_admin;
64 let mod_person_id = if hide_modlog_names {
69 let other_person_id = data.other_person_id;
70 let params = ModlogListParams {
78 let removed_posts = match type_ {
79 All | ModRemovePost => ModRemovePostView::list(&mut context.pool(), params).await?,
80 _ => Default::default(),
83 let locked_posts = match type_ {
84 All | ModLockPost => ModLockPostView::list(&mut context.pool(), params).await?,
85 _ => Default::default(),
88 let featured_posts = match type_ {
89 All | ModFeaturePost => ModFeaturePostView::list(&mut context.pool(), params).await?,
90 _ => Default::default(),
93 let removed_comments = match type_ {
94 All | ModRemoveComment => ModRemoveCommentView::list(&mut context.pool(), params).await?,
95 _ => Default::default(),
98 let banned_from_community = match type_ {
99 All | ModBanFromCommunity => {
100 ModBanFromCommunityView::list(&mut context.pool(), params).await?
102 _ => Default::default(),
105 let added_to_community = match type_ {
106 All | ModAddCommunity => ModAddCommunityView::list(&mut context.pool(), params).await?,
107 _ => Default::default(),
110 let transferred_to_community = match type_ {
111 All | ModTransferCommunity => {
112 ModTransferCommunityView::list(&mut context.pool(), params).await?
114 _ => Default::default(),
117 let hidden_communities = match type_ {
118 All | ModHideCommunity if other_person_id.is_none() => {
119 ModHideCommunityView::list(&mut context.pool(), params).await?
121 _ => Default::default(),
124 // These arrays are only for the full modlog, when a community isn't given
129 admin_purged_persons,
130 admin_purged_communities,
132 admin_purged_comments,
133 ) = if data.community_id.is_none() {
136 All | ModBan => ModBanView::list(&mut context.pool(), params).await?,
137 _ => Default::default(),
140 All | ModAdd => ModAddView::list(&mut context.pool(), params).await?,
141 _ => Default::default(),
144 All | ModRemoveCommunity if other_person_id.is_none() => {
145 ModRemoveCommunityView::list(&mut context.pool(), params).await?
147 _ => Default::default(),
150 All | AdminPurgePerson if other_person_id.is_none() => {
151 AdminPurgePersonView::list(&mut context.pool(), params).await?
153 _ => Default::default(),
156 All | AdminPurgeCommunity if other_person_id.is_none() => {
157 AdminPurgeCommunityView::list(&mut context.pool(), params).await?
159 _ => Default::default(),
162 All | AdminPurgePost if other_person_id.is_none() => {
163 AdminPurgePostView::list(&mut context.pool(), params).await?
165 _ => Default::default(),
168 All | AdminPurgeComment if other_person_id.is_none() => {
169 AdminPurgeCommentView::list(&mut context.pool(), params).await?
171 _ => Default::default(),
179 Ok(GetModlogResponse {
185 banned_from_community,
189 transferred_to_community,
190 admin_purged_persons,
191 admin_purged_communities,
193 admin_purged_comments,