]> Untitled Git - lemmy.git/blob - crates/db_views_actor/src/community_block_view.rs
d87bea37fe26257c96ddaabe8716d9db7a918ed4
[lemmy.git] / crates / db_views_actor / src / community_block_view.rs
1 use crate::structs::CommunityBlockView;
2 use diesel::{result::Error, ExpressionMethods, QueryDsl};
3 use diesel_async::RunQueryDsl;
4 use lemmy_db_schema::{
5   newtypes::PersonId,
6   schema::{community, community_block, person},
7   source::{community::Community, person::Person},
8   traits::JoinView,
9   utils::{get_conn, DbPool},
10 };
11
12 type CommunityBlockViewTuple = (Person, Community);
13
14 impl CommunityBlockView {
15   pub async fn for_person(pool: &DbPool, person_id: PersonId) -> Result<Vec<Self>, Error> {
16     let conn = &mut get_conn(pool).await?;
17     let res = community_block::table
18       .inner_join(person::table)
19       .inner_join(community::table)
20       .select((person::all_columns, community::all_columns))
21       .filter(community_block::person_id.eq(person_id))
22       .filter(community::deleted.eq(false))
23       .filter(community::removed.eq(false))
24       .order_by(community_block::published)
25       .load::<CommunityBlockViewTuple>(conn)
26       .await?;
27
28     Ok(res.into_iter().map(Self::from_tuple).collect())
29   }
30 }
31
32 impl JoinView for CommunityBlockView {
33   type JoinTuple = CommunityBlockViewTuple;
34   fn from_tuple(a: Self::JoinTuple) -> Self {
35     Self {
36       person: a.0,
37       community: a.1,
38     }
39   }
40 }