2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 site::{GetModlog, GetModlogResponse},
5 utils::{check_private_instance, get_local_user_view_from_jwt_opt, is_admin, is_mod_or_admin},
9 newtypes::{CommunityId, PersonId},
10 source::local_site::LocalSite,
13 use lemmy_db_views_moderator::structs::{
14 AdminPurgeCommentView,
15 AdminPurgeCommunityView,
20 ModBanFromCommunityView,
25 ModRemoveCommunityView,
28 ModTransferCommunityView,
31 use lemmy_utils::{error::LemmyError, ConnectionId};
32 use ModlogActionType::*;
34 #[async_trait::async_trait(?Send)]
35 impl Perform for GetModlog {
36 type Response = GetModlogResponse;
38 #[tracing::instrument(skip(context, _websocket_id))]
41 context: &Data<LemmyContext>,
42 _websocket_id: Option<ConnectionId>,
43 ) -> Result<GetModlogResponse, LemmyError> {
44 let data: &GetModlog = self;
47 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
49 let local_site = LocalSite::read(context.pool()).await?;
51 check_private_instance(&local_user_view, &local_site)?;
53 let type_ = data.type_.unwrap_or(All);
54 let community_id = data.community_id;
56 let (local_person_id, is_admin) = match local_user_view {
57 Some(s) => (s.person.id, is_admin(&s).is_ok()),
58 None => (PersonId(-1), false),
60 let community_id_value = match community_id {
62 None => CommunityId(-1),
64 let is_mod_of_community = data.community_id.is_some()
65 && is_mod_or_admin(context.pool(), local_person_id, community_id_value)
68 let hide_modlog_names = local_site.hide_modlog_mod_names && !is_mod_of_community && !is_admin;
70 let mod_person_id = if hide_modlog_names {
75 let other_person_id = data.other_person_id;
76 let params = ModlogListParams {
84 let removed_posts = match type_ {
85 All | ModRemovePost => ModRemovePostView::list(context.pool(), params).await?,
86 _ => Default::default(),
89 let locked_posts = match type_ {
90 All | ModLockPost => ModLockPostView::list(context.pool(), params).await?,
91 _ => Default::default(),
94 let stickied_posts = match type_ {
95 All | ModStickyPost => ModStickyPostView::list(context.pool(), params).await?,
96 _ => Default::default(),
99 let removed_comments = match type_ {
100 All | ModRemoveComment => ModRemoveCommentView::list(context.pool(), params).await?,
101 _ => Default::default(),
104 let banned_from_community = match type_ {
105 All | ModBanFromCommunity => ModBanFromCommunityView::list(context.pool(), params).await?,
106 _ => Default::default(),
109 let added_to_community = match type_ {
110 All | ModAddCommunity => ModAddCommunityView::list(context.pool(), params).await?,
111 _ => Default::default(),
114 let transferred_to_community = match type_ {
115 All | ModTransferCommunity => ModTransferCommunityView::list(context.pool(), params).await?,
116 _ => Default::default(),
119 let hidden_communities = match type_ {
120 All | ModHideCommunity if other_person_id.is_none() => {
121 ModHideCommunityView::list(context.pool(), params).await?
123 _ => Default::default(),
126 // These arrays are only for the full modlog, when a community isn't given
131 admin_purged_persons,
132 admin_purged_communities,
134 admin_purged_comments,
135 ) = if data.community_id.is_none() {
138 All | ModBan => ModBanView::list(context.pool(), params).await?,
139 _ => Default::default(),
142 All | ModAdd => ModAddView::list(context.pool(), params).await?,
143 _ => Default::default(),
146 All | ModRemoveCommunity if other_person_id.is_none() => {
147 ModRemoveCommunityView::list(context.pool(), params).await?
149 _ => Default::default(),
152 All | AdminPurgePerson if other_person_id.is_none() => {
153 AdminPurgePersonView::list(context.pool(), params).await?
155 _ => Default::default(),
158 All | AdminPurgeCommunity if other_person_id.is_none() => {
159 AdminPurgeCommunityView::list(context.pool(), params).await?
161 _ => Default::default(),
164 All | AdminPurgePost if other_person_id.is_none() => {
165 AdminPurgePostView::list(context.pool(), params).await?
167 _ => Default::default(),
170 All | AdminPurgeComment if other_person_id.is_none() => {
171 AdminPurgeCommentView::list(context.pool(), params).await?
173 _ => Default::default(),
181 Ok(GetModlogResponse {
187 banned_from_community,
191 transferred_to_community,
192 admin_purged_persons,
193 admin_purged_communities,
195 admin_purged_comments,