2023-12-17 20:30:20 +00:00
|
|
|
from sqlalchemy import bindparam, distinct, or_
|
2023-12-02 22:14:36 +00:00
|
|
|
from sqlalchemy.orm import aliased, joinedload
|
|
|
|
from sqlalchemy.sql.expression import and_, asc, case, desc, func, nulls_last, select
|
|
|
|
from starlette.exceptions import HTTPException
|
2023-10-23 14:47:11 +00:00
|
|
|
|
2023-12-17 20:30:20 +00:00
|
|
|
from orm.author import Author, AuthorFollower
|
|
|
|
from orm.reaction import Reaction, ReactionKind
|
|
|
|
from orm.shout import Shout, ShoutAuthor, ShoutTopic, ShoutVisibility
|
|
|
|
from orm.topic import Topic, TopicFollower
|
2023-10-23 14:47:11 +00:00
|
|
|
from services.auth import login_required
|
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
|
2023-12-02 22:22:16 +00:00
|
|
|
from services.search import SearchService
|
2023-11-22 16:38:39 +00:00
|
|
|
from services.viewed import ViewedStorage
|
2023-12-22 18:08:37 +00:00
|
|
|
from resolvers.topic import get_random_topic
|
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)
|
2023-12-02 22:14:36 +00:00
|
|
|
|
2023-01-17 21:07:44 +00:00
|
|
|
q = q.outerjoin(aliased_reaction).add_columns(
|
2023-10-05 18:46:18 +00:00
|
|
|
func.sum(aliased_reaction.id).label("reacted_stat"),
|
2023-12-02 20:38:28 +00:00
|
|
|
func.sum(case((aliased_reaction.kind == ReactionKind.COMMENT.value, 1), else_=0)).label("commented_stat"),
|
2023-12-17 05:40:05 +00:00
|
|
|
get_rating_func(aliased_reaction).label("rating_stat"),
|
2023-10-05 18:46:18 +00:00
|
|
|
func.max(
|
|
|
|
case(
|
2023-12-02 22:14:36 +00:00
|
|
|
(aliased_reaction.kind != ReactionKind.COMMENT.value, 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
|
2022-11-24 17:19:43 +00:00
|
|
|
|
|
|
|
|
2023-12-02 22:14:36 +00:00
|
|
|
def apply_filters(q, filters, author_id=None): # noqa: C901
|
2023-10-23 14:47:11 +00:00
|
|
|
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
|
|
|
|
2023-11-29 12:11:05 +00:00
|
|
|
by_published = filters.get("published")
|
|
|
|
if by_published:
|
2023-11-30 07:38:41 +00:00
|
|
|
q = q.filter(Shout.visibility == ShoutVisibility.PUBLIC.value)
|
2023-11-29 09:29:09 +00:00
|
|
|
by_layouts = filters.get("layouts")
|
|
|
|
if by_layouts:
|
|
|
|
q = q.filter(Shout.layout.in_(by_layouts))
|
|
|
|
by_author = filters.get("author")
|
|
|
|
if by_author:
|
|
|
|
q = q.filter(Shout.authors.any(slug=by_author))
|
|
|
|
by_topic = filters.get("topic")
|
|
|
|
if by_topic:
|
|
|
|
q = q.filter(Shout.topics.any(slug=by_topic))
|
|
|
|
by_after = filters.get("after")
|
|
|
|
if by_after:
|
|
|
|
ts = int(by_after)
|
2023-11-29 07:23:41 +00:00
|
|
|
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
|
|
|
|
|
2023-05-08 17:06:01 +00:00
|
|
|
|
2023-11-28 07:53:48 +00:00
|
|
|
@query.field("get_shout")
|
|
|
|
async def get_shout(_, _info, slug=None, shout_id=None):
|
2022-11-18 02:19:10 +00:00
|
|
|
with local_session() as session:
|
2022-11-24 17:19:43 +00:00
|
|
|
q = select(Shout).options(
|
2022-11-23 21:53:53 +00:00
|
|
|
joinedload(Shout.authors),
|
|
|
|
joinedload(Shout.topics),
|
2022-11-24 17:19:43 +00:00
|
|
|
)
|
2023-12-02 22:14:36 +00:00
|
|
|
|
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-12-02 22:14:36 +00:00
|
|
|
[shout, reacted_stat, commented_stat, rating_stat, _last_comment] = session.execute(q).first()
|
2023-11-27 17:35:26 +00:00
|
|
|
|
2023-12-02 22:14:36 +00:00
|
|
|
shout.stat = {
|
2023-12-02 22:42:16 +00:00
|
|
|
"viewed": await ViewedStorage.get_shout(shout.slug),
|
2023-12-02 22:14:36 +00:00
|
|
|
"reacted": reacted_stat,
|
|
|
|
"commented": commented_stat,
|
|
|
|
"rating": rating_stat,
|
|
|
|
}
|
2023-11-29 11:24:59 +00:00
|
|
|
|
2023-12-02 22:14:36 +00:00
|
|
|
for author_caption in session.query(ShoutAuthor).join(Shout).where(Shout.slug == slug):
|
|
|
|
for author in shout.authors:
|
2023-12-02 22:42:16 +00:00
|
|
|
if author.id == author_caption.author:
|
2023-12-02 22:14:36 +00:00
|
|
|
author.caption = author_caption.caption
|
2023-12-09 18:15:30 +00:00
|
|
|
main_topic = (
|
2023-12-09 18:21:38 +00:00
|
|
|
session.query(Topic.slug)
|
2023-12-16 15:24:30 +00:00
|
|
|
.join(
|
|
|
|
ShoutTopic,
|
|
|
|
and_(ShoutTopic.topic == Topic.id, ShoutTopic.shout == shout.id, ShoutTopic.main == True),
|
|
|
|
)
|
2023-12-09 18:15:30 +00:00
|
|
|
.first()
|
|
|
|
)
|
|
|
|
|
|
|
|
if main_topic:
|
|
|
|
shout.main_topic = main_topic[0]
|
2023-12-02 22:14:36 +00:00
|
|
|
return shout
|
|
|
|
except Exception:
|
2023-12-02 22:42:16 +00:00
|
|
|
raise HTTPException(status_code=404, detail=f"shout {slug or shout_id} not found")
|
2022-11-23 21:53:53 +00:00
|
|
|
|
|
|
|
|
2023-11-28 07:53:48 +00:00
|
|
|
@query.field("load_shouts_by")
|
2023-12-02 22:14:36 +00:00
|
|
|
async def load_shouts_by(_, _info, options):
|
2022-11-15 02:36:30 +00:00
|
|
|
"""
|
2022-11-18 00:38:29 +00:00
|
|
|
:param options: {
|
|
|
|
filters: {
|
2023-11-28 11:17:21 +00:00
|
|
|
layouts: ['audio', 'video', ..],
|
2023-11-29 09:33:33 +00:00
|
|
|
reacted: True,
|
2023-11-29 12:11:05 +00:00
|
|
|
published: True, // filter published-only
|
2022-11-18 00:38:29 +00:00
|
|
|
author: 'discours',
|
|
|
|
topic: 'culture',
|
2023-11-29 07:23:41 +00:00
|
|
|
after: 1234567 // unixtime
|
2022-11-18 00:38:29 +00:00
|
|
|
}
|
|
|
|
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-18 00:38:29 +00:00
|
|
|
|
2022-11-15 02:36:30 +00:00
|
|
|
}
|
|
|
|
:return: Shout[]
|
|
|
|
"""
|
2023-12-09 16:22:47 +00:00
|
|
|
|
2023-12-02 22:14:36 +00:00
|
|
|
# base
|
2023-10-05 18:46:18 +00:00
|
|
|
q = (
|
2023-12-02 22:14:36 +00:00
|
|
|
select(Shout)
|
2023-10-05 18:46:18 +00:00
|
|
|
.options(
|
2023-12-02 22:14:36 +00:00
|
|
|
joinedload(Shout.authors),
|
2023-10-05 18:46:18 +00:00
|
|
|
joinedload(Shout.topics),
|
2023-10-01 17:27:08 +00:00
|
|
|
)
|
2023-12-02 22:14:36 +00:00
|
|
|
.where(and_(Shout.deleted_at.is_(None), Shout.layout.is_not(None)))
|
2022-11-15 02:36:30 +00:00
|
|
|
)
|
2022-11-25 18:31:53 +00:00
|
|
|
|
2023-12-02 22:14:36 +00:00
|
|
|
# stats
|
2022-11-25 18:31:53 +00:00
|
|
|
q = add_stat_columns(q)
|
2023-12-02 06:25:08 +00:00
|
|
|
|
|
|
|
# filters
|
2023-11-29 09:29:09 +00:00
|
|
|
q = apply_filters(q, options.get("filters", {}))
|
2022-11-25 18:31:53 +00:00
|
|
|
|
2023-12-02 06:25:08 +00:00
|
|
|
# group
|
2023-12-02 22:14:36 +00:00
|
|
|
q = q.group_by(Shout.id)
|
2023-12-02 06:25:08 +00:00
|
|
|
|
|
|
|
# order
|
2023-11-03 10:10:22 +00:00
|
|
|
order_by = options.get("order_by", Shout.published_at)
|
2023-11-23 20:30:00 +00:00
|
|
|
query_order_by = desc(order_by) if options.get("order_by_desc", True) else asc(order_by)
|
2023-12-02 06:25:08 +00:00
|
|
|
q = q.order_by(nulls_last(query_order_by))
|
|
|
|
|
|
|
|
# limit offset
|
2022-11-21 04:47:59 +00:00
|
|
|
offset = options.get("offset", 0)
|
|
|
|
limit = options.get("limit", 10)
|
2023-12-02 06:25:08 +00:00
|
|
|
q = q.limit(limit).offset(offset)
|
2022-11-18 00:38:29 +00:00
|
|
|
|
2023-07-05 14:08:17 +00:00
|
|
|
shouts = []
|
2022-11-18 00:38:29 +00:00
|
|
|
with local_session() as session:
|
2023-12-02 20:38:28 +00:00
|
|
|
for [shout, reacted_stat, commented_stat, rating_stat, _last_comment] in session.execute(q).unique():
|
2023-12-09 18:15:30 +00:00
|
|
|
main_topic = (
|
2023-12-09 18:21:38 +00:00
|
|
|
session.query(Topic.slug)
|
2023-12-16 15:24:30 +00:00
|
|
|
.join(
|
|
|
|
ShoutTopic,
|
2023-12-17 20:30:20 +00:00
|
|
|
and_(
|
|
|
|
ShoutTopic.topic == Topic.id, ShoutTopic.shout == shout.id, ShoutTopic.main == True
|
|
|
|
), # noqa: E712
|
2023-12-16 15:24:30 +00:00
|
|
|
)
|
2023-12-09 18:15:30 +00:00
|
|
|
.first()
|
|
|
|
)
|
|
|
|
|
|
|
|
if main_topic:
|
|
|
|
shout.main_topic = main_topic[0]
|
2022-11-25 18:31:53 +00:00
|
|
|
shout.stat = {
|
2023-12-02 22:42:16 +00:00
|
|
|
"viewed": await ViewedStorage.get_shout(shout.slug),
|
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-12-02 20:17:26 +00:00
|
|
|
shouts.append(shout)
|
2023-01-17 21:07:44 +00:00
|
|
|
|
2022-11-18 00:38:29 +00:00
|
|
|
return shouts
|
2023-02-06 14:27:23 +00:00
|
|
|
|
2023-05-08 17:06:01 +00:00
|
|
|
|
2023-12-02 22:14:36 +00:00
|
|
|
@query.field("load_shouts_drafts")
|
2023-02-06 14:27:23 +00:00
|
|
|
@login_required
|
2023-12-02 22:14:36 +00:00
|
|
|
async def load_shouts_drafts(_, info):
|
|
|
|
user_id = info.context["user_id"]
|
|
|
|
|
|
|
|
q = (
|
|
|
|
select(Shout)
|
|
|
|
.options(
|
|
|
|
joinedload(Shout.authors),
|
|
|
|
joinedload(Shout.topics),
|
|
|
|
)
|
|
|
|
.filter(Shout.deleted_at.is_(None))
|
2023-12-02 22:22:16 +00:00
|
|
|
.filter(Shout.visibility == ShoutVisibility.AUTHORS.value)
|
2023-12-02 22:14:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
shouts = []
|
|
|
|
with local_session() as session:
|
|
|
|
reader = session.query(Author).filter(Author.user == user_id).first()
|
|
|
|
if reader:
|
|
|
|
q = q.filter(Shout.created_by == reader.id)
|
|
|
|
q = q.group_by(Shout.id)
|
|
|
|
for [shout] in session.execute(q).unique():
|
2023-12-09 18:15:30 +00:00
|
|
|
main_topic = (
|
2023-12-09 18:21:38 +00:00
|
|
|
session.query(Topic.slug)
|
2023-12-16 15:24:30 +00:00
|
|
|
.join(
|
|
|
|
ShoutTopic,
|
|
|
|
and_(ShoutTopic.topic == Topic.id, ShoutTopic.shout == shout.id, ShoutTopic.main == True),
|
|
|
|
)
|
2023-12-09 18:15:30 +00:00
|
|
|
.first()
|
|
|
|
)
|
|
|
|
|
|
|
|
if main_topic:
|
|
|
|
shout.main_topic = main_topic[0]
|
2023-12-02 22:14:36 +00:00
|
|
|
shouts.append(shout)
|
|
|
|
|
|
|
|
return shouts
|
|
|
|
|
|
|
|
|
2023-11-28 07:53:48 +00:00
|
|
|
@query.field("load_shouts_feed")
|
2023-12-02 22:14:36 +00:00
|
|
|
@login_required
|
2023-11-28 07:53:48 +00:00
|
|
|
async def load_shouts_feed(_, info, options):
|
2023-11-23 23:00:28 +00:00
|
|
|
user_id = info.context["user_id"]
|
2023-12-02 22:14:36 +00:00
|
|
|
|
2023-10-23 14:47:11 +00:00
|
|
|
with local_session() as session:
|
2023-12-02 22:14:36 +00:00
|
|
|
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)
|
2023-11-27 18:18:52 +00:00
|
|
|
|
|
|
|
subquery = (
|
|
|
|
select(Shout.id)
|
|
|
|
.where(Shout.id == ShoutAuthor.shout)
|
|
|
|
.where(Shout.id == ShoutTopic.shout)
|
2023-12-16 15:24:30 +00:00
|
|
|
.where((ShoutAuthor.user.in_(reader_followed_authors)) | (ShoutTopic.topic.in_(reader_followed_topics)))
|
2023-10-23 14:47:11 +00:00
|
|
|
)
|
2023-11-27 18:18:52 +00:00
|
|
|
|
|
|
|
q = (
|
|
|
|
select(Shout)
|
|
|
|
.options(
|
|
|
|
joinedload(Shout.authors),
|
|
|
|
joinedload(Shout.topics),
|
|
|
|
)
|
2023-12-16 15:24:30 +00:00
|
|
|
.where(and_(Shout.published_at.is_not(None), Shout.deleted_at.is_(None), Shout.id.in_(subquery)))
|
2023-10-05 18:46:18 +00:00
|
|
|
)
|
2023-02-06 14:27:23 +00:00
|
|
|
|
2023-11-27 18:18:52 +00:00
|
|
|
q = add_stat_columns(q)
|
2023-12-02 22:14:36 +00:00
|
|
|
q = apply_filters(q, options.get("filters", {}), reader.id)
|
2023-02-16 10:08:55 +00:00
|
|
|
|
2023-11-27 18:18:52 +00:00
|
|
|
order_by = options.get("order_by", Shout.published_at)
|
2023-02-16 10:08:55 +00:00
|
|
|
|
2023-11-27 18:18:52 +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)
|
2023-10-23 14:47:11 +00:00
|
|
|
|
2023-11-27 18:18:52 +00:00
|
|
|
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-12-02 22:14:36 +00:00
|
|
|
# print(q.compile(compile_kwargs={"literal_binds": True}))
|
|
|
|
|
2023-11-27 18:18:52 +00:00
|
|
|
shouts = []
|
2023-12-02 20:44:36 +00:00
|
|
|
for [shout, reacted_stat, commented_stat, rating_stat, _last_comment] in session.execute(q).unique():
|
2023-12-09 18:15:30 +00:00
|
|
|
main_topic = (
|
2023-12-09 18:21:38 +00:00
|
|
|
session.query(Topic.slug)
|
2023-12-16 15:24:30 +00:00
|
|
|
.join(
|
|
|
|
ShoutTopic,
|
|
|
|
and_(ShoutTopic.topic == Topic.id, ShoutTopic.shout == shout.id, ShoutTopic.main == True),
|
|
|
|
)
|
2023-12-09 18:15:30 +00:00
|
|
|
.first()
|
|
|
|
)
|
|
|
|
|
|
|
|
if main_topic:
|
|
|
|
shout.main_topic = main_topic[0]
|
2023-11-27 18:18:52 +00:00
|
|
|
shout.stat = {
|
2023-12-02 22:42:16 +00:00
|
|
|
"viewed": await ViewedStorage.get_shout(shout.slug),
|
2023-11-27 18:18:52 +00:00
|
|
|
"reacted": reacted_stat,
|
|
|
|
"commented": commented_stat,
|
|
|
|
"rating": rating_stat,
|
|
|
|
}
|
|
|
|
shouts.append(shout)
|
2023-12-02 20:44:36 +00:00
|
|
|
|
2023-12-02 22:14:36 +00:00
|
|
|
return shouts
|
2023-12-02 22:22:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
@query.field("load_shouts_search")
|
|
|
|
async def load_shouts_search(_, _info, text, limit=50, offset=0):
|
|
|
|
if text and len(text) > 2:
|
2023-12-19 12:32:34 +00:00
|
|
|
return await SearchService.search(text, limit, offset)
|
2023-12-02 22:22:16 +00:00
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2023-12-16 15:24:30 +00:00
|
|
|
|
2023-12-17 05:16:08 +00:00
|
|
|
@login_required
|
2023-12-16 15:24:30 +00:00
|
|
|
@query.field("load_shouts_unrated")
|
2023-12-17 05:16:08 +00:00
|
|
|
async def load_shouts_unrated(_, info, options):
|
2023-12-16 15:24:30 +00:00
|
|
|
q = (
|
|
|
|
select(Shout)
|
|
|
|
.options(
|
|
|
|
joinedload(Shout.authors),
|
|
|
|
joinedload(Shout.topics),
|
|
|
|
)
|
|
|
|
.outerjoin(
|
|
|
|
Reaction,
|
|
|
|
and_(
|
|
|
|
Reaction.shout == Shout.id,
|
|
|
|
Reaction.replyTo.is_(None),
|
2023-12-17 05:16:08 +00:00
|
|
|
Reaction.kind.in_([ReactionKind.LIKE.value, ReactionKind.DISLIKE.value]),
|
2023-12-16 15:24:30 +00:00
|
|
|
),
|
|
|
|
)
|
2023-12-17 05:28:34 +00:00
|
|
|
.outerjoin(Author, Author.user == bindparam("user_id"))
|
2023-12-17 05:16:08 +00:00
|
|
|
.where(
|
|
|
|
and_(
|
|
|
|
Shout.deleted_at.is_(None),
|
|
|
|
Shout.layout.is_not(None),
|
|
|
|
Shout.created_at >= options.get("after"),
|
2023-12-17 05:28:34 +00:00
|
|
|
or_(Author.id.is_(None), Reaction.created_by != Author.id),
|
2023-12-17 05:16:08 +00:00
|
|
|
)
|
|
|
|
)
|
2023-12-16 15:24:30 +00:00
|
|
|
)
|
|
|
|
|
2023-12-17 05:16:08 +00:00
|
|
|
# 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)
|
|
|
|
|
2023-12-16 15:24:30 +00:00
|
|
|
q = add_stat_columns(q)
|
|
|
|
|
2023-12-17 05:16:08 +00:00
|
|
|
q = q.group_by(Shout.id).order_by(func.random()).limit(options.get("limit", 50)).offset(options.get("offset", 0))
|
2023-12-16 15:24:30 +00:00
|
|
|
|
2023-12-17 05:28:34 +00:00
|
|
|
return get_shouts_from_query(q, info.context.get("user_id"))
|
2023-12-16 15:24:30 +00:00
|
|
|
|
|
|
|
|
2023-12-17 05:28:34 +00:00
|
|
|
def get_shouts_from_query(q, user_id=None):
|
2023-12-16 15:24:30 +00:00
|
|
|
shouts = []
|
|
|
|
with local_session() as session:
|
2023-12-17 05:28:34 +00:00
|
|
|
for [shout, reacted_stat, commented_stat, rating_stat, last_comment] in session.execute(
|
|
|
|
q, {"user_id": user_id}
|
|
|
|
).unique():
|
2023-12-16 15:24:30 +00:00
|
|
|
shouts.append(shout)
|
|
|
|
shout.stat = {
|
|
|
|
"viewed": shout.views,
|
|
|
|
"reacted": reacted_stat,
|
|
|
|
"commented": commented_stat,
|
|
|
|
"rating": rating_stat,
|
|
|
|
}
|
|
|
|
|
|
|
|
return shouts
|
|
|
|
|
|
|
|
|
|
|
|
def get_rating_func(aliased_reaction):
|
|
|
|
return func.sum(
|
|
|
|
case(
|
2023-12-17 05:40:05 +00:00
|
|
|
(aliased_reaction.kind == str(ReactionKind.AGREE.value), 1),
|
|
|
|
(aliased_reaction.kind == str(ReactionKind.DISAGREE.value), -1),
|
|
|
|
(aliased_reaction.kind == str(ReactionKind.PROOF.value), 1),
|
|
|
|
(aliased_reaction.kind == str(ReactionKind.DISPROOF.value), -1),
|
|
|
|
(aliased_reaction.kind == str(ReactionKind.ACCEPT.value), 1),
|
|
|
|
(aliased_reaction.kind == str(ReactionKind.REJECT.value), -1),
|
|
|
|
(aliased_reaction.kind == str(ReactionKind.DISLIKE.value), -1),
|
2023-12-16 15:24:30 +00:00
|
|
|
(aliased_reaction.reply_to.is_not(None), 0),
|
|
|
|
else_=0,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@query.field("load_shouts_random_top")
|
|
|
|
async def load_shouts_random_top(_, _info, params):
|
|
|
|
"""
|
2023-12-17 05:40:05 +00:00
|
|
|
:param _
|
|
|
|
:param _info: GraphQLInfoContext
|
2023-12-16 15:24:30 +00:00
|
|
|
:param params: {
|
|
|
|
filters: {
|
|
|
|
layouts: ['music']
|
|
|
|
after: 13245678
|
2023-12-17 05:40:05 +00:00
|
|
|
}
|
|
|
|
random_limit: 100
|
2023-12-16 15:24:30 +00:00
|
|
|
limit: 50
|
|
|
|
offset: 0
|
|
|
|
}
|
|
|
|
:return: Shout[]
|
|
|
|
"""
|
|
|
|
|
|
|
|
aliased_reaction = aliased(Reaction)
|
|
|
|
|
|
|
|
subquery = select(Shout.id).outerjoin(aliased_reaction).where(Shout.deleted_at.is_(None))
|
|
|
|
|
|
|
|
subquery = apply_filters(subquery, params.get("filters", {}))
|
|
|
|
subquery = subquery.group_by(Shout.id).order_by(desc(get_rating_func(aliased_reaction)))
|
|
|
|
|
2023-12-17 05:40:05 +00:00
|
|
|
random_limit = params.get("random_limit")
|
|
|
|
if random_limit:
|
|
|
|
subquery = subquery.limit(random_limit)
|
2023-12-16 15:24:30 +00:00
|
|
|
|
|
|
|
q = (
|
|
|
|
select(Shout)
|
|
|
|
.options(
|
|
|
|
joinedload(Shout.authors),
|
|
|
|
joinedload(Shout.topics),
|
|
|
|
)
|
|
|
|
.where(Shout.id.in_(subquery))
|
|
|
|
)
|
|
|
|
|
|
|
|
q = add_stat_columns(q)
|
|
|
|
|
|
|
|
limit = params.get("limit", 10)
|
|
|
|
q = q.group_by(Shout.id).order_by(func.random()).limit(limit)
|
|
|
|
|
|
|
|
# print(q.compile(compile_kwargs={"literal_binds": True}))
|
|
|
|
|
|
|
|
return get_shouts_from_query(q)
|
2023-12-22 18:08:37 +00:00
|
|
|
|
|
|
|
@query.field("load_shouts_random_topic")
|
|
|
|
async def load_shouts_random_topic(_, info, limit):
|
|
|
|
topic = get_random_topic()
|
|
|
|
shouts = []
|
|
|
|
if topic:
|
|
|
|
q = (
|
|
|
|
select(Shout)
|
|
|
|
.options(
|
|
|
|
joinedload(Shout.authors),
|
|
|
|
joinedload(Shout.topics),
|
|
|
|
)
|
|
|
|
.join(ShoutTopic, and_(Shout.id == ShoutTopic.shout, ShoutTopic.topic == topic.id))
|
|
|
|
.where(
|
|
|
|
and_(Shout.deletedAt.is_(None), Shout.layout.is_not(None), Shout.visibility == "public")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
q = add_stat_columns(q)
|
|
|
|
|
|
|
|
q = q.group_by(Shout.id).order_by(desc(Shout.createdAt)).limit(limit)
|
|
|
|
|
|
|
|
shouts = get_shouts_from_query(q)
|
|
|
|
|
|
|
|
return {"topic": topic, "shouts": shouts}
|