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},
|
2023-03-01 17:19:46 +00:00
|
|
|
source::person::Person,
|
|
|
|
traits::JoinView,
|
2022-11-09 10:05:00 +00:00
|
|
|
utils::{get_conn, DbPool},
|
2021-08-19 20:54:15 +00:00
|
|
|
};
|
|
|
|
|
2023-03-01 17:19:46 +00:00
|
|
|
type PersonBlockViewTuple = (Person, Person);
|
2021-08-19 20:54:15 +00:00
|
|
|
|
|
|
|
impl PersonBlockView {
|
2023-07-11 13:09:59 +00:00
|
|
|
pub async fn for_person(pool: &mut DbPool<'_>, person_id: PersonId) -> Result<Vec<Self>, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
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
|
2023-04-17 19:19:51 +00:00
|
|
|
.inner_join(person::table.on(person_block::person_id.eq(person::id)))
|
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((
|
2023-03-01 17:19:46 +00:00
|
|
|
person::all_columns,
|
|
|
|
target_person_alias.fields(person::all_columns),
|
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
|
|
|
|
2023-03-01 17:19:46 +00:00
|
|
|
Ok(res.into_iter().map(Self::from_tuple).collect())
|
2021-08-19 20:54:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-01 17:19:46 +00:00
|
|
|
impl JoinView for PersonBlockView {
|
|
|
|
type JoinTuple = PersonBlockViewTuple;
|
|
|
|
fn from_tuple(a: Self::JoinTuple) -> Self {
|
|
|
|
Self {
|
|
|
|
person: a.0,
|
|
|
|
target: a.1,
|
|
|
|
}
|
2021-08-19 20:54:15 +00:00
|
|
|
}
|
|
|
|
}
|