2022-05-03 17:44:13 +00:00
|
|
|
use crate::structs::PersonBlockView;
|
2023-02-03 13:45:32 +00:00
|
|
|
use diesel::{result::Error, ExpressionMethods, JoinOnDsl, QueryDsl};
|
2022-11-09 10:05:00 +00:00
|
|
|
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?;
|
2023-02-03 13:45:32 +00:00
|
|
|
let target_person_alias = diesel::alias!(person as person1);
|
2022-09-26 14:09:32 +00:00
|
|
|
|
2021-08-19 20:54:15 +00:00
|
|
|
let res = person_block::table
|
|
|
|
.inner_join(person::table)
|
2023-02-03 13:45:32 +00:00
|
|
|
.inner_join(
|
|
|
|
target_person_alias.on(person_block::target_id.eq(target_person_alias.field(person::id))),
|
|
|
|
)
|
2021-08-19 20:54:15 +00:00
|
|
|
.select((
|
|
|
|
Person::safe_columns_tuple(),
|
2023-02-03 13:45:32 +00:00
|
|
|
target_person_alias.fields(Person::safe_columns_tuple()),
|
2021-08-19 20:54:15 +00:00
|
|
|
))
|
|
|
|
.filter(person_block::person_id.eq(person_id))
|
2023-02-03 13:45:32 +00:00
|
|
|
.filter(target_person_alias.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>>()
|
|
|
|
}
|
|
|
|
}
|