2022-05-03 11:44:13 -06:00
|
|
|
use crate::structs::CommunityModeratorView;
|
2023-07-06 06:44:26 -06:00
|
|
|
use diesel::{dsl::exists, result::Error, select, ExpressionMethods, QueryDsl};
|
2022-11-09 03:05:00 -07:00
|
|
|
use diesel_async::RunQueryDsl;
|
2020-12-18 11:38:32 -07:00
|
|
|
use lemmy_db_schema::{
|
2021-10-16 07:33:38 -06:00
|
|
|
newtypes::{CommunityId, PersonId},
|
2021-03-10 15:33:55 -07:00
|
|
|
schema::{community, community_moderator, person},
|
2022-11-09 03:05:00 -07:00
|
|
|
utils::{get_conn, DbPool},
|
2020-12-18 11:38:32 -07:00
|
|
|
};
|
2020-12-05 21:37:16 -07:00
|
|
|
|
|
|
|
impl CommunityModeratorView {
|
2023-07-06 06:44:26 -06:00
|
|
|
pub async fn is_community_moderator(
|
2023-07-11 07:09:59 -06:00
|
|
|
pool: &mut DbPool<'_>,
|
2023-07-06 06:44:26 -06:00
|
|
|
find_community_id: CommunityId,
|
|
|
|
find_person_id: PersonId,
|
|
|
|
) -> Result<bool, Error> {
|
|
|
|
use lemmy_db_schema::schema::community_moderator::dsl::{
|
|
|
|
community_id,
|
|
|
|
community_moderator,
|
|
|
|
person_id,
|
|
|
|
};
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
|
|
|
select(exists(
|
|
|
|
community_moderator
|
|
|
|
.filter(community_id.eq(find_community_id))
|
|
|
|
.filter(person_id.eq(find_person_id)),
|
|
|
|
))
|
|
|
|
.get_result::<bool>(conn)
|
|
|
|
.await
|
|
|
|
}
|
2023-11-21 08:33:49 -07:00
|
|
|
|
|
|
|
pub(crate) async fn is_community_moderator_of_any(
|
|
|
|
pool: &mut DbPool<'_>,
|
|
|
|
find_person_id: PersonId,
|
|
|
|
) -> Result<bool, Error> {
|
|
|
|
use lemmy_db_schema::schema::community_moderator::dsl::{community_moderator, person_id};
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
|
|
|
select(exists(
|
|
|
|
community_moderator.filter(person_id.eq(find_person_id)),
|
|
|
|
))
|
|
|
|
.get_result::<bool>(conn)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2023-07-11 07:09:59 -06:00
|
|
|
pub async fn for_community(
|
|
|
|
pool: &mut DbPool<'_>,
|
|
|
|
community_id: CommunityId,
|
|
|
|
) -> Result<Vec<Self>, Error> {
|
2022-11-09 03:05:00 -07:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2023-08-31 07:26:10 -06:00
|
|
|
community_moderator::table
|
2020-12-05 21:37:16 -07:00
|
|
|
.inner_join(community::table)
|
2021-03-10 15:33:55 -07:00
|
|
|
.inner_join(person::table)
|
2020-12-16 11:59:43 -07:00
|
|
|
.filter(community_moderator::community_id.eq(community_id))
|
2023-07-06 04:27:25 -06:00
|
|
|
.select((community::all_columns, person::all_columns))
|
|
|
|
.order_by(community_moderator::published)
|
2023-08-31 07:26:10 -06:00
|
|
|
.load::<CommunityModeratorView>(conn)
|
|
|
|
.await
|
2020-12-05 21:37:16 -07:00
|
|
|
}
|
|
|
|
|
2023-07-11 07:09:59 -06:00
|
|
|
pub async fn for_person(pool: &mut DbPool<'_>, person_id: PersonId) -> Result<Vec<Self>, Error> {
|
2022-11-09 03:05:00 -07:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2023-08-31 07:26:10 -06:00
|
|
|
community_moderator::table
|
2020-12-05 21:37:16 -07:00
|
|
|
.inner_join(community::table)
|
2021-03-10 15:33:55 -07:00
|
|
|
.inner_join(person::table)
|
|
|
|
.filter(community_moderator::person_id.eq(person_id))
|
2022-09-28 14:54:32 -06:00
|
|
|
.filter(community::deleted.eq(false))
|
|
|
|
.filter(community::removed.eq(false))
|
2023-07-06 04:27:25 -06:00
|
|
|
.select((community::all_columns, person::all_columns))
|
2023-08-31 07:26:10 -06:00
|
|
|
.load::<CommunityModeratorView>(conn)
|
|
|
|
.await
|
2020-12-05 21:37:16 -07:00
|
|
|
}
|
2021-08-13 11:39:56 -06:00
|
|
|
|
|
|
|
/// Finds all communities first mods / creators
|
|
|
|
/// Ideally this should be a group by, but diesel doesn't support it yet
|
2023-07-11 07:09:59 -06:00
|
|
|
pub async fn get_community_first_mods(pool: &mut DbPool<'_>) -> Result<Vec<Self>, Error> {
|
2022-11-09 03:05:00 -07:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2023-08-31 07:26:10 -06:00
|
|
|
community_moderator::table
|
2021-08-13 11:39:56 -06:00
|
|
|
.inner_join(community::table)
|
|
|
|
.inner_join(person::table)
|
2023-03-01 10:19:46 -07:00
|
|
|
.select((community::all_columns, person::all_columns))
|
2021-08-13 11:39:56 -06:00
|
|
|
// A hacky workaround instead of group_bys
|
|
|
|
// https://stackoverflow.com/questions/24042359/how-to-join-only-one-row-in-joined-table-with-postgres
|
|
|
|
.distinct_on(community_moderator::community_id)
|
|
|
|
.order_by((
|
|
|
|
community_moderator::community_id,
|
|
|
|
community_moderator::person_id,
|
|
|
|
))
|
2023-08-31 07:26:10 -06:00
|
|
|
.load::<CommunityModeratorView>(conn)
|
|
|
|
.await
|
2020-12-10 18:39:42 -07:00
|
|
|
}
|
2020-12-05 21:37:16 -07:00
|
|
|
}
|