diff --git a/crates/db_schema/src/impls/community.rs b/crates/db_schema/src/impls/community.rs index 2d0fcfe57..d5d558fa2 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] @@ -214,6 +225,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_report_view.rs b/crates/db_views/src/comment_report_view.rs index a92b38063..1b943eab8 100644 --- a/crates/db_views/src/comment_report_view.rs +++ b/crates/db_views/src/comment_report_view.rs @@ -28,7 +28,7 @@ use lemmy_db_schema::{ source::{ comment::Comment, comment_report::CommentReport, - community::{Community, CommunityPersonBan}, + community::Community, person::Person, post::Post, }, @@ -71,7 +71,7 @@ fn queries<'a>() -> Queries< person::all_columns, aliases::person1.fields(person::all_columns), comment_aggregates::all_columns, - community_person_ban::all_columns.nullable(), + community_person_ban::id.nullable().is_not_null(), comment_like::score.nullable(), aliases::person2.fields(person::all_columns).nullable(), ); @@ -228,7 +228,7 @@ impl JoinView for CommentReportView { Person, Person, CommentAggregates, - Option, + bool, Option, Option, ); @@ -242,7 +242,7 @@ impl JoinView for CommentReportView { creator: a.4, comment_creator: a.5, counts: a.6, - creator_banned_from_community: a.7.is_some(), + creator_banned_from_community: a.7, my_vote: a.8, resolver: a.9, } diff --git a/crates/db_views/src/comment_view.rs b/crates/db_views/src/comment_view.rs index 506efb323..f4f26dd2f 100644 --- a/crates/db_views/src/comment_view.rs +++ b/crates/db_views/src/comment_view.rs @@ -29,16 +29,16 @@ use lemmy_db_schema::{ post, }, source::{ - comment::{Comment, CommentSaved}, - community::{Community, CommunityFollower, CommunityPersonBan}, + comment::Comment, + community::{Community, CommunityFollower}, person::Person, - person_block::PersonBlock, post::Post, }, traits::JoinView, utils::{fuzzy_search, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn}, CommentSortType, ListingType, + SubscribedType, }; type CommentViewTuple = ( @@ -47,10 +47,10 @@ type CommentViewTuple = ( Post, Community, CommentAggregates, - Option, - Option, - Option, - Option, + bool, + SubscribedType, + bool, + bool, Option, ); @@ -109,10 +109,10 @@ fn queries<'a>() -> Queries< post::all_columns, community::all_columns, comment_aggregates::all_columns, - community_person_ban::all_columns.nullable(), - community_follower::all_columns.nullable(), - comment_saved::all_columns.nullable(), - person_block::all_columns.nullable(), + community_person_ban::id.nullable().is_not_null(), + CommunityFollower::select_subscribed_type(), + comment_saved::id.nullable().is_not_null(), + person_block::id.nullable().is_not_null(), comment_like::score.nullable(), ); @@ -171,9 +171,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 @@ -338,10 +336,10 @@ impl JoinView for CommentView { post: a.2, community: a.3, counts: a.4, - creator_banned_from_community: a.5.is_some(), - subscribed: CommunityFollower::to_subscribed_type(&a.6), - saved: a.7.is_some(), - creator_blocked: a.8.is_some(), + creator_banned_from_community: a.5, + subscribed: a.6, + saved: a.7, + creator_blocked: a.8, my_vote: a.9, } } @@ -361,7 +359,6 @@ mod tests { Community, DbPool, Person, - PersonBlock, Post, }, structs::LocalUserView, @@ -378,7 +375,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_report_view.rs b/crates/db_views/src/post_report_view.rs index 4ef6067fd..83526a1b0 100644 --- a/crates/db_views/src/post_report_view.rs +++ b/crates/db_views/src/post_report_view.rs @@ -23,12 +23,7 @@ use lemmy_db_schema::{ post_like, post_report, }, - source::{ - community::{Community, CommunityPersonBan}, - person::Person, - post::Post, - post_report::PostReport, - }, + source::{community::Community, person::Person, post::Post, post_report::PostReport}, traits::JoinView, utils::{get_conn, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn}, }; @@ -39,7 +34,7 @@ type PostReportViewTuple = ( Community, Person, Person, - Option, + bool, Option, PostAggregates, Option, @@ -80,7 +75,7 @@ fn queries<'a>() -> Queries< community::all_columns, person::all_columns, aliases::person1.fields(person::all_columns), - community_person_ban::all_columns.nullable(), + community_person_ban::id.nullable().is_not_null(), post_like::score.nullable(), post_aggregates::all_columns, aliases::person2.fields(person::all_columns.nullable()), @@ -213,7 +208,7 @@ impl JoinView for PostReportView { community: a.2, creator: a.3, post_creator: a.4, - creator_banned_from_community: a.5.is_some(), + creator_banned_from_community: a.5, my_vote: a.6, counts: a.7, resolver: a.8, diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index a832e1118..5fcb73f69 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -34,15 +34,15 @@ use lemmy_db_schema::{ post_saved, }, source::{ - community::{Community, CommunityFollower, CommunityPersonBan}, + community::{Community, CommunityFollower}, person::Person, - person_block::PersonBlock, - post::{Post, PostRead, PostSaved}, + post::Post, }, traits::JoinView, utils::{fuzzy_search, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn}, ListingType, SortType, + SubscribedType, }; use tracing::debug; @@ -50,12 +50,12 @@ type PostViewTuple = ( Post, Person, Community, - Option, + bool, PostAggregates, - Option, - Option, - Option, - Option, + SubscribedType, + bool, + bool, + bool, Option, i64, ); @@ -136,12 +136,12 @@ fn queries<'a>() -> Queries< post::all_columns, person::all_columns, community::all_columns, - community_person_ban::all_columns.nullable(), + community_person_ban::id.nullable().is_not_null(), post_aggregates::all_columns, - community_follower::all_columns.nullable(), - post_saved::all_columns.nullable(), - post_read::all_columns.nullable(), - person_block::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(), post_like::score.nullable(), coalesce( post_aggregates::comments.nullable() - person_post_aggregates::read_comments.nullable(), @@ -242,9 +242,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 @@ -294,7 +292,7 @@ fn queries<'a>() -> Queries< }; if options.saved_only.unwrap_or(false) { - query = query.filter(post_saved::post_id.is_not_null()); + query = query.filter(post_saved::id.is_not_null()); } if options.moderator_view.unwrap_or(false) { @@ -453,12 +451,12 @@ impl JoinView for PostView { post: a.0, creator: a.1, community: a.2, - creator_banned_from_community: a.3.is_some(), + creator_banned_from_community: a.3, counts: a.4, - subscribed: CommunityFollower::to_subscribed_type(&a.5), - saved: a.6.is_some(), - read: a.7.is_some(), - creator_blocked: a.8.is_some(), + subscribed: a.5, + saved: a.6, + read: a.7, + creator_blocked: a.8, my_vote: a.9, unread_comments: a.10, } diff --git a/crates/db_views_actor/src/comment_reply_view.rs b/crates/db_views_actor/src/comment_reply_view.rs index 869345200..ad5b0875d 100644 --- a/crates/db_views_actor/src/comment_reply_view.rs +++ b/crates/db_views_actor/src/comment_reply_view.rs @@ -27,16 +27,16 @@ use lemmy_db_schema::{ post, }, source::{ - comment::{Comment, CommentSaved}, + comment::Comment, comment_reply::CommentReply, - community::{Community, CommunityFollower, CommunityPersonBan}, + community::{Community, CommunityFollower}, person::Person, - person_block::PersonBlock, post::Post, }, traits::JoinView, utils::{get_conn, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn}, CommentSortType, + SubscribedType, }; type CommentReplyViewTuple = ( @@ -47,10 +47,10 @@ type CommentReplyViewTuple = ( Community, Person, CommentAggregates, - Option, - Option, - Option, - Option, + bool, + SubscribedType, + bool, + bool, Option, ); @@ -112,10 +112,10 @@ fn queries<'a>() -> Queries< community::all_columns, aliases::person1.fields(person::all_columns), comment_aggregates::all_columns, - community_person_ban::all_columns.nullable(), - community_follower::all_columns.nullable(), - comment_saved::all_columns.nullable(), - person_block::all_columns.nullable(), + community_person_ban::id.nullable().is_not_null(), + CommunityFollower::select_subscribed_type(), + comment_saved::id.nullable().is_not_null(), + person_block::id.nullable().is_not_null(), comment_like::score.nullable(), )) }; @@ -226,10 +226,10 @@ impl JoinView for CommentReplyView { community: a.4, recipient: a.5, counts: a.6, - creator_banned_from_community: a.7.is_some(), - subscribed: CommunityFollower::to_subscribed_type(&a.8), - saved: a.9.is_some(), - creator_blocked: a.10.is_some(), + creator_banned_from_community: a.7, + 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 9ca5c218c..28a492579 100644 --- a/crates/db_views_actor/src/community_view.rs +++ b/crates/db_views_actor/src/community_view.rs @@ -16,21 +16,16 @@ use lemmy_db_schema::{ schema::{community, community_aggregates, community_block, community_follower, local_user}, source::{ community::{Community, CommunityFollower}, - community_block::CommunityBlock, local_user::LocalUser, }, traits::JoinView, utils::{fuzzy_search, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn}, ListingType, SortType, + SubscribedType, }; -type CommunityViewTuple = ( - Community, - CommunityAggregates, - Option, - Option, -); +type CommunityViewTuple = (Community, CommunityAggregates, SubscribedType, bool); fn queries<'a>() -> Queries< impl ReadFn<'a, CommunityView, (CommunityId, Option, Option)>, @@ -61,8 +56,8 @@ fn queries<'a>() -> Queries< let selection = ( community::all_columns, community_aggregates::all_columns, - community_follower::all_columns.nullable(), - community_block::all_columns.nullable(), + CommunityFollower::select_subscribed_type(), + community_block::id.nullable().is_not_null(), ); let not_removed_or_deleted = community::removed @@ -138,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, }; @@ -217,8 +212,8 @@ impl JoinView for CommunityView { Self { community: a.0, counts: a.1, - subscribed: CommunityFollower::to_subscribed_type(&a.2), - blocked: a.3.is_some(), + 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 6528ab5da..0d8aed279 100644 --- a/crates/db_views_actor/src/person_mention_view.rs +++ b/crates/db_views_actor/src/person_mention_view.rs @@ -28,16 +28,16 @@ use lemmy_db_schema::{ post, }, source::{ - comment::{Comment, CommentSaved}, - community::{Community, CommunityFollower, CommunityPersonBan}, + comment::Comment, + community::{Community, CommunityFollower}, person::Person, - person_block::PersonBlock, person_mention::PersonMention, post::Post, }, traits::JoinView, utils::{get_conn, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn}, CommentSortType, + SubscribedType, }; type PersonMentionViewTuple = ( @@ -48,10 +48,10 @@ type PersonMentionViewTuple = ( Community, Person, CommentAggregates, - Option, - Option, - Option, - Option, + bool, + SubscribedType, + bool, + bool, Option, ); @@ -108,10 +108,10 @@ fn queries<'a>() -> Queries< community::all_columns, aliases::person1.fields(person::all_columns), comment_aggregates::all_columns, - community_person_ban::all_columns.nullable(), - community_follower::all_columns.nullable(), - comment_saved::all_columns.nullable(), - person_block::all_columns.nullable(), + community_person_ban::id.nullable().is_not_null(), + CommunityFollower::select_subscribed_type(), + comment_saved::id.nullable().is_not_null(), + person_block::id.nullable().is_not_null(), comment_like::score.nullable(), ); @@ -243,10 +243,10 @@ impl JoinView for PersonMentionView { community: a.4, recipient: a.5, counts: a.6, - creator_banned_from_community: a.7.is_some(), - subscribed: CommunityFollower::to_subscribed_type(&a.8), - saved: a.9.is_some(), - creator_blocked: a.10.is_some(), + creator_banned_from_community: a.7, + subscribed: a.8, + saved: a.9, + creator_blocked: a.10, my_vote: a.11, } }