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, 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;
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?;
49 check_private_instance(&local_user_view, &local_site)?;
51 let type_ = data.type_.unwrap_or(All);
52 let community_id = data.community_id;
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),
58 let community_id_value = match community_id {
60 None => CommunityId(-1),
62 let is_mod_of_community = data.community_id.is_some()
63 && is_mod_or_admin(context.pool(), local_person_id, community_id_value)
66 let hide_modlog_names = local_site.hide_modlog_mod_names && !is_mod_of_community && !is_admin;
68 let mod_person_id = if hide_modlog_names {
73 let other_person_id = data.other_person_id;
74 let params = ModlogListParams {
82 let removed_posts = match type_ {
83 All | ModRemovePost => ModRemovePostView::list(context.pool(), params).await?,
84 _ => Default::default(),
87 let locked_posts = match type_ {
88 All | ModLockPost => ModLockPostView::list(context.pool(), params).await?,
89 _ => Default::default(),
92 let featured_posts = match type_ {
93 All | ModFeaturePost => ModFeaturePostView::list(context.pool(), params).await?,
94 _ => Default::default(),
97 let removed_comments = match type_ {
98 All | ModRemoveComment => ModRemoveCommentView::list(context.pool(), params).await?,
99 _ => Default::default(),
102 let banned_from_community = match type_ {
103 All | ModBanFromCommunity => ModBanFromCommunityView::list(context.pool(), params).await?,
104 _ => Default::default(),
107 let added_to_community = match type_ {
108 All | ModAddCommunity => ModAddCommunityView::list(context.pool(), params).await?,
109 _ => Default::default(),
112 let transferred_to_community = match type_ {
113 All | ModTransferCommunity => ModTransferCommunityView::list(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(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(context.pool(), params).await?,
137 _ => Default::default(),
140 All | ModAdd => ModAddView::list(context.pool(), params).await?,
141 _ => Default::default(),
144 All | ModRemoveCommunity if other_person_id.is_none() => {
145 ModRemoveCommunityView::list(context.pool(), params).await?
147 _ => Default::default(),
150 All | AdminPurgePerson if other_person_id.is_none() => {
151 AdminPurgePersonView::list(context.pool(), params).await?
153 _ => Default::default(),
156 All | AdminPurgeCommunity if other_person_id.is_none() => {
157 AdminPurgeCommunityView::list(context.pool(), params).await?
159 _ => Default::default(),
162 All | AdminPurgePost if other_person_id.is_none() => {
163 AdminPurgePostView::list(context.pool(), params).await?
165 _ => Default::default(),
168 All | AdminPurgeComment if other_person_id.is_none() => {
169 AdminPurgeCommentView::list(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,