1 use diesel::{result::Error, *};
2 use lemmy_db_queries::{ToSafe, ViewToVec};
4 schema::{community, community_block, person},
6 community::{Community, CommunitySafe},
7 person::{Person, PersonSafe},
11 use serde::{Deserialize, Serialize};
13 #[derive(Debug, Serialize, Deserialize, Clone)]
14 pub struct CommunityBlockView {
15 pub person: PersonSafe,
16 pub community: CommunitySafe,
19 type CommunityBlockViewTuple = (PersonSafe, CommunitySafe);
21 impl CommunityBlockView {
22 pub fn for_person(conn: &PgConnection, person_id: PersonId) -> Result<Vec<Self>, Error> {
23 let res = community_block::table
24 .inner_join(person::table)
25 .inner_join(community::table)
27 Person::safe_columns_tuple(),
28 Community::safe_columns_tuple(),
30 .filter(community_block::person_id.eq(person_id))
31 .order_by(community_block::published)
32 .load::<CommunityBlockViewTuple>(conn)?;
34 Ok(Self::from_tuple_to_vec(res))
38 impl ViewToVec for CommunityBlockView {
39 type DbTuple = CommunityBlockViewTuple;
40 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
44 person: a.0.to_owned(),
45 community: a.1.to_owned(),
47 .collect::<Vec<Self>>()