core/resolvers/reaction.py

590 lines
18 KiB
Python
Raw Normal View History

2023-11-03 10:10:22 +00:00
import time
2024-08-12 08:00:01 +00:00
from sqlalchemy import and_, asc, case, desc, func, select
2024-08-07 11:18:05 +00:00
from sqlalchemy.orm import aliased
2023-12-17 20:30:20 +00:00
from orm.author import Author
2024-04-24 07:42:33 +00:00
from orm.rating import PROPOSAL_REACTIONS, RATING_REACTIONS, is_negative, is_positive
2024-02-19 11:45:55 +00:00
from orm.reaction import Reaction, ReactionKind
2024-11-02 19:34:20 +00:00
from orm.shout import Shout, ShoutAuthor
2024-06-05 15:29:15 +00:00
from resolvers.follower import follow
2024-11-02 10:49:22 +00:00
from resolvers.proposals import handle_proposing
2024-04-19 15:22:07 +00:00
from resolvers.stat import update_author_stat
2024-01-25 19:41:27 +00:00
from services.auth import add_user_role, login_required
2023-10-23 14:51:13 +00:00
from services.db import local_session
2023-12-17 20:30:20 +00:00
from services.notify import notify_reaction
2023-10-23 14:51:13 +00:00
from services.schema import mutation, query
2024-08-12 08:00:01 +00:00
from utils.logger import root_logger as logger
2024-01-13 08:49:12 +00:00
2023-01-17 21:07:44 +00:00
2024-08-09 05:17:40 +00:00
def query_reactions():
"""
Base query for fetching reactions with associated authors and shouts.
:return: Base query.
"""
return (
select(
Reaction,
Author,
Shout,
)
.select_from(Reaction)
.join(Author, Reaction.created_by == Author.id)
.join(Shout, Reaction.shout == Shout.id)
)
def add_reaction_stat_columns(q):
2024-08-07 11:18:05 +00:00
"""
2024-08-09 05:17:40 +00:00
Add statistical columns to a reaction query.
2024-08-07 11:18:05 +00:00
2024-08-09 05:17:40 +00:00
:param q: SQL query for reactions.
:return: Query with added statistics columns.
2024-08-07 11:18:05 +00:00
"""
2024-08-09 05:17:40 +00:00
aliased_reaction = aliased(Reaction)
# Join reactions and add statistical columns
q = q.outerjoin(
aliased_reaction,
and_(
aliased_reaction.reply_to == Reaction.id,
aliased_reaction.deleted_at.is_(None),
),
).add_columns(
# Count unique comments
2024-10-15 08:12:09 +00:00
func.coalesce(
func.count(aliased_reaction.id).filter(aliased_reaction.kind == ReactionKind.COMMENT.value), 0
).label("comments_stat"),
2024-08-09 05:17:40 +00:00
# Calculate rating as the difference between likes and dislikes
2024-08-07 07:22:37 +00:00
func.sum(
case(
(aliased_reaction.kind == ReactionKind.LIKE.value, 1),
(aliased_reaction.kind == ReactionKind.DISLIKE.value, -1),
else_=0,
)
).label("rating_stat"),
2024-08-06 15:53:25 +00:00
)
2024-08-07 07:22:37 +00:00
return q
2024-08-09 05:17:40 +00:00
def get_reactions_with_stat(q, limit, offset):
"""
Execute the reaction query and retrieve reactions with statistics.
:param q: Query with reactions and statistics.
:param limit: Number of reactions to load.
:param offset: Pagination offset.
:return: List of reactions.
"""
q = q.limit(limit).offset(offset)
reactions = []
with local_session() as session:
result_rows = session.execute(q)
for reaction, author, shout, commented_stat, rating_stat in result_rows:
2024-11-02 16:48:43 +00:00
# Пропускаем реакции с отсутствующими shout или author
if not shout or not author:
logger.error(f"Пропущена реакция из-за отсутствия shout или author: {reaction.dict()}")
continue
2024-11-02 10:35:30 +00:00
reaction.created_by = author.dict()
reaction.shout = shout.dict()
2024-08-09 05:17:40 +00:00
reaction.stat = {"rating": rating_stat, "comments": commented_stat}
reactions.append(reaction)
return reactions
2024-08-07 11:18:05 +00:00
def is_featured_author(session, author_id) -> bool:
"""
2024-08-09 05:17:40 +00:00
Check if an author has at least one featured article.
2024-08-07 11:18:05 +00:00
2024-08-09 05:17:40 +00:00
:param session: Database session.
:param author_id: Author ID.
:return: True if the author has a featured article, else False.
2024-08-07 11:18:05 +00:00
"""
2024-08-09 05:17:40 +00:00
return session.query(
session.query(Shout).where(Shout.authors.any(id=author_id)).filter(Shout.featured_at.is_not(None)).exists()
).scalar()
2022-11-13 15:24:29 +00:00
2024-02-02 12:59:22 +00:00
2024-08-07 11:18:05 +00:00
def check_to_feature(session, approver_id, reaction) -> bool:
"""
2024-08-09 05:17:40 +00:00
Make a shout featured if it receives more than 4 votes.
2024-08-07 11:18:05 +00:00
2024-08-09 05:17:40 +00:00
:param session: Database session.
:param approver_id: Approver author ID.
:param reaction: Reaction object.
:return: True if shout should be featured, else False.
2024-08-07 11:18:05 +00:00
"""
2024-01-23 13:04:38 +00:00
if not reaction.reply_to and is_positive(reaction.kind):
2024-08-09 05:17:40 +00:00
approvers = {approver_id}
# Count the number of approvers
reacted_readers = (
session.query(Reaction.created_by)
.filter(Reaction.shout == reaction.shout, is_positive(Reaction.kind), Reaction.deleted_at.is_(None))
.distinct()
)
for reader_id in reacted_readers:
if is_featured_author(session, reader_id):
approvers.add(reader_id)
return len(approvers) > 4
2022-11-13 15:24:29 +00:00
return False
2024-08-07 11:18:05 +00:00
def check_to_unfeature(session, rejecter_id, reaction) -> bool:
"""
2024-08-09 05:17:40 +00:00
Unfeature a shout if 20% of reactions are negative.
2024-08-07 11:18:05 +00:00
2024-08-09 05:17:40 +00:00
:param session: Database session.
:param rejecter_id: Rejecter author ID.
:param reaction: Reaction object.
:return: True if shout should be unfeatured, else False.
2024-08-07 11:18:05 +00:00
"""
2024-01-23 13:04:38 +00:00
if not reaction.reply_to and is_negative(reaction.kind):
2024-08-09 05:17:40 +00:00
total_reactions = (
session.query(Reaction)
.filter(
Reaction.shout == reaction.shout, Reaction.kind.in_(RATING_REACTIONS), Reaction.deleted_at.is_(None)
2024-02-02 12:59:22 +00:00
)
2024-08-09 05:17:40 +00:00
.count()
)
negative_reactions = (
session.query(Reaction)
.filter(Reaction.shout == reaction.shout, is_negative(Reaction.kind), Reaction.deleted_at.is_(None))
.count()
)
return total_reactions > 0 and (negative_reactions / total_reactions) >= 0.2
2022-11-13 15:24:29 +00:00
return False
2024-02-02 12:03:44 +00:00
async def set_featured(session, shout_id):
2024-08-07 11:18:05 +00:00
"""
2024-08-09 05:17:40 +00:00
Feature a shout and update the author's role.
2024-08-07 11:18:05 +00:00
2024-08-09 05:17:40 +00:00
:param session: Database session.
:param shout_id: Shout ID.
2024-08-07 11:18:05 +00:00
"""
2024-08-09 05:17:40 +00:00
s = session.query(Shout).filter(Shout.id == shout_id).first()
if s:
current_time = int(time.time())
s.featured_at = current_time
session.commit()
author = session.query(Author).filter(Author.id == s.created_by).first()
if author:
await add_user_role(str(author.user))
session.add(s)
session.commit()
2022-11-13 15:24:29 +00:00
2024-02-02 12:03:44 +00:00
def set_unfeatured(session, shout_id):
2024-08-07 11:18:05 +00:00
"""
2024-08-09 05:17:40 +00:00
Unfeature a shout.
2024-08-07 11:18:05 +00:00
2024-08-09 05:17:40 +00:00
:param session: Database session.
:param shout_id: Shout ID.
2024-08-07 11:18:05 +00:00
"""
2024-08-09 05:17:40 +00:00
session.query(Shout).filter(Shout.id == shout_id).update({"featured_at": None})
2022-11-13 15:24:29 +00:00
session.commit()
2024-01-23 13:04:38 +00:00
2024-11-02 16:16:52 +00:00
async def _create_reaction(session, shout_id: int, is_author: bool, author_id: int, reaction) -> dict:
2024-08-07 11:18:05 +00:00
"""
2024-08-09 05:17:40 +00:00
Create a new reaction and perform related actions such as updating counters and notification.
2024-08-07 11:18:05 +00:00
2024-08-09 05:17:40 +00:00
:param session: Database session.
:param info: GraphQL context info.
:param shout: Shout object.
:param author_id: Author ID.
:param reaction: Dictionary with reaction data.
:return: Dictionary with created reaction data.
2024-08-07 11:18:05 +00:00
"""
2024-01-23 13:04:38 +00:00
r = Reaction(**reaction)
session.add(r)
session.commit()
2024-01-31 00:09:58 +00:00
rdict = r.dict()
2024-01-23 13:04:38 +00:00
2024-08-09 05:17:40 +00:00
# Update author stat for comments
if r.kind == ReactionKind.COMMENT.value:
2024-06-11 19:46:35 +00:00
update_author_stat(author_id)
2024-04-09 13:43:06 +00:00
2024-08-09 05:17:40 +00:00
# Handle proposal
2024-11-02 10:35:30 +00:00
if r.reply_to and r.kind in PROPOSAL_REACTIONS and is_author:
2024-11-02 16:16:52 +00:00
handle_proposing(r.kind, r.reply_to, shout_id)
2024-11-02 10:44:00 +00:00
2024-08-09 05:17:40 +00:00
# Handle rating
2024-04-09 11:03:50 +00:00
if r.kind in RATING_REACTIONS:
2024-04-19 15:22:07 +00:00
if check_to_unfeature(session, author_id, r):
2024-11-02 16:16:52 +00:00
set_unfeatured(session, shout_id)
2024-04-19 15:22:07 +00:00
elif check_to_feature(session, author_id, r):
2024-11-02 16:16:52 +00:00
await set_featured(session, shout_id)
2024-01-23 13:04:38 +00:00
2024-08-09 05:17:40 +00:00
# Notify creation
2024-04-17 15:32:23 +00:00
await notify_reaction(rdict, "create")
2024-01-23 13:04:38 +00:00
return rdict
2024-01-25 19:41:27 +00:00
2024-04-19 15:22:07 +00:00
def prepare_new_rating(reaction: dict, shout_id: int, session, author_id: int):
2024-08-07 11:18:05 +00:00
"""
2024-08-09 05:17:40 +00:00
Check for the possibility of rating a shout.
2024-08-07 11:18:05 +00:00
2024-08-09 05:17:40 +00:00
:param reaction: Dictionary with reaction data.
:param shout_id: Shout ID.
:param session: Database session.
:param author_id: Author ID.
:return: Dictionary with error or None.
2024-08-07 11:18:05 +00:00
"""
2024-04-17 15:32:23 +00:00
kind = reaction.get("kind")
2024-05-30 04:12:00 +00:00
opposite_kind = ReactionKind.DISLIKE.value if is_positive(kind) else ReactionKind.LIKE.value
2024-02-24 18:45:38 +00:00
2024-08-09 05:17:40 +00:00
existing_ratings = (
session.query(Reaction)
.filter(
2024-02-24 18:45:38 +00:00
Reaction.shout == shout_id,
2024-04-19 15:22:07 +00:00
Reaction.created_by == author_id,
2024-02-24 18:45:38 +00:00
Reaction.kind.in_(RATING_REACTIONS),
2024-08-09 05:17:40 +00:00
Reaction.deleted_at.is_(None),
2024-02-24 18:45:38 +00:00
)
2024-08-09 05:17:40 +00:00
.all()
2024-02-24 18:45:38 +00:00
)
2024-08-09 05:17:40 +00:00
for r in existing_ratings:
if r.kind == kind:
2024-05-04 21:00:58 +00:00
return {"error": "You can't rate the same thing twice"}
2024-08-09 05:17:40 +00:00
if r.kind == opposite_kind:
2024-05-04 21:00:58 +00:00
return {"error": "Remove opposite vote first"}
2024-08-09 05:17:40 +00:00
if shout_id in [r.shout for r in existing_ratings]:
return {"error": "You can't rate your own thing"}
2024-02-24 18:45:38 +00:00
return
2024-04-17 15:32:23 +00:00
@mutation.field("create_reaction")
@login_required
2024-11-02 10:52:03 +00:00
async def create_reaction(_, info, reaction):
2024-08-07 11:18:05 +00:00
"""
2024-08-09 05:17:40 +00:00
Create a new reaction through a GraphQL request.
2024-08-07 11:18:05 +00:00
2024-08-09 05:17:40 +00:00
:param info: GraphQL context info.
:param reaction: Dictionary with reaction data.
:return: Dictionary with created reaction data or error.
2024-08-07 11:18:05 +00:00
"""
2024-11-02 10:52:03 +00:00
reaction_input = reaction
2024-05-18 14:41:04 +00:00
author_dict = info.context.get("author", {})
author_id = author_dict.get("id")
2024-11-02 10:49:22 +00:00
shout_id = int(reaction_input.get("shout", "0"))
2024-11-02 01:24:41 +00:00
2024-11-02 10:49:22 +00:00
logger.debug(f"Creating reaction with data: {reaction_input}")
2024-11-02 01:24:41 +00:00
logger.debug(f"Author ID: {author_id}, Shout ID: {shout_id}")
2024-08-09 05:17:40 +00:00
2025-02-03 21:01:54 +00:00
if not author_id:
return {"error": "Author ID is required to create a reaction."}
if not shout_id:
return {"error": "Shout ID is required to create a reaction."}
2024-01-22 20:54:02 +00:00
try:
with local_session() as session:
2024-11-02 19:34:20 +00:00
authors = session.query(ShoutAuthor.author).filter(ShoutAuthor.shout == shout_id).scalar()
is_author = (
bool(list(filter(lambda x: x == int(author_id), authors))) if isinstance(authors, list) else False
)
reaction_input["created_by"] = author_id
kind = reaction_input.get("kind")
# handle ratings
if kind in RATING_REACTIONS:
logger.debug(f"creating rating reaction: {kind}")
error_result = prepare_new_rating(reaction_input, shout_id, session, author_id)
if error_result:
logger.error(f"Rating preparation error: {error_result}")
return error_result
# handle all reactions
rdict = await _create_reaction(session, shout_id, is_author, author_id, reaction_input)
logger.debug(f"Created reaction result: {rdict}")
# follow if liked
if kind == ReactionKind.LIKE.value:
try:
follow(None, info, "shout", shout_id=shout_id)
except Exception:
pass
2024-11-02 19:38:40 +00:00
shout = session.query(Shout).filter(Shout.id == shout_id).first()
if not shout:
return {"error": "Shout not found"}
2024-11-12 14:56:20 +00:00
rdict["shout"] = shout.dict()
2024-11-02 19:34:20 +00:00
rdict["created_by"] = author_dict
return {"reaction": rdict}
2024-01-22 20:54:02 +00:00
except Exception as e:
2024-11-02 10:44:00 +00:00
import traceback
2024-11-02 10:49:22 +00:00
2024-11-02 10:44:00 +00:00
traceback.print_exc()
2024-04-17 15:32:23 +00:00
logger.error(f"{type(e).__name__}: {e}")
2024-08-09 05:17:40 +00:00
return {"error": "Cannot create reaction."}
2024-04-17 15:32:23 +00:00
@mutation.field("update_reaction")
@login_required
2024-02-16 16:46:57 +00:00
async def update_reaction(_, info, reaction):
2024-08-07 11:18:05 +00:00
"""
2024-08-09 05:17:40 +00:00
Update an existing reaction through a GraphQL request.
2024-08-07 11:18:05 +00:00
2024-08-09 05:17:40 +00:00
:param info: GraphQL context info.
:param reaction: Dictionary with reaction data.
:return: Dictionary with updated reaction data or error.
2024-08-07 11:18:05 +00:00
"""
2024-04-17 15:32:23 +00:00
user_id = info.context.get("user_id")
roles = info.context.get("roles")
rid = reaction.get("id")
2024-08-09 05:17:40 +00:00
if not rid or not user_id or not roles:
return {"error": "Invalid input data"}
del reaction["id"]
with local_session() as session:
try:
reaction_query = query_reactions().filter(Reaction.id == rid)
reaction_query = add_reaction_stat_columns(reaction_query)
2024-10-14 10:10:20 +00:00
reaction_query = reaction_query.group_by(Reaction.id, Author.id, Shout.id)
2022-11-28 08:47:39 +00:00
2024-08-09 05:17:40 +00:00
result = session.execute(reaction_query).unique().first()
if result:
2024-10-14 10:10:20 +00:00
r, author, shout, commented_stat, rating_stat = result
2024-08-09 05:17:40 +00:00
if not r or not author:
return {"error": "Invalid reaction ID or unauthorized"}
if r.created_by != author.id and "editor" not in roles:
return {"error": "Access denied"}
# Update reaction
r.body = reaction.get("body", r.body)
r.updated_at = int(time.time())
Reaction.update(r, reaction)
session.add(r)
session.commit()
r.stat = {
"commented": commented_stat,
"rating": rating_stat,
}
2024-02-16 16:46:57 +00:00
2024-08-09 05:17:40 +00:00
await notify_reaction(r.dict(), "update")
return {"reaction": r}
except Exception as e:
logger.error(f"{type(e).__name__}: {e}")
return {"error": "Cannot update reaction"}
2024-01-25 19:41:27 +00:00
2024-04-17 15:32:23 +00:00
@mutation.field("delete_reaction")
@login_required
2024-02-07 13:41:17 +00:00
async def delete_reaction(_, info, reaction_id: int):
2024-08-07 11:18:05 +00:00
"""
2024-08-09 05:17:40 +00:00
Delete an existing reaction through a GraphQL request.
2024-08-07 11:18:05 +00:00
2024-08-09 05:17:40 +00:00
:param info: GraphQL context info.
:param reaction_id: Reaction ID to delete.
:return: Dictionary with deleted reaction data or error.
2024-08-07 11:18:05 +00:00
"""
2024-04-17 15:32:23 +00:00
user_id = info.context.get("user_id")
2024-04-19 15:22:07 +00:00
author_id = info.context.get("author", {}).get("id")
2024-04-17 15:32:23 +00:00
roles = info.context.get("roles", [])
2024-04-09 13:43:06 +00:00
2024-08-09 05:17:40 +00:00
if not user_id:
return {"error": "Unauthorized"}
with local_session() as session:
try:
author = session.query(Author).filter(Author.user == user_id).one()
r = session.query(Reaction).filter(Reaction.id == reaction_id).one()
if r.created_by != author_id and "editor" not in roles:
return {"error": "Access denied"}
logger.debug(f"{user_id} user removing his #{reaction_id} reaction")
reaction_dict = r.dict()
session.delete(r)
session.commit()
# Update author stat
if r.kind == ReactionKind.COMMENT.value:
update_author_stat(author.id)
await notify_reaction(reaction_dict, "delete")
2024-04-08 18:33:47 +00:00
2024-08-09 05:17:40 +00:00
return {"error": None, "reaction": reaction_dict}
except Exception as e:
logger.error(f"{type(e).__name__}: {e}")
return {"error": "Cannot delete reaction"}
2022-11-28 20:29:02 +00:00
2023-11-29 09:59:00 +00:00
def apply_reaction_filters(by, q):
2024-08-07 11:18:05 +00:00
"""
2024-08-09 05:17:40 +00:00
Apply filters to a reaction query.
2024-08-07 11:18:05 +00:00
2024-08-09 05:17:40 +00:00
:param by: Dictionary with filter parameters.
:param q: SQL query.
:return: Query with applied filters.
2024-08-07 11:18:05 +00:00
"""
2024-08-09 05:17:40 +00:00
shout_slug = by.get("shout")
2024-02-02 22:39:57 +00:00
if shout_slug:
q = q.filter(Shout.slug == shout_slug)
2023-11-29 09:59:00 +00:00
2024-08-09 05:17:40 +00:00
shouts = by.get("shouts")
if shouts:
q = q.filter(Shout.slug.in_(shouts))
2023-11-29 09:59:00 +00:00
2024-08-09 05:17:40 +00:00
created_by = by.get("created_by")
2024-02-02 22:39:57 +00:00
if created_by:
q = q.filter(Author.id == created_by)
2023-11-29 09:59:00 +00:00
2024-08-09 05:17:40 +00:00
author_slug = by.get("author")
2024-07-18 06:05:10 +00:00
if author_slug:
q = q.filter(Author.slug == author_slug)
2024-08-09 05:17:40 +00:00
topic = by.get("topic")
2024-04-09 19:24:47 +00:00
if isinstance(topic, int):
2024-04-09 19:09:26 +00:00
q = q.filter(Shout.topics.any(id=topic))
2023-11-29 09:59:00 +00:00
2024-10-21 07:52:23 +00:00
kinds = by.get("kinds")
if isinstance(kinds, list):
q = q.filter(Reaction.kind.in_(kinds))
2024-07-15 22:06:43 +00:00
2024-10-21 07:52:23 +00:00
if by.get("reply_to"):
q = q.filter(Reaction.reply_to == by.get("reply_to"))
2023-11-29 09:59:00 +00:00
2024-04-17 15:32:23 +00:00
by_search = by.get("search", "")
2023-11-29 09:59:00 +00:00
if len(by_search) > 2:
2024-04-17 15:32:23 +00:00
q = q.filter(Reaction.body.ilike(f"%{by_search}%"))
2023-11-29 09:59:00 +00:00
2024-08-09 05:17:40 +00:00
after = by.get("after")
2024-02-07 16:50:01 +00:00
if isinstance(after, int):
q = q.filter(Reaction.created_at > after)
2023-11-29 09:59:00 +00:00
return q
2024-04-17 15:32:23 +00:00
@query.field("load_reactions_by")
2024-11-18 19:03:11 +00:00
async def load_reactions_by(_, _info, by, limit=50, offset=0):
2022-11-23 02:05:34 +00:00
"""
2024-08-09 05:17:40 +00:00
Load reactions based on specified parameters.
2022-09-03 10:50:14 +00:00
2024-08-09 05:17:40 +00:00
:param info: GraphQL context info.
:param by: Filter parameters.
:param limit: Number of reactions to load.
:param offset: Pagination offset.
:return: List of reactions.
"""
q = query_reactions()
2023-11-29 11:16:09 +00:00
2024-08-09 05:17:40 +00:00
# Add statistics and apply filters
q = add_reaction_stat_columns(q)
2023-11-29 09:59:00 +00:00
q = apply_reaction_filters(by, q)
2024-02-29 12:21:46 +00:00
q = q.where(Reaction.deleted_at.is_(None))
2023-11-29 11:16:09 +00:00
2024-08-09 05:17:40 +00:00
# Group and sort
q = q.group_by(Reaction.id, Author.id, Shout.id)
order_stat = by.get("sort", "").lower()
2024-02-29 12:39:55 +00:00
order_by_stmt = desc(Reaction.created_at)
2024-04-17 15:32:23 +00:00
if order_stat == "oldest":
2024-02-29 12:39:55 +00:00
order_by_stmt = asc(Reaction.created_at)
2024-04-17 15:32:23 +00:00
elif order_stat.endswith("like"):
2024-08-07 11:18:05 +00:00
order_by_stmt = desc("rating_stat")
2024-02-29 12:39:55 +00:00
q = q.order_by(order_by_stmt)
2023-11-29 11:16:09 +00:00
2024-08-09 05:17:40 +00:00
# Retrieve and return reactions
return get_reactions_with_stat(q, limit, offset)
2023-10-23 14:47:11 +00:00
2024-07-22 07:42:41 +00:00
@query.field("load_shout_ratings")
async def load_shout_ratings(_, info, shout: int, limit=100, offset=0):
"""
2024-08-09 05:17:40 +00:00
Load ratings for a specified shout with pagination.
2024-07-22 07:42:41 +00:00
2024-08-09 05:17:40 +00:00
:param info: GraphQL context info.
:param shout: Shout ID.
:param limit: Number of reactions to load.
:param offset: Pagination offset.
:return: List of reactions.
2024-08-07 11:18:05 +00:00
"""
2024-08-09 05:17:40 +00:00
q = query_reactions()
2024-07-22 07:42:41 +00:00
2024-08-09 05:17:40 +00:00
# Filter, group, sort, limit, offset
q = q.filter(
and_(
Reaction.deleted_at.is_(None),
Reaction.shout == shout,
Reaction.kind.in_(RATING_REACTIONS),
)
)
q = q.group_by(Reaction.id, Author.id, Shout.id)
2024-07-22 07:42:41 +00:00
q = q.order_by(desc(Reaction.created_at))
2024-08-09 05:17:40 +00:00
# Retrieve and return reactions
return get_reactions_with_stat(q, limit, offset)
2024-07-22 07:42:41 +00:00
@query.field("load_shout_comments")
async def load_shout_comments(_, info, shout: int, limit=50, offset=0):
"""
2024-08-09 05:17:40 +00:00
Load comments for a specified shout with pagination and statistics.
2024-08-07 11:18:05 +00:00
2024-08-09 05:17:40 +00:00
:param info: GraphQL context info.
:param shout: Shout ID.
:param limit: Number of comments to load.
:param offset: Pagination offset.
:return: List of reactions.
2024-07-22 07:42:41 +00:00
"""
2024-08-09 05:17:40 +00:00
q = query_reactions()
q = add_reaction_stat_columns(q)
# Filter, group, sort, limit, offset
q = q.filter(
and_(
Reaction.deleted_at.is_(None),
Reaction.shout == shout,
Reaction.body.is_not(None),
2024-07-26 13:56:30 +00:00
)
2024-07-22 07:42:41 +00:00
)
2024-07-26 13:56:30 +00:00
q = q.group_by(Reaction.id, Author.id, Shout.id)
2024-07-22 07:42:41 +00:00
q = q.order_by(desc(Reaction.created_at))
2024-08-09 05:17:40 +00:00
# Retrieve and return reactions
return get_reactions_with_stat(q, limit, offset)
2024-07-26 16:04:40 +00:00
2024-08-06 11:34:12 +00:00
2024-07-26 16:04:40 +00:00
@query.field("load_comment_ratings")
async def load_comment_ratings(_, info, comment: int, limit=50, offset=0):
"""
2024-08-09 05:17:40 +00:00
Load ratings for a specified comment with pagination and statistics.
2024-08-07 11:18:05 +00:00
2024-08-09 05:17:40 +00:00
:param info: GraphQL context info.
:param comment: Comment ID.
:param limit: Number of ratings to load.
:param offset: Pagination offset.
:return: List of reactions.
2024-07-26 16:04:40 +00:00
"""
2024-08-09 05:17:40 +00:00
q = query_reactions()
q = add_reaction_stat_columns(q)
# Filter, group, sort, limit, offset
q = q.filter(
and_(
Reaction.deleted_at.is_(None),
Reaction.reply_to == comment,
Reaction.kind == ReactionKind.COMMENT.value,
2024-07-26 16:04:40 +00:00
)
)
q = q.group_by(Reaction.id, Author.id, Shout.id)
q = q.order_by(desc(Reaction.created_at))
2024-08-09 05:17:40 +00:00
# Retrieve and return reactions
return get_reactions_with_stat(q, limit, offset)