2023-11-04 16:44:58 +00:00
|
|
|
import json
|
2023-12-13 22:56:01 +00:00
|
|
|
from datetime import datetime, timedelta
|
2023-10-26 21:07:35 +00:00
|
|
|
|
2023-10-30 21:00:55 +00:00
|
|
|
from sqlalchemy.orm import aliased, joinedload
|
|
|
|
from sqlalchemy.sql.expression import and_, asc, case, desc, func, nulls_last, select
|
2023-10-26 21:07:35 +00:00
|
|
|
|
2023-02-06 14:27:23 +00:00
|
|
|
from auth.authenticate import login_required
|
2022-12-01 14:45:19 +00:00
|
|
|
from auth.credentials import AuthCredentials
|
2023-10-30 21:00:55 +00:00
|
|
|
from base.exceptions import ObjectNotExist
|
2022-09-17 18:12:14 +00:00
|
|
|
from base.orm import local_session
|
2022-11-21 08:13:57 +00:00
|
|
|
from base.resolvers import query
|
2023-08-04 22:21:54 +00:00
|
|
|
from orm 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
|
|
|
|
from orm.user import AuthorFollower
|
2022-11-15 02:36:30 +00:00
|
|
|
|
|
|
|
|
2023-12-13 22:56:01 +00:00
|
|
|
def get_shouts_from_query(q):
|
|
|
|
shouts = []
|
|
|
|
with local_session() as session:
|
|
|
|
for [shout, reacted_stat, commented_stat, rating_stat, last_comment] in session.execute(
|
|
|
|
q
|
|
|
|
).unique():
|
|
|
|
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(
|
|
|
|
# do not count comments' reactions
|
|
|
|
(aliased_reaction.replyTo.is_not(None), 0),
|
|
|
|
(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,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
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-30 21:00:55 +00:00
|
|
|
func.sum(aliased_reaction.id).label("reacted_stat"),
|
|
|
|
func.sum(case((aliased_reaction.kind == ReactionKind.COMMENT, 1), else_=0)).label(
|
|
|
|
"commented_stat"
|
|
|
|
),
|
2023-12-13 22:56:01 +00:00
|
|
|
get_rating_func(aliased_reaction).label("rating_stat"),
|
2023-10-30 21:00:55 +00:00
|
|
|
func.max(
|
|
|
|
case(
|
|
|
|
(aliased_reaction.kind != ReactionKind.COMMENT, None),
|
|
|
|
else_=aliased_reaction.createdAt,
|
2023-10-26 17:56:42 +00:00
|
|
|
)
|
2023-10-30 21:00:55 +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-10-30 21:00:55 +00:00
|
|
|
def apply_filters(q, filters, user_id=None): # noqa: C901
|
2022-12-01 14:45:19 +00:00
|
|
|
if filters.get("reacted") and user_id:
|
|
|
|
q.join(Reaction, Reaction.createdBy == user_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"))
|
2023-10-30 21:00:55 +00:00
|
|
|
if filters.get("excludeLayout"):
|
2023-10-01 17:27:08 +00:00
|
|
|
q = q.filter(Shout.layout != filters.get("excludeLayout"))
|
2022-11-21 08:13:57 +00:00
|
|
|
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")))
|
2023-12-13 22:56:01 +00:00
|
|
|
if filters.get("fromDate"):
|
|
|
|
# fromDate: '2022-12-31
|
|
|
|
date_from = datetime.strptime(filters.get("fromDate"), "%Y-%m-%d")
|
|
|
|
q = q.filter(Shout.createdAt >= date_from)
|
|
|
|
if filters.get("toDate"):
|
|
|
|
# toDate: '2023-12-31'
|
|
|
|
date_to = datetime.strptime(filters.get("toDate"), "%Y-%m-%d")
|
|
|
|
q = q.filter(Shout.createdAt < (date_to + timedelta(days=1)))
|
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
|
|
|
|
2022-11-18 00:38:29 +00:00
|
|
|
@query.field("loadShout")
|
2023-05-08 17:06:01 +00:00
|
|
|
async def load_shout(_, info, slug=None, shout_id=None):
|
2023-11-04 16:44:58 +00:00
|
|
|
# for testing, soon will be removed
|
|
|
|
if slug == "testtesttest":
|
|
|
|
with open("test/test.json") as json_file:
|
|
|
|
test_shout = json.load(json_file)["data"]["loadShout"]
|
|
|
|
test_shout["createdAt"] = datetime.fromisoformat(test_shout["createdAt"])
|
|
|
|
test_shout["publishedAt"] = datetime.fromisoformat(test_shout["publishedAt"])
|
|
|
|
return test_shout
|
|
|
|
|
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
|
|
|
)
|
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-30 21:00:55 +00:00
|
|
|
q = q.filter(Shout.slug == slug)
|
2023-05-08 17:06:01 +00:00
|
|
|
|
|
|
|
if shout_id is not None:
|
2023-10-30 21:00:55 +00:00
|
|
|
q = q.filter(Shout.id == shout_id)
|
2023-05-08 17:06:01 +00:00
|
|
|
|
2023-10-30 21:00:55 +00:00
|
|
|
q = q.filter(Shout.deletedAt.is_(None)).group_by(Shout.id)
|
2023-01-17 21:07:44 +00:00
|
|
|
|
2022-12-03 07:37:45 +00:00
|
|
|
try:
|
2023-10-30 21:00:55 +00:00
|
|
|
[shout, reacted_stat, commented_stat, rating_stat, 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-08-04 22:21:54 +00:00
|
|
|
"viewed": shout.views,
|
2022-12-03 07:37:45 +00:00
|
|
|
"reacted": reacted_stat,
|
|
|
|
"commented": commented_stat,
|
2023-10-30 21:00:55 +00:00
|
|
|
"rating": rating_stat,
|
2022-12-03 07:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for author_caption in session.query(ShoutAuthor).join(Shout).where(Shout.slug == slug):
|
|
|
|
for author in shout.authors:
|
|
|
|
if author.id == author_caption.user:
|
|
|
|
author.caption = author_caption.caption
|
|
|
|
return shout
|
|
|
|
except Exception:
|
|
|
|
raise ObjectNotExist("Slug was not found: %s" % slug)
|
2022-11-23 21:53:53 +00:00
|
|
|
|
|
|
|
|
2022-11-18 00:38:29 +00:00
|
|
|
@query.field("loadShouts")
|
|
|
|
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-10-10 13:37:28 +00:00
|
|
|
layout: 'music',
|
2023-10-01 17:27:08 +00:00
|
|
|
excludeLayout: 'article',
|
2022-11-18 00:38:29 +00:00
|
|
|
visibility: "public",
|
|
|
|
author: 'discours',
|
|
|
|
topic: 'culture',
|
|
|
|
title: 'something',
|
|
|
|
body: 'something else',
|
2023-12-13 22:56:01 +00:00
|
|
|
fromDate: '2022-12-31',
|
|
|
|
toDate: '2023-12-31'
|
2022-11-18 00:38:29 +00:00
|
|
|
}
|
|
|
|
offset: 0
|
|
|
|
limit: 50
|
2022-11-25 18:31:53 +00:00
|
|
|
order_by: 'createdAt' | '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-10-30 21:00:55 +00:00
|
|
|
q = (
|
|
|
|
select(Shout)
|
|
|
|
.options(
|
|
|
|
joinedload(Shout.authors),
|
|
|
|
joinedload(Shout.topics),
|
2023-10-01 17:27:08 +00:00
|
|
|
)
|
2023-10-30 21:00:55 +00:00
|
|
|
.where(and_(Shout.deletedAt.is_(None), Shout.layout.is_not(None)))
|
2022-11-15 02:36:30 +00:00
|
|
|
)
|
2022-11-25 18:31:53 +00:00
|
|
|
|
|
|
|
q = add_stat_columns(q)
|
|
|
|
|
2022-12-01 14:45:19 +00:00
|
|
|
auth: AuthCredentials = info.context["request"].auth
|
|
|
|
q = apply_filters(q, options.get("filters", {}), auth.user_id)
|
2022-11-25 18:31:53 +00:00
|
|
|
|
2023-07-07 16:30:55 +00:00
|
|
|
order_by = options.get("order_by", Shout.publishedAt)
|
2022-11-25 18:31:53 +00:00
|
|
|
|
2023-10-30 21:00:55 +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-07-07 16:30:55 +00:00
|
|
|
q = q.group_by(Shout.id).order_by(nulls_last(query_order_by)).limit(limit).offset(offset)
|
2022-11-18 00:38:29 +00:00
|
|
|
|
2023-12-13 22:56:01 +00:00
|
|
|
return get_shouts_from_query(q)
|
2022-11-24 17:19:43 +00:00
|
|
|
|
2023-01-17 21:07:44 +00:00
|
|
|
|
2023-12-13 22:56:01 +00:00
|
|
|
@query.field("loadRandomTopShouts")
|
|
|
|
async def load_random_top_shouts(_, info, params):
|
|
|
|
"""
|
|
|
|
:param params: {
|
|
|
|
filters: {
|
|
|
|
layout: 'music',
|
|
|
|
excludeLayout: 'article',
|
|
|
|
fromDate: '2022-12-31'
|
|
|
|
toDate: '2023-12-31'
|
|
|
|
}
|
|
|
|
fromRandomCount: 100,
|
|
|
|
limit: 50
|
|
|
|
}
|
|
|
|
:return: Shout[]
|
|
|
|
"""
|
|
|
|
|
|
|
|
aliased_reaction = aliased(Reaction)
|
|
|
|
|
|
|
|
subquery = (
|
|
|
|
select(Shout.id)
|
|
|
|
.outerjoin(aliased_reaction)
|
|
|
|
.where(and_(Shout.deletedAt.is_(None), Shout.layout.is_not(None)))
|
|
|
|
)
|
|
|
|
|
|
|
|
subquery = apply_filters(subquery, params.get("filters", {}))
|
|
|
|
subquery = subquery.group_by(Shout.id).order_by(desc(get_rating_func(aliased_reaction)))
|
|
|
|
|
|
|
|
from_random_count = params.get("fromRandomCount")
|
|
|
|
if from_random_count:
|
|
|
|
subquery = subquery.limit(from_random_count)
|
|
|
|
|
|
|
|
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}))
|
2023-12-14 18:40:12 +00:00
|
|
|
|
|
|
|
return get_shouts_from_query(q)
|
|
|
|
|
|
|
|
|
|
|
|
@query.field("loadUnratedShouts")
|
|
|
|
async def load_unrated_shouts(_, info, limit):
|
|
|
|
q = (
|
|
|
|
select(Shout)
|
|
|
|
.options(
|
|
|
|
joinedload(Shout.authors),
|
|
|
|
joinedload(Shout.topics),
|
|
|
|
)
|
|
|
|
.outerjoin(
|
|
|
|
Reaction,
|
|
|
|
and_(
|
|
|
|
Reaction.shout == Shout.id,
|
|
|
|
Reaction.replyTo.is_(None),
|
|
|
|
Reaction.kind.in_([ReactionKind.LIKE, ReactionKind.DISLIKE]),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.where(and_(Shout.deletedAt.is_(None), Shout.layout.is_not(None), Reaction.id.is_(None)))
|
|
|
|
)
|
|
|
|
|
|
|
|
q = add_stat_columns(q)
|
|
|
|
|
|
|
|
q = q.group_by(Shout.id).order_by(desc(Shout.createdAt)).limit(limit)
|
|
|
|
|
|
|
|
# print(q.compile(compile_kwargs={"literal_binds": True}))
|
2023-12-13 22:56:01 +00:00
|
|
|
|
|
|
|
return get_shouts_from_query(q)
|
2023-02-06 14:27:23 +00:00
|
|
|
|
2023-05-08 17:06:01 +00:00
|
|
|
|
2023-05-03 15:47:09 +00:00
|
|
|
@query.field("loadDrafts")
|
2023-10-10 06:35:27 +00:00
|
|
|
@login_required
|
2023-05-04 22:59:04 +00:00
|
|
|
async def get_drafts(_, info):
|
2023-05-03 15:47:09 +00:00
|
|
|
auth: AuthCredentials = info.context["request"].auth
|
|
|
|
user_id = auth.user_id
|
|
|
|
|
2023-10-30 21:00:55 +00:00
|
|
|
q = (
|
|
|
|
select(Shout)
|
|
|
|
.options(
|
|
|
|
joinedload(Shout.authors),
|
|
|
|
joinedload(Shout.topics),
|
|
|
|
)
|
|
|
|
.where(and_(Shout.deletedAt.is_(None), Shout.createdBy == user_id))
|
2023-05-03 15:47:09 +00:00
|
|
|
)
|
|
|
|
|
2023-05-04 22:59:04 +00:00
|
|
|
q = q.group_by(Shout.id)
|
2023-05-03 15:47:09 +00:00
|
|
|
|
|
|
|
shouts = []
|
|
|
|
with local_session() as session:
|
|
|
|
for [shout] in session.execute(q).unique():
|
|
|
|
shouts.append(shout)
|
|
|
|
|
|
|
|
return shouts
|
|
|
|
|
|
|
|
|
2023-02-06 14:27:23 +00:00
|
|
|
@query.field("myFeed")
|
|
|
|
@login_required
|
2023-02-16 10:08:55 +00:00
|
|
|
async def get_my_feed(_, info, options):
|
2023-02-06 14:27:23 +00:00
|
|
|
auth: AuthCredentials = info.context["request"].auth
|
|
|
|
user_id = auth.user_id
|
|
|
|
|
2023-10-31 13:52:58 +00:00
|
|
|
user_followed_authors = select(AuthorFollower.author).where(AuthorFollower.follower == user_id)
|
|
|
|
user_followed_topics = select(TopicFollower.topic).where(TopicFollower.follower == user_id)
|
|
|
|
|
2023-10-30 21:00:55 +00:00
|
|
|
subquery = (
|
|
|
|
select(Shout.id)
|
2023-10-31 13:52:58 +00:00
|
|
|
.where(Shout.id == ShoutAuthor.shout)
|
|
|
|
.where(Shout.id == ShoutTopic.shout)
|
|
|
|
.where(
|
|
|
|
(ShoutAuthor.user.in_(user_followed_authors))
|
|
|
|
| (ShoutTopic.topic.in_(user_followed_topics))
|
|
|
|
)
|
2023-07-05 14:08:17 +00:00
|
|
|
)
|
|
|
|
|
2023-10-30 21:00:55 +00:00
|
|
|
q = (
|
|
|
|
select(Shout)
|
|
|
|
.options(
|
|
|
|
joinedload(Shout.authors),
|
|
|
|
joinedload(Shout.topics),
|
|
|
|
)
|
|
|
|
.where(
|
|
|
|
and_(Shout.publishedAt.is_not(None), Shout.deletedAt.is_(None), Shout.id.in_(subquery))
|
2023-07-05 14:08:17 +00:00
|
|
|
)
|
|
|
|
)
|
2023-02-06 14:27:23 +00:00
|
|
|
|
2023-02-16 10:08:55 +00:00
|
|
|
q = add_stat_columns(q)
|
|
|
|
q = apply_filters(q, options.get("filters", {}), user_id)
|
|
|
|
|
2023-07-05 14:08:17 +00:00
|
|
|
order_by = options.get("order_by", Shout.publishedAt)
|
2023-02-16 10:08:55 +00:00
|
|
|
|
2023-10-30 21:00:55 +00:00
|
|
|
query_order_by = desc(order_by) if options.get("order_by_desc", True) else asc(order_by)
|
2023-02-16 10:08:55 +00:00
|
|
|
offset = options.get("offset", 0)
|
|
|
|
limit = options.get("limit", 10)
|
|
|
|
|
2023-07-07 16:30:55 +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-10-31 13:52:58 +00:00
|
|
|
# print(q.compile(compile_kwargs={"literal_binds": True}))
|
|
|
|
|
2023-12-13 22:56:01 +00:00
|
|
|
return get_shouts_from_query(q)
|