From 86a433b0b31997048807ec1f48496622c9b09738 Mon Sep 17 00:00:00 2001 From: dull b Date: Sun, 30 Jul 2023 04:40:32 +0000 Subject: [PATCH] CommunityFollower::select_subscribed_type --- crates/db_schema/src/impls/community.rs | 30 +++++++++++++++++-- crates/db_views/src/comment_view.rs | 14 ++++----- crates/db_views/src/post_view.rs | 11 ++++--- .../db_views_actor/src/comment_reply_view.rs | 7 +++-- crates/db_views_actor/src/community_view.rs | 14 ++++----- .../db_views_actor/src/person_mention_view.rs | 7 +++-- 6 files changed, 52 insertions(+), 31 deletions(-) diff --git a/crates/db_schema/src/impls/community.rs b/crates/db_schema/src/impls/community.rs index 258e41504..b87b90920 100644 --- a/crates/db_schema/src/impls/community.rs +++ b/crates/db_schema/src/impls/community.rs @@ -1,6 +1,6 @@ use crate::{ newtypes::{CommunityId, DbUrl, PersonId}, - schema::{community, instance}, + schema::{community, community_follower, instance}, source::{ actor_language::CommunityLanguage, community::{ @@ -19,7 +19,18 @@ use crate::{ utils::{functions::lower, get_conn, DbPool}, SubscribedType, }; -use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl}; +use diesel::{ + deserialize, + dsl, + dsl::insert_into, + pg::Pg, + result::Error, + sql_types, + ExpressionMethods, + NullableExpressionMethods, + QueryDsl, + Queryable, +}; use diesel_async::RunQueryDsl; #[async_trait] @@ -228,6 +239,21 @@ impl CommunityFollower { None => SubscribedType::NotSubscribed, } } + + pub fn select_subscribed_type() -> dsl::Nullable { + community_follower::pending.nullable() + } +} + +impl Queryable, Pg> for SubscribedType { + type Row = Option; + fn build(row: Self::Row) -> deserialize::Result { + Ok(match row { + Some(true) => SubscribedType::Pending, + Some(false) => SubscribedType::Subscribed, + None => SubscribedType::NotSubscribed, + }) + } } #[async_trait] diff --git a/crates/db_views/src/comment_view.rs b/crates/db_views/src/comment_view.rs index 007359be2..8406a76a5 100644 --- a/crates/db_views/src/comment_view.rs +++ b/crates/db_views/src/comment_view.rs @@ -39,6 +39,7 @@ use lemmy_db_schema::{ utils::{fuzzy_search, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn}, CommentSortType, ListingType, + SubscribedType, }; type CommentViewTuple = ( @@ -48,7 +49,7 @@ type CommentViewTuple = ( Community, CommentAggregatesNotInComment, bool, - Option, + SubscribedType, bool, bool, Option, @@ -110,7 +111,7 @@ fn queries<'a>() -> Queries< community::all_columns, CommentAggregatesNotInComment::as_select(), community_person_ban::id.nullable().is_not_null(), - community_follower::all_columns.nullable(), + CommunityFollower::select_subscribed_type(), comment_saved::id.nullable().is_not_null(), person_block::id.nullable().is_not_null(), comment_like::score.nullable(), @@ -171,9 +172,7 @@ fn queries<'a>() -> Queries< if let Some(listing_type) = options.listing_type { match listing_type { - ListingType::Subscribed => { - query = query.filter(community_follower::person_id.is_not_null()) - } // TODO could be this: and(community_follower::person_id.eq(person_id_join)), + ListingType::Subscribed => query = query.filter(community_follower::pending.is_not_null()), // TODO could be this: and(community_follower::person_id.eq(person_id_join)), ListingType::Local => { query = query.filter(community::local.eq(true)).filter( community::hidden @@ -334,7 +333,7 @@ impl JoinView for CommentView { community: a.3, counts, creator_banned_from_community: a.5, - subscribed: CommunityFollower::to_subscribed_type(&a.6), + subscribed: a.6, saved: a.7, creator_blocked: a.8, my_vote: a.9, @@ -356,7 +355,6 @@ mod tests { Community, DbPool, Person, - PersonBlock, Post, }, structs::LocalUserView, @@ -373,7 +371,7 @@ mod tests { language::Language, local_user::{LocalUser, LocalUserInsertForm}, person::PersonInsertForm, - person_block::PersonBlockForm, + person_block::{PersonBlock, PersonBlockForm}, post::PostInsertForm, }, traits::{Blockable, Crud, Likeable}, diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index df62fccfa..9d0d1b8c4 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -43,6 +43,7 @@ use lemmy_db_schema::{ utils::{fuzzy_search, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn}, ListingType, SortType, + SubscribedType, }; use tracing::debug; @@ -52,7 +53,7 @@ type PostViewTuple = ( Community, bool, PostAggregatesNotInPost, - Option, + SubscribedType, bool, bool, bool, @@ -138,7 +139,7 @@ fn queries<'a>() -> Queries< community::all_columns, community_person_ban::id.nullable().is_not_null(), PostAggregatesNotInPost::as_select(), - community_follower::all_columns.nullable(), + CommunityFollower::select_subscribed_type(), post_saved::id.nullable().is_not_null(), post_read::id.nullable().is_not_null(), person_block::id.nullable().is_not_null(), @@ -248,9 +249,7 @@ fn queries<'a>() -> Queries< if let Some(listing_type) = options.listing_type { match listing_type { - ListingType::Subscribed => { - query = query.filter(community_follower::person_id.is_not_null()) - } + ListingType::Subscribed => query = query.filter(community_follower::pending.is_not_null()), ListingType::Local => { query = query.filter(community::local.eq(true)).filter( community::hidden @@ -447,7 +446,7 @@ impl JoinView for PostView { community: a.2, creator_banned_from_community: a.3, counts, - subscribed: CommunityFollower::to_subscribed_type(&a.5), + subscribed: a.5, saved: a.6, read: a.7, creator_blocked: a.8, diff --git a/crates/db_views_actor/src/comment_reply_view.rs b/crates/db_views_actor/src/comment_reply_view.rs index e47a71361..ba6329a40 100644 --- a/crates/db_views_actor/src/comment_reply_view.rs +++ b/crates/db_views_actor/src/comment_reply_view.rs @@ -37,6 +37,7 @@ use lemmy_db_schema::{ traits::JoinView, utils::{get_conn, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn}, CommentSortType, + SubscribedType, }; type CommentReplyViewTuple = ( @@ -48,7 +49,7 @@ type CommentReplyViewTuple = ( Person, CommentAggregatesNotInComment, bool, - Option, + SubscribedType, bool, bool, Option, @@ -113,7 +114,7 @@ fn queries<'a>() -> Queries< aliases::person1.fields(person::all_columns), CommentAggregatesNotInComment::as_select(), community_person_ban::id.nullable().is_not_null(), - community_follower::all_columns.nullable(), + CommunityFollower::select_subscribed_type(), comment_saved::id.nullable().is_not_null(), person_block::id.nullable().is_not_null(), comment_like::score.nullable(), @@ -228,7 +229,7 @@ impl JoinView for CommentReplyView { recipient: a.5, counts, creator_banned_from_community: a.7, - subscribed: CommunityFollower::to_subscribed_type(&a.8), + subscribed: a.8, saved: a.9, creator_blocked: a.10, my_vote: a.11, diff --git a/crates/db_views_actor/src/community_view.rs b/crates/db_views_actor/src/community_view.rs index d306e7c23..28a492579 100644 --- a/crates/db_views_actor/src/community_view.rs +++ b/crates/db_views_actor/src/community_view.rs @@ -22,14 +22,10 @@ use lemmy_db_schema::{ utils::{fuzzy_search, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn}, ListingType, SortType, + SubscribedType, }; -type CommunityViewTuple = ( - Community, - CommunityAggregates, - Option, - bool, -); +type CommunityViewTuple = (Community, CommunityAggregates, SubscribedType, bool); fn queries<'a>() -> Queries< impl ReadFn<'a, CommunityView, (CommunityId, Option, Option)>, @@ -60,7 +56,7 @@ fn queries<'a>() -> Queries< let selection = ( community::all_columns, community_aggregates::all_columns, - community_follower::all_columns.nullable(), + CommunityFollower::select_subscribed_type(), community_block::id.nullable().is_not_null(), ); @@ -137,7 +133,7 @@ fn queries<'a>() -> Queries< if let Some(listing_type) = options.listing_type { query = match listing_type { - ListingType::Subscribed => query.filter(community_follower::person_id.is_not_null()), // TODO could be this: and(community_follower::person_id.eq(person_id_join)), + ListingType::Subscribed => query.filter(community_follower::pending.is_not_null()), // TODO could be this: and(community_follower::person_id.eq(person_id_join)), ListingType::Local => query.filter(community::local.eq(true)), _ => query, }; @@ -216,7 +212,7 @@ impl JoinView for CommunityView { Self { community: a.0, counts: a.1, - subscribed: CommunityFollower::to_subscribed_type(&a.2), + subscribed: a.2, blocked: a.3, } } diff --git a/crates/db_views_actor/src/person_mention_view.rs b/crates/db_views_actor/src/person_mention_view.rs index 3b629bd8d..2582e8f84 100644 --- a/crates/db_views_actor/src/person_mention_view.rs +++ b/crates/db_views_actor/src/person_mention_view.rs @@ -38,6 +38,7 @@ use lemmy_db_schema::{ traits::JoinView, utils::{get_conn, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn}, CommentSortType, + SubscribedType, }; type PersonMentionViewTuple = ( @@ -49,7 +50,7 @@ type PersonMentionViewTuple = ( Person, CommentAggregatesNotInComment, bool, - Option, + SubscribedType, bool, bool, Option, @@ -109,7 +110,7 @@ fn queries<'a>() -> Queries< aliases::person1.fields(person::all_columns), CommentAggregatesNotInComment::as_select(), community_person_ban::id.nullable().is_not_null(), - community_follower::all_columns.nullable(), + CommunityFollower::select_subscribed_type(), comment_saved::id.nullable().is_not_null(), person_block::id.nullable().is_not_null(), comment_like::score.nullable(), @@ -245,7 +246,7 @@ impl JoinView for PersonMentionView { recipient: a.5, counts, creator_banned_from_community: a.7, - subscribed: CommunityFollower::to_subscribed_type(&a.8), + subscribed: a.8, saved: a.9, creator_blocked: a.10, my_vote: a.11,