1 use crate::structs::CommunityPersonBanView;
2 use diesel::{dsl::*, result::Error, BoolExpressionMethods, ExpressionMethods, QueryDsl};
3 use diesel_async::RunQueryDsl;
5 newtypes::{CommunityId, PersonId},
6 schema::{community, community_person_ban, person},
8 community::{Community, CommunitySafe},
9 person::{Person, PersonSafe},
12 utils::{get_conn, DbPool},
15 impl CommunityPersonBanView {
18 from_person_id: PersonId,
19 from_community_id: CommunityId,
20 ) -> Result<Self, Error> {
21 let conn = &mut get_conn(pool).await?;
22 let (community, person) = community_person_ban::table
23 .inner_join(community::table)
24 .inner_join(person::table)
26 Community::safe_columns_tuple(),
27 Person::safe_columns_tuple(),
29 .filter(community_person_ban::community_id.eq(from_community_id))
30 .filter(community_person_ban::person_id.eq(from_person_id))
32 community_person_ban::expires
34 .or(community_person_ban::expires.gt(now)),
36 .order_by(community_person_ban::published)
37 .first::<(CommunitySafe, PersonSafe)>(conn)
40 Ok(CommunityPersonBanView { community, person })