feed-fixes
All checks were successful
Deploy on push / deploy (push) Successful in 1m16s

This commit is contained in:
Untone 2024-11-01 11:29:41 +03:00
parent a105372b15
commit f7c41532a5
2 changed files with 61 additions and 68 deletions

View File

@ -80,14 +80,14 @@ __all__ = [
"get_shout", "get_shout",
"load_shouts_by", "load_shouts_by",
"load_shouts_random_top", "load_shouts_random_top",
"load_shouts_search",
"load_shouts_unrated",
# feed # feed
"load_shouts_feed", "load_shouts_feed",
"load_shouts_search",
"load_shouts_followed_by",
"load_shouts_unrated",
"load_shouts_coauthored", "load_shouts_coauthored",
"load_shouts_discussed", "load_shouts_discussed",
"load_shouts_with_topic", "load_shouts_with_topic",
"load_shouts_followed_by",
"load_shouts_authored_by", "load_shouts_authored_by",
# follower # follower
"follow", "follow",

View File

@ -26,7 +26,7 @@ def apply_options(q, options, author_id: int):
filters = options.get("filters") filters = options.get("filters")
if isinstance(filters, dict): if isinstance(filters, dict):
q = apply_filters(q, filters) q = apply_filters(q, filters)
if "reacted" in filters: if author_id and "reacted" in filters:
reacted = filters.get("reacted") reacted = filters.get("reacted")
q = q.join(Reaction, Reaction.shout == Shout.id) q = q.join(Reaction, Reaction.shout == Shout.id)
if reacted: if reacted:
@ -137,19 +137,15 @@ async def load_shouts_discussed(_, info, options):
return get_shouts_with_links(info, q, limit, offset=offset) return get_shouts_with_links(info, q, limit, offset=offset)
# применяется сортировка публикаций по последней реакции def shouts_by_follower(info, follower_id: int, options):
async def reacted_shouts_updates(info, follower_id: int, options) -> List[Shout]:
""" """
Обновляет публикации, на которые подписан автор, с учетом реакций. Загружает публикации, на которые подписан автор.
:param follower_id: Идентификатор подписчика. :param info: Информация о контексте GraphQL.
:param follower_id: Идентификатор автора.
:param options: Опции фильтрации и сортировки. :param options: Опции фильтрации и сортировки.
:return: Список публикаций. :return: Список публикаций.
""" """
shouts: List[Shout] = []
with local_session() as session:
author = session.query(Author).filter(Author.id == follower_id).first()
if author:
# Публикации, где подписчик является автором # Публикации, где подписчик является автором
q1 = ( q1 = (
query_with_stat() query_with_stat()
@ -173,33 +169,9 @@ async def reacted_shouts_updates(info, follower_id: int, options) -> List[Shout]
# извлечение ожидаемой структуры данных # извлечение ожидаемой структуры данных
q, limit, offset = apply_options(combined_query, options, follower_id) q, limit, offset = apply_options(combined_query, options, follower_id)
shouts = get_shouts_with_links(info, q, limit, offset=offset) shouts = get_shouts_with_links(info, q, limit, offset=offset)
return shouts return shouts
@query.field("load_shouts_feed")
@login_required
async def load_shouts_feed(_, info, options) -> List[Shout]:
"""
Загружает публикации, на которые подписан пользователь.
:param info: Информация о контексте GraphQL.
:param options: Опции фильтрации и сортировки.
:return: Список публикаций.
"""
user_id = info.context["user_id"]
with local_session() as session:
author = session.query(Author).filter(Author.user == user_id).first()
if author:
try:
author_id: int = author.dict()["id"]
shouts = await reacted_shouts_updates(info, author_id, options)
return shouts
except Exception as error:
logger.debug(error)
return []
@query.field("load_shouts_followed_by") @query.field("load_shouts_followed_by")
async def load_shouts_followed_by(_, info, slug: str, options) -> List[Shout]: async def load_shouts_followed_by(_, info, slug: str, options) -> List[Shout]:
""" """
@ -213,15 +185,26 @@ async def load_shouts_followed_by(_, info, slug: str, options) -> List[Shout]:
with local_session() as session: with local_session() as session:
author = session.query(Author).filter(Author.slug == slug).first() author = session.query(Author).filter(Author.slug == slug).first()
if author: if author:
try: follower_id = author.dict()["id"]
author_id: int = author.dict()["id"] shouts = shouts_by_follower(info, follower_id, options)
shouts = await reacted_shouts_updates(info, author_id, options)
return shouts return shouts
except Exception as error:
logger.debug(error)
return [] return []
@query.field("load_shouts_feed")
@login_required
async def load_shouts_feed(_, info, options) -> List[Shout]:
"""
Загружает публикации, на которые подписан авторизованный пользователь.
:param info: Информация о контексте GraphQL.
:param options: Опции фильтрации и сортировки.
:return: Список публикаций.
"""
author_id = info.context.get("author", {}).get("id")
return shouts_by_follower(info, author_id, options) if author_id else []
@query.field("load_shouts_authored_by") @query.field("load_shouts_authored_by")
async def load_shouts_authored_by(_, info, slug: str, options) -> List[Shout]: async def load_shouts_authored_by(_, info, slug: str, options) -> List[Shout]:
""" """
@ -232,8 +215,11 @@ async def load_shouts_authored_by(_, info, slug: str, options) -> List[Shout]:
if author: if author:
try: try:
author_id: int = author.dict()["id"] author_id: int = author.dict()["id"]
q = query_with_stat() if has_field(info, "stat") else select(Shout) q = (
q = q.filter(and_(Shout.published_at.is_not(None), Shout.deleted_at.is_(None))) query_with_stat()
if has_field(info, "stat")
else select(Shout).filter(and_(Shout.published_at.is_not(None), Shout.deleted_at.is_(None)))
)
q = q.filter(Shout.authors.any(id=author_id)) q = q.filter(Shout.authors.any(id=author_id))
q, limit, offset = apply_options(q, options, author_id) q, limit, offset = apply_options(q, options, author_id)
shouts = get_shouts_with_links(info, q, limit, offset=offset) shouts = get_shouts_with_links(info, q, limit, offset=offset)
@ -253,7 +239,14 @@ async def load_shouts_with_topic(_, info, slug: str, options) -> List[Shout]:
if topic: if topic:
try: try:
topic_id: int = topic.dict()["id"] topic_id: int = topic.dict()["id"]
shouts = await reacted_shouts_updates(info, topic_id, options) q = (
query_with_stat()
if has_field(info, "stat")
else select(Shout).filter(and_(Shout.published_at.is_not(None), Shout.deleted_at.is_(None)))
)
q = q.filter(Shout.topics.any(id=topic_id))
q, limit, offset = apply_options(q, options)
shouts = get_shouts_with_links(info, q, limit, offset=offset)
return shouts return shouts
except Exception as error: except Exception as error:
logger.debug(error) logger.debug(error)