CommunityFollower::select_subscribed_type

This commit is contained in:
dull b 2023-07-30 04:40:32 +00:00
parent 2f3f6bb3c9
commit 86a433b0b3
6 changed files with 52 additions and 31 deletions

View file

@ -1,6 +1,6 @@
use crate::{ use crate::{
newtypes::{CommunityId, DbUrl, PersonId}, newtypes::{CommunityId, DbUrl, PersonId},
schema::{community, instance}, schema::{community, community_follower, instance},
source::{ source::{
actor_language::CommunityLanguage, actor_language::CommunityLanguage,
community::{ community::{
@ -19,7 +19,18 @@ use crate::{
utils::{functions::lower, get_conn, DbPool}, utils::{functions::lower, get_conn, DbPool},
SubscribedType, 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; use diesel_async::RunQueryDsl;
#[async_trait] #[async_trait]
@ -228,6 +239,21 @@ impl CommunityFollower {
None => SubscribedType::NotSubscribed, None => SubscribedType::NotSubscribed,
} }
} }
pub fn select_subscribed_type() -> dsl::Nullable<community_follower::pending> {
community_follower::pending.nullable()
}
}
impl Queryable<sql_types::Nullable<sql_types::Bool>, Pg> for SubscribedType {
type Row = Option<bool>;
fn build(row: Self::Row) -> deserialize::Result<Self> {
Ok(match row {
Some(true) => SubscribedType::Pending,
Some(false) => SubscribedType::Subscribed,
None => SubscribedType::NotSubscribed,
})
}
} }
#[async_trait] #[async_trait]

View file

@ -39,6 +39,7 @@ use lemmy_db_schema::{
utils::{fuzzy_search, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn}, utils::{fuzzy_search, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn},
CommentSortType, CommentSortType,
ListingType, ListingType,
SubscribedType,
}; };
type CommentViewTuple = ( type CommentViewTuple = (
@ -48,7 +49,7 @@ type CommentViewTuple = (
Community, Community,
CommentAggregatesNotInComment, CommentAggregatesNotInComment,
bool, bool,
Option<CommunityFollower>, SubscribedType,
bool, bool,
bool, bool,
Option<i16>, Option<i16>,
@ -110,7 +111,7 @@ fn queries<'a>() -> Queries<
community::all_columns, community::all_columns,
CommentAggregatesNotInComment::as_select(), CommentAggregatesNotInComment::as_select(),
community_person_ban::id.nullable().is_not_null(), community_person_ban::id.nullable().is_not_null(),
community_follower::all_columns.nullable(), CommunityFollower::select_subscribed_type(),
comment_saved::id.nullable().is_not_null(), comment_saved::id.nullable().is_not_null(),
person_block::id.nullable().is_not_null(), person_block::id.nullable().is_not_null(),
comment_like::score.nullable(), comment_like::score.nullable(),
@ -171,9 +172,7 @@ fn queries<'a>() -> Queries<
if let Some(listing_type) = options.listing_type { if let Some(listing_type) = options.listing_type {
match listing_type { match listing_type {
ListingType::Subscribed => { ListingType::Subscribed => query = query.filter(community_follower::pending.is_not_null()), // TODO could be this: and(community_follower::person_id.eq(person_id_join)),
query = query.filter(community_follower::person_id.is_not_null())
} // TODO could be this: and(community_follower::person_id.eq(person_id_join)),
ListingType::Local => { ListingType::Local => {
query = query.filter(community::local.eq(true)).filter( query = query.filter(community::local.eq(true)).filter(
community::hidden community::hidden
@ -334,7 +333,7 @@ impl JoinView for CommentView {
community: a.3, community: a.3,
counts, counts,
creator_banned_from_community: a.5, creator_banned_from_community: a.5,
subscribed: CommunityFollower::to_subscribed_type(&a.6), subscribed: a.6,
saved: a.7, saved: a.7,
creator_blocked: a.8, creator_blocked: a.8,
my_vote: a.9, my_vote: a.9,
@ -356,7 +355,6 @@ mod tests {
Community, Community,
DbPool, DbPool,
Person, Person,
PersonBlock,
Post, Post,
}, },
structs::LocalUserView, structs::LocalUserView,
@ -373,7 +371,7 @@ mod tests {
language::Language, language::Language,
local_user::{LocalUser, LocalUserInsertForm}, local_user::{LocalUser, LocalUserInsertForm},
person::PersonInsertForm, person::PersonInsertForm,
person_block::PersonBlockForm, person_block::{PersonBlock, PersonBlockForm},
post::PostInsertForm, post::PostInsertForm,
}, },
traits::{Blockable, Crud, Likeable}, traits::{Blockable, Crud, Likeable},

View file

@ -43,6 +43,7 @@ use lemmy_db_schema::{
utils::{fuzzy_search, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn}, utils::{fuzzy_search, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn},
ListingType, ListingType,
SortType, SortType,
SubscribedType,
}; };
use tracing::debug; use tracing::debug;
@ -52,7 +53,7 @@ type PostViewTuple = (
Community, Community,
bool, bool,
PostAggregatesNotInPost, PostAggregatesNotInPost,
Option<CommunityFollower>, SubscribedType,
bool, bool,
bool, bool,
bool, bool,
@ -138,7 +139,7 @@ fn queries<'a>() -> Queries<
community::all_columns, community::all_columns,
community_person_ban::id.nullable().is_not_null(), community_person_ban::id.nullable().is_not_null(),
PostAggregatesNotInPost::as_select(), PostAggregatesNotInPost::as_select(),
community_follower::all_columns.nullable(), CommunityFollower::select_subscribed_type(),
post_saved::id.nullable().is_not_null(), post_saved::id.nullable().is_not_null(),
post_read::id.nullable().is_not_null(), post_read::id.nullable().is_not_null(),
person_block::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 { if let Some(listing_type) = options.listing_type {
match listing_type { match listing_type {
ListingType::Subscribed => { ListingType::Subscribed => query = query.filter(community_follower::pending.is_not_null()),
query = query.filter(community_follower::person_id.is_not_null())
}
ListingType::Local => { ListingType::Local => {
query = query.filter(community::local.eq(true)).filter( query = query.filter(community::local.eq(true)).filter(
community::hidden community::hidden
@ -447,7 +446,7 @@ impl JoinView for PostView {
community: a.2, community: a.2,
creator_banned_from_community: a.3, creator_banned_from_community: a.3,
counts, counts,
subscribed: CommunityFollower::to_subscribed_type(&a.5), subscribed: a.5,
saved: a.6, saved: a.6,
read: a.7, read: a.7,
creator_blocked: a.8, creator_blocked: a.8,

View file

@ -37,6 +37,7 @@ use lemmy_db_schema::{
traits::JoinView, traits::JoinView,
utils::{get_conn, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn}, utils::{get_conn, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn},
CommentSortType, CommentSortType,
SubscribedType,
}; };
type CommentReplyViewTuple = ( type CommentReplyViewTuple = (
@ -48,7 +49,7 @@ type CommentReplyViewTuple = (
Person, Person,
CommentAggregatesNotInComment, CommentAggregatesNotInComment,
bool, bool,
Option<CommunityFollower>, SubscribedType,
bool, bool,
bool, bool,
Option<i16>, Option<i16>,
@ -113,7 +114,7 @@ fn queries<'a>() -> Queries<
aliases::person1.fields(person::all_columns), aliases::person1.fields(person::all_columns),
CommentAggregatesNotInComment::as_select(), CommentAggregatesNotInComment::as_select(),
community_person_ban::id.nullable().is_not_null(), community_person_ban::id.nullable().is_not_null(),
community_follower::all_columns.nullable(), CommunityFollower::select_subscribed_type(),
comment_saved::id.nullable().is_not_null(), comment_saved::id.nullable().is_not_null(),
person_block::id.nullable().is_not_null(), person_block::id.nullable().is_not_null(),
comment_like::score.nullable(), comment_like::score.nullable(),
@ -228,7 +229,7 @@ impl JoinView for CommentReplyView {
recipient: a.5, recipient: a.5,
counts, counts,
creator_banned_from_community: a.7, creator_banned_from_community: a.7,
subscribed: CommunityFollower::to_subscribed_type(&a.8), subscribed: a.8,
saved: a.9, saved: a.9,
creator_blocked: a.10, creator_blocked: a.10,
my_vote: a.11, my_vote: a.11,

View file

@ -22,14 +22,10 @@ use lemmy_db_schema::{
utils::{fuzzy_search, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn}, utils::{fuzzy_search, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn},
ListingType, ListingType,
SortType, SortType,
SubscribedType,
}; };
type CommunityViewTuple = ( type CommunityViewTuple = (Community, CommunityAggregates, SubscribedType, bool);
Community,
CommunityAggregates,
Option<CommunityFollower>,
bool,
);
fn queries<'a>() -> Queries< fn queries<'a>() -> Queries<
impl ReadFn<'a, CommunityView, (CommunityId, Option<PersonId>, Option<bool>)>, impl ReadFn<'a, CommunityView, (CommunityId, Option<PersonId>, Option<bool>)>,
@ -60,7 +56,7 @@ fn queries<'a>() -> Queries<
let selection = ( let selection = (
community::all_columns, community::all_columns,
community_aggregates::all_columns, community_aggregates::all_columns,
community_follower::all_columns.nullable(), CommunityFollower::select_subscribed_type(),
community_block::id.nullable().is_not_null(), community_block::id.nullable().is_not_null(),
); );
@ -137,7 +133,7 @@ fn queries<'a>() -> Queries<
if let Some(listing_type) = options.listing_type { if let Some(listing_type) = options.listing_type {
query = match 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)), ListingType::Local => query.filter(community::local.eq(true)),
_ => query, _ => query,
}; };
@ -216,7 +212,7 @@ impl JoinView for CommunityView {
Self { Self {
community: a.0, community: a.0,
counts: a.1, counts: a.1,
subscribed: CommunityFollower::to_subscribed_type(&a.2), subscribed: a.2,
blocked: a.3, blocked: a.3,
} }
} }

View file

@ -38,6 +38,7 @@ use lemmy_db_schema::{
traits::JoinView, traits::JoinView,
utils::{get_conn, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn}, utils::{get_conn, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn},
CommentSortType, CommentSortType,
SubscribedType,
}; };
type PersonMentionViewTuple = ( type PersonMentionViewTuple = (
@ -49,7 +50,7 @@ type PersonMentionViewTuple = (
Person, Person,
CommentAggregatesNotInComment, CommentAggregatesNotInComment,
bool, bool,
Option<CommunityFollower>, SubscribedType,
bool, bool,
bool, bool,
Option<i16>, Option<i16>,
@ -109,7 +110,7 @@ fn queries<'a>() -> Queries<
aliases::person1.fields(person::all_columns), aliases::person1.fields(person::all_columns),
CommentAggregatesNotInComment::as_select(), CommentAggregatesNotInComment::as_select(),
community_person_ban::id.nullable().is_not_null(), community_person_ban::id.nullable().is_not_null(),
community_follower::all_columns.nullable(), CommunityFollower::select_subscribed_type(),
comment_saved::id.nullable().is_not_null(), comment_saved::id.nullable().is_not_null(),
person_block::id.nullable().is_not_null(), person_block::id.nullable().is_not_null(),
comment_like::score.nullable(), comment_like::score.nullable(),
@ -245,7 +246,7 @@ impl JoinView for PersonMentionView {
recipient: a.5, recipient: a.5,
counts, counts,
creator_banned_from_community: a.7, creator_banned_from_community: a.7,
subscribed: CommunityFollower::to_subscribed_type(&a.8), subscribed: a.8,
saved: a.9, saved: a.9,
creator_blocked: a.10, creator_blocked: a.10,
my_vote: a.11, my_vote: a.11,