mirror of https://github.com/LemmyNet/lemmy
Hide community v2 (#2055)
* 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>pull/2104/head
parent
762b85b27e
commit
7f9b55e793
@ -0,0 +1,75 @@
|
||||
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>>()
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
alter table community drop column hidden;
|
||||
|
||||
drop table mod_hide_community;
|
@ -0,0 +1,13 @@
|
||||
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
|
||||
);
|
||||
|
Loading…
Reference in New Issue