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,
})
.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| {
added_to_community,
added,
transferred_to_community,
+ hidden_communities,
})
}
}
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,
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,
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)]
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;
icon,
banner,
nsfw: data.nsfw,
+ hidden: Some(read_community.hidden),
updated: Some(naive_now()),
..CommunityForm::default()
};
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
+ }
+}
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())),
local,
icon,
banner,
+ hidden,
);
impl ToSafe for Community {
local,
icon,
banner,
+ hidden,
)
}
}
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 {
}
}
+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;
followers_url -> Varchar,
inbox_url -> Varchar,
shared_inbox_url -> Nullable<Varchar>,
+ hidden -> Bool,
}
}
}
}
+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));
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,
mod_remove_community,
mod_remove_post,
mod_sticky_post,
+ mod_hide_community,
password_reset_request,
person,
person_aggregates,
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
pub local: bool,
pub icon: Option<DbUrl>,
pub banner: Option<DbUrl>,
+ pub hidden: bool,
}
#[derive(Insertable, AsChangeset, Debug, Default)]
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)]
mod_add_community,
mod_ban,
mod_ban_from_community,
+ mod_hide_community,
mod_lock_post,
mod_remove_comment,
mod_remove_community,
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 {
description: None,
updated: None,
banner: None,
+ hidden: false,
published: inserted_community.published,
},
creator: PersonSafe {
};
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 => {}
};
}
description: None,
updated: None,
banner: None,
+ hidden: false,
published: inserted_community.published,
},
counts: CommentAggregates {
description: None,
updated: None,
banner: None,
+ hidden: false,
published: inserted_community.published,
},
creator: PersonSafe {
.into_boxed();
if let Some(listing_type) = self.listing_type {
- query = match listing_type {
- ListingType::Subscribed => query.filter(community_follower::person_id.is_not_null()),
- ListingType::Local => query.filter(community::local.eq(true)),
- _ => query,
- };
- }
-
- if let Some(community_id) = self.community_id {
- query = query
- .filter(post::community_id.eq(community_id))
- .then_order_by(post_aggregates::stickied.desc());
+ match listing_type {
+ ListingType::Subscribed => {
+ query = query.filter(community_follower::person_id.is_not_null())
+ }
+ 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 => {
+ if let Some(community_id) = self.community_id {
+ query = query
+ .filter(post::community_id.eq(community_id))
+ .then_order_by(post_aggregates::stickied.desc());
+ }
+ }
+ }
}
if let Some(community_actor_id) = self.community_actor_id {
description: None,
updated: None,
banner: None,
+ hidden: false,
published: inserted_community.published,
},
counts: PostAggregates {
SortType::New => query = query.order_by(community::published.desc()),
SortType::TopAll => query = query.order_by(community_aggregates::subscribers.desc()),
SortType::TopMonth => query = query.order_by(community_aggregates::users_active_month.desc()),
- // Covers all other sorts, including hot
+ SortType::Hot => {
+ query = query
+ .order_by(
+ hot_rank(
+ community_aggregates::subscribers,
+ community_aggregates::published,
+ )
+ .desc(),
+ )
+ .then_order_by(community_aggregates::published.desc());
+ // Don't show hidden communities in Hot (trending)
+ query = query.filter(
+ community::hidden
+ .eq(false)
+ .or(community_follower::person_id.eq(person_id_join)),
+ );
+ }
+ // Covers all other sorts
_ => {
query = query
.order_by(
pub mod mod_add_view;
pub mod mod_ban_from_community_view;
pub mod mod_ban_view;
+pub mod mod_hide_community_view;
pub mod mod_lock_post_view;
pub mod mod_remove_comment_view;
pub mod mod_remove_community_view;
--- /dev/null
+use diesel::{result::Error, *};
+use lemmy_db_schema::{
+ limit_and_offset,
+ newtypes::{CommunityId, PersonId},
+ schema::{community, mod_hide_community, person},
+ source::{
+ community::{Community, CommunitySafe},
+ moderator::ModHideCommunity,
+ person::{Person, PersonSafe},
+ },
+ traits::{ToSafe, ViewToVec},
+};
+use serde::{Deserialize, Serialize};
+
+#[derive(Debug, Serialize, Deserialize, Clone)]
+pub struct ModHideCommunityView {
+ pub mod_hide_community: ModHideCommunity,
+ pub admin: PersonSafe,
+ pub community: CommunitySafe,
+}
+
+type ModHideCommunityViewTuple = (ModHideCommunity, PersonSafe, 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<CommunityId>,
+ admin_id: Option<PersonId>,
+ page: Option<i64>,
+ limit: Option<i64>,
+ ) -> Result<Vec<Self>, Error> {
+ let mut query = mod_hide_community::table
+ .inner_join(person::table)
+ .inner_join(community::table.on(mod_hide_community::community_id.eq(community::id)))
+ .select((
+ mod_hide_community::all_columns,
+ Person::safe_columns_tuple(),
+ Community::safe_columns_tuple(),
+ ))
+ .into_boxed();
+
+ if let Some(community_id) = community_id {
+ query = query.filter(mod_hide_community::community_id.eq(community_id));
+ };
+
+ if let Some(admin_id) = admin_id {
+ query = query.filter(mod_hide_community::mod_person_id.eq(admin_id));
+ };
+
+ let (limit, offset) = limit_and_offset(page, limit);
+
+ let res = query
+ .limit(limit)
+ .offset(offset)
+ .order_by(mod_hide_community::when_.desc())
+ .load::<ModHideCommunityViewTuple>(conn)?;
+
+ Ok(Self::from_tuple_to_vec(res))
+ }
+}
+
+impl ViewToVec for ModHideCommunityView {
+ type DbTuple = ModHideCommunityViewTuple;
+ fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
+ items
+ .iter()
+ .map(|a| Self {
+ mod_hide_community: a.0.to_owned(),
+ admin: a.1.to_owned(),
+ community: a.2.to_owned(),
+ })
+ .collect::<Vec<Self>>()
+ }
+}
--- /dev/null
+alter table community drop column hidden;
+
+drop table mod_hide_community;
--- /dev/null
+alter table community add column hidden boolean default false;
+
+
+create table mod_hide_community
+(
+ id serial primary key,
+ community_id int references community on update cascade on delete cascade not null,
+ mod_person_id int references person on update cascade on delete cascade not null,
+ when_ timestamp not null default now(),
+ reason text,
+ hidden boolean default false
+);
+
.wrap(rate_limit.message())
.route("", web::get().to(route_get_crud::<GetCommunity>))
.route("", web::put().to(route_post_crud::<EditCommunity>))
+ .route("/hide", web::put().to(route_post_crud::<HideCommunity>))
.route("/list", web::get().to(route_get_crud::<ListCommunities>))
.route("/follow", web::post().to(route_post::<FollowCommunity>))
.route("/block", web::post().to(route_post::<BlockCommunity>))
deleted: None,
nsfw: None,
updated: None,
+ hidden: Some(false),
actor_id: Some(community_actor_id.to_owned()),
local: Some(ccommunity.local),
private_key: Some(Some(keypair.private_key)),