2022-05-03 11:44:13 -06:00
|
|
|
use crate::structs::CommunityPersonBanView;
|
2022-11-18 21:33:54 -07:00
|
|
|
use diesel::{dsl::now, result::Error, BoolExpressionMethods, ExpressionMethods, QueryDsl};
|
2022-11-09 03:05:00 -07:00
|
|
|
use diesel_async::RunQueryDsl;
|
2021-03-10 15:33:55 -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_person_ban, person},
|
|
|
|
source::{
|
|
|
|
community::{Community, CommunitySafe},
|
2021-03-10 21:43:11 -07:00
|
|
|
person::{Person, PersonSafe},
|
2021-03-10 15:33:55 -07:00
|
|
|
},
|
2021-10-16 07:33:38 -06:00
|
|
|
traits::ToSafe,
|
2022-11-09 03:05:00 -07:00
|
|
|
utils::{get_conn, DbPool},
|
2021-03-10 15:33:55 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
impl CommunityPersonBanView {
|
2022-11-09 03:05:00 -07:00
|
|
|
pub async fn get(
|
|
|
|
pool: &DbPool,
|
2021-03-18 14:25:21 -06:00
|
|
|
from_person_id: PersonId,
|
|
|
|
from_community_id: CommunityId,
|
2021-03-10 15:33:55 -07:00
|
|
|
) -> Result<Self, Error> {
|
2022-11-09 03:05:00 -07:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2021-03-10 15:33:55 -07:00
|
|
|
let (community, person) = community_person_ban::table
|
|
|
|
.inner_join(community::table)
|
|
|
|
.inner_join(person::table)
|
2021-03-10 21:43:11 -07:00
|
|
|
.select((
|
|
|
|
Community::safe_columns_tuple(),
|
|
|
|
Person::safe_columns_tuple(),
|
|
|
|
))
|
2021-03-10 15:33:55 -07:00
|
|
|
.filter(community_person_ban::community_id.eq(from_community_id))
|
|
|
|
.filter(community_person_ban::person_id.eq(from_person_id))
|
2022-01-08 05:37:07 -07:00
|
|
|
.filter(
|
|
|
|
community_person_ban::expires
|
|
|
|
.is_null()
|
|
|
|
.or(community_person_ban::expires.gt(now)),
|
|
|
|
)
|
2021-03-10 15:33:55 -07:00
|
|
|
.order_by(community_person_ban::published)
|
2022-11-09 03:05:00 -07:00
|
|
|
.first::<(CommunitySafe, PersonSafe)>(conn)
|
|
|
|
.await?;
|
2021-03-10 15:33:55 -07:00
|
|
|
|
|
|
|
Ok(CommunityPersonBanView { community, person })
|
|
|
|
}
|
|
|
|
}
|