2022-05-03 17:44:13 +00:00
|
|
|
use crate::structs::PersonBlockView;
|
2022-11-09 10:05:00 +00:00
|
|
|
use diesel::{result::Error, ExpressionMethods, QueryDsl};
|
|
|
|
use diesel_async::RunQueryDsl;
|
2021-08-19 20:54:15 +00:00
|
|
|
use lemmy_db_schema::{
|
2021-10-16 13:33:38 +00:00
|
|
|
newtypes::PersonId,
|
2022-09-26 14:09:32 +00:00
|
|
|
schema::{person, person_block},
|
|
|
|
source::person::{Person, PersonSafe},
|
2021-10-16 13:33:38 +00:00
|
|
|
traits::{ToSafe, ViewToVec},
|
2022-11-09 10:05:00 +00:00
|
|
|
utils::{get_conn, DbPool},
|
2021-08-19 20:54:15 +00:00
|
|
|
};
|
|
|
|
|
2022-09-26 14:09:32 +00:00
|
|
|
type PersonBlockViewTuple = (PersonSafe, PersonSafe);
|
2021-08-19 20:54:15 +00:00
|
|
|
|
|
|
|
impl PersonBlockView {
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn for_person(pool: &DbPool, person_id: PersonId) -> Result<Vec<Self>, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
2022-09-26 14:09:32 +00:00
|
|
|
let person_alias_1 = diesel::alias!(person as person1);
|
|
|
|
|
2021-08-19 20:54:15 +00:00
|
|
|
let res = person_block::table
|
|
|
|
.inner_join(person::table)
|
2022-09-26 14:09:32 +00:00
|
|
|
.inner_join(person_alias_1)
|
2021-08-19 20:54:15 +00:00
|
|
|
.select((
|
|
|
|
Person::safe_columns_tuple(),
|
2022-09-26 14:09:32 +00:00
|
|
|
person_alias_1.fields(Person::safe_columns_tuple()),
|
2021-08-19 20:54:15 +00:00
|
|
|
))
|
|
|
|
.filter(person_block::person_id.eq(person_id))
|
2022-09-28 20:54:32 +00:00
|
|
|
.filter(person_alias_1.field(person::deleted).eq(false))
|
2021-08-19 20:54:15 +00:00
|
|
|
.order_by(person_block::published)
|
2022-11-09 10:05:00 +00:00
|
|
|
.load::<PersonBlockViewTuple>(conn)
|
|
|
|
.await?;
|
2021-08-19 20:54:15 +00:00
|
|
|
|
|
|
|
Ok(Self::from_tuple_to_vec(res))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ViewToVec for PersonBlockView {
|
|
|
|
type DbTuple = PersonBlockViewTuple;
|
|
|
|
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
|
|
|
|
items
|
2022-08-04 19:30:17 +00:00
|
|
|
.into_iter()
|
2021-08-19 20:54:15 +00:00
|
|
|
.map(|a| Self {
|
2022-08-04 19:30:17 +00:00
|
|
|
person: a.0,
|
|
|
|
target: a.1,
|
2021-08-19 20:54:15 +00:00
|
|
|
})
|
|
|
|
.collect::<Vec<Self>>()
|
|
|
|
}
|
|
|
|
}
|