2022-05-03 17:44:13 +00:00
|
|
|
use crate::structs::CommunityBlockView;
|
2021-08-19 20:54:15 +00:00
|
|
|
use diesel::{result::Error, *};
|
|
|
|
use lemmy_db_schema::{
|
2021-10-16 13:33:38 +00:00
|
|
|
newtypes::PersonId,
|
2021-08-19 20:54:15 +00:00
|
|
|
schema::{community, community_block, person},
|
|
|
|
source::{
|
|
|
|
community::{Community, CommunitySafe},
|
|
|
|
person::{Person, PersonSafe},
|
|
|
|
},
|
2021-10-16 13:33:38 +00:00
|
|
|
traits::{ToSafe, ViewToVec},
|
2021-08-19 20:54:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type CommunityBlockViewTuple = (PersonSafe, CommunitySafe);
|
|
|
|
|
|
|
|
impl CommunityBlockView {
|
2022-09-26 14:09:32 +00:00
|
|
|
pub fn for_person(conn: &mut PgConnection, person_id: PersonId) -> Result<Vec<Self>, Error> {
|
2021-08-19 20:54:15 +00:00
|
|
|
let res = community_block::table
|
|
|
|
.inner_join(person::table)
|
|
|
|
.inner_join(community::table)
|
|
|
|
.select((
|
|
|
|
Person::safe_columns_tuple(),
|
|
|
|
Community::safe_columns_tuple(),
|
|
|
|
))
|
|
|
|
.filter(community_block::person_id.eq(person_id))
|
|
|
|
.order_by(community_block::published)
|
|
|
|
.load::<CommunityBlockViewTuple>(conn)?;
|
|
|
|
|
|
|
|
Ok(Self::from_tuple_to_vec(res))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ViewToVec for CommunityBlockView {
|
|
|
|
type DbTuple = CommunityBlockViewTuple;
|
|
|
|
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,
|
|
|
|
community: a.1,
|
2021-08-19 20:54:15 +00:00
|
|
|
})
|
|
|
|
.collect::<Vec<Self>>()
|
|
|
|
}
|
|
|
|
}
|