]> Untitled Git - lemmy.git/blobdiff - crates/api/src/site/mod_log.rs
Add Modlog Filters (#2313)
[lemmy.git] / crates / api / src / site / mod_log.rs
index a96595da9c38a23c1def8dcb699c4f3483be321f..a3ac67b9afbd5b79303e7399458962daa0ef59bf 100644 (file)
@@ -2,7 +2,18 @@ use crate::Perform;
 use actix_web::web::Data;
 use lemmy_api_common::{
   site::{GetModlog, GetModlogResponse},
-  utils::{blocking, check_private_instance, get_local_user_view_from_jwt_opt},
+  utils::{
+    blocking,
+    check_private_instance,
+    get_local_user_view_from_jwt_opt,
+    is_admin,
+    is_mod_or_admin,
+  },
+};
+use lemmy_db_schema::{
+  newtypes::{CommunityId, PersonId},
+  source::site::Site,
+  ModlogActionType,
 };
 use lemmy_db_views_moderator::structs::{
   AdminPurgeCommentView,
@@ -20,9 +31,11 @@ use lemmy_db_views_moderator::structs::{
   ModRemovePostView,
   ModStickyPostView,
   ModTransferCommunityView,
+  ModlogListParams,
 };
 use lemmy_utils::{error::LemmyError, ConnectionId};
 use lemmy_websocket::LemmyContext;
+use ModlogActionType::*;
 
 #[async_trait::async_trait(?Send)]
 impl Perform for GetModlog {
@@ -42,55 +55,123 @@ impl Perform for GetModlog {
 
     check_private_instance(&local_user_view, context.pool()).await?;
 
+    let type_ = data.type_.unwrap_or(All);
     let community_id = data.community_id;
-    let mod_person_id = data.mod_person_id;
-    let page = data.page;
-    let limit = data.limit;
-    let removed_posts = blocking(context.pool(), move |conn| {
-      ModRemovePostView::list(conn, community_id, mod_person_id, page, limit)
-    })
-    .await??;
 
-    let locked_posts = blocking(context.pool(), move |conn| {
-      ModLockPostView::list(conn, community_id, mod_person_id, page, limit)
-    })
-    .await??;
+    let site = blocking(context.pool(), Site::read_local_site).await??;
+    let (local_person_id, is_admin) = match local_user_view {
+      Some(s) => (s.person.id, is_admin(&s).is_ok()),
+      None => (PersonId(-1), false),
+    };
+    let community_id_value = match community_id {
+      Some(s) => s,
+      None => CommunityId(-1),
+    };
+    let is_mod_of_community = data.community_id.is_some()
+      && is_mod_or_admin(context.pool(), local_person_id, community_id_value)
+        .await
+        .is_ok();
+    let hide_modlog_names = site.hide_modlog_mod_names && !is_mod_of_community && !is_admin;
 
-    let stickied_posts = blocking(context.pool(), move |conn| {
-      ModStickyPostView::list(conn, community_id, mod_person_id, page, limit)
-    })
-    .await??;
+    let mod_person_id = if hide_modlog_names {
+      None
+    } else {
+      data.mod_person_id
+    };
+    let other_person_id = data.other_person_id;
+    let params = ModlogListParams {
+      community_id,
+      mod_person_id,
+      other_person_id,
+      page: data.page,
+      limit: data.limit,
+      hide_modlog_names,
+    };
+    let removed_posts = match type_ {
+      All | ModRemovePost => {
+        blocking(context.pool(), move |conn| {
+          ModRemovePostView::list(conn, params)
+        })
+        .await??
+      }
+      _ => Default::default(),
+    };
 
-    let removed_comments = blocking(context.pool(), move |conn| {
-      ModRemoveCommentView::list(conn, community_id, mod_person_id, page, limit)
-    })
-    .await??;
+    let locked_posts = match type_ {
+      All | ModLockPost => {
+        blocking(context.pool(), move |conn| {
+          ModLockPostView::list(conn, params)
+        })
+        .await??
+      }
+      _ => Default::default(),
+    };
 
-    let banned_from_community = blocking(context.pool(), move |conn| {
-      ModBanFromCommunityView::list(conn, community_id, mod_person_id, page, limit)
-    })
-    .await??;
+    let stickied_posts = match type_ {
+      All | ModStickyPost => {
+        blocking(context.pool(), move |conn| {
+          ModStickyPostView::list(conn, params)
+        })
+        .await??
+      }
+      _ => Default::default(),
+    };
 
-    let added_to_community = blocking(context.pool(), move |conn| {
-      ModAddCommunityView::list(conn, community_id, mod_person_id, page, limit)
-    })
-    .await??;
+    let removed_comments = match type_ {
+      All | ModRemoveComment => {
+        blocking(context.pool(), move |conn| {
+          ModRemoveCommentView::list(conn, params)
+        })
+        .await??
+      }
+      _ => Default::default(),
+    };
 
-    let transferred_to_community = blocking(context.pool(), move |conn| {
-      ModTransferCommunityView::list(conn, community_id, mod_person_id, page, limit)
-    })
-    .await??;
+    let banned_from_community = match type_ {
+      All | ModBanFromCommunity => {
+        blocking(context.pool(), move |conn| {
+          ModBanFromCommunityView::list(conn, params)
+        })
+        .await??
+      }
+      _ => Default::default(),
+    };
 
-    let hidden_communities = blocking(context.pool(), move |conn| {
-      ModHideCommunityView::list(conn, community_id, mod_person_id, page, limit)
-    })
-    .await??;
+    let added_to_community = match type_ {
+      All | ModAddCommunity => {
+        blocking(context.pool(), move |conn| {
+          ModAddCommunityView::list(conn, params)
+        })
+        .await??
+      }
+      _ => Default::default(),
+    };
+
+    let transferred_to_community = match type_ {
+      All | ModTransferCommunity => {
+        blocking(context.pool(), move |conn| {
+          ModTransferCommunityView::list(conn, params)
+        })
+        .await??
+      }
+      _ => Default::default(),
+    };
+
+    let hidden_communities = match type_ {
+      All | ModHideCommunity if other_person_id.is_none() => {
+        blocking(context.pool(), move |conn| {
+          ModHideCommunityView::list(conn, params)
+        })
+        .await??
+      }
+      _ => Default::default(),
+    };
 
     // These arrays are only for the full modlog, when a community isn't given
     let (
-      removed_communities,
       banned,
       added,
+      removed_communities,
       admin_purged_persons,
       admin_purged_communities,
       admin_purged_posts,
@@ -98,13 +179,44 @@ impl Perform for GetModlog {
     ) = if data.community_id.is_none() {
       blocking(context.pool(), move |conn| {
         Ok((
-          ModRemoveCommunityView::list(conn, mod_person_id, page, limit)?,
-          ModBanView::list(conn, mod_person_id, page, limit)?,
-          ModAddView::list(conn, mod_person_id, page, limit)?,
-          AdminPurgePersonView::list(conn, mod_person_id, page, limit)?,
-          AdminPurgeCommunityView::list(conn, mod_person_id, page, limit)?,
-          AdminPurgePostView::list(conn, mod_person_id, page, limit)?,
-          AdminPurgeCommentView::list(conn, mod_person_id, page, limit)?,
+          match type_ {
+            All | ModBan => ModBanView::list(conn, params)?,
+            _ => Default::default(),
+          },
+          match type_ {
+            All | ModAdd => ModAddView::list(conn, params)?,
+            _ => Default::default(),
+          },
+          match type_ {
+            All | ModRemoveCommunity if other_person_id.is_none() => {
+              ModRemoveCommunityView::list(conn, params)?
+            }
+            _ => Default::default(),
+          },
+          match type_ {
+            All | AdminPurgePerson if other_person_id.is_none() => {
+              AdminPurgePersonView::list(conn, params)?
+            }
+            _ => Default::default(),
+          },
+          match type_ {
+            All | AdminPurgeCommunity if other_person_id.is_none() => {
+              AdminPurgeCommunityView::list(conn, params)?
+            }
+            _ => Default::default(),
+          },
+          match type_ {
+            All | AdminPurgePost if other_person_id.is_none() => {
+              AdminPurgePostView::list(conn, params)?
+            }
+            _ => Default::default(),
+          },
+          match type_ {
+            All | AdminPurgeComment if other_person_id.is_none() => {
+              AdminPurgeCommentView::list(conn, params)?
+            }
+            _ => Default::default(),
+          },
         )) as Result<_, LemmyError>
       })
       .await??