Skip to content

Commit

Permalink
Add support for filtering mod logs
Browse files Browse the repository at this point in the history
  • Loading branch information
makotech222 committed Jul 12, 2022
1 parent 54aab82 commit 8a202ef
Show file tree
Hide file tree
Showing 25 changed files with 528 additions and 111 deletions.
287 changes: 246 additions & 41 deletions crates/api/src/site/mod_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ 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::{source::site::Site, ModlogActionType};
use lemmy_db_views_moderator::structs::{
AdminPurgeCommentView,
AdminPurgeCommunityView,
Expand Down Expand Up @@ -42,49 +49,185 @@ impl Perform for GetModlog {

check_private_instance(&local_user_view, context.pool()).await?;

let type_ = data.type_.unwrap_or(ModlogActionType::All);
let community_id = data.community_id;
let mod_person_id = data.mod_person_id;

let site = blocking(context.pool(), Site::read_local_site).await??;
let is_admin = is_admin(&local_user_view.as_ref().expect("")).is_ok();
let is_mod_of_community = data.community_id.is_some()
&& is_mod_or_admin(
context.pool(),
local_user_view.expect("").person.id,
data.community_id.expect(""),
)
.await
.is_ok();
let hide_modlog_names = site.hide_modlog_mod_names && !is_mod_of_community && !is_admin;

let mod_person_id = if hide_modlog_names {
None
} else {
data.mod_person_id
};
let other_person_id = data.other_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 removed_posts = if matches!(
type_,
ModlogActionType::All | ModlogActionType::ModRemovePost
) {
blocking(context.pool(), move |conn| {
ModRemovePostView::list(
conn,
community_id,
mod_person_id,
other_person_id,
page,
limit,
hide_modlog_names,
)
})
.await??
} else {
Vec::<ModRemovePostView>::new()
};

let locked_posts = blocking(context.pool(), move |conn| {
ModLockPostView::list(conn, community_id, mod_person_id, page, limit)
})
.await??;
let locked_posts = if matches!(type_, ModlogActionType::All | ModlogActionType::ModLockPost) {
blocking(context.pool(), move |conn| {
ModLockPostView::list(
conn,
community_id,
mod_person_id,
other_person_id,
page,
limit,
hide_modlog_names,
)
})
.await??
} else {
Vec::<ModLockPostView>::new()
};

let stickied_posts = blocking(context.pool(), move |conn| {
ModStickyPostView::list(conn, community_id, mod_person_id, page, limit)
})
.await??;
let stickied_posts = if matches!(
type_,
ModlogActionType::All | ModlogActionType::ModStickyPost
) {
blocking(context.pool(), move |conn| {
ModStickyPostView::list(
conn,
community_id,
mod_person_id,
other_person_id,
page,
limit,
hide_modlog_names,
)
})
.await??
} else {
Vec::<ModStickyPostView>::new()
};

let removed_comments = blocking(context.pool(), move |conn| {
ModRemoveCommentView::list(conn, community_id, mod_person_id, page, limit)
})
.await??;
let removed_comments = if matches!(
type_,
ModlogActionType::All | ModlogActionType::ModRemoveComment
) {
blocking(context.pool(), move |conn| {
ModRemoveCommentView::list(
conn,
community_id,
mod_person_id,
other_person_id,
page,
limit,
hide_modlog_names,
)
})
.await??
} else {
Vec::<ModRemoveCommentView>::new()
};

let banned_from_community = blocking(context.pool(), move |conn| {
ModBanFromCommunityView::list(conn, community_id, mod_person_id, page, limit)
})
.await??;
let banned_from_community = if matches!(
type_,
ModlogActionType::All | ModlogActionType::ModBanFromCommunity
) {
blocking(context.pool(), move |conn| {
ModBanFromCommunityView::list(
conn,
community_id,
mod_person_id,
other_person_id,
page,
limit,
hide_modlog_names,
)
})
.await??
} else {
Vec::<ModBanFromCommunityView>::new()
};

let added_to_community = blocking(context.pool(), move |conn| {
ModAddCommunityView::list(conn, community_id, mod_person_id, page, limit)
})
.await??;
let added_to_community = if matches!(
type_,
ModlogActionType::All | ModlogActionType::ModAddCommunity
) {
blocking(context.pool(), move |conn| {
ModAddCommunityView::list(
conn,
community_id,
mod_person_id,
other_person_id,
page,
limit,
hide_modlog_names,
)
})
.await??
} else {
Vec::<ModAddCommunityView>::new()
};

let transferred_to_community = blocking(context.pool(), move |conn| {
ModTransferCommunityView::list(conn, community_id, mod_person_id, page, limit)
})
.await??;
let transferred_to_community = if matches!(
type_,
ModlogActionType::All | ModlogActionType::ModTransferCommunity
) {
blocking(context.pool(), move |conn| {
ModTransferCommunityView::list(
conn,
community_id,
mod_person_id,
other_person_id,
page,
limit,
hide_modlog_names,
)
})
.await??
} else {
Vec::<ModTransferCommunityView>::new()
};

let hidden_communities = blocking(context.pool(), move |conn| {
ModHideCommunityView::list(conn, community_id, mod_person_id, page, limit)
})
.await??;
let hidden_communities = if matches!(
type_,
ModlogActionType::All | ModlogActionType::ModHideCommunity
) && other_person_id.is_none()
{
blocking(context.pool(), move |conn| {
ModHideCommunityView::list(
conn,
community_id,
mod_person_id,
page,
limit,
hide_modlog_names,
)
})
.await??
} else {
Vec::<ModHideCommunityView>::new()
};

// These arrays are only for the full modlog, when a community isn't given
let (
Expand All @@ -98,13 +241,75 @@ 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)?,
if matches!(
type_,
ModlogActionType::All | ModlogActionType::ModRemoveCommunity
) && other_person_id.is_none()
{
ModRemoveCommunityView::list(conn, mod_person_id, page, limit, hide_modlog_names)?
} else {
Vec::<ModRemoveCommunityView>::new()
},
if matches!(type_, ModlogActionType::All | ModlogActionType::ModBan) {
ModBanView::list(
conn,
mod_person_id,
other_person_id,
page,
limit,
hide_modlog_names,
)?
} else {
Vec::<ModBanView>::new()
},
if matches!(type_, ModlogActionType::All | ModlogActionType::ModAdd) {
ModAddView::list(
conn,
mod_person_id,
other_person_id,
page,
limit,
hide_modlog_names,
)?
} else {
Vec::<ModAddView>::new()
},
if matches!(
type_,
ModlogActionType::All | ModlogActionType::AdminPurgePerson
) && other_person_id.is_none()
{
AdminPurgePersonView::list(conn, mod_person_id, page, limit, hide_modlog_names)?
} else {
Vec::<AdminPurgePersonView>::new()
},
if matches!(
type_,
ModlogActionType::All | ModlogActionType::AdminPurgeCommunity
) && other_person_id.is_none()
{
AdminPurgeCommunityView::list(conn, mod_person_id, page, limit, hide_modlog_names)?
} else {
Vec::<AdminPurgeCommunityView>::new()
},
if matches!(
type_,
ModlogActionType::All | ModlogActionType::AdminPurgePost
) && other_person_id.is_none()
{
AdminPurgePostView::list(conn, mod_person_id, page, limit, hide_modlog_names)?
} else {
Vec::<AdminPurgePostView>::new()
},
if matches!(
type_,
ModlogActionType::All | ModlogActionType::AdminPurgeComment
) && other_person_id.is_none()
{
AdminPurgeCommentView::list(conn, mod_person_id, page, limit, hide_modlog_names)?
} else {
Vec::<AdminPurgeCommentView>::new()
},
)) as Result<_, LemmyError>
})
.await??
Expand Down
5 changes: 5 additions & 0 deletions crates/api_common/src/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::sensitive::Sensitive;
use lemmy_db_schema::{
newtypes::{CommentId, CommunityId, PersonId, PostId},
ListingType,
ModlogActionType,
SearchType,
SortType,
};
Expand Down Expand Up @@ -83,6 +84,8 @@ pub struct GetModlog {
pub page: Option<i64>,
pub limit: Option<i64>,
pub auth: Option<Sensitive<String>>,
pub type_: Option<ModlogActionType>,
pub other_person_id: Option<PersonId>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
Expand Down Expand Up @@ -122,6 +125,7 @@ pub struct CreateSite {
pub default_theme: Option<String>,
pub default_post_listing_type: Option<String>,
pub auth: Sensitive<String>,
pub hide_modlog_mod_names: Option<bool>,
}

#[derive(Debug, Serialize, Deserialize, Clone, Default)]
Expand All @@ -143,6 +147,7 @@ pub struct EditSite {
pub default_post_listing_type: Option<String>,
pub legal_information: Option<String>,
pub auth: Sensitive<String>,
pub hide_modlog_mod_names: Option<bool>,
}

#[derive(Debug, Serialize, Deserialize, Clone, Default)]
Expand Down
1 change: 1 addition & 0 deletions crates/api_crud/src/site/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,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()
};

Expand Down
1 change: 1 addition & 0 deletions crates/api_crud/src/site/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ impl PerformCrud for EditSite {
default_theme: data.default_theme.clone(),
default_post_listing_type: data.default_post_listing_type.clone(),
legal_information: data.legal_information.clone(),
hide_modlog_mod_names: data.hide_modlog_mod_names,
..SiteForm::default()
};

Expand Down
20 changes: 20 additions & 0 deletions crates/db_schema/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,23 @@ pub enum SearchType {
Users,
Url,
}

#[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,
}
1 change: 1 addition & 0 deletions crates/db_schema/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ table! {
default_theme -> Text,
default_post_listing_type -> Text,
legal_information -> Nullable<Text>,
hide_modlog_mod_names -> Bool,
}
}

Expand Down
Loading

0 comments on commit 8a202ef

Please sign in to comment.