]> Untitled Git - lemmy.git/blob - crates/db_views_actor/src/community_person_ban_view.rs
Diesel 2.0.0 upgrade (#2452)
[lemmy.git] / crates / db_views_actor / src / community_person_ban_view.rs
1 use crate::structs::CommunityPersonBanView;
2 use diesel::{dsl::*, result::Error, *};
3 use lemmy_db_schema::{
4   newtypes::{CommunityId, PersonId},
5   schema::{community, community_person_ban, person},
6   source::{
7     community::{Community, CommunitySafe},
8     person::{Person, PersonSafe},
9   },
10   traits::ToSafe,
11 };
12
13 impl CommunityPersonBanView {
14   pub fn get(
15     conn: &mut PgConnection,
16     from_person_id: PersonId,
17     from_community_id: CommunityId,
18   ) -> Result<Self, Error> {
19     let (community, person) = community_person_ban::table
20       .inner_join(community::table)
21       .inner_join(person::table)
22       .select((
23         Community::safe_columns_tuple(),
24         Person::safe_columns_tuple(),
25       ))
26       .filter(community_person_ban::community_id.eq(from_community_id))
27       .filter(community_person_ban::person_id.eq(from_person_id))
28       .filter(
29         community_person_ban::expires
30           .is_null()
31           .or(community_person_ban::expires.gt(now)),
32       )
33       .order_by(community_person_ban::published)
34       .first::<(CommunitySafe, PersonSafe)>(conn)?;
35
36     Ok(CommunityPersonBanView { community, person })
37   }
38 }