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::{
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> {
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]

View file

@ -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<CommunityFollower>,
SubscribedType,
bool,
bool,
Option<i16>,
@ -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},

View file

@ -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<CommunityFollower>,
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,

View file

@ -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<CommunityFollower>,
SubscribedType,
bool,
bool,
Option<i16>,
@ -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,

View file

@ -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<CommunityFollower>,
bool,
);
type CommunityViewTuple = (Community, CommunityAggregates, SubscribedType, bool);
fn queries<'a>() -> Queries<
impl ReadFn<'a, CommunityView, (CommunityId, Option<PersonId>, Option<bool>)>,
@ -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,
}
}

View file

@ -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<CommunityFollower>,
SubscribedType,
bool,
bool,
Option<i16>,
@ -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,