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