From 21455d6b73a559e3cbaff894c68d2bf069f06c92 Mon Sep 17 00:00:00 2001 From: Anon Date: Tue, 16 Aug 2022 06:52:04 -0500 Subject: [PATCH] Add Modlog Filters (#2313) * Fix crash running locally on windows. * Add support for filtering mod logs * Refactor cleanup * Clippy fix * Condense match statements * Clippy fix 2 --- crates/api/src/site/mod_log.rs | 200 ++++++++++++++---- crates/api_common/src/site.rs | 5 + crates/api_crud/src/site/create.rs | 1 + crates/api_crud/src/site/update.rs | 1 + crates/db_schema/src/lib.rs | 20 ++ crates/db_schema/src/schema.rs | 1 + crates/db_schema/src/source/site.rs | 2 + .../src/admin_purge_comment_view.rs | 30 +-- .../src/admin_purge_community_view.rs | 30 +-- .../src/admin_purge_person_view.rs | 29 +-- .../src/admin_purge_post_view.rs | 29 +-- .../src/mod_add_community_view.rs | 43 ++-- crates/db_views_moderator/src/mod_add_view.rs | 33 +-- .../src/mod_ban_from_community_view.rs | 38 ++-- crates/db_views_moderator/src/mod_ban_view.rs | 33 +-- .../src/mod_hide_community_view.rs | 34 +-- .../src/mod_lock_post_view.rs | 41 ++-- .../src/mod_remove_comment_view.rs | 38 ++-- .../src/mod_remove_community_view.rs | 29 +-- .../src/mod_remove_post_view.rs | 41 ++-- .../src/mod_sticky_post_view.rs | 41 ++-- .../src/mod_transfer_community_view.rs | 38 ++-- crates/db_views_moderator/src/structs.rs | 85 ++++---- .../down.sql | 1 + .../up.sql | 1 + src/main.rs | 1 + 26 files changed, 543 insertions(+), 302 deletions(-) create mode 100644 migrations/2022-06-12-012121_add_site_hide_modlog_names/down.sql create mode 100644 migrations/2022-06-12-012121_add_site_hide_modlog_names/up.sql diff --git a/crates/api/src/site/mod_log.rs b/crates/api/src/site/mod_log.rs index a96595da9..a3ac67b9a 100644 --- a/crates/api/src/site/mod_log.rs +++ b/crates/api/src/site/mod_log.rs @@ -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?? diff --git a/crates/api_common/src/site.rs b/crates/api_common/src/site.rs index 8a0e16a94..f5098a5ee 100644 --- a/crates/api_common/src/site.rs +++ b/crates/api_common/src/site.rs @@ -2,6 +2,7 @@ use crate::sensitive::Sensitive; use lemmy_db_schema::{ newtypes::{CommentId, CommunityId, PersonId, PostId}, ListingType, + ModlogActionType, SearchType, SortType, }; @@ -83,6 +84,8 @@ pub struct GetModlog { pub page: Option, pub limit: Option, pub auth: Option>, + pub type_: Option, + pub other_person_id: Option, } #[derive(Debug, Serialize, Deserialize, Clone)] @@ -122,6 +125,7 @@ pub struct CreateSite { pub default_theme: Option, pub default_post_listing_type: Option, pub auth: Sensitive, + pub hide_modlog_mod_names: Option, } #[derive(Debug, Serialize, Deserialize, Clone, Default)] @@ -143,6 +147,7 @@ pub struct EditSite { pub default_post_listing_type: Option, pub legal_information: Option, pub auth: Sensitive, + pub hide_modlog_mod_names: Option, } #[derive(Debug, Serialize, Deserialize, Clone, Default)] diff --git a/crates/api_crud/src/site/create.rs b/crates/api_crud/src/site/create.rs index 05a8c202e..c61e3feae 100644 --- a/crates/api_crud/src/site/create.rs +++ b/crates/api_crud/src/site/create.rs @@ -76,6 +76,7 @@ impl PerformCrud for CreateSite { public_key: Some(keypair.public_key), default_theme: data.default_theme.clone(), default_post_listing_type: data.default_post_listing_type.clone(), + hide_modlog_mod_names: data.hide_modlog_mod_names, ..SiteForm::default() }; diff --git a/crates/api_crud/src/site/update.rs b/crates/api_crud/src/site/update.rs index dabd118c5..e5a56468b 100644 --- a/crates/api_crud/src/site/update.rs +++ b/crates/api_crud/src/site/update.rs @@ -86,6 +86,7 @@ impl PerformCrud for EditSite { default_theme: data.default_theme.clone(), default_post_listing_type: data.default_post_listing_type.clone(), legal_information, + hide_modlog_mod_names: data.hide_modlog_mod_names, ..SiteForm::default() }; diff --git a/crates/db_schema/src/lib.rs b/crates/db_schema/src/lib.rs index a89d1c987..901ff287a 100644 --- a/crates/db_schema/src/lib.rs +++ b/crates/db_schema/src/lib.rs @@ -71,3 +71,23 @@ pub enum SubscribedType { NotSubscribed, Pending, } + +#[derive(EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy, PartialEq)] +pub enum ModlogActionType { + All, + ModRemovePost, + ModLockPost, + ModStickyPost, + ModRemoveComment, + ModRemoveCommunity, + ModBanFromCommunity, + ModAddCommunity, + ModTransferCommunity, + ModAdd, + ModBan, + ModHideCommunity, + AdminPurgePerson, + AdminPurgeCommunity, + AdminPurgePost, + AdminPurgeComment, +} diff --git a/crates/db_schema/src/schema.rs b/crates/db_schema/src/schema.rs index 9416bc437..44e8e0bbc 100644 --- a/crates/db_schema/src/schema.rs +++ b/crates/db_schema/src/schema.rs @@ -477,6 +477,7 @@ table! { default_theme -> Text, default_post_listing_type -> Text, legal_information -> Nullable, + hide_modlog_mod_names -> Bool, } } diff --git a/crates/db_schema/src/source/site.rs b/crates/db_schema/src/source/site.rs index cc5b75054..7975071c7 100644 --- a/crates/db_schema/src/source/site.rs +++ b/crates/db_schema/src/source/site.rs @@ -32,6 +32,7 @@ pub struct Site { pub default_theme: String, pub default_post_listing_type: String, pub legal_information: Option, + pub hide_modlog_mod_names: bool, } #[derive(Default)] @@ -61,4 +62,5 @@ pub struct SiteForm { pub default_theme: Option, pub default_post_listing_type: Option, pub legal_information: Option>, + pub hide_modlog_mod_names: Option, } diff --git a/crates/db_views_moderator/src/admin_purge_comment_view.rs b/crates/db_views_moderator/src/admin_purge_comment_view.rs index 033991a8f..cd94a0854 100644 --- a/crates/db_views_moderator/src/admin_purge_comment_view.rs +++ b/crates/db_views_moderator/src/admin_purge_comment_view.rs @@ -1,4 +1,4 @@ -use crate::structs::AdminPurgeCommentView; +use crate::structs::{AdminPurgeCommentView, ModlogListParams}; use diesel::{result::Error, *}; use lemmy_db_schema::{ newtypes::PersonId, @@ -12,30 +12,33 @@ use lemmy_db_schema::{ utils::limit_and_offset, }; -type AdminPurgeCommentViewTuple = (AdminPurgeComment, PersonSafe, Post); +type AdminPurgeCommentViewTuple = (AdminPurgeComment, Option, Post); impl AdminPurgeCommentView { - pub fn list( - conn: &PgConnection, - admin_person_id: Option, - page: Option, - limit: Option, - ) -> Result, Error> { + pub fn list(conn: &PgConnection, params: ModlogListParams) -> Result, Error> { + let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); + let show_mod_names = !params.hide_modlog_names; + let show_mod_names_expr = show_mod_names.as_sql::(); + + let admin_names_join = admin_purge_comment::admin_person_id + .eq(person::id) + .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join))); + let mut query = admin_purge_comment::table - .inner_join(person::table.on(admin_purge_comment::admin_person_id.eq(person::id))) + .left_join(person::table.on(admin_names_join)) .inner_join(post::table) .select(( admin_purge_comment::all_columns, - Person::safe_columns_tuple(), + Person::safe_columns_tuple().nullable(), post::all_columns, )) .into_boxed(); - if let Some(admin_person_id) = admin_person_id { + if let Some(admin_person_id) = params.mod_person_id { query = query.filter(admin_purge_comment::admin_person_id.eq(admin_person_id)); }; - let (limit, offset) = limit_and_offset(page, limit)?; + let (limit, offset) = limit_and_offset(params.page, params.limit)?; let res = query .limit(limit) @@ -43,7 +46,8 @@ impl AdminPurgeCommentView { .order_by(admin_purge_comment::when_.desc()) .load::(conn)?; - Ok(Self::from_tuple_to_vec(res)) + let results = Self::from_tuple_to_vec(res); + Ok(results) } } diff --git a/crates/db_views_moderator/src/admin_purge_community_view.rs b/crates/db_views_moderator/src/admin_purge_community_view.rs index 7510779ff..80528d00b 100644 --- a/crates/db_views_moderator/src/admin_purge_community_view.rs +++ b/crates/db_views_moderator/src/admin_purge_community_view.rs @@ -1,4 +1,4 @@ -use crate::structs::AdminPurgeCommunityView; +use crate::structs::{AdminPurgeCommunityView, ModlogListParams}; use diesel::{result::Error, *}; use lemmy_db_schema::{ newtypes::PersonId, @@ -11,28 +11,31 @@ use lemmy_db_schema::{ utils::limit_and_offset, }; -type AdminPurgeCommunityViewTuple = (AdminPurgeCommunity, PersonSafe); +type AdminPurgeCommunityViewTuple = (AdminPurgeCommunity, Option); impl AdminPurgeCommunityView { - pub fn list( - conn: &PgConnection, - admin_person_id: Option, - page: Option, - limit: Option, - ) -> Result, Error> { + pub fn list(conn: &PgConnection, params: ModlogListParams) -> Result, Error> { + let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); + let show_mod_names = !params.hide_modlog_names; + let show_mod_names_expr = show_mod_names.as_sql::(); + + let admin_names_join = admin_purge_community::admin_person_id + .eq(person::id) + .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join))); + let mut query = admin_purge_community::table - .inner_join(person::table.on(admin_purge_community::admin_person_id.eq(person::id))) + .left_join(person::table.on(admin_names_join)) .select(( admin_purge_community::all_columns, - Person::safe_columns_tuple(), + Person::safe_columns_tuple().nullable(), )) .into_boxed(); - if let Some(admin_person_id) = admin_person_id { + if let Some(admin_person_id) = params.mod_person_id { query = query.filter(admin_purge_community::admin_person_id.eq(admin_person_id)); }; - let (limit, offset) = limit_and_offset(page, limit)?; + let (limit, offset) = limit_and_offset(params.page, params.limit)?; let res = query .limit(limit) @@ -40,7 +43,8 @@ impl AdminPurgeCommunityView { .order_by(admin_purge_community::when_.desc()) .load::(conn)?; - Ok(Self::from_tuple_to_vec(res)) + let results = Self::from_tuple_to_vec(res); + Ok(results) } } diff --git a/crates/db_views_moderator/src/admin_purge_person_view.rs b/crates/db_views_moderator/src/admin_purge_person_view.rs index eb0b714ab..84a33a53f 100644 --- a/crates/db_views_moderator/src/admin_purge_person_view.rs +++ b/crates/db_views_moderator/src/admin_purge_person_view.rs @@ -1,4 +1,4 @@ -use crate::structs::AdminPurgePersonView; +use crate::structs::{AdminPurgePersonView, ModlogListParams}; use diesel::{result::Error, *}; use lemmy_db_schema::{ newtypes::PersonId, @@ -11,28 +11,30 @@ use lemmy_db_schema::{ utils::limit_and_offset, }; -type AdminPurgePersonViewTuple = (AdminPurgePerson, PersonSafe); +type AdminPurgePersonViewTuple = (AdminPurgePerson, Option); impl AdminPurgePersonView { - pub fn list( - conn: &PgConnection, - admin_person_id: Option, - page: Option, - limit: Option, - ) -> Result, Error> { + pub fn list(conn: &PgConnection, params: ModlogListParams) -> Result, Error> { + let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); + let show_mod_names = !params.hide_modlog_names; + let show_mod_names_expr = show_mod_names.as_sql::(); + + let admin_names_join = admin_purge_person::admin_person_id + .eq(person::id) + .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join))); let mut query = admin_purge_person::table - .inner_join(person::table.on(admin_purge_person::admin_person_id.eq(person::id))) + .left_join(person::table.on(admin_names_join)) .select(( admin_purge_person::all_columns, - Person::safe_columns_tuple(), + Person::safe_columns_tuple().nullable(), )) .into_boxed(); - if let Some(admin_person_id) = admin_person_id { + if let Some(admin_person_id) = params.mod_person_id { query = query.filter(admin_purge_person::admin_person_id.eq(admin_person_id)); }; - let (limit, offset) = limit_and_offset(page, limit)?; + let (limit, offset) = limit_and_offset(params.page, params.limit)?; let res = query .limit(limit) @@ -40,7 +42,8 @@ impl AdminPurgePersonView { .order_by(admin_purge_person::when_.desc()) .load::(conn)?; - Ok(Self::from_tuple_to_vec(res)) + let results = Self::from_tuple_to_vec(res); + Ok(results) } } diff --git a/crates/db_views_moderator/src/admin_purge_post_view.rs b/crates/db_views_moderator/src/admin_purge_post_view.rs index 368b7c53f..9a7f73997 100644 --- a/crates/db_views_moderator/src/admin_purge_post_view.rs +++ b/crates/db_views_moderator/src/admin_purge_post_view.rs @@ -1,4 +1,4 @@ -use crate::structs::AdminPurgePostView; +use crate::structs::{AdminPurgePostView, ModlogListParams}; use diesel::{result::Error, *}; use lemmy_db_schema::{ newtypes::PersonId, @@ -12,30 +12,32 @@ use lemmy_db_schema::{ utils::limit_and_offset, }; -type AdminPurgePostViewTuple = (AdminPurgePost, PersonSafe, CommunitySafe); +type AdminPurgePostViewTuple = (AdminPurgePost, Option, CommunitySafe); impl AdminPurgePostView { - pub fn list( - conn: &PgConnection, - admin_person_id: Option, - page: Option, - limit: Option, - ) -> Result, Error> { + pub fn list(conn: &PgConnection, params: ModlogListParams) -> Result, Error> { + let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); + let show_mod_names = !params.hide_modlog_names; + let show_mod_names_expr = show_mod_names.as_sql::(); + + let admin_names_join = admin_purge_post::admin_person_id + .eq(person::id) + .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join))); let mut query = admin_purge_post::table - .inner_join(person::table.on(admin_purge_post::admin_person_id.eq(person::id))) + .left_join(person::table.on(admin_names_join)) .inner_join(community::table) .select(( admin_purge_post::all_columns, - Person::safe_columns_tuple(), + Person::safe_columns_tuple().nullable(), Community::safe_columns_tuple(), )) .into_boxed(); - if let Some(admin_person_id) = admin_person_id { + if let Some(admin_person_id) = params.mod_person_id { query = query.filter(admin_purge_post::admin_person_id.eq(admin_person_id)); }; - let (limit, offset) = limit_and_offset(page, limit)?; + let (limit, offset) = limit_and_offset(params.page, params.limit)?; let res = query .limit(limit) @@ -43,7 +45,8 @@ impl AdminPurgePostView { .order_by(admin_purge_post::when_.desc()) .load::(conn)?; - Ok(Self::from_tuple_to_vec(res)) + let results = Self::from_tuple_to_vec(res); + Ok(results) } } diff --git a/crates/db_views_moderator/src/mod_add_community_view.rs b/crates/db_views_moderator/src/mod_add_community_view.rs index 0182b809e..4c4d8f878 100644 --- a/crates/db_views_moderator/src/mod_add_community_view.rs +++ b/crates/db_views_moderator/src/mod_add_community_view.rs @@ -1,7 +1,7 @@ -use crate::structs::ModAddCommunityView; +use crate::structs::{ModAddCommunityView, ModlogListParams}; use diesel::{result::Error, *}; use lemmy_db_schema::{ - newtypes::{CommunityId, PersonId}, + newtypes::PersonId, schema::{community, mod_add_community, person, person_alias_1}, source::{ community::{Community, CommunitySafe}, @@ -12,39 +12,49 @@ use lemmy_db_schema::{ utils::limit_and_offset, }; -type ModAddCommunityViewTuple = (ModAddCommunity, PersonSafe, CommunitySafe, PersonSafeAlias1); +type ModAddCommunityViewTuple = ( + ModAddCommunity, + Option, + CommunitySafe, + PersonSafeAlias1, +); impl ModAddCommunityView { - pub fn list( - conn: &PgConnection, - community_id: Option, - mod_person_id: Option, - page: Option, - limit: Option, - ) -> Result, Error> { + pub fn list(conn: &PgConnection, params: ModlogListParams) -> Result, Error> { + let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); + let show_mod_names = !params.hide_modlog_names; + let show_mod_names_expr = show_mod_names.as_sql::(); + + let admin_names_join = mod_add_community::mod_person_id + .eq(person::id) + .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join))); let mut query = mod_add_community::table - .inner_join(person::table.on(mod_add_community::mod_person_id.eq(person::id))) + .left_join(person::table.on(admin_names_join)) .inner_join(community::table) .inner_join( person_alias_1::table.on(mod_add_community::other_person_id.eq(person_alias_1::id)), ) .select(( mod_add_community::all_columns, - Person::safe_columns_tuple(), + Person::safe_columns_tuple().nullable(), Community::safe_columns_tuple(), PersonAlias1::safe_columns_tuple(), )) .into_boxed(); - if let Some(mod_person_id) = mod_person_id { + if let Some(mod_person_id) = params.mod_person_id { query = query.filter(mod_add_community::mod_person_id.eq(mod_person_id)); }; - if let Some(community_id) = community_id { + if let Some(community_id) = params.community_id { query = query.filter(mod_add_community::community_id.eq(community_id)); }; - let (limit, offset) = limit_and_offset(page, limit)?; + if let Some(other_person_id) = params.other_person_id { + query = query.filter(person_alias_1::id.eq(other_person_id)); + }; + + let (limit, offset) = limit_and_offset(params.page, params.limit)?; let res = query .limit(limit) @@ -52,7 +62,8 @@ impl ModAddCommunityView { .order_by(mod_add_community::when_.desc()) .load::(conn)?; - Ok(Self::from_tuple_to_vec(res)) + let results = Self::from_tuple_to_vec(res); + Ok(results) } } diff --git a/crates/db_views_moderator/src/mod_add_view.rs b/crates/db_views_moderator/src/mod_add_view.rs index 86c7ef197..fe9afea5e 100644 --- a/crates/db_views_moderator/src/mod_add_view.rs +++ b/crates/db_views_moderator/src/mod_add_view.rs @@ -1,4 +1,4 @@ -use crate::structs::ModAddView; +use crate::structs::{ModAddView, ModlogListParams}; use diesel::{result::Error, *}; use lemmy_db_schema::{ newtypes::PersonId, @@ -11,30 +11,36 @@ use lemmy_db_schema::{ utils::limit_and_offset, }; -type ModAddViewTuple = (ModAdd, PersonSafe, PersonSafeAlias1); +type ModAddViewTuple = (ModAdd, Option, PersonSafeAlias1); impl ModAddView { - pub fn list( - conn: &PgConnection, - mod_person_id: Option, - page: Option, - limit: Option, - ) -> Result, Error> { + pub fn list(conn: &PgConnection, params: ModlogListParams) -> Result, Error> { + let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); + let show_mod_names = !params.hide_modlog_names; + let show_mod_names_expr = show_mod_names.as_sql::(); + + let admin_names_join = mod_add::mod_person_id + .eq(person::id) + .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join))); let mut query = mod_add::table - .inner_join(person::table.on(mod_add::mod_person_id.eq(person::id))) + .left_join(person::table.on(admin_names_join)) .inner_join(person_alias_1::table.on(mod_add::other_person_id.eq(person_alias_1::id))) .select(( mod_add::all_columns, - Person::safe_columns_tuple(), + Person::safe_columns_tuple().nullable(), PersonAlias1::safe_columns_tuple(), )) .into_boxed(); - if let Some(mod_person_id) = mod_person_id { + if let Some(mod_person_id) = params.mod_person_id { query = query.filter(mod_add::mod_person_id.eq(mod_person_id)); }; - let (limit, offset) = limit_and_offset(page, limit)?; + if let Some(other_person_id) = params.other_person_id { + query = query.filter(person_alias_1::id.eq(other_person_id)); + }; + + let (limit, offset) = limit_and_offset(params.page, params.limit)?; let res = query .limit(limit) @@ -42,7 +48,8 @@ impl ModAddView { .order_by(mod_add::when_.desc()) .load::(conn)?; - Ok(Self::from_tuple_to_vec(res)) + let results = Self::from_tuple_to_vec(res); + Ok(results) } } diff --git a/crates/db_views_moderator/src/mod_ban_from_community_view.rs b/crates/db_views_moderator/src/mod_ban_from_community_view.rs index 70696aace..9c5cc7f77 100644 --- a/crates/db_views_moderator/src/mod_ban_from_community_view.rs +++ b/crates/db_views_moderator/src/mod_ban_from_community_view.rs @@ -1,7 +1,7 @@ -use crate::structs::ModBanFromCommunityView; +use crate::structs::{ModBanFromCommunityView, ModlogListParams}; use diesel::{result::Error, *}; use lemmy_db_schema::{ - newtypes::{CommunityId, PersonId}, + newtypes::PersonId, schema::{community, mod_ban_from_community, person, person_alias_1}, source::{ community::{Community, CommunitySafe}, @@ -14,42 +14,47 @@ use lemmy_db_schema::{ type ModBanFromCommunityViewTuple = ( ModBanFromCommunity, - PersonSafe, + Option, CommunitySafe, PersonSafeAlias1, ); impl ModBanFromCommunityView { - pub fn list( - conn: &PgConnection, - community_id: Option, - mod_person_id: Option, - page: Option, - limit: Option, - ) -> Result, Error> { + pub fn list(conn: &PgConnection, params: ModlogListParams) -> Result, Error> { + let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); + let show_mod_names = !params.hide_modlog_names; + let show_mod_names_expr = show_mod_names.as_sql::(); + + let admin_names_join = mod_ban_from_community::mod_person_id + .eq(person::id) + .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join))); let mut query = mod_ban_from_community::table - .inner_join(person::table.on(mod_ban_from_community::mod_person_id.eq(person::id))) + .left_join(person::table.on(admin_names_join)) .inner_join(community::table) .inner_join( person_alias_1::table.on(mod_ban_from_community::other_person_id.eq(person_alias_1::id)), ) .select(( mod_ban_from_community::all_columns, - Person::safe_columns_tuple(), + Person::safe_columns_tuple().nullable(), Community::safe_columns_tuple(), PersonAlias1::safe_columns_tuple(), )) .into_boxed(); - if let Some(mod_person_id) = mod_person_id { + if let Some(mod_person_id) = params.mod_person_id { query = query.filter(mod_ban_from_community::mod_person_id.eq(mod_person_id)); }; - if let Some(community_id) = community_id { + if let Some(community_id) = params.community_id { query = query.filter(mod_ban_from_community::community_id.eq(community_id)); }; - let (limit, offset) = limit_and_offset(page, limit)?; + if let Some(other_person_id) = params.other_person_id { + query = query.filter(mod_ban_from_community::other_person_id.eq(other_person_id)); + }; + + let (limit, offset) = limit_and_offset(params.page, params.limit)?; let res = query .limit(limit) @@ -57,7 +62,8 @@ impl ModBanFromCommunityView { .order_by(mod_ban_from_community::when_.desc()) .load::(conn)?; - Ok(Self::from_tuple_to_vec(res)) + let results = Self::from_tuple_to_vec(res); + Ok(results) } } diff --git a/crates/db_views_moderator/src/mod_ban_view.rs b/crates/db_views_moderator/src/mod_ban_view.rs index 482d4c37b..43e577a31 100644 --- a/crates/db_views_moderator/src/mod_ban_view.rs +++ b/crates/db_views_moderator/src/mod_ban_view.rs @@ -1,4 +1,4 @@ -use crate::structs::ModBanView; +use crate::structs::{ModBanView, ModlogListParams}; use diesel::{result::Error, *}; use lemmy_db_schema::{ newtypes::PersonId, @@ -11,30 +11,36 @@ use lemmy_db_schema::{ utils::limit_and_offset, }; -type ModBanViewTuple = (ModBan, PersonSafe, PersonSafeAlias1); +type ModBanViewTuple = (ModBan, Option, PersonSafeAlias1); impl ModBanView { - pub fn list( - conn: &PgConnection, - mod_person_id: Option, - page: Option, - limit: Option, - ) -> Result, Error> { + pub fn list(conn: &PgConnection, params: ModlogListParams) -> Result, Error> { + let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); + let show_mod_names = !params.hide_modlog_names; + let show_mod_names_expr = show_mod_names.as_sql::(); + + let admin_names_join = mod_ban::mod_person_id + .eq(person::id) + .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join))); let mut query = mod_ban::table - .inner_join(person::table.on(mod_ban::mod_person_id.eq(person::id))) + .left_join(person::table.on(admin_names_join)) .inner_join(person_alias_1::table.on(mod_ban::other_person_id.eq(person_alias_1::id))) .select(( mod_ban::all_columns, - Person::safe_columns_tuple(), + Person::safe_columns_tuple().nullable(), PersonAlias1::safe_columns_tuple(), )) .into_boxed(); - if let Some(mod_person_id) = mod_person_id { + if let Some(mod_person_id) = params.mod_person_id { query = query.filter(mod_ban::mod_person_id.eq(mod_person_id)); }; - let (limit, offset) = limit_and_offset(page, limit)?; + if let Some(other_person_id) = params.other_person_id { + query = query.filter(person_alias_1::id.eq(other_person_id)); + }; + + let (limit, offset) = limit_and_offset(params.page, params.limit)?; let res = query .limit(limit) @@ -42,7 +48,8 @@ impl ModBanView { .order_by(mod_ban::when_.desc()) .load::(conn)?; - Ok(Self::from_tuple_to_vec(res)) + let results = Self::from_tuple_to_vec(res); + Ok(results) } } diff --git a/crates/db_views_moderator/src/mod_hide_community_view.rs b/crates/db_views_moderator/src/mod_hide_community_view.rs index 10397076c..24514a3df 100644 --- a/crates/db_views_moderator/src/mod_hide_community_view.rs +++ b/crates/db_views_moderator/src/mod_hide_community_view.rs @@ -1,7 +1,7 @@ -use crate::structs::ModHideCommunityView; +use crate::structs::{ModHideCommunityView, ModlogListParams}; use diesel::{result::Error, *}; use lemmy_db_schema::{ - newtypes::{CommunityId, PersonId}, + newtypes::PersonId, schema::{community, mod_hide_community, person}, source::{ community::{Community, CommunitySafe}, @@ -12,36 +12,37 @@ use lemmy_db_schema::{ utils::limit_and_offset, }; -type ModHideCommunityViewTuple = (ModHideCommunity, PersonSafe, CommunitySafe); +type ModHideCommunityViewTuple = (ModHideCommunity, Option, CommunitySafe); impl ModHideCommunityView { // Pass in mod_id as admin_id because only admins can do this action - pub fn list( - conn: &PgConnection, - community_id: Option, - admin_id: Option, - page: Option, - limit: Option, - ) -> Result, Error> { + pub fn list(conn: &PgConnection, params: ModlogListParams) -> Result, Error> { + let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); + let show_mod_names = !params.hide_modlog_names; + let show_mod_names_expr = show_mod_names.as_sql::(); + + let admin_names_join = mod_hide_community::mod_person_id + .eq(person::id) + .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join))); let mut query = mod_hide_community::table - .inner_join(person::table) + .left_join(person::table.on(admin_names_join)) .inner_join(community::table.on(mod_hide_community::community_id.eq(community::id))) .select(( mod_hide_community::all_columns, - Person::safe_columns_tuple(), + Person::safe_columns_tuple().nullable(), Community::safe_columns_tuple(), )) .into_boxed(); - if let Some(community_id) = community_id { + if let Some(community_id) = params.community_id { query = query.filter(mod_hide_community::community_id.eq(community_id)); }; - if let Some(admin_id) = admin_id { + if let Some(admin_id) = params.mod_person_id { query = query.filter(mod_hide_community::mod_person_id.eq(admin_id)); }; - let (limit, offset) = limit_and_offset(page, limit)?; + let (limit, offset) = limit_and_offset(params.page, params.limit)?; let res = query .limit(limit) @@ -49,7 +50,8 @@ impl ModHideCommunityView { .order_by(mod_hide_community::when_.desc()) .load::(conn)?; - Ok(Self::from_tuple_to_vec(res)) + let results = Self::from_tuple_to_vec(res); + Ok(results) } } diff --git a/crates/db_views_moderator/src/mod_lock_post_view.rs b/crates/db_views_moderator/src/mod_lock_post_view.rs index dba2d5889..dad5c5b59 100644 --- a/crates/db_views_moderator/src/mod_lock_post_view.rs +++ b/crates/db_views_moderator/src/mod_lock_post_view.rs @@ -1,8 +1,8 @@ -use crate::structs::ModLockPostView; +use crate::structs::{ModLockPostView, ModlogListParams}; use diesel::{result::Error, *}; use lemmy_db_schema::{ - newtypes::{CommunityId, PersonId}, - schema::{community, mod_lock_post, person, post}, + newtypes::PersonId, + schema::{community, mod_lock_post, person, person_alias_1, post}, source::{ community::{Community, CommunitySafe}, moderator::ModLockPost, @@ -13,37 +13,43 @@ use lemmy_db_schema::{ utils::limit_and_offset, }; -type ModLockPostViewTuple = (ModLockPost, PersonSafe, Post, CommunitySafe); +type ModLockPostViewTuple = (ModLockPost, Option, Post, CommunitySafe); impl ModLockPostView { - pub fn list( - conn: &PgConnection, - community_id: Option, - mod_person_id: Option, - page: Option, - limit: Option, - ) -> Result, Error> { + pub fn list(conn: &PgConnection, params: ModlogListParams) -> Result, Error> { + let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); + let show_mod_names = !params.hide_modlog_names; + let show_mod_names_expr = show_mod_names.as_sql::(); + + let admin_names_join = mod_lock_post::mod_person_id + .eq(person::id) + .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join))); let mut query = mod_lock_post::table - .inner_join(person::table) + .left_join(person::table.on(admin_names_join)) .inner_join(post::table) .inner_join(community::table.on(post::community_id.eq(community::id))) + .inner_join(person_alias_1::table.on(post::creator_id.eq(person_alias_1::id))) .select(( mod_lock_post::all_columns, - Person::safe_columns_tuple(), + Person::safe_columns_tuple().nullable(), post::all_columns, Community::safe_columns_tuple(), )) .into_boxed(); - if let Some(community_id) = community_id { + if let Some(community_id) = params.community_id { query = query.filter(post::community_id.eq(community_id)); }; - if let Some(mod_person_id) = mod_person_id { + if let Some(mod_person_id) = params.mod_person_id { query = query.filter(mod_lock_post::mod_person_id.eq(mod_person_id)); }; - let (limit, offset) = limit_and_offset(page, limit)?; + if let Some(other_person_id) = params.other_person_id { + query = query.filter(person_alias_1::id.eq(other_person_id)); + }; + + let (limit, offset) = limit_and_offset(params.page, params.limit)?; let res = query .limit(limit) @@ -51,7 +57,8 @@ impl ModLockPostView { .order_by(mod_lock_post::when_.desc()) .load::(conn)?; - Ok(Self::from_tuple_to_vec(res)) + let results = Self::from_tuple_to_vec(res); + Ok(results) } } diff --git a/crates/db_views_moderator/src/mod_remove_comment_view.rs b/crates/db_views_moderator/src/mod_remove_comment_view.rs index 29d3a19c1..2ee230d11 100644 --- a/crates/db_views_moderator/src/mod_remove_comment_view.rs +++ b/crates/db_views_moderator/src/mod_remove_comment_view.rs @@ -1,7 +1,7 @@ -use crate::structs::ModRemoveCommentView; +use crate::structs::{ModRemoveCommentView, ModlogListParams}; use diesel::{result::Error, *}; use lemmy_db_schema::{ - newtypes::{CommunityId, PersonId}, + newtypes::PersonId, schema::{comment, community, mod_remove_comment, person, person_alias_1, post}, source::{ comment::Comment, @@ -16,7 +16,7 @@ use lemmy_db_schema::{ type ModRemoveCommentViewTuple = ( ModRemoveComment, - PersonSafe, + Option, Comment, PersonSafeAlias1, Post, @@ -24,22 +24,23 @@ type ModRemoveCommentViewTuple = ( ); impl ModRemoveCommentView { - pub fn list( - conn: &PgConnection, - community_id: Option, - mod_person_id: Option, - page: Option, - limit: Option, - ) -> Result, Error> { + pub fn list(conn: &PgConnection, params: ModlogListParams) -> Result, Error> { + let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); + let show_mod_names = !params.hide_modlog_names; + let show_mod_names_expr = show_mod_names.as_sql::(); + + let admin_names_join = mod_remove_comment::mod_person_id + .eq(person::id) + .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join))); let mut query = mod_remove_comment::table - .inner_join(person::table) + .left_join(person::table.on(admin_names_join)) .inner_join(comment::table) .inner_join(person_alias_1::table.on(comment::creator_id.eq(person_alias_1::id))) .inner_join(post::table.on(comment::post_id.eq(post::id))) .inner_join(community::table.on(post::community_id.eq(community::id))) .select(( mod_remove_comment::all_columns, - Person::safe_columns_tuple(), + Person::safe_columns_tuple().nullable(), comment::all_columns, PersonAlias1::safe_columns_tuple(), post::all_columns, @@ -47,15 +48,19 @@ impl ModRemoveCommentView { )) .into_boxed(); - if let Some(community_id) = community_id { + if let Some(community_id) = params.community_id { query = query.filter(post::community_id.eq(community_id)); }; - if let Some(mod_person_id) = mod_person_id { + if let Some(mod_person_id) = params.mod_person_id { query = query.filter(mod_remove_comment::mod_person_id.eq(mod_person_id)); }; - let (limit, offset) = limit_and_offset(page, limit)?; + if let Some(other_person_id) = params.other_person_id { + query = query.filter(person_alias_1::id.eq(other_person_id)); + }; + + let (limit, offset) = limit_and_offset(params.page, params.limit)?; let res = query .limit(limit) @@ -63,7 +68,8 @@ impl ModRemoveCommentView { .order_by(mod_remove_comment::when_.desc()) .load::(conn)?; - Ok(Self::from_tuple_to_vec(res)) + let results = Self::from_tuple_to_vec(res); + Ok(results) } } diff --git a/crates/db_views_moderator/src/mod_remove_community_view.rs b/crates/db_views_moderator/src/mod_remove_community_view.rs index b49fa1d5d..ec462df9f 100644 --- a/crates/db_views_moderator/src/mod_remove_community_view.rs +++ b/crates/db_views_moderator/src/mod_remove_community_view.rs @@ -1,4 +1,4 @@ -use crate::structs::ModRemoveCommunityView; +use crate::structs::{ModRemoveCommunityView, ModlogListParams}; use diesel::{result::Error, *}; use lemmy_db_schema::{ newtypes::PersonId, @@ -12,30 +12,32 @@ use lemmy_db_schema::{ utils::limit_and_offset, }; -type ModRemoveCommunityTuple = (ModRemoveCommunity, PersonSafe, CommunitySafe); +type ModRemoveCommunityTuple = (ModRemoveCommunity, Option, CommunitySafe); impl ModRemoveCommunityView { - pub fn list( - conn: &PgConnection, - mod_person_id: Option, - page: Option, - limit: Option, - ) -> Result, Error> { + pub fn list(conn: &PgConnection, params: ModlogListParams) -> Result, Error> { + let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); + let show_mod_names = !params.hide_modlog_names; + let show_mod_names_expr = show_mod_names.as_sql::(); + + let admin_names_join = mod_remove_community::mod_person_id + .eq(person::id) + .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join))); let mut query = mod_remove_community::table - .inner_join(person::table) + .left_join(person::table.on(admin_names_join)) .inner_join(community::table) .select(( mod_remove_community::all_columns, - Person::safe_columns_tuple(), + Person::safe_columns_tuple().nullable(), Community::safe_columns_tuple(), )) .into_boxed(); - if let Some(mod_person_id) = mod_person_id { + if let Some(mod_person_id) = params.mod_person_id { query = query.filter(mod_remove_community::mod_person_id.eq(mod_person_id)); }; - let (limit, offset) = limit_and_offset(page, limit)?; + let (limit, offset) = limit_and_offset(params.page, params.limit)?; let res = query .limit(limit) @@ -43,7 +45,8 @@ impl ModRemoveCommunityView { .order_by(mod_remove_community::when_.desc()) .load::(conn)?; - Ok(Self::from_tuple_to_vec(res)) + let results = Self::from_tuple_to_vec(res); + Ok(results) } } diff --git a/crates/db_views_moderator/src/mod_remove_post_view.rs b/crates/db_views_moderator/src/mod_remove_post_view.rs index e46f5ea19..497305ece 100644 --- a/crates/db_views_moderator/src/mod_remove_post_view.rs +++ b/crates/db_views_moderator/src/mod_remove_post_view.rs @@ -1,8 +1,8 @@ -use crate::structs::ModRemovePostView; +use crate::structs::{ModRemovePostView, ModlogListParams}; use diesel::{result::Error, *}; use lemmy_db_schema::{ - newtypes::{CommunityId, PersonId}, - schema::{community, mod_remove_post, person, post}, + newtypes::PersonId, + schema::{community, mod_remove_post, person, person_alias_1, post}, source::{ community::{Community, CommunitySafe}, moderator::ModRemovePost, @@ -13,37 +13,43 @@ use lemmy_db_schema::{ utils::limit_and_offset, }; -type ModRemovePostViewTuple = (ModRemovePost, PersonSafe, Post, CommunitySafe); +type ModRemovePostViewTuple = (ModRemovePost, Option, Post, CommunitySafe); impl ModRemovePostView { - pub fn list( - conn: &PgConnection, - community_id: Option, - mod_person_id: Option, - page: Option, - limit: Option, - ) -> Result, Error> { + pub fn list(conn: &PgConnection, params: ModlogListParams) -> Result, Error> { + let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); + let show_mod_names = !params.hide_modlog_names; + let show_mod_names_expr = show_mod_names.as_sql::(); + + let admin_names_join = mod_remove_post::mod_person_id + .eq(person::id) + .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join))); let mut query = mod_remove_post::table - .inner_join(person::table) + .left_join(person::table.on(admin_names_join)) .inner_join(post::table) .inner_join(community::table.on(post::community_id.eq(community::id))) + .inner_join(person_alias_1::table.on(post::creator_id.eq(person_alias_1::id))) .select(( mod_remove_post::all_columns, - Person::safe_columns_tuple(), + Person::safe_columns_tuple().nullable(), post::all_columns, Community::safe_columns_tuple(), )) .into_boxed(); - if let Some(community_id) = community_id { + if let Some(community_id) = params.community_id { query = query.filter(post::community_id.eq(community_id)); }; - if let Some(mod_person_id) = mod_person_id { + if let Some(mod_person_id) = params.mod_person_id { query = query.filter(mod_remove_post::mod_person_id.eq(mod_person_id)); }; - let (limit, offset) = limit_and_offset(page, limit)?; + if let Some(other_person_id) = params.other_person_id { + query = query.filter(person_alias_1::id.eq(other_person_id)); + }; + + let (limit, offset) = limit_and_offset(params.page, params.limit)?; let res = query .limit(limit) @@ -51,7 +57,8 @@ impl ModRemovePostView { .order_by(mod_remove_post::when_.desc()) .load::(conn)?; - Ok(Self::from_tuple_to_vec(res)) + let results = Self::from_tuple_to_vec(res); + Ok(results) } } diff --git a/crates/db_views_moderator/src/mod_sticky_post_view.rs b/crates/db_views_moderator/src/mod_sticky_post_view.rs index 5e0d20569..a47e1dc89 100644 --- a/crates/db_views_moderator/src/mod_sticky_post_view.rs +++ b/crates/db_views_moderator/src/mod_sticky_post_view.rs @@ -1,8 +1,8 @@ -use crate::structs::ModStickyPostView; +use crate::structs::{ModStickyPostView, ModlogListParams}; use diesel::{result::Error, *}; use lemmy_db_schema::{ - newtypes::{CommunityId, PersonId}, - schema::{community, mod_sticky_post, person, post}, + newtypes::PersonId, + schema::{community, mod_sticky_post, person, person_alias_1, post}, source::{ community::{Community, CommunitySafe}, moderator::ModStickyPost, @@ -13,37 +13,43 @@ use lemmy_db_schema::{ utils::limit_and_offset, }; -type ModStickyPostViewTuple = (ModStickyPost, PersonSafe, Post, CommunitySafe); +type ModStickyPostViewTuple = (ModStickyPost, Option, Post, CommunitySafe); impl ModStickyPostView { - pub fn list( - conn: &PgConnection, - community_id: Option, - mod_person_id: Option, - page: Option, - limit: Option, - ) -> Result, Error> { + pub fn list(conn: &PgConnection, params: ModlogListParams) -> Result, Error> { + let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); + let show_mod_names = !params.hide_modlog_names; + let show_mod_names_expr = show_mod_names.as_sql::(); + + let admin_names_join = mod_sticky_post::mod_person_id + .eq(person::id) + .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join))); let mut query = mod_sticky_post::table - .inner_join(person::table) + .left_join(person::table.on(admin_names_join)) .inner_join(post::table) + .inner_join(person_alias_1::table.on(post::creator_id.eq(person_alias_1::id))) .inner_join(community::table.on(post::community_id.eq(community::id))) .select(( mod_sticky_post::all_columns, - Person::safe_columns_tuple(), + Person::safe_columns_tuple().nullable(), post::all_columns, Community::safe_columns_tuple(), )) .into_boxed(); - if let Some(community_id) = community_id { + if let Some(community_id) = params.community_id { query = query.filter(post::community_id.eq(community_id)); }; - if let Some(mod_person_id) = mod_person_id { + if let Some(mod_person_id) = params.mod_person_id { query = query.filter(mod_sticky_post::mod_person_id.eq(mod_person_id)); }; - let (limit, offset) = limit_and_offset(page, limit)?; + if let Some(other_person_id) = params.other_person_id { + query = query.filter(person_alias_1::id.eq(other_person_id)); + }; + + let (limit, offset) = limit_and_offset(params.page, params.limit)?; let res = query .limit(limit) @@ -51,7 +57,8 @@ impl ModStickyPostView { .order_by(mod_sticky_post::when_.desc()) .load::(conn)?; - Ok(Self::from_tuple_to_vec(res)) + let results = Self::from_tuple_to_vec(res); + Ok(results) } } diff --git a/crates/db_views_moderator/src/mod_transfer_community_view.rs b/crates/db_views_moderator/src/mod_transfer_community_view.rs index bc8ccd97d..bada7b39f 100644 --- a/crates/db_views_moderator/src/mod_transfer_community_view.rs +++ b/crates/db_views_moderator/src/mod_transfer_community_view.rs @@ -1,7 +1,7 @@ -use crate::structs::ModTransferCommunityView; +use crate::structs::{ModTransferCommunityView, ModlogListParams}; use diesel::{result::Error, *}; use lemmy_db_schema::{ - newtypes::{CommunityId, PersonId}, + newtypes::PersonId, schema::{community, mod_transfer_community, person, person_alias_1}, source::{ community::{Community, CommunitySafe}, @@ -14,42 +14,47 @@ use lemmy_db_schema::{ type ModTransferCommunityViewTuple = ( ModTransferCommunity, - PersonSafe, + Option, CommunitySafe, PersonSafeAlias1, ); impl ModTransferCommunityView { - pub fn list( - conn: &PgConnection, - community_id: Option, - mod_person_id: Option, - page: Option, - limit: Option, - ) -> Result, Error> { + pub fn list(conn: &PgConnection, params: ModlogListParams) -> Result, Error> { + let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); + let show_mod_names = !params.hide_modlog_names; + let show_mod_names_expr = show_mod_names.as_sql::(); + + let admin_names_join = mod_transfer_community::mod_person_id + .eq(person::id) + .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join))); let mut query = mod_transfer_community::table - .inner_join(person::table.on(mod_transfer_community::mod_person_id.eq(person::id))) + .left_join(person::table.on(admin_names_join)) .inner_join(community::table) .inner_join( person_alias_1::table.on(mod_transfer_community::other_person_id.eq(person_alias_1::id)), ) .select(( mod_transfer_community::all_columns, - Person::safe_columns_tuple(), + Person::safe_columns_tuple().nullable(), Community::safe_columns_tuple(), PersonAlias1::safe_columns_tuple(), )) .into_boxed(); - if let Some(mod_person_id) = mod_person_id { + if let Some(mod_person_id) = params.mod_person_id { query = query.filter(mod_transfer_community::mod_person_id.eq(mod_person_id)); }; - if let Some(community_id) = community_id { + if let Some(community_id) = params.community_id { query = query.filter(mod_transfer_community::community_id.eq(community_id)); }; - let (limit, offset) = limit_and_offset(page, limit)?; + if let Some(other_person_id) = params.other_person_id { + query = query.filter(person_alias_1::id.eq(other_person_id)); + }; + + let (limit, offset) = limit_and_offset(params.page, params.limit)?; let res = query .limit(limit) @@ -57,7 +62,8 @@ impl ModTransferCommunityView { .order_by(mod_transfer_community::when_.desc()) .load::(conn)?; - Ok(Self::from_tuple_to_vec(res)) + let results = Self::from_tuple_to_vec(res); + Ok(results) } } diff --git a/crates/db_views_moderator/src/structs.rs b/crates/db_views_moderator/src/structs.rs index ebe93399c..4f6fbbd3e 100644 --- a/crates/db_views_moderator/src/structs.rs +++ b/crates/db_views_moderator/src/structs.rs @@ -1,32 +1,35 @@ -use lemmy_db_schema::source::{ - comment::Comment, - community::CommunitySafe, - moderator::{ - AdminPurgeComment, - AdminPurgeCommunity, - AdminPurgePerson, - AdminPurgePost, - ModAdd, - ModAddCommunity, - ModBan, - ModBanFromCommunity, - ModHideCommunity, - ModLockPost, - ModRemoveComment, - ModRemoveCommunity, - ModRemovePost, - ModStickyPost, - ModTransferCommunity, +use lemmy_db_schema::{ + newtypes::{CommunityId, PersonId}, + source::{ + comment::Comment, + community::CommunitySafe, + moderator::{ + AdminPurgeComment, + AdminPurgeCommunity, + AdminPurgePerson, + AdminPurgePost, + ModAdd, + ModAddCommunity, + ModBan, + ModBanFromCommunity, + ModHideCommunity, + ModLockPost, + ModRemoveComment, + ModRemoveCommunity, + ModRemovePost, + ModStickyPost, + ModTransferCommunity, + }, + person::{PersonSafe, PersonSafeAlias1}, + post::Post, }, - person::{PersonSafe, PersonSafeAlias1}, - post::Post, }; use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize, Clone)] pub struct ModAddCommunityView { pub mod_add_community: ModAddCommunity, - pub moderator: PersonSafe, + pub moderator: Option, pub community: CommunitySafe, pub modded_person: PersonSafeAlias1, } @@ -34,14 +37,14 @@ pub struct ModAddCommunityView { #[derive(Debug, Serialize, Deserialize, Clone)] pub struct ModAddView { pub mod_add: ModAdd, - pub moderator: PersonSafe, + pub moderator: Option, pub modded_person: PersonSafeAlias1, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct ModBanFromCommunityView { pub mod_ban_from_community: ModBanFromCommunity, - pub moderator: PersonSafe, + pub moderator: Option, pub community: CommunitySafe, pub banned_person: PersonSafeAlias1, } @@ -49,21 +52,21 @@ pub struct ModBanFromCommunityView { #[derive(Debug, Serialize, Deserialize, Clone)] pub struct ModBanView { pub mod_ban: ModBan, - pub moderator: PersonSafe, + pub moderator: Option, pub banned_person: PersonSafeAlias1, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct ModHideCommunityView { pub mod_hide_community: ModHideCommunity, - pub admin: PersonSafe, + pub admin: Option, pub community: CommunitySafe, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct ModLockPostView { pub mod_lock_post: ModLockPost, - pub moderator: PersonSafe, + pub moderator: Option, pub post: Post, pub community: CommunitySafe, } @@ -71,7 +74,7 @@ pub struct ModLockPostView { #[derive(Debug, Serialize, Deserialize, Clone)] pub struct ModRemoveCommentView { pub mod_remove_comment: ModRemoveComment, - pub moderator: PersonSafe, + pub moderator: Option, pub comment: Comment, pub commenter: PersonSafeAlias1, pub post: Post, @@ -81,14 +84,14 @@ pub struct ModRemoveCommentView { #[derive(Debug, Serialize, Deserialize, Clone)] pub struct ModRemoveCommunityView { pub mod_remove_community: ModRemoveCommunity, - pub moderator: PersonSafe, + pub moderator: Option, pub community: CommunitySafe, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct ModRemovePostView { pub mod_remove_post: ModRemovePost, - pub moderator: PersonSafe, + pub moderator: Option, pub post: Post, pub community: CommunitySafe, } @@ -96,7 +99,7 @@ pub struct ModRemovePostView { #[derive(Debug, Serialize, Deserialize, Clone)] pub struct ModStickyPostView { pub mod_sticky_post: ModStickyPost, - pub moderator: PersonSafe, + pub moderator: Option, pub post: Post, pub community: CommunitySafe, } @@ -104,7 +107,7 @@ pub struct ModStickyPostView { #[derive(Debug, Serialize, Deserialize, Clone)] pub struct ModTransferCommunityView { pub mod_transfer_community: ModTransferCommunity, - pub moderator: PersonSafe, + pub moderator: Option, pub community: CommunitySafe, pub modded_person: PersonSafeAlias1, } @@ -112,25 +115,35 @@ pub struct ModTransferCommunityView { #[derive(Debug, Serialize, Deserialize, Clone)] pub struct AdminPurgeCommentView { pub admin_purge_comment: AdminPurgeComment, - pub admin: PersonSafe, + pub admin: Option, pub post: Post, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct AdminPurgeCommunityView { pub admin_purge_community: AdminPurgeCommunity, - pub admin: PersonSafe, + pub admin: Option, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct AdminPurgePersonView { pub admin_purge_person: AdminPurgePerson, - pub admin: PersonSafe, + pub admin: Option, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct AdminPurgePostView { pub admin_purge_post: AdminPurgePost, - pub admin: PersonSafe, + pub admin: Option, pub community: CommunitySafe, } + +#[derive(Debug, Serialize, Deserialize, Clone, Copy)] +pub struct ModlogListParams { + pub community_id: Option, + pub mod_person_id: Option, + pub other_person_id: Option, + pub page: Option, + pub limit: Option, + pub hide_modlog_names: bool, +} diff --git a/migrations/2022-06-12-012121_add_site_hide_modlog_names/down.sql b/migrations/2022-06-12-012121_add_site_hide_modlog_names/down.sql new file mode 100644 index 000000000..c3f658175 --- /dev/null +++ b/migrations/2022-06-12-012121_add_site_hide_modlog_names/down.sql @@ -0,0 +1 @@ +alter table site drop column hide_modlog_mod_names; \ No newline at end of file diff --git a/migrations/2022-06-12-012121_add_site_hide_modlog_names/up.sql b/migrations/2022-06-12-012121_add_site_hide_modlog_names/up.sql new file mode 100644 index 000000000..329ad6f42 --- /dev/null +++ b/migrations/2022-06-12-012121_add_site_hide_modlog_names/up.sql @@ -0,0 +1 @@ +alter table site add column hide_modlog_mod_names boolean default true NOT NULL; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 81a108b89..07be683e9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -70,6 +70,7 @@ async fn main() -> Result<(), LemmyError> { let manager = ConnectionManager::::new(&db_url); let pool = Pool::builder() .max_size(settings.database.pool_size) + .min_idle(Some(1)) .build(manager) .unwrap_or_else(|_| panic!("Error connecting to {}", db_url));