2022-11-13 16:25:57 +00:00
|
|
|
from datetime import datetime, timedelta
|
2022-11-17 20:33:03 +00:00
|
|
|
import sqlalchemy as sa
|
2022-09-17 18:12:14 +00:00
|
|
|
from sqlalchemy.orm import selectinload
|
2022-11-17 20:33:03 +00:00
|
|
|
from sqlalchemy.sql.expression import or_, desc, asc, select, case
|
|
|
|
from timeit import default_timer as timer
|
2022-09-17 18:12:14 +00:00
|
|
|
from auth.authenticate import login_required
|
|
|
|
from base.orm import local_session
|
|
|
|
from base.resolvers import mutation, query
|
2022-11-15 02:36:30 +00:00
|
|
|
from orm.shout import Shout
|
2022-11-17 20:33:03 +00:00
|
|
|
from orm.reaction import Reaction, ReactionsWeights, ReactionKind
|
2022-11-15 02:36:30 +00:00
|
|
|
# from resolvers.community import community_follow, community_unfollow
|
2022-09-17 18:12:14 +00:00
|
|
|
from resolvers.profile import author_follow, author_unfollow
|
2022-07-21 11:58:50 +00:00
|
|
|
from resolvers.reactions import reactions_follow, reactions_unfollow
|
2022-09-17 18:12:14 +00:00
|
|
|
from resolvers.topics import topic_follow, topic_unfollow
|
|
|
|
from services.zine.shoutauthor import ShoutAuthorStorage
|
2022-11-15 02:36:30 +00:00
|
|
|
from services.stat.reacted import ReactedStorage
|
|
|
|
|
|
|
|
|
|
|
|
@query.field("loadShoutsBy")
|
2022-11-17 20:33:03 +00:00
|
|
|
async def load_shouts_by(_, info, filter_by, limit, offset, order_by="createdAt", order_by_desc=True):
|
2022-11-15 02:36:30 +00:00
|
|
|
"""
|
2022-11-17 20:33:03 +00:00
|
|
|
:param filterBy: {
|
2022-11-15 02:36:30 +00:00
|
|
|
layout: 'audio',
|
2022-11-15 10:30:09 +00:00
|
|
|
visibility: "public",
|
2022-11-15 02:36:30 +00:00
|
|
|
author: 'discours',
|
|
|
|
topic: 'culture',
|
|
|
|
title: 'something',
|
|
|
|
body: 'something else',
|
|
|
|
days: 30
|
|
|
|
}
|
2022-11-17 20:33:03 +00:00
|
|
|
:param order_by: 'rating' | 'comments' | 'reacted' | 'views' | 'createdAt
|
|
|
|
:param order_by_desc: order be desc/ask (desc by default)
|
2022-11-16 07:32:24 +00:00
|
|
|
:param limit: int amount of shouts
|
2022-11-15 02:36:30 +00:00
|
|
|
:param offset: int offset in this order
|
|
|
|
:return: Shout[]
|
|
|
|
"""
|
|
|
|
|
2022-11-17 20:33:03 +00:00
|
|
|
q = select(Shout).options(
|
|
|
|
# TODO add cation
|
2022-11-15 02:36:30 +00:00
|
|
|
selectinload(Shout.authors),
|
|
|
|
selectinload(Shout.topics),
|
|
|
|
).where(
|
|
|
|
Shout.deletedAt.is_(None)
|
|
|
|
)
|
2022-11-17 20:33:03 +00:00
|
|
|
|
|
|
|
if filter_by.get("slug"):
|
|
|
|
q = q.filter(Shout.slug == filter_by["slug"])
|
2022-08-17 07:59:17 +00:00
|
|
|
else:
|
2022-11-17 20:33:03 +00:00
|
|
|
if filter_by.get("reacted"):
|
|
|
|
user = info.context["request"].user
|
|
|
|
q.join(Reaction, Reaction.createdBy == user.slug)
|
|
|
|
if filter_by.get("visibility"):
|
2022-11-15 16:49:38 +00:00
|
|
|
q = q.filter(or_(
|
2022-11-17 20:33:03 +00:00
|
|
|
Shout.visibility.ilike(f"%{filter_by.get('visibility')}%"),
|
2022-11-15 16:49:38 +00:00
|
|
|
Shout.visibility.ilike(f"%{'public'}%"),
|
|
|
|
))
|
2022-11-17 20:33:03 +00:00
|
|
|
if filter_by.get("layout"):
|
|
|
|
q = q.filter(Shout.layout == filter_by["layout"])
|
|
|
|
if filter_by.get("author"):
|
|
|
|
q = q.filter(Shout.authors.any(slug=filter_by["author"]))
|
|
|
|
if filter_by.get("topic"):
|
|
|
|
q = q.filter(Shout.topics.any(slug=filter_by["topic"]))
|
|
|
|
if filter_by.get("title"):
|
|
|
|
q = q.filter(Shout.title.ilike(f'%{filter_by["title"]}%'))
|
|
|
|
if filter_by.get("body"):
|
|
|
|
q = q.filter(Shout.body.ilike(f'%{filter_by["body"]}%'))
|
|
|
|
if filter_by.get("days"):
|
|
|
|
before = datetime.now() - timedelta(days=int(filter_by["days"]) or 30)
|
2022-11-15 02:36:30 +00:00
|
|
|
q = q.filter(Shout.createdAt > before)
|
2022-11-17 20:33:03 +00:00
|
|
|
if order_by == 'comments':
|
|
|
|
q = q.join(Reaction).add_columns(sa.func.count(Reaction.id).label(order_by))
|
|
|
|
if order_by == 'reacted':
|
|
|
|
# TODO ?
|
|
|
|
q = q.join(Reaction).add_columns(sa.func.count(Reaction.id).label(order_by))
|
|
|
|
if order_by == "rating":
|
|
|
|
q = q.join(Reaction).add_columns(sa.func.sum(case(
|
|
|
|
(Reaction.kind == ReactionKind.AGREE, 1),
|
|
|
|
(Reaction.kind == ReactionKind.DISAGREE, -1),
|
|
|
|
(Reaction.kind == ReactionKind.PROOF, 1),
|
|
|
|
(Reaction.kind == ReactionKind.DISPROOF, -1),
|
|
|
|
(Reaction.kind == ReactionKind.ACCEPT, 1),
|
|
|
|
(Reaction.kind == ReactionKind.REJECT, -1),
|
|
|
|
(Reaction.kind == ReactionKind.LIKE, 1),
|
|
|
|
(Reaction.kind == ReactionKind.DISLIKE, -1),
|
|
|
|
else_=0
|
|
|
|
)).label(order_by))
|
|
|
|
# if order_by == 'views':
|
|
|
|
# TODO dump ackee data to db periodically
|
|
|
|
|
|
|
|
query_order_by = desc(order_by) if order_by_desc else asc(order_by)
|
|
|
|
|
|
|
|
q = q.group_by(Shout.id).order_by(query_order_by).limit(limit).offset(offset)
|
|
|
|
|
|
|
|
with local_session() as session:
|
|
|
|
# post query stats and author's captions
|
|
|
|
# start = timer()
|
|
|
|
shouts = list(map(lambda r: r.Shout, session.execute(q)))
|
|
|
|
for s in shouts:
|
|
|
|
s.stat = await ReactedStorage.get_shout_stat(s.slug)
|
|
|
|
for a in s.authors:
|
|
|
|
a.caption = await ShoutAuthorStorage.get_author_caption(s.slug, a.slug)
|
|
|
|
|
|
|
|
# end = timer()
|
|
|
|
# print(end - start)
|
|
|
|
# print(q)
|
|
|
|
|
|
|
|
return shouts
|
2022-01-30 11:28:27 +00:00
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
|
2022-07-21 11:58:50 +00:00
|
|
|
@mutation.field("follow")
|
2022-02-15 10:34:41 +00:00
|
|
|
@login_required
|
2022-07-21 11:58:50 +00:00
|
|
|
async def follow(_, info, what, slug):
|
2022-08-14 12:48:35 +00:00
|
|
|
user = info.context["request"].user
|
|
|
|
try:
|
|
|
|
if what == "AUTHOR":
|
|
|
|
author_follow(user, slug)
|
|
|
|
elif what == "TOPIC":
|
|
|
|
topic_follow(user, slug)
|
|
|
|
elif what == "COMMUNITY":
|
2022-11-15 02:36:30 +00:00
|
|
|
# community_follow(user, slug)
|
|
|
|
pass
|
2022-08-14 12:48:35 +00:00
|
|
|
elif what == "REACTIONS":
|
|
|
|
reactions_follow(user, slug)
|
|
|
|
except Exception as e:
|
2022-09-03 10:50:14 +00:00
|
|
|
return {"error": str(e)}
|
2022-08-14 12:48:35 +00:00
|
|
|
|
|
|
|
return {}
|
2022-01-31 11:34:43 +00:00
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
|
2022-07-21 11:58:50 +00:00
|
|
|
@mutation.field("unfollow")
|
2022-02-15 10:34:41 +00:00
|
|
|
@login_required
|
2022-07-21 11:58:50 +00:00
|
|
|
async def unfollow(_, info, what, slug):
|
2022-08-14 12:48:35 +00:00
|
|
|
user = info.context["request"].user
|
|
|
|
|
|
|
|
try:
|
|
|
|
if what == "AUTHOR":
|
|
|
|
author_unfollow(user, slug)
|
|
|
|
elif what == "TOPIC":
|
|
|
|
topic_unfollow(user, slug)
|
|
|
|
elif what == "COMMUNITY":
|
2022-11-15 02:36:30 +00:00
|
|
|
# community_unfollow(user, slug)
|
|
|
|
pass
|
2022-08-14 12:48:35 +00:00
|
|
|
elif what == "REACTIONS":
|
|
|
|
reactions_unfollow(user, slug)
|
|
|
|
except Exception as e:
|
2022-09-03 10:50:14 +00:00
|
|
|
return {"error": str(e)}
|
2022-08-14 12:48:35 +00:00
|
|
|
|
|
|
|
return {}
|