core/resolvers/reader.py

259 lines
7.8 KiB
Python
Raw Normal View History

2023-11-03 10:10:22 +00:00
import time
2022-11-28 13:37:05 +00:00
from sqlalchemy.orm import joinedload, aliased
2023-10-23 14:47:11 +00:00
from sqlalchemy.sql.expression import desc, asc, select, func, case, and_, nulls_last
from services.auth import login_required
2023-10-09 21:34:51 +00:00
from services.db import local_session
2023-10-23 14:47:11 +00:00
from orm.topic import TopicFollower
2023-01-17 21:07:44 +00:00
from orm.reaction import Reaction, ReactionKind
2023-02-06 14:27:23 +00:00
from orm.shout import Shout, ShoutAuthor, ShoutTopic
2023-10-23 14:47:11 +00:00
from orm.author import AuthorFollower
2023-11-22 16:38:39 +00:00
from services.viewed import ViewedStorage
2022-11-15 02:36:30 +00:00
2022-11-28 20:29:02 +00:00
def add_stat_columns(q):
2023-01-17 21:07:44 +00:00
aliased_reaction = aliased(Reaction)
q = q.outerjoin(aliased_reaction).add_columns(
2023-10-05 18:46:18 +00:00
func.sum(aliased_reaction.id).label("reacted_stat"),
2023-01-17 21:07:44 +00:00
func.sum(
2023-10-05 18:46:18 +00:00
case((aliased_reaction.kind == ReactionKind.COMMENT, 1), else_=0)
).label("commented_stat"),
2023-01-17 21:07:44 +00:00
func.sum(
case(
2023-10-05 18:46:18 +00:00
# do not count comments' reactions
2023-11-03 10:10:22 +00:00
(aliased_reaction.body.is_not(""), 0),
2023-10-05 18:46:18 +00:00
(aliased_reaction.kind == ReactionKind.AGREE, 1),
(aliased_reaction.kind == ReactionKind.DISAGREE, -1),
(aliased_reaction.kind == ReactionKind.PROOF, 1),
(aliased_reaction.kind == ReactionKind.DISPROOF, -1),
(aliased_reaction.kind == ReactionKind.ACCEPT, 1),
(aliased_reaction.kind == ReactionKind.REJECT, -1),
(aliased_reaction.kind == ReactionKind.LIKE, 1),
(aliased_reaction.kind == ReactionKind.DISLIKE, -1),
else_=0,
2023-01-17 21:07:44 +00:00
)
2023-10-05 18:46:18 +00:00
).label("rating_stat"),
func.max(
case(
(aliased_reaction.kind != ReactionKind.COMMENT, None),
2023-11-03 10:10:22 +00:00
else_=aliased_reaction.created_at,
2023-10-05 18:46:18 +00:00
)
).label("last_comment"),
)
2022-11-25 18:31:53 +00:00
2023-01-17 21:07:44 +00:00
return q
2023-10-23 14:47:11 +00:00
def apply_filters(q, filters, author_id=None):
if filters.get("reacted") and author_id:
2023-11-03 10:10:22 +00:00
q.join(Reaction, Reaction.created_by == author_id)
2022-11-28 20:29:02 +00:00
2022-11-26 01:44:40 +00:00
v = filters.get("visibility")
if v == "public":
2022-11-21 08:13:57 +00:00
q = q.filter(Shout.visibility == filters.get("visibility"))
2022-11-26 01:44:40 +00:00
if v == "community":
q = q.filter(Shout.visibility.in_(["public", "community"]))
2022-11-28 20:29:02 +00:00
2022-11-21 08:13:57 +00:00
if filters.get("layout"):
q = q.filter(Shout.layout == filters.get("layout"))
if filters.get("author"):
q = q.filter(Shout.authors.any(slug=filters.get("author")))
if filters.get("topic"):
q = q.filter(Shout.topics.any(slug=filters.get("topic")))
if filters.get("title"):
q = q.filter(Shout.title.ilike(f'%{filters.get("title")}%'))
if filters.get("body"):
q = q.filter(Shout.body.ilike(f'%{filters.get("body")}%s'))
2023-11-03 10:10:22 +00:00
if filters.get("time_ago"):
before = int(time.time()) - int(filters.get("time_ago"))
q = q.filter(Shout.created_at > before)
2022-11-21 08:13:57 +00:00
2022-11-25 18:31:53 +00:00
return q
2023-05-08 17:06:01 +00:00
2023-10-23 14:47:11 +00:00
async def load_shout(_, _info, slug=None, shout_id=None):
2022-11-18 02:19:10 +00:00
with local_session() as session:
q = select(Shout).options(
2022-11-23 21:53:53 +00:00
joinedload(Shout.authors),
joinedload(Shout.topics),
)
2022-11-25 18:31:53 +00:00
q = add_stat_columns(q)
2023-05-08 17:06:01 +00:00
if slug is not None:
2023-10-05 18:46:18 +00:00
q = q.filter(Shout.slug == slug)
2023-05-08 17:06:01 +00:00
if shout_id is not None:
2023-10-05 18:46:18 +00:00
q = q.filter(Shout.id == shout_id)
2023-05-08 17:06:01 +00:00
2023-11-03 10:10:22 +00:00
q = q.filter(Shout.deleted_at.is_(None)).group_by(Shout.id)
2023-01-17 21:07:44 +00:00
2023-10-23 14:47:11 +00:00
try:
2023-10-05 18:46:18 +00:00
[
shout,
2023-11-03 10:10:22 +00:00
viewed_stat,
2023-10-05 18:46:18 +00:00
reacted_stat,
commented_stat,
rating_stat,
2023-10-23 14:47:11 +00:00
_last_comment,
] = session.execute(q).first()
2023-01-17 21:07:44 +00:00
2022-12-03 07:37:45 +00:00
shout.stat = {
2023-11-03 10:10:22 +00:00
"viewed": viewed_stat,
2022-12-03 07:37:45 +00:00
"reacted": reacted_stat,
"commented": commented_stat,
2023-10-05 18:46:18 +00:00
"rating": rating_stat,
2022-12-03 07:37:45 +00:00
}
2023-10-05 18:46:18 +00:00
for author_caption in (
session.query(ShoutAuthor).join(Shout).where(Shout.slug == slug)
):
2022-12-03 07:37:45 +00:00
for author in shout.authors:
2023-10-23 14:47:11 +00:00
if author.id == author_caption.author:
2022-12-03 07:37:45 +00:00
author.caption = author_caption.caption
return shout
2023-10-23 14:47:11 +00:00
except Exception:
2023-11-22 16:38:39 +00:00
return None
2022-11-23 21:53:53 +00:00
async def load_shouts_by(_, info, options):
2022-11-15 02:36:30 +00:00
"""
2023-10-23 14:47:11 +00:00
:param _:
:param info:GraphQLInfo
:param options: {
filters: {
2023-10-23 14:47:11 +00:00
layout: 'audio',
visibility: "public",
author: 'discours',
topic: 'culture',
title: 'something',
body: 'something else',
days: 30
}
offset: 0
limit: 50
2023-11-03 10:10:22 +00:00
order_by: 'created_at' | 'commented' | 'reacted' | 'rating'
2022-11-21 08:13:57 +00:00
order_by_desc: true
2022-11-15 02:36:30 +00:00
}
:return: Shout[]
"""
2023-10-05 18:46:18 +00:00
q = (
select(Shout)
.options(
joinedload(Shout.authors),
joinedload(Shout.topics),
2023-10-01 17:27:08 +00:00
)
2023-11-03 10:10:22 +00:00
.where(Shout.deleted_at.is_(None))
2022-11-15 02:36:30 +00:00
)
2022-11-25 18:31:53 +00:00
q = add_stat_columns(q)
2023-10-23 14:47:11 +00:00
author_id = info.context["author_id"]
q = apply_filters(q, options.get("filters", {}), author_id)
2022-11-25 18:31:53 +00:00
2023-11-03 10:10:22 +00:00
order_by = options.get("order_by", Shout.published_at)
2022-11-25 18:31:53 +00:00
2023-10-05 18:46:18 +00:00
query_order_by = (
desc(order_by) if options.get("order_by_desc", True) else asc(order_by)
)
2022-11-21 04:47:59 +00:00
offset = options.get("offset", 0)
limit = options.get("limit", 10)
2022-11-25 18:31:53 +00:00
2023-10-05 18:46:18 +00:00
q = (
q.group_by(Shout.id)
.order_by(nulls_last(query_order_by))
.limit(limit)
.offset(offset)
)
2023-07-05 14:08:17 +00:00
shouts = []
2023-10-23 14:47:11 +00:00
shouts_map = {}
with local_session() as session:
2023-10-05 18:46:18 +00:00
for [
shout,
2023-11-03 10:10:22 +00:00
viewed_stat,
2023-10-05 18:46:18 +00:00
reacted_stat,
commented_stat,
rating_stat,
2023-10-23 14:47:11 +00:00
_last_comment,
2023-10-05 18:46:18 +00:00
] in session.execute(q).unique():
2022-11-25 18:31:53 +00:00
shouts.append(shout)
shout.stat = {
2023-11-03 10:10:22 +00:00
"viewed": viewed_stat,
2022-11-25 18:31:53 +00:00
"reacted": reacted_stat,
"commented": commented_stat,
2023-10-05 18:46:18 +00:00
"rating": rating_stat,
2022-11-25 18:31:53 +00:00
}
2023-01-17 21:07:44 +00:00
shouts_map[shout.id] = shout
return shouts
2023-02-06 14:27:23 +00:00
2023-05-08 17:06:01 +00:00
2023-02-06 14:27:23 +00:00
@login_required
2023-02-16 10:08:55 +00:00
async def get_my_feed(_, info, options):
2023-10-23 14:47:11 +00:00
author_id = info.context["author_id"]
with local_session() as session:
2023-11-03 10:10:22 +00:00
author_followed_authors = select(AuthorFollower.author).where(AuthorFollower.follower == author_id)
author_followed_topics = select(TopicFollower.topic).where(TopicFollower.follower == author_id)
2023-10-23 14:47:11 +00:00
subquery = (
select(Shout.id)
2023-11-03 10:10:22 +00:00
.where(Shout.id == ShoutAuthor.shout)
.where(Shout.id == ShoutTopic.shout)
.where(
(ShoutAuthor.author.in_(author_followed_authors))
| (ShoutTopic.topic.in_(author_followed_topics))
)
2023-10-05 18:46:18 +00:00
)
2023-10-23 14:47:11 +00:00
q = (
select(Shout)
.options(
joinedload(Shout.authors),
joinedload(Shout.topics),
)
.where(
and_(
2023-11-03 10:10:22 +00:00
Shout.published_at.is_not(None),
Shout.deleted_at.is_(None),
2023-10-23 14:47:11 +00:00
Shout.id.in_(subquery),
)
2023-10-05 18:46:18 +00:00
)
2023-07-05 14:08:17 +00:00
)
2023-02-06 14:27:23 +00:00
2023-10-23 14:47:11 +00:00
q = add_stat_columns(q)
q = apply_filters(q, options.get("filters", {}), author_id)
2023-02-16 10:08:55 +00:00
2023-11-03 10:10:22 +00:00
order_by = options.get("order_by", Shout.published_at)
2023-02-16 10:08:55 +00:00
2023-10-23 14:47:11 +00:00
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)
)
2023-02-16 10:08:55 +00:00
2023-10-23 14:47:11 +00:00
shouts = []
2023-10-05 18:46:18 +00:00
for [
shout,
reacted_stat,
commented_stat,
rating_stat,
2023-10-23 14:47:11 +00:00
_last_comment,
2023-10-05 18:46:18 +00:00
] in session.execute(q).unique():
2023-02-06 14:27:23 +00:00
shout.stat = {
2023-11-03 10:10:22 +00:00
"viewed": ViewedStorage.get_shout(shout.slug),
2023-02-06 14:27:23 +00:00
"reacted": reacted_stat,
"commented": commented_stat,
2023-10-05 18:46:18 +00:00
"rating": rating_stat,
2023-02-06 14:27:23 +00:00
}
2023-11-03 10:10:22 +00:00
shouts.append(shout)
2023-02-16 10:08:55 +00:00
return shouts