2022-05-03 11:44:13 -06:00
|
|
|
use crate::structs::PersonBlockView;
|
2023-02-03 06:45:32 -07:00
|
|
|
use diesel::{result::Error, ExpressionMethods, JoinOnDsl, QueryDsl};
|
2022-11-09 03:05:00 -07:00
|
|
|
use diesel_async::RunQueryDsl;
|
2021-08-19 14:54:15 -06:00
|
|
|
use lemmy_db_schema::{
|
2021-10-16 07:33:38 -06:00
|
|
|
newtypes::PersonId,
|
2022-09-26 08:09:32 -06:00
|
|
|
schema::{person, person_block},
|
2022-11-09 03:05:00 -07:00
|
|
|
utils::{get_conn, DbPool},
|
2021-08-19 14:54:15 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
impl PersonBlockView {
|
2023-07-11 07:09:59 -06:00
|
|
|
pub async fn for_person(pool: &mut DbPool<'_>, person_id: PersonId) -> Result<Vec<Self>, Error> {
|
2022-11-09 03:05:00 -07:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2023-02-03 06:45:32 -07:00
|
|
|
let target_person_alias = diesel::alias!(person as person1);
|
2022-09-26 08:09:32 -06:00
|
|
|
|
2023-08-31 07:26:10 -06:00
|
|
|
person_block::table
|
2023-04-17 13:19:51 -06:00
|
|
|
.inner_join(person::table.on(person_block::person_id.eq(person::id)))
|
2023-02-03 06:45:32 -07:00
|
|
|
.inner_join(
|
|
|
|
target_person_alias.on(person_block::target_id.eq(target_person_alias.field(person::id))),
|
|
|
|
)
|
2021-08-19 14:54:15 -06:00
|
|
|
.select((
|
2023-03-01 10:19:46 -07:00
|
|
|
person::all_columns,
|
|
|
|
target_person_alias.fields(person::all_columns),
|
2021-08-19 14:54:15 -06:00
|
|
|
))
|
|
|
|
.filter(person_block::person_id.eq(person_id))
|
2023-02-03 06:45:32 -07:00
|
|
|
.filter(target_person_alias.field(person::deleted).eq(false))
|
2021-08-19 14:54:15 -06:00
|
|
|
.order_by(person_block::published)
|
2023-08-31 07:26:10 -06:00
|
|
|
.load::<PersonBlockView>(conn)
|
|
|
|
.await
|
2021-08-19 14:54:15 -06:00
|
|
|
}
|
|
|
|
}
|