2024-02-25 14:39:38 +00:00
|
|
|
from sqlalchemy import bindparam, distinct, or_, text
|
2024-03-25 17:28:58 +00:00
|
|
|
from sqlalchemy.orm import aliased, joinedload
|
2024-04-24 07:42:33 +00:00
|
|
|
from sqlalchemy.sql.expression import and_, asc, case, desc, func, nulls_last, select
|
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
|
2024-02-02 16:36:30 +00:00
|
|
|
from orm.shout import Shout, ShoutAuthor, ShoutTopic
|
2023-12-17 20:30:20 +00:00
|
|
|
from orm.topic import Topic, TopicFollower
|
2024-02-22 23:08:43 +00:00
|
|
|
from resolvers.reaction import add_reaction_stat_columns
|
2024-02-28 16:24:05 +00:00
|
|
|
from resolvers.topic import get_topics_random
|
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
|
2024-04-08 07:38:58 +00:00
|
|
|
from services.logger import root_logger as logger
|
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
|
2023-11-22 16:38:39 +00:00
|
|
|
from services.viewed import ViewedStorage
|
2024-01-28 20:21:02 +00:00
|
|
|
|
|
|
|
|
2024-03-25 17:28:58 +00:00
|
|
|
def query_shouts():
|
2024-03-28 12:56:32 +00:00
|
|
|
return (
|
|
|
|
select(Shout)
|
|
|
|
.options(joinedload(Shout.authors), joinedload(Shout.topics))
|
|
|
|
.where(and_(Shout.published_at.is_not(None), Shout.deleted_at.is_(None)))
|
|
|
|
)
|
2024-03-25 17:28:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
def filter_my(info, session, q):
|
2024-04-19 15:22:07 +00:00
|
|
|
user_id = info.context.get("user_id")
|
|
|
|
reader_id = info.context.get("author", {}).get("id")
|
|
|
|
if user_id and reader_id:
|
|
|
|
reader_followed_authors = select(AuthorFollower.author).where(
|
|
|
|
AuthorFollower.follower == reader_id
|
|
|
|
)
|
|
|
|
reader_followed_topics = select(TopicFollower.topic).where(
|
|
|
|
TopicFollower.follower == reader_id
|
|
|
|
)
|
|
|
|
|
|
|
|
subquery = (
|
|
|
|
select(Shout.id)
|
|
|
|
.where(Shout.id == ShoutAuthor.shout)
|
|
|
|
.where(Shout.id == ShoutTopic.shout)
|
|
|
|
.where(
|
|
|
|
(ShoutAuthor.author.in_(reader_followed_authors))
|
|
|
|
| (ShoutTopic.topic.in_(reader_followed_topics))
|
2024-03-25 17:28:58 +00:00
|
|
|
)
|
2024-04-19 15:22:07 +00:00
|
|
|
)
|
|
|
|
q = q.filter(Shout.id.in_(subquery))
|
2024-03-25 17:28:58 +00:00
|
|
|
return q, reader_id
|
|
|
|
|
|
|
|
|
2024-01-25 19:41:27 +00:00
|
|
|
def apply_filters(q, filters, author_id=None):
|
2024-03-25 17:41:28 +00:00
|
|
|
if isinstance(filters, dict):
|
2024-04-17 15:32:23 +00:00
|
|
|
if filters.get("reacted"):
|
2024-04-25 09:19:42 +00:00
|
|
|
q = q.join(
|
|
|
|
Reaction,
|
|
|
|
and_(
|
|
|
|
Reaction.shout == Shout.id,
|
|
|
|
Reaction.created_by == author_id,
|
|
|
|
),
|
|
|
|
)
|
2024-03-25 17:41:28 +00:00
|
|
|
|
2024-05-01 02:02:35 +00:00
|
|
|
featured_filter = filters.get("featured", "")
|
2024-05-01 02:08:54 +00:00
|
|
|
if featured_filter:
|
|
|
|
q = q.filter(Shout.featured_at.is_not(None))
|
|
|
|
elif "featured" in filters:
|
|
|
|
q = q.filter(Shout.featured_at.is_(None))
|
|
|
|
else:
|
|
|
|
pass
|
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
|
|
|
|
|
2023-05-08 17:06:01 +00:00
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
@query.field("get_shout")
|
2024-03-05 17:12:17 +00:00
|
|
|
async def get_shout(_, info, slug: str):
|
2024-04-26 08:43:22 +00:00
|
|
|
try:
|
|
|
|
with local_session() as session:
|
|
|
|
q = query_shouts()
|
|
|
|
aliased_reaction = aliased(Reaction)
|
|
|
|
q = add_reaction_stat_columns(q, aliased_reaction)
|
|
|
|
q = q.filter(Shout.slug == slug)
|
|
|
|
q = q.group_by(Shout.id)
|
|
|
|
|
|
|
|
results = session.execute(q).first()
|
|
|
|
if results:
|
|
|
|
[
|
|
|
|
shout,
|
|
|
|
reacted_stat,
|
|
|
|
commented_stat,
|
|
|
|
likes_stat,
|
|
|
|
dislikes_stat,
|
|
|
|
last_comment,
|
|
|
|
] = results
|
|
|
|
|
|
|
|
shout.stat = {
|
|
|
|
"viewed": await ViewedStorage.get_shout(shout.slug),
|
|
|
|
"reacted": reacted_stat,
|
|
|
|
"commented": commented_stat,
|
|
|
|
"rating": int(likes_stat or 0) - int(dislikes_stat or 0),
|
|
|
|
"last_comment": last_comment,
|
|
|
|
}
|
|
|
|
|
|
|
|
for author_caption in (
|
|
|
|
session.query(ShoutAuthor)
|
|
|
|
.join(Shout)
|
|
|
|
.where(
|
|
|
|
and_(
|
|
|
|
Shout.slug == slug,
|
|
|
|
Shout.published_at.is_not(None),
|
|
|
|
Shout.deleted_at.is_(None),
|
|
|
|
)
|
2024-03-06 09:25:55 +00:00
|
|
|
)
|
2024-04-26 08:43:22 +00:00
|
|
|
):
|
|
|
|
for author in shout.authors:
|
|
|
|
if author.id == author_caption.author:
|
|
|
|
author.caption = author_caption.caption
|
|
|
|
main_topic = (
|
|
|
|
session.query(Topic.slug)
|
|
|
|
.join(
|
|
|
|
ShoutTopic,
|
|
|
|
and_(
|
|
|
|
ShoutTopic.topic == Topic.id,
|
|
|
|
ShoutTopic.shout == shout.id,
|
|
|
|
ShoutTopic.main.is_(True),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.first()
|
2024-03-06 09:25:55 +00:00
|
|
|
)
|
2022-11-23 21:53:53 +00:00
|
|
|
|
2024-04-26 08:43:22 +00:00
|
|
|
if main_topic:
|
|
|
|
shout.main_topic = main_topic[0]
|
|
|
|
return shout
|
|
|
|
except Exception as _exc:
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
logger.error(traceback.format_exc())
|
2024-03-05 11:38:04 +00:00
|
|
|
|
2022-11-23 21:53:53 +00:00
|
|
|
|
2024-04-17 15:32:23 +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,
|
2024-02-02 12:03:44 +00:00
|
|
|
featured: True, // filter featured-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
|
2024-01-23 14:14:43 +00:00
|
|
|
order_by: 'created_at' | 'commented' | 'likes_stat'
|
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
|
2024-03-25 17:28:58 +00:00
|
|
|
q = query_shouts()
|
2022-11-25 18:31:53 +00:00
|
|
|
|
2023-12-02 22:14:36 +00:00
|
|
|
# stats
|
2024-01-23 01:34:48 +00:00
|
|
|
aliased_reaction = aliased(Reaction)
|
2024-02-22 23:08:43 +00:00
|
|
|
q = add_reaction_stat_columns(q, aliased_reaction)
|
2023-12-02 06:25:08 +00:00
|
|
|
|
|
|
|
# filters
|
2024-04-17 15:32:23 +00:00
|
|
|
filters = options.get("filters", {})
|
2024-02-02 12:03:44 +00:00
|
|
|
q = apply_filters(q, 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
|
2024-04-17 15:32:23 +00:00
|
|
|
order_by = Shout.featured_at if filters.get("featured") else Shout.published_at
|
|
|
|
order_str = options.get("order_by")
|
2024-05-01 02:02:35 +00:00
|
|
|
if order_str in ["likes", "followers", "comments", "last_comment"]:
|
2024-04-17 15:32:23 +00:00
|
|
|
q = q.order_by(desc(text(f"{order_str}_stat")))
|
2024-02-21 07:27:16 +00:00
|
|
|
query_order_by = (
|
2024-04-17 15:32:23 +00:00
|
|
|
desc(order_by) if options.get("order_by_desc", True) else asc(order_by)
|
2024-02-21 07:27:16 +00:00
|
|
|
)
|
2023-12-02 06:25:08 +00:00
|
|
|
q = q.order_by(nulls_last(query_order_by))
|
|
|
|
|
|
|
|
# limit offset
|
2024-04-17 15:32:23 +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:
|
2024-01-25 19:41:27 +00:00
|
|
|
for [
|
|
|
|
shout,
|
2024-01-29 12:20:28 +00:00
|
|
|
reacted_stat,
|
2024-01-25 19:41:27 +00:00
|
|
|
commented_stat,
|
|
|
|
likes_stat,
|
|
|
|
dislikes_stat,
|
2024-03-25 16:50:23 +00:00
|
|
|
last_comment,
|
2024-01-25 19:41:27 +00:00
|
|
|
] 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_(
|
2024-01-25 19:41:27 +00:00
|
|
|
ShoutTopic.topic == Topic.id,
|
|
|
|
ShoutTopic.shout == shout.id,
|
2024-02-17 10:18:54 +00:00
|
|
|
ShoutTopic.main.is_(True),
|
2024-01-25 19:41:27 +00:00
|
|
|
),
|
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 = {
|
2024-04-17 15:32:23 +00:00
|
|
|
"viewed": await ViewedStorage.get_shout(shout.slug),
|
|
|
|
"reacted": reacted_stat,
|
|
|
|
"commented": commented_stat,
|
|
|
|
"rating": int(likes_stat) - int(dislikes_stat),
|
|
|
|
"last_comment": last_comment,
|
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
|
|
|
|
2024-03-05 13:59:55 +00:00
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
@query.field("load_shouts_feed")
|
2024-03-05 15:53:18 +00:00
|
|
|
@login_required
|
2023-11-28 07:53:48 +00:00
|
|
|
async def load_shouts_feed(_, info, options):
|
2024-01-23 01:34:48 +00:00
|
|
|
shouts = []
|
2023-10-23 14:47:11 +00:00
|
|
|
with local_session() as session:
|
2024-03-25 17:28:58 +00:00
|
|
|
q = query_shouts()
|
2023-11-27 18:18:52 +00:00
|
|
|
|
2024-03-25 17:28:58 +00:00
|
|
|
aliased_reaction = aliased(Reaction)
|
|
|
|
q = add_reaction_stat_columns(q, aliased_reaction)
|
2023-11-27 18:18:52 +00:00
|
|
|
|
2024-03-25 17:28:58 +00:00
|
|
|
# filters
|
2024-04-17 15:32:23 +00:00
|
|
|
filters = options.get("filters", {})
|
2024-03-25 17:28:58 +00:00
|
|
|
if filters:
|
|
|
|
q, reader_id = filter_my(info, session, q)
|
|
|
|
q = apply_filters(q, filters, reader_id)
|
2023-02-06 14:27:23 +00:00
|
|
|
|
2024-03-25 17:28:58 +00:00
|
|
|
# sort order
|
|
|
|
order_by = options.get(
|
2024-04-17 15:32:23 +00:00
|
|
|
"order_by",
|
|
|
|
Shout.featured_at if filters.get("featured") else Shout.published_at,
|
2024-03-25 17:28:58 +00:00
|
|
|
)
|
2023-02-16 10:08:55 +00:00
|
|
|
|
2024-03-25 17:28:58 +00:00
|
|
|
query_order_by = (
|
2024-04-17 15:32:23 +00:00
|
|
|
desc(order_by) if options.get("order_by_desc", True) else asc(order_by)
|
2024-03-25 17:28:58 +00:00
|
|
|
)
|
2023-02-16 10:08:55 +00:00
|
|
|
|
2024-03-25 17:28:58 +00:00
|
|
|
# pagination
|
2024-04-17 15:32:23 +00:00
|
|
|
offset = options.get("offset", 0)
|
|
|
|
limit = options.get("limit", 10)
|
2023-02-16 10:08:55 +00:00
|
|
|
|
2024-03-25 17:28:58 +00:00
|
|
|
q = (
|
|
|
|
q.group_by(Shout.id)
|
|
|
|
.order_by(nulls_last(query_order_by))
|
|
|
|
.limit(limit)
|
|
|
|
.offset(offset)
|
|
|
|
)
|
2023-12-02 22:14:36 +00:00
|
|
|
|
2024-04-25 11:06:21 +00:00
|
|
|
logger.debug(q.compile(compile_kwargs={"literal_binds": True}))
|
2024-03-25 17:28:58 +00:00
|
|
|
|
|
|
|
for [
|
|
|
|
shout,
|
|
|
|
reacted_stat,
|
|
|
|
commented_stat,
|
|
|
|
likes_stat,
|
|
|
|
dislikes_stat,
|
|
|
|
last_comment,
|
|
|
|
] in session.execute(q).unique():
|
|
|
|
main_topic = (
|
|
|
|
session.query(Topic.slug)
|
|
|
|
.join(
|
|
|
|
ShoutTopic,
|
|
|
|
and_(
|
|
|
|
ShoutTopic.topic == Topic.id,
|
|
|
|
ShoutTopic.shout == shout.id,
|
|
|
|
ShoutTopic.main.is_(True),
|
|
|
|
),
|
2023-12-09 18:15:30 +00:00
|
|
|
)
|
2024-03-25 17:28:58 +00:00
|
|
|
.first()
|
|
|
|
)
|
2023-12-09 18:15:30 +00:00
|
|
|
|
2024-03-25 17:28:58 +00:00
|
|
|
if main_topic:
|
|
|
|
shout.main_topic = main_topic[0]
|
|
|
|
shout.stat = {
|
2024-04-17 15:32:23 +00:00
|
|
|
"viewed": await ViewedStorage.get_shout(shout.slug),
|
|
|
|
"reacted": reacted_stat,
|
|
|
|
"commented": commented_stat,
|
|
|
|
"rating": likes_stat - dislikes_stat,
|
|
|
|
"last_comment": last_comment,
|
2024-03-25 17:28:58 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
@query.field("load_shouts_search")
|
2023-12-02 22:22:16 +00:00
|
|
|
async def load_shouts_search(_, _info, text, limit=50, 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)
|
|
|
|
logger.debug(results)
|
|
|
|
return results
|
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-03-25 18:07:32 +00:00
|
|
|
@login_required
|
2023-12-22 18:12:42 +00:00
|
|
|
async def load_shouts_unrated(_, info, limit: int = 50, offset: int = 0):
|
2024-03-25 17:28:58 +00:00
|
|
|
q = query_shouts()
|
2024-03-28 12:56:32 +00:00
|
|
|
q = (
|
|
|
|
q.outerjoin(
|
2023-12-16 15:24:30 +00:00
|
|
|
Reaction,
|
|
|
|
and_(
|
|
|
|
Reaction.shout == Shout.id,
|
2024-05-27 16:39:48 +00:00
|
|
|
Reaction.reply_to.is_(None),
|
2024-02-21 07:27:16 +00:00
|
|
|
Reaction.kind.in_(
|
|
|
|
[ReactionKind.LIKE.value, ReactionKind.DISLIKE.value]
|
|
|
|
),
|
2023-12-16 15:24:30 +00:00
|
|
|
),
|
2024-03-28 12:56:32 +00:00
|
|
|
)
|
2024-04-17 15:32:23 +00:00
|
|
|
.outerjoin(Author, Author.user == bindparam("user_id"))
|
2024-03-28 12:56:32 +00:00
|
|
|
.where(
|
2023-12-17 05:16:08 +00:00
|
|
|
and_(
|
|
|
|
Shout.deleted_at.is_(None),
|
|
|
|
Shout.layout.is_not(None),
|
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
|
|
|
)
|
|
|
|
)
|
2024-03-28 12:56:32 +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)
|
|
|
|
|
2024-01-23 01:34:48 +00:00
|
|
|
aliased_reaction = aliased(Reaction)
|
2024-02-22 23:08:43 +00:00
|
|
|
q = add_reaction_stat_columns(q, aliased_reaction)
|
2023-12-16 15:24:30 +00:00
|
|
|
|
2023-12-22 18:12:42 +00:00
|
|
|
q = q.group_by(Shout.id).order_by(func.random()).limit(limit).offset(offset)
|
2024-04-17 15:32:23 +00:00
|
|
|
user_id = info.context.get("user_id") if isinstance(info.context, dict) else None
|
2023-12-23 19:00:22 +00:00
|
|
|
if user_id:
|
|
|
|
with local_session() as session:
|
|
|
|
author = session.query(Author).filter(Author.user == user_id).first()
|
|
|
|
if author:
|
2024-01-23 13:04:38 +00:00
|
|
|
return await get_shouts_from_query(q, author.id)
|
2023-12-23 19:00:22 +00:00
|
|
|
else:
|
2024-01-23 13:04:38 +00:00
|
|
|
return await get_shouts_from_query(q)
|
2023-12-16 15:24:30 +00:00
|
|
|
|
|
|
|
|
2024-01-23 13:04:38 +00:00
|
|
|
async def get_shouts_from_query(q, author_id=None):
|
2023-12-16 15:24:30 +00:00
|
|
|
shouts = []
|
|
|
|
with local_session() as session:
|
2024-01-25 19:41:27 +00:00
|
|
|
for [
|
|
|
|
shout,
|
2024-01-29 12:20:28 +00:00
|
|
|
reacted_stat,
|
2024-01-25 19:41:27 +00:00
|
|
|
commented_stat,
|
|
|
|
likes_stat,
|
|
|
|
dislikes_stat,
|
2024-01-29 12:20:28 +00:00
|
|
|
last_comment,
|
2024-04-17 15:32:23 +00:00
|
|
|
] in session.execute(q, {"author_id": author_id}).unique():
|
2023-12-16 15:24:30 +00:00
|
|
|
shouts.append(shout)
|
|
|
|
shout.stat = {
|
2024-04-17 15:32:23 +00:00
|
|
|
"viewed": await ViewedStorage.get_shout(shout_slug=shout.slug),
|
|
|
|
"reacted": reacted_stat,
|
|
|
|
"commented": commented_stat,
|
|
|
|
"rating": int(likes_stat or 0) - int(dislikes_stat or 0),
|
|
|
|
"last_comment": last_comment,
|
2023-12-16 15:24:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return shouts
|
|
|
|
|
2024-01-25 19:41:27 +00:00
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
@query.field("load_shouts_random_top")
|
2024-01-18 11:45:47 +00:00
|
|
|
async def load_shouts_random_top(_, _info, options):
|
2023-12-16 15:24:30 +00:00
|
|
|
"""
|
2023-12-17 05:40:05 +00:00
|
|
|
:param _
|
|
|
|
:param _info: GraphQLInfoContext
|
2024-01-18 11:45:47 +00:00
|
|
|
:param options: {
|
2023-12-16 15:24:30 +00:00
|
|
|
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)
|
|
|
|
|
2024-02-21 07:27:16 +00:00
|
|
|
subquery = (
|
2024-03-25 12:03:03 +00:00
|
|
|
select(Shout.id)
|
|
|
|
.outerjoin(aliased_reaction)
|
|
|
|
.where(and_(Shout.deleted_at.is_(None), Shout.layout.is_not(None)))
|
2024-02-21 07:27:16 +00:00
|
|
|
)
|
2023-12-16 15:24:30 +00:00
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
subquery = apply_filters(subquery, options.get("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-03-25 12:03:03 +00:00
|
|
|
# do not count comments' reactions
|
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)
|
2023-12-16 15:24:30 +00:00
|
|
|
|
|
|
|
q = (
|
|
|
|
select(Shout)
|
2024-02-21 16:14:58 +00:00
|
|
|
.options(joinedload(Shout.authors), joinedload(Shout.topics))
|
2023-12-16 15:24:30 +00:00
|
|
|
.where(Shout.id.in_(subquery))
|
|
|
|
)
|
2024-03-25 12:03:03 +00:00
|
|
|
|
2024-02-22 23:08:43 +00:00
|
|
|
q = add_reaction_stat_columns(q, aliased_reaction)
|
2023-12-16 15:24:30 +00:00
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
limit = options.get("limit", 10)
|
2024-01-23 01:58:45 +00:00
|
|
|
q = q.group_by(Shout.id).order_by(func.random()).limit(limit)
|
2023-12-16 15:24:30 +00:00
|
|
|
|
2024-02-26 12:21:12 +00:00
|
|
|
shouts = await get_shouts_from_query(q)
|
|
|
|
|
|
|
|
return shouts
|
2023-12-22 18:08:37 +00:00
|
|
|
|
2023-12-23 19:00:22 +00:00
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
@query.field("load_shouts_random_topic")
|
2023-12-23 19:00:22 +00:00
|
|
|
async def load_shouts_random_topic(_, info, limit: int = 10):
|
2024-02-28 16:24:05 +00:00
|
|
|
[topic] = get_topics_random(None, None, 1)
|
|
|
|
if topic:
|
|
|
|
shouts = fetch_shouts_by_topic(topic, limit)
|
|
|
|
if shouts:
|
2024-04-17 15:32:23 +00:00
|
|
|
return {"topic": topic, "shouts": shouts}
|
2024-02-28 16:24:05 +00:00
|
|
|
return {
|
2024-04-17 15:32:23 +00:00
|
|
|
"error": "failed to get random topic after few retries",
|
|
|
|
"shouts": [],
|
|
|
|
"topic": {},
|
2024-02-28 16:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def fetch_shouts_by_topic(topic, limit):
|
2024-02-19 08:58:02 +00:00
|
|
|
q = (
|
2024-02-28 16:24:05 +00:00
|
|
|
select(Shout)
|
|
|
|
.options(joinedload(Shout.authors), joinedload(Shout.topics))
|
2024-02-19 08:58:02 +00:00
|
|
|
.filter(
|
|
|
|
and_(
|
|
|
|
Shout.deleted_at.is_(None),
|
|
|
|
Shout.featured_at.is_not(None),
|
2024-02-28 16:24:05 +00:00
|
|
|
Shout.topics.any(slug=topic.slug),
|
2024-01-25 19:41:27 +00:00
|
|
|
)
|
2023-12-22 18:08:37 +00:00
|
|
|
)
|
2024-02-19 08:58:02 +00:00
|
|
|
)
|
2023-12-22 18:08:37 +00:00
|
|
|
|
2024-02-19 08:58:02 +00:00
|
|
|
aliased_reaction = aliased(Reaction)
|
2024-02-22 23:08:43 +00:00
|
|
|
q = add_reaction_stat_columns(q, aliased_reaction)
|
2023-12-22 18:08:37 +00:00
|
|
|
|
2024-02-28 16:24:05 +00:00
|
|
|
q = q.group_by(Shout.id).order_by(desc(Shout.created_at)).limit(limit)
|
2023-12-22 18:08:37 +00:00
|
|
|
|
2024-02-28 16:24:05 +00:00
|
|
|
shouts = get_shouts_from_query(q)
|
|
|
|
|
|
|
|
return shouts
|