core/resolvers/reader.py

447 lines
17 KiB
Python
Raw Normal View History

2024-10-31 18:11:54 +00:00
import json
2024-11-01 06:50:19 +00:00
from sqlalchemy.orm import aliased
from sqlalchemy.sql.expression import and_, asc, case, desc, func, nulls_last, select
from orm.author import Author
2023-12-17 20:30:20 +00:00
from orm.reaction import Reaction, ReactionKind
2024-11-01 06:50:19 +00:00
from orm.shout import Shout, ShoutAuthor, ShoutTopic
from orm.topic import Topic
2023-10-09 21:34:51 +00:00
from services.db import local_session
2023-11-23 23:00:28 +00:00
from services.schema import query
2024-01-29 01:41:46 +00:00
from services.search import search_text
2024-08-07 10:15:58 +00:00
from services.viewed import ViewedStorage
2024-08-09 06:37:06 +00:00
from utils.logger import root_logger as logger
2024-08-07 11:54:13 +00:00
2024-08-08 13:10:45 +00:00
2024-10-31 17:28:52 +00:00
def has_field(info, fieldname: str) -> bool:
2024-11-01 07:04:32 +00:00
"""
Проверяет, запрошено ли поле :fieldname: в GraphQL запросе
:param info: Информация о контексте GraphQL
:param fieldname: Имя запрашиваемого поля
:return: True, если поле запрошено, False в противном случае
"""
2024-10-31 17:28:52 +00:00
field_node = info.field_nodes[0]
for selection in field_node.selection_set.selections:
if hasattr(selection, "name") and selection.name.value == fieldname:
return True
return False
2024-11-01 10:50:47 +00:00
def query_with_stat(info):
2024-11-01 07:04:32 +00:00
"""
2024-11-01 10:50:47 +00:00
Добавляет подзапрос статистики
2024-11-01 07:04:32 +00:00
2024-11-01 10:50:47 +00:00
:param info: Информация о контексте GraphQL
2024-11-01 07:04:32 +00:00
:return: Запрос с подзапросом статистики.
"""
2024-10-31 11:09:33 +00:00
2024-11-01 10:50:47 +00:00
q = select(Shout)
if has_field(info, "stat"):
stats_subquery = (
select(
Reaction.shout,
func.count(case([(Reaction.kind == ReactionKind.COMMENT.value, 1)], else_=None)).label(
"comments_count"
),
func.sum(
case(
[
(Reaction.kind == ReactionKind.LIKE.value, 1),
(Reaction.kind == ReactionKind.DISLIKE.value, -1),
],
else_=0,
)
).label("rating"),
func.max(case([(Reaction.reply_to.is_(None), Reaction.created_at)], else_=None)).label(
"last_reacted_at"
),
)
.where(Reaction.deleted_at.is_(None))
.group_by(Reaction.shout)
.subquery()
)
2024-10-31 10:39:38 +00:00
2024-11-01 10:50:47 +00:00
q = q.outerjoin(stats_subquery, stats_subquery.c.shout == Shout.id)
q = q.add_columns(stats_subquery.c.comments_count, stats_subquery.c.rating, stats_subquery.c.last_reacted_at)
2024-10-31 18:11:54 +00:00
if has_field(info, "created_by"):
2024-11-01 10:50:47 +00:00
q = q.outerjoin(Author, Shout.created_by == Author.id)
q = q.add_columns(
2024-10-31 18:11:54 +00:00
Author.id.label("main_author_id"),
Author.name.label("main_author_name"),
Author.slug.label("main_author_slug"),
Author.pic.label("main_author_pic"),
)
2024-11-01 10:50:47 +00:00
2024-10-31 18:11:54 +00:00
if has_field(info, "main_topic"):
2024-10-31 18:58:24 +00:00
q = q.outerjoin(ShoutTopic, and_(ShoutTopic.shout == Shout.id, ShoutTopic.main.is_(True)))
q = q.outerjoin(Topic, ShoutTopic.topic == Topic.id)
q = q.add_columns(
2024-11-01 10:50:47 +00:00
Topic.id.label("main_topic_id"), Topic.title.label("main_topic_title"), Topic.slug.label("main_topic_slug")
)
if has_field(info, "authors"):
topics_subquery = (
select(
ShoutTopic.shout,
Topic.id.label("topic_id"),
Topic.title.label("topic_title"),
Topic.slug.label("topic_slug"),
ShoutTopic.main.label("is_main"),
)
.outerjoin(Topic, ShoutTopic.topic == Topic.id)
.where(ShoutTopic.shout == Shout.id)
.subquery()
)
q = q.outerjoin(topics_subquery, topics_subquery.c.shout == Shout.id)
q = q.add_columns(
topics_subquery.c.topic_id,
topics_subquery.c.topic_title,
topics_subquery.c.topic_slug,
topics_subquery.c.is_main,
)
authors_subquery = (
select(
ShoutAuthor.shout,
Author.id.label("author_id"),
Author.name.label("author_name"),
Author.slug.label("author_slug"),
Author.pic.label("author_pic"),
ShoutAuthor.caption.label("author_caption"),
)
.outerjoin(Author, ShoutAuthor.author == Author.id)
.where(ShoutAuthor.shout == Shout.id)
.subquery()
)
q = q.outerjoin(authors_subquery, authors_subquery.c.shout == Shout.id)
q = q.add_columns(
authors_subquery.c.author_id,
authors_subquery.c.author_name,
authors_subquery.c.author_slug,
authors_subquery.c.author_pic,
authors_subquery.c.author_caption,
2024-10-31 18:11:54 +00:00
)
2024-10-31 17:34:25 +00:00
2024-11-01 10:50:47 +00:00
# Фильтр опубликованных
q = q.where(and_(Shout.published_at.is_not(None), Shout.deleted_at.is_(None)))
return q
def get_shouts_with_links(info, q, limit=20, offset=0):
"""
получение публикаций с применением пагинации
"""
q = q.limit(limit).offset(offset)
includes_authors = has_field(info, "authors")
includes_topics = has_field(info, "topics")
includes_stat = has_field(info, "stat")
includes_media = has_field(info, "media")
2024-10-31 11:14:54 +00:00
with local_session() as session:
2024-11-01 09:06:23 +00:00
shouts_result = session.execute(q).all()
2024-10-31 16:57:09 +00:00
if not shouts_result:
2024-10-31 16:48:06 +00:00
return []
2024-11-01 10:50:47 +00:00
shouts_result = []
2024-11-01 04:51:03 +00:00
for row in shouts_result:
2024-11-01 10:50:47 +00:00
logger.debug(row)
shout_id = row.Shout.id
2024-11-01 09:06:23 +00:00
shout_dict = row.Shout.dict()
2024-11-01 10:50:47 +00:00
shout_dict.update(
{
"authors": [],
"topics": set(),
"media": json.dumps(shout_dict.get("media", [])) if includes_media else [],
"stat": {
"viewed": ViewedStorage.get_shout(shout_id=shout_id) or 0,
"commented": row.comments_count or 0,
"rating": row.rating or 0,
"last_reacted_at": row.last_reacted_at,
}
if includes_stat and hasattr(row, "comments_count")
else {},
}
)
if includes_authors and hasattr(row, "main_author_id"):
shout_dict["created_by"] = {
2024-11-01 04:51:03 +00:00
"id": row.main_author_id,
"name": row.main_author_name or "Аноним",
"slug": row.main_author_slug or "",
"pic": row.main_author_pic or "",
}
2024-11-01 10:50:47 +00:00
if includes_topics and hasattr(row, "main_topic_id"):
shout_dict["main_topic"] = {
2024-11-01 04:51:03 +00:00
"id": row.main_topic_id or 0,
"title": row.main_topic_title or "",
"slug": row.main_topic_slug or "",
}
2024-11-01 10:50:47 +00:00
if includes_authors and hasattr(row, "author_id"):
2024-10-31 17:34:25 +00:00
author = {
"id": row.author_id,
"name": row.author_name,
"slug": row.author_slug,
"pic": row.author_pic,
"caption": row.author_caption,
}
2024-11-01 10:50:47 +00:00
if not filter(lambda x: x["id"] == author["id"], shout_dict["authors"]):
shout_dict["authors"].append(author)
2024-10-31 17:28:52 +00:00
2024-11-01 10:50:47 +00:00
if includes_topics and hasattr(row, "topic_id"):
2024-10-31 16:48:06 +00:00
topic = {
2024-10-31 17:28:52 +00:00
"id": row.topic_id,
"title": row.topic_title,
"slug": row.topic_slug,
2024-11-01 10:50:47 +00:00
"is_main": row.is_main,
2024-10-31 16:06:58 +00:00
}
2024-11-01 10:50:47 +00:00
if not filter(lambda x: x["id"] == topic["id"], shout_dict["topics"]):
shout_dict["topics"].add(frozenset(topic.items()))
2024-11-01 04:51:03 +00:00
2024-11-01 10:50:47 +00:00
if includes_topics:
shout_dict["topics"] = sorted(
[dict(t) for t in shout_dict["topics"]], key=lambda x: (not x.get("is_main", False), x["id"])
2024-11-01 04:51:03 +00:00
)
2024-10-31 16:06:58 +00:00
2024-11-01 10:50:47 +00:00
return shouts_result
2024-03-25 17:28:58 +00:00
2024-08-08 13:10:45 +00:00
2024-11-01 06:50:19 +00:00
def apply_filters(q, filters):
2024-08-07 08:35:59 +00:00
"""
2024-11-01 06:50:19 +00:00
Применение общих фильтров к запросу.
2024-08-07 08:35:59 +00:00
:param q: Исходный запрос.
:param filters: Словарь фильтров.
:return: Запрос с примененными фильтрами.
"""
2024-03-25 17:41:28 +00:00
if isinstance(filters, dict):
2024-10-24 13:27:16 +00:00
if "featured" in filters:
featured_filter = filters.get("featured")
if featured_filter:
q = q.filter(Shout.featured_at.is_not(None))
else:
q = q.filter(Shout.featured_at.is_(None))
2024-04-17 15:32:23 +00:00
by_layouts = filters.get("layouts")
2024-05-01 02:02:35 +00:00
if by_layouts and isinstance(by_layouts, list):
2024-03-25 17:41:28 +00:00
q = q.filter(Shout.layout.in_(by_layouts))
2024-04-17 15:32:23 +00:00
by_author = filters.get("author")
2024-03-25 17:41:28 +00:00
if by_author:
q = q.filter(Shout.authors.any(slug=by_author))
2024-04-17 15:32:23 +00:00
by_topic = filters.get("topic")
2024-03-25 17:41:28 +00:00
if by_topic:
q = q.filter(Shout.topics.any(slug=by_topic))
2024-04-17 15:32:23 +00:00
by_after = filters.get("after")
2024-03-25 17:41:28 +00:00
if by_after:
ts = int(by_after)
q = q.filter(Shout.created_at > ts)
2022-11-21 08:13:57 +00:00
2022-11-25 18:31:53 +00:00
return q
2024-08-08 13:10:45 +00:00
2024-04-17 15:32:23 +00:00
@query.field("get_shout")
2024-10-31 17:28:52 +00:00
async def get_shout(_, info, slug="", shout_id=0):
2024-08-07 08:35:59 +00:00
"""
2024-10-31 16:06:58 +00:00
Получение публикации по slug или id.
2024-08-07 08:35:59 +00:00
2024-10-31 16:06:58 +00:00
:param _: Корневой объект запроса (не используется)
2024-10-31 18:11:54 +00:00
:param info: Информация о контексте GraphQL
2024-10-31 16:06:58 +00:00
:param slug: Уникальный идентификатор публикации
:param shout_id: ID публикации
:return: Данные публикации с включенной статистикой
2024-08-07 08:35:59 +00:00
"""
2024-04-26 08:43:22 +00:00
try:
2024-10-31 16:06:58 +00:00
# Получаем базовый запрос с подзапросами статистики
2024-11-01 10:50:47 +00:00
q = query_with_stat(info)
2024-10-31 16:06:58 +00:00
# Применяем фильтр по slug или id
if slug:
q = q.where(Shout.slug == slug)
elif shout_id:
q = q.where(Shout.id == shout_id)
else:
return None
2024-08-12 08:00:01 +00:00
2024-10-31 16:06:58 +00:00
# Получаем результат через get_shouts_with_stats с limit=1
2024-10-31 17:28:52 +00:00
shouts = get_shouts_with_links(info, q, limit=1)
2024-08-12 08:00:01 +00:00
2024-10-31 16:06:58 +00:00
# Возвращаем первую (и единственную) публикацию, если она найдена
return shouts[0] if shouts else None
2024-08-08 14:36:20 +00:00
2024-10-31 16:06:58 +00:00
except Exception as exc:
logger.error(f"Error in get_shout: {exc}", exc_info=True)
return None
2022-11-23 21:53:53 +00:00
2024-08-08 13:10:45 +00:00
2024-11-01 06:50:19 +00:00
def apply_sorting(q, options):
2024-11-01 07:04:32 +00:00
"""
Применение сортировки к запросу.
:param q: Исходный запрос.
:param options: Опции фильтрации и сортировки.
:return: Запрос с примененной сортировкой.
"""
2024-11-01 06:50:19 +00:00
# Определение поля для сортировки
order_str = options.get("order_by")
# Проверка, требуется ли сортировка по одному из статистических полей
if order_str in ["rating", "comments_count", "last_reacted_at"]:
# Сортировка по выбранному статистическому полю в указанном порядке
q = q.order_by(desc(order_str))
query_order_by = desc(order_str) if options.get("order_by_desc", True) else asc(order_str)
# Применение сортировки с размещением NULL значений в конце
q = q.order_by(nulls_last(query_order_by))
else:
q = q.order_by(Shout.published_at.desc())
return q
2024-04-17 15:32:23 +00:00
@query.field("load_shouts_by")
2024-10-31 17:28:52 +00:00
async def load_shouts_by(_, info, options):
2022-11-15 02:36:30 +00:00
"""
2024-08-07 08:35:59 +00:00
Загрузка публикаций с фильтрацией, сортировкой и пагинацией.
2024-11-01 07:04:32 +00:00
:param _: Корневой объект запроса (не используется)
:param info: Информация о контексте GraphQL
2024-08-07 08:35:59 +00:00
:param options: Опции фильтрации и сортировки.
:return: Список публикаций, удовлетворяющих критериям.
2022-11-15 02:36:30 +00:00
"""
2024-11-01 06:50:19 +00:00
# Базовый запрос: если запрашиваются статистические данные, используем специальный запрос с статистикой
2024-11-01 10:50:47 +00:00
q = query_with_stat(info)
2023-12-02 06:25:08 +00:00
2024-11-01 06:50:19 +00:00
filters = options.get("filters")
if isinstance(filters, dict):
q = apply_filters(q, filters)
2022-11-25 18:31:53 +00:00
2024-11-01 06:50:19 +00:00
q = apply_sorting(q, options)
2023-12-02 06:25:08 +00:00
2024-11-01 06:50:19 +00:00
# Установка лимита и смещения для пагинации
2024-04-17 15:32:23 +00:00
offset = options.get("offset", 0)
limit = options.get("limit", 10)
2023-12-09 18:15:30 +00:00
2024-11-01 06:50:19 +00:00
# Передача сформированного запроса в метод получения публикаций с учетом сортировки и пагинации
2024-10-31 17:28:52 +00:00
return get_shouts_with_links(info, q, limit, offset)
2023-02-06 14:27:23 +00:00
2024-03-05 13:59:55 +00:00
2024-04-17 15:32:23 +00:00
@query.field("load_shouts_search")
2024-11-01 06:50:19 +00:00
async def load_shouts_search(_, info, text, options):
2024-08-07 08:35:59 +00:00
"""
Поиск публикаций по тексту.
2024-11-01 07:04:32 +00:00
:param _: Корневой объект запроса (не используется)
:param info: Информация о контексте GraphQL
2024-08-07 08:35:59 +00:00
:param text: Строка поиска.
2024-11-01 06:50:19 +00:00
:param options: Опции фильтрации и сортировки.
2024-08-07 08:35:59 +00:00
:return: Список публикаций, найденных по тексту.
"""
2024-11-01 06:50:19 +00:00
limit = options.get("limit", 10)
offset = options.get("offset", 0)
2024-01-29 06:45:00 +00:00
if isinstance(text, str) and len(text) > 2:
2024-01-29 07:37:21 +00:00
results = await search_text(text, limit, offset)
2024-06-02 12:56:17 +00:00
scores = {}
hits_ids = []
2024-06-02 12:32:02 +00:00
for sr in results:
shout_id = sr.get("id")
if shout_id:
2024-06-02 14:36:34 +00:00
shout_id = str(shout_id)
2024-06-02 12:56:17 +00:00
scores[shout_id] = sr.get("score")
hits_ids.append(shout_id)
2024-06-02 16:19:30 +00:00
2024-10-31 17:28:52 +00:00
q = (
2024-11-01 10:50:47 +00:00
query_with_stat(info)
2024-10-31 17:28:52 +00:00
if has_field(info, "stat")
else select(Shout).filter(and_(Shout.published_at.is_not(None), Shout.deleted_at.is_(None)))
)
2024-08-07 09:48:57 +00:00
q = q.filter(Shout.id.in_(hits_ids))
2024-11-01 06:50:19 +00:00
q = apply_filters(q, options)
q = apply_sorting(q, options)
2024-10-31 17:28:52 +00:00
shouts = get_shouts_with_links(info, q, limit, offset)
2024-08-07 08:35:59 +00:00
for shout in shouts:
shout.score = scores[f"{shout.id}"]
shouts.sort(key=lambda x: x.score, reverse=True)
2024-06-02 14:01:22 +00:00
return shouts
2024-01-28 21:28:04 +00:00
return []
2023-12-02 22:22:16 +00:00
2023-12-16 15:24:30 +00:00
2024-04-17 15:32:23 +00:00
@query.field("load_shouts_unrated")
2024-11-01 06:50:19 +00:00
async def load_shouts_unrated(_, info, options):
2024-08-07 08:35:59 +00:00
"""
2024-10-31 16:57:09 +00:00
Загрузка публикаций с менее чем 3 реакциями типа LIKE/DISLIKE
2024-11-01 07:04:32 +00:00
:param _: Корневой объект запроса (не используется)
:param info: Информация о контексте GraphQL
:param options: Опции фильтрации и сортировки.
:return: Список публикаций.
2024-08-07 08:35:59 +00:00
"""
2024-10-31 16:57:09 +00:00
rated_shouts = (
select(Reaction.shout)
2024-10-31 14:47:07 +00:00
.where(
2023-12-16 15:24:30 +00:00
and_(
2024-10-31 17:28:52 +00:00
Reaction.deleted_at.is_(None), Reaction.kind.in_([ReactionKind.LIKE.value, ReactionKind.DISLIKE.value])
2024-10-31 14:47:07 +00:00
)
2024-03-28 12:56:32 +00:00
)
2024-10-31 16:57:09 +00:00
.group_by(Reaction.shout)
2024-10-31 17:28:52 +00:00
.having(func.count("*") >= 3)
2024-10-31 14:47:07 +00:00
.scalar_subquery()
2024-03-28 12:56:32 +00:00
)
2023-12-16 15:24:30 +00:00
2024-10-31 16:57:09 +00:00
q = (
select(Shout)
2024-10-31 17:28:52 +00:00
.where(and_(Shout.published_at.is_not(None), Shout.deleted_at.is_(None), ~Shout.id.in_(rated_shouts)))
2024-10-31 17:42:09 +00:00
# .order_by(desc(Shout.published_at))
.order_by(func.random())
2024-10-31 16:57:09 +00:00
)
2024-11-01 06:50:19 +00:00
limit = options.get("limit", 5)
offset = options.get("offset", 0)
2024-10-31 17:28:52 +00:00
return get_shouts_with_links(info, q, limit, offset)
2023-12-16 15:24:30 +00:00
2024-01-25 19:41:27 +00:00
2024-04-17 15:32:23 +00:00
@query.field("load_shouts_random_top")
2024-10-31 17:28:52 +00:00
async def load_shouts_random_top(_, info, options):
2023-12-16 15:24:30 +00:00
"""
2024-08-07 08:35:59 +00:00
Загрузка случайных публикаций, упорядоченных по топовым реакциям.
:param _info: Информация о контексте GraphQL.
:param options: Опции фильтрации и сортировки.
:return: Список случайных публикаций.
2023-12-16 15:24:30 +00:00
"""
aliased_reaction = aliased(Reaction)
2024-11-01 06:50:19 +00:00
subquery = select(Shout.id).outerjoin(aliased_reaction).where(Shout.deleted_at.is_(None))
2023-12-16 15:24:30 +00:00
2024-11-01 06:50:19 +00:00
filters = options.get("filters")
if isinstance(filters, dict):
subquery = apply_filters(subquery, filters)
2024-03-25 12:03:03 +00:00
2024-01-25 19:41:27 +00:00
subquery = subquery.group_by(Shout.id).order_by(
desc(
2024-01-23 13:04:38 +00:00
func.sum(
case(
2024-08-07 08:35:59 +00:00
# не учитывать реакции на комментарии
2024-03-25 12:31:16 +00:00
(aliased_reaction.reply_to.is_not(None), 0),
2024-03-25 12:03:03 +00:00
(aliased_reaction.kind == ReactionKind.LIKE.value, 1),
(aliased_reaction.kind == ReactionKind.DISLIKE.value, -1),
2024-01-25 19:41:27 +00:00
else_=0,
2024-01-23 13:04:38 +00:00
)
)
2024-03-28 12:56:32 +00:00
)
2024-01-23 13:04:38 +00:00
)
2023-12-16 15:24:30 +00:00
2024-04-17 15:32:23 +00:00
random_limit = options.get("random_limit", 100)
2023-12-17 05:40:05 +00:00
if random_limit:
subquery = subquery.limit(random_limit)
2024-10-31 17:28:52 +00:00
q = (
2024-11-01 10:50:47 +00:00
query_with_stat(info)
2024-10-31 17:28:52 +00:00
if has_field(info, "stat")
else select(Shout).filter(and_(Shout.published_at.is_not(None), Shout.deleted_at.is_(None)))
)
2024-08-07 09:48:57 +00:00
q = q.filter(Shout.id.in_(subquery))
2024-08-07 08:35:59 +00:00
q = q.order_by(func.random())
2024-04-17 15:32:23 +00:00
limit = options.get("limit", 10)
2024-10-31 17:28:52 +00:00
return get_shouts_with_links(info, q, limit)