lemmy/crates/db_views_actor/src/community_person_ban_view.rs

45 lines
1.3 KiB
Rust
Raw Normal View History

use diesel::{dsl::*, result::Error, *};
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,
2021-03-10 22:33:55 +00:00
};
use serde::{Deserialize, Serialize};
2021-03-10 22:33:55 +00:00
#[derive(Debug, Serialize, Deserialize, Clone)]
2021-03-10 22:33:55 +00:00
pub struct CommunityPersonBanView {
pub community: CommunitySafe,
pub person: PersonSafe,
}
impl CommunityPersonBanView {
pub fn get(
conn: &PgConnection,
from_person_id: PersonId,
from_community_id: CommunityId,
2021-03-10 22:33:55 +00:00
) -> Result<Self, Error> {
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))
.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)
.first::<(CommunitySafe, PersonSafe)>(conn)?;
Ok(CommunityPersonBanView { community, person })
}
}