Skip to content

Commit

Permalink
Hide community v2 (#2055)
Browse files Browse the repository at this point in the history
* Initial working of hiding communities and adding a db entry for mod log

* Return mod log for hidden communities

* Clean up hidding communities PR

* use lower case like other migration files

* Formatting fix

* pass in admin id to list, make match logic the same in post_view as community_view. Dont force non null for reason

* Clean PR review stuff

* Change person_id to mod_person_id on hide community table

* Make bools optional, add a space for formating

Co-authored-by: Thor Odinson <odinson@asgard.com>
Co-authored-by: Dessalines <dessalines@users.noreply.github.com>
  • Loading branch information
3 people authored Feb 18, 2022
1 parent 762b85b commit 7f9b55e
Show file tree
Hide file tree
Showing 21 changed files with 319 additions and 18 deletions.
7 changes: 7 additions & 0 deletions crates/api/src/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ use lemmy_db_views_moderator::{
mod_add_view::ModAddView,
mod_ban_from_community_view::ModBanFromCommunityView,
mod_ban_view::ModBanView,
mod_hide_community_view::ModHideCommunityView,
mod_lock_post_view::ModLockPostView,
mod_remove_comment_view::ModRemoveCommentView,
mod_remove_community_view::ModRemoveCommunityView,
Expand Down Expand Up @@ -117,6 +118,11 @@ impl Perform for GetModlog {
})
.await??;

let hidden_communities = blocking(context.pool(), move |conn| {
ModHideCommunityView::list(conn, community_id, mod_person_id, page, limit)
})
.await??;

// These arrays are only for the full modlog, when a community isn't given
let (removed_communities, banned, added) = if data.community_id.is_none() {
blocking(context.pool(), move |conn| {
Expand All @@ -143,6 +149,7 @@ impl Perform for GetModlog {
added_to_community,
added,
transferred_to_community,
hidden_communities,
})
}
}
Expand Down
8 changes: 8 additions & 0 deletions crates/api_common/src/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ pub struct EditCommunity {
pub auth: Sensitive<String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct HideCommunity {
pub community_id: CommunityId,
pub hidden: bool,
pub reason: Option<String>,
pub auth: Sensitive<String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct DeleteCommunity {
pub community_id: CommunityId,
Expand Down
2 changes: 2 additions & 0 deletions crates/api_common/src/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use lemmy_db_views_moderator::{
mod_add_view::ModAddView,
mod_ban_from_community_view::ModBanFromCommunityView,
mod_ban_view::ModBanView,
mod_hide_community_view::ModHideCommunityView,
mod_lock_post_view::ModLockPostView,
mod_remove_comment_view::ModRemoveCommentView,
mod_remove_community_view::ModRemoveCommunityView,
Expand Down Expand Up @@ -87,6 +88,7 @@ pub struct GetModlogResponse {
pub added_to_community: Vec<ModAddCommunityView>,
pub transferred_to_community: Vec<ModTransferCommunityView>,
pub added: Vec<ModAddView>,
pub hidden_communities: Vec<ModHideCommunityView>,
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down
77 changes: 75 additions & 2 deletions crates/api_crud/src/community/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ use crate::PerformCrud;
use actix_web::web::Data;
use lemmy_api_common::{
blocking,
community::{CommunityResponse, EditCommunity},
community::{CommunityResponse, EditCommunity, HideCommunity},
get_local_user_view_from_jwt,
is_admin,
};
use lemmy_apub::protocol::activities::community::update::UpdateCommunity;
use lemmy_db_schema::{
diesel_option_overwrite_to_url,
naive_now,
newtypes::PersonId,
source::community::{Community, CommunityForm},
source::{
community::{Community, CommunityForm},
moderator::{ModHideCommunity, ModHideCommunityForm},
},
traits::Crud,
};
use lemmy_db_views_actor::community_moderator_view::CommunityModeratorView;
Expand Down Expand Up @@ -62,6 +66,7 @@ impl PerformCrud for EditCommunity {
icon,
banner,
nsfw: data.nsfw,
hidden: Some(read_community.hidden),
updated: Some(naive_now()),
..CommunityForm::default()
};
Expand All @@ -85,3 +90,71 @@ impl PerformCrud for EditCommunity {
send_community_ws_message(data.community_id, op, websocket_id, None, context).await
}
}

#[async_trait::async_trait(?Send)]
impl PerformCrud for HideCommunity {
type Response = CommunityResponse;

#[tracing::instrument(skip(context, websocket_id))]
async fn perform(
&self,
context: &Data<LemmyContext>,
websocket_id: Option<ConnectionId>,
) -> Result<CommunityResponse, LemmyError> {
let data: &HideCommunity = self;

// Verify its a admin (only admin can hide or unhide it)
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
is_admin(&local_user_view)?;

let community_id = data.community_id;
let read_community = blocking(context.pool(), move |conn| {
Community::read(conn, community_id)
})
.await??;

let community_form = CommunityForm {
name: read_community.name,
title: read_community.title,
description: read_community.description.to_owned(),
public_key: read_community.public_key,
icon: Some(read_community.icon),
banner: Some(read_community.banner),
nsfw: Some(read_community.nsfw),
updated: Some(naive_now()),
hidden: Some(data.hidden),
..CommunityForm::default()
};

let mod_hide_community_form = ModHideCommunityForm {
community_id: data.community_id,
mod_person_id: local_user_view.person.id,
reason: data.reason.clone(),
hidden: Some(data.hidden),
};

let community_id = data.community_id;
let updated_community = blocking(context.pool(), move |conn| {
Community::update(conn, community_id, &community_form)
})
.await?
.map_err(LemmyError::from)
.map_err(|e| e.with_message("couldnt_update_community_hidden_status"))?;

blocking(context.pool(), move |conn| {
ModHideCommunity::create(conn, &mod_hide_community_form)
})
.await??;

UpdateCommunity::send(
updated_community.into(),
&local_user_view.person.into(),
context,
)
.await?;

let op = UserOperationCrud::EditCommunity;
send_community_ws_message(data.community_id, op, websocket_id, None, context).await
}
}
1 change: 1 addition & 0 deletions crates/apub/src/protocol/objects/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ impl Group {
actor_id: Some(self.id.into()),
local: Some(false),
private_key: None,
hidden: Some(false),
public_key: self.public_key.public_key_pem,
last_refreshed_at: Some(naive_now()),
icon: Some(self.icon.map(|i| i.url.into())),
Expand Down
3 changes: 3 additions & 0 deletions crates/db_schema/src/impls/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ mod safe_type {
local,
icon,
banner,
hidden,
);

impl ToSafe for Community {
Expand All @@ -62,6 +63,7 @@ mod safe_type {
local,
icon,
banner,
hidden,
)
}
}
Expand Down Expand Up @@ -372,6 +374,7 @@ mod tests {
followers_url: inserted_community.followers_url.to_owned(),
inbox_url: inserted_community.inbox_url.to_owned(),
shared_inbox_url: None,
hidden: false,
};

let community_follower_form = CommunityFollowerForm {
Expand Down
24 changes: 24 additions & 0 deletions crates/db_schema/src/impls/moderator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,30 @@ impl Crud for ModBan {
}
}

impl Crud for ModHideCommunity {
type Form = ModHideCommunityForm;
type IdType = i32;

fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_hide_community::dsl::*;
mod_hide_community.find(from_id).first::<Self>(conn)
}

fn create(conn: &PgConnection, form: &ModHideCommunityForm) -> Result<Self, Error> {
use crate::schema::mod_hide_community::dsl::*;
insert_into(mod_hide_community)
.values(form)
.get_result::<Self>(conn)
}

fn update(conn: &PgConnection, from_id: i32, form: &ModHideCommunityForm) -> Result<Self, Error> {
use crate::schema::mod_hide_community::dsl::*;
diesel::update(mod_hide_community.find(from_id))
.set(form)
.get_result::<Self>(conn)
}
}

impl Crud for ModAddCommunity {
type Form = ModAddCommunityForm;
type IdType = i32;
Expand Down
15 changes: 15 additions & 0 deletions crates/db_schema/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ table! {
followers_url -> Varchar,
inbox_url -> Varchar,
shared_inbox_url -> Nullable<Varchar>,
hidden -> Bool,
}
}

Expand Down Expand Up @@ -593,6 +594,17 @@ table! {
}
}

table! {
mod_hide_community (id) {
id -> Int4,
community_id -> Int4,
mod_person_id -> Int4,
reason -> Nullable<Text>,
hidden -> Nullable<Bool>,
when_ -> Timestamp,
}
}

joinable!(comment_alias_1 -> person_alias_1 (creator_id));
joinable!(comment -> comment_alias_1 (parent_id));
joinable!(person_mention -> person_alias_1 (recipient_id));
Expand Down Expand Up @@ -656,6 +668,8 @@ joinable!(site_aggregates -> site (site_id));
joinable!(email_verification -> local_user (local_user_id));
joinable!(registration_application -> local_user (local_user_id));
joinable!(registration_application -> person (admin_id));
joinable!(mod_hide_community -> person (mod_person_id));
joinable!(mod_hide_community -> community (community_id));

allow_tables_to_appear_in_same_query!(
activity,
Expand All @@ -681,6 +695,7 @@ allow_tables_to_appear_in_same_query!(
mod_remove_community,
mod_remove_post,
mod_sticky_post,
mod_hide_community,
password_reset_request,
person,
person_aggregates,
Expand Down
3 changes: 3 additions & 0 deletions crates/db_schema/src/source/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct Community {
pub followers_url: DbUrl,
pub inbox_url: DbUrl,
pub shared_inbox_url: Option<DbUrl>,
pub hidden: bool,
}

/// A safe representation of community, without the sensitive info
Expand All @@ -45,6 +46,7 @@ pub struct CommunitySafe {
pub local: bool,
pub icon: Option<DbUrl>,
pub banner: Option<DbUrl>,
pub hidden: bool,
}

#[derive(Insertable, AsChangeset, Debug, Default)]
Expand All @@ -68,6 +70,7 @@ pub struct CommunityForm {
pub followers_url: Option<DbUrl>,
pub inbox_url: Option<DbUrl>,
pub shared_inbox_url: Option<Option<DbUrl>>,
pub hidden: Option<bool>,
}

#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
Expand Down
20 changes: 20 additions & 0 deletions crates/db_schema/src/source/moderator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
mod_add_community,
mod_ban,
mod_ban_from_community,
mod_hide_community,
mod_lock_post,
mod_remove_comment,
mod_remove_community,
Expand Down Expand Up @@ -149,6 +150,25 @@ pub struct ModBan {
pub when_: chrono::NaiveDateTime,
}

#[derive(Insertable, AsChangeset)]
#[table_name = "mod_hide_community"]
pub struct ModHideCommunityForm {
pub community_id: CommunityId,
pub mod_person_id: PersonId,
pub hidden: Option<bool>,
pub reason: Option<String>,
}
#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
#[table_name = "mod_hide_community"]
pub struct ModHideCommunity {
pub id: i32,
pub community_id: CommunityId,
pub mod_person_id: PersonId,
pub reason: Option<String>,
pub hidden: Option<bool>,
pub when_: chrono::NaiveDateTime,
}

#[derive(Insertable, AsChangeset)]
#[table_name = "mod_ban"]
pub struct ModBanForm {
Expand Down
1 change: 1 addition & 0 deletions crates/db_views/src/comment_report_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ mod tests {
description: None,
updated: None,
banner: None,
hidden: false,
published: inserted_community.published,
},
creator: PersonSafe {
Expand Down
24 changes: 20 additions & 4 deletions crates/db_views/src/comment_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,10 +448,25 @@ impl<'a> CommentQueryBuilder<'a> {
};

if let Some(listing_type) = self.listing_type {
query = match listing_type {
ListingType::Subscribed => query.filter(community_follower::person_id.is_not_null()), // TODO could be this: and(community_follower::person_id.eq(person_id_join)),
ListingType::Local => query.filter(community::local.eq(true)),
_ => query,
match listing_type {
ListingType::Subscribed => {
query = query.filter(community_follower::person_id.is_not_null())
} // TODO could be this: and(community_follower::person_id.eq(person_id_join)),
ListingType::Local => {
query = query.filter(community::local.eq(true)).filter(
community::hidden
.eq(false)
.or(community_follower::person_id.eq(person_id_join)),
)
}
ListingType::All => {
query = query.filter(
community::hidden
.eq(false)
.or(community_follower::person_id.eq(person_id_join)),
)
}
ListingType::Community => {}
};
}

Expand Down Expand Up @@ -693,6 +708,7 @@ mod tests {
description: None,
updated: None,
banner: None,
hidden: false,
published: inserted_community.published,
},
counts: CommentAggregates {
Expand Down
1 change: 1 addition & 0 deletions crates/db_views/src/post_report_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ mod tests {
description: None,
updated: None,
banner: None,
hidden: false,
published: inserted_community.published,
},
creator: PersonSafe {
Expand Down
Loading

0 comments on commit 7f9b55e

Please sign in to comment.