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