feed-filters-fix
All checks were successful
Deploy on push / deploy (push) Successful in 21s

This commit is contained in:
Untone 2024-03-25 20:28:58 +03:00
parent 337fa82fb4
commit e9611fc8c1

View File

@ -1,5 +1,5 @@
from sqlalchemy import bindparam, distinct, or_, text from sqlalchemy import bindparam, distinct, or_, text
from sqlalchemy.orm import aliased, joinedload, selectinload from sqlalchemy.orm import aliased, joinedload
from sqlalchemy.sql.expression import and_, asc, case, desc, func, nulls_last, select from sqlalchemy.sql.expression import and_, asc, case, desc, func, nulls_last, select
from orm.author import Author, AuthorFollower from orm.author import Author, AuthorFollower
@ -16,8 +16,44 @@ from services.viewed import ViewedStorage
from services.logger import root_logger as logger from services.logger import root_logger as logger
def query_shouts():
return select(Shout).options(joinedload(Shout.authors), joinedload(Shout.topics)).where(
and_(
Shout.published_at.is_not(None),
Shout.deleted_at.is_(None),
)
)
def filter_my(info, session, q):
reader_id = None
user_id = info.context.get('user_id')
if user_id:
reader = session.query(Author).filter(Author.user == user_id).first()
if reader:
reader_followed_authors = select(AuthorFollower.author).where(
AuthorFollower.follower == reader.id
)
reader_followed_topics = select(TopicFollower.topic).where(
TopicFollower.follower == reader.id
)
subquery = (
select(Shout.id)
.where(Shout.id == ShoutAuthor.shout)
.where(Shout.id == ShoutTopic.shout)
.where(
(ShoutAuthor.author.in_(reader_followed_authors))
| (ShoutTopic.topic.in_(reader_followed_topics))
)
)
q = q.filter(Shout.id.in_(subquery))
reader_id = reader.id
return q, reader_id
def apply_filters(q, filters, author_id=None): def apply_filters(q, filters, author_id=None):
if filters.get('reacted') and author_id: if filters.get('reacted'):
q.join(Reaction, Reaction.created_by == author_id) q.join(Reaction, Reaction.created_by == author_id)
by_featured = filters.get('featured') by_featured = filters.get('featured')
@ -43,11 +79,11 @@ def apply_filters(q, filters, author_id=None):
@query.field('get_shout') @query.field('get_shout')
async def get_shout(_, info, slug: str): async def get_shout(_, info, slug: str):
with local_session() as session: with local_session() as session:
q = select(Shout).options(joinedload(Shout.authors), joinedload(Shout.topics)) q = query_shouts()
aliased_reaction = aliased(Reaction) aliased_reaction = aliased(Reaction)
q = add_reaction_stat_columns(q, aliased_reaction) q = add_reaction_stat_columns(q, aliased_reaction)
q = q.filter(Shout.slug == slug) q = q.filter(Shout.slug == slug)
q = q.filter(Shout.deleted_at.is_(None)).group_by(Shout.id) q = q.group_by(Shout.id)
results = session.execute(q).first() results = session.execute(q).first()
if results: if results:
@ -122,11 +158,7 @@ async def load_shouts_by(_, _info, options):
""" """
# base # base
q = ( q = query_shouts()
select(Shout)
.options(joinedload(Shout.authors), joinedload(Shout.topics))
.where(and_(Shout.deleted_at.is_(None), Shout.published_at.is_not(None)))
)
# stats # stats
aliased_reaction = aliased(Reaction) aliased_reaction = aliased(Reaction)
@ -194,97 +226,73 @@ async def load_shouts_by(_, _info, options):
@query.field('load_shouts_feed') @query.field('load_shouts_feed')
@login_required @login_required
async def load_shouts_feed(_, info, options): async def load_shouts_feed(_, info, options):
user_id = info.context['user_id']
shouts = [] shouts = []
with local_session() as session: with local_session() as session:
reader = session.query(Author).filter(Author.user == user_id).first() q = query_shouts()
if reader:
reader_followed_authors = select(AuthorFollower.author).where(
AuthorFollower.follower == reader.id
)
reader_followed_topics = select(TopicFollower.topic).where(
TopicFollower.follower == reader.id
)
subquery = ( aliased_reaction = aliased(Reaction)
select(Shout.id) q = add_reaction_stat_columns(q, aliased_reaction)
.where(Shout.id == ShoutAuthor.shout)
.where(Shout.id == ShoutTopic.shout)
.where(
(ShoutAuthor.author.in_(reader_followed_authors))
| (ShoutTopic.topic.in_(reader_followed_topics))
)
)
q = ( # filters
select(Shout) filters = options.get('filters')
.options(joinedload(Shout.authors), joinedload(Shout.topics)) if filters:
.where( q, reader_id = filter_my(info, session, q)
q = apply_filters(q, filters, reader_id)
# sort order
order_by = options.get(
'order_by',
Shout.featured_at if filters.get('featured') else Shout.published_at,
)
query_order_by = (
desc(order_by) if options.get('order_by_desc', True) else asc(order_by)
)
# pagination
offset = options.get('offset', 0)
limit = options.get('limit', 10)
q = (
q.group_by(Shout.id)
.order_by(nulls_last(query_order_by))
.limit(limit)
.offset(offset)
)
# print(q.compile(compile_kwargs={"literal_binds": True}))
for [
shout,
reacted_stat,
commented_stat,
likes_stat,
dislikes_stat,
last_comment,
] in session.execute(q).unique():
main_topic = (
session.query(Topic.slug)
.join(
ShoutTopic,
and_( and_(
Shout.published_at.is_not(None), ShoutTopic.topic == Topic.id,
Shout.deleted_at.is_(None), ShoutTopic.shout == shout.id,
Shout.id.in_(subquery), ShoutTopic.main.is_(True),
) ),
) )
.first()
) )
aliased_reaction = aliased(Reaction) if main_topic:
q = add_reaction_stat_columns(q, aliased_reaction) shout.main_topic = main_topic[0]
filters = options.get('filters', {}) shout.stat = {
q = apply_filters(q, filters, reader.id) 'viewed': await ViewedStorage.get_shout(shout.slug),
'reacted': reacted_stat,
order_by = options.get( 'commented': commented_stat,
'order_by', 'rating': likes_stat - dislikes_stat,
Shout.featured_at if filters.get('featured') else Shout.published_at, 'last_comment': last_comment
) }
shouts.append(shout)
query_order_by = (
desc(order_by) if options.get('order_by_desc', True) else asc(order_by)
)
offset = options.get('offset', 0)
limit = options.get('limit', 10)
q = (
q.group_by(Shout.id)
.order_by(nulls_last(query_order_by))
.limit(limit)
.offset(offset)
)
# print(q.compile(compile_kwargs={"literal_binds": True}))
for [
shout,
reacted_stat,
commented_stat,
likes_stat,
dislikes_stat,
last_comment,
] in session.execute(q).unique():
main_topic = (
session.query(Topic.slug)
.join(
ShoutTopic,
and_(
ShoutTopic.topic == Topic.id,
ShoutTopic.shout == shout.id,
ShoutTopic.main.is_(True),
),
)
.first()
)
if main_topic:
shout.main_topic = main_topic[0]
shout.stat = {
'viewed': await ViewedStorage.get_shout(shout.slug),
'reacted': reacted_stat,
'commented': commented_stat,
'rating': likes_stat - dislikes_stat,
'last_comment': last_comment
}
shouts.append(shout)
return shouts return shouts
@ -301,10 +309,8 @@ async def load_shouts_search(_, _info, text, limit=50, offset=0):
@login_required @login_required
@query.field('load_shouts_unrated') @query.field('load_shouts_unrated')
async def load_shouts_unrated(_, info, limit: int = 50, offset: int = 0): async def load_shouts_unrated(_, info, limit: int = 50, offset: int = 0):
q = ( q = query_shouts()
select(Shout) q = q.outerjoin(
.options(selectinload(Shout.authors), selectinload(Shout.topics))
.outerjoin(
Reaction, Reaction,
and_( and_(
Reaction.shout == Shout.id, Reaction.shout == Shout.id,
@ -313,16 +319,14 @@ async def load_shouts_unrated(_, info, limit: int = 50, offset: int = 0):
[ReactionKind.LIKE.value, ReactionKind.DISLIKE.value] [ReactionKind.LIKE.value, ReactionKind.DISLIKE.value]
), ),
), ),
) ).outerjoin(Author, Author.user == bindparam('user_id')).where(
.outerjoin(Author, Author.user == bindparam('user_id'))
.where(
and_( and_(
Shout.deleted_at.is_(None), Shout.deleted_at.is_(None),
Shout.layout.is_not(None), Shout.layout.is_not(None),
or_(Author.id.is_(None), Reaction.created_by != Author.id), or_(Author.id.is_(None), Reaction.created_by != Author.id),
) )
) )
)
# 3 or fewer votes is 0, 1, 2 or 3 votes (null, reaction id1, reaction id2, reaction id3) # 3 or fewer votes is 0, 1, 2 or 3 votes (null, reaction id1, reaction id2, reaction id3)
q = q.having(func.count(distinct(Reaction.id)) <= 4) q = q.having(func.count(distinct(Reaction.id)) <= 4)