]> Untitled Git - lemmy.git/blob - crates/db_views_actor/src/community_block_view.rs
Dont show deleted users or communities on profile page. (#2450)
[lemmy.git] / crates / db_views_actor / src / community_block_view.rs
1 use crate::structs::CommunityBlockView;
2 use diesel::{result::Error, *};
3 use lemmy_db_schema::{
4   newtypes::PersonId,
5   schema::{community, community_block, person},
6   source::{
7     community::{Community, CommunitySafe},
8     person::{Person, PersonSafe},
9   },
10   traits::{ToSafe, ViewToVec},
11 };
12
13 type CommunityBlockViewTuple = (PersonSafe, CommunitySafe);
14
15 impl CommunityBlockView {
16   pub fn for_person(conn: &mut PgConnection, person_id: PersonId) -> Result<Vec<Self>, Error> {
17     let res = community_block::table
18       .inner_join(person::table)
19       .inner_join(community::table)
20       .select((
21         Person::safe_columns_tuple(),
22         Community::safe_columns_tuple(),
23       ))
24       .filter(community_block::person_id.eq(person_id))
25       .filter(community::deleted.eq(false))
26       .filter(community::removed.eq(false))
27       .order_by(community_block::published)
28       .load::<CommunityBlockViewTuple>(conn)?;
29
30     Ok(Self::from_tuple_to_vec(res))
31   }
32 }
33
34 impl ViewToVec for CommunityBlockView {
35   type DbTuple = CommunityBlockViewTuple;
36   fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
37     items
38       .into_iter()
39       .map(|a| Self {
40         person: a.0,
41         community: a.1,
42       })
43       .collect::<Vec<Self>>()
44   }
45 }