2022-11-13 16:25:57 +00:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
2022-09-17 18:12:14 +00:00
|
|
|
from sqlalchemy.orm import selectinload
|
2022-11-15 02:36:30 +00:00
|
|
|
from sqlalchemy.sql.expression import desc, select
|
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
|
|
|
|
from orm.reaction import Reaction
|
|
|
|
# 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")
|
|
|
|
async def load_shouts_by(_, info, by, amount=50, offset=0):
|
|
|
|
"""
|
|
|
|
:param by: {
|
|
|
|
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',
|
|
|
|
stat: 'rating' | 'comments' | 'reacted' | 'views',
|
|
|
|
days: 30
|
|
|
|
}
|
|
|
|
:param amount: int amount of shouts
|
|
|
|
:param offset: int offset in this order
|
|
|
|
:return: Shout[]
|
|
|
|
"""
|
|
|
|
|
|
|
|
q = select(Shout, Reaction).options(
|
|
|
|
selectinload(Shout.authors),
|
|
|
|
selectinload(Shout.topics),
|
|
|
|
selectinload(Shout.reactions)
|
|
|
|
).where(
|
|
|
|
Shout.deletedAt.is_(None)
|
|
|
|
).join(
|
|
|
|
Reaction, Reaction.shout == Shout.slug
|
|
|
|
)
|
|
|
|
if by.get("slug"):
|
|
|
|
q = q.filter(Shout.slug == by["slug"])
|
2022-08-17 07:59:17 +00:00
|
|
|
else:
|
2022-11-15 02:36:30 +00:00
|
|
|
if by.get("reacted"):
|
|
|
|
user = info.context["request"].user
|
|
|
|
q = q.filter(Reaction.createdBy == user.slug)
|
2022-11-15 10:30:09 +00:00
|
|
|
if by.get("visibility"):
|
|
|
|
q = q.filter(Shout.visibility == by.get("visibility") or "public")
|
2022-11-15 02:36:30 +00:00
|
|
|
if by.get("layout"):
|
|
|
|
q = q.filter(Shout.layout == by["layout"])
|
|
|
|
if by.get("author"):
|
|
|
|
q = q.filter(Shout.authors.contains(by["author"]))
|
|
|
|
if by.get("topic"):
|
|
|
|
q = q.filter(Shout.topics.contains(by["topic"]))
|
|
|
|
if by.get("title"):
|
|
|
|
q = q.filter(Shout.title.ilike(f'%{by["title"]}%'))
|
|
|
|
if by.get("body"):
|
|
|
|
q = q.filter(Shout.body.ilike(f'%{by["body"]}%'))
|
|
|
|
if by.get("days"):
|
|
|
|
before = datetime.now() - timedelta(days=int(by["days"]) or 30)
|
|
|
|
q = q.filter(Shout.createdAt > before)
|
2022-11-15 10:25:36 +00:00
|
|
|
q = q.group_by(Shout.id, Reaction.id).order_by(
|
2022-11-15 02:36:30 +00:00
|
|
|
desc(by.get("order") or "createdAt")
|
|
|
|
).limit(amount).offset(offset)
|
2022-11-15 12:04:22 +00:00
|
|
|
print(q)
|
2022-11-15 02:36:30 +00:00
|
|
|
shouts = []
|
|
|
|
with local_session() as session:
|
|
|
|
# post query stats and author's captions
|
|
|
|
for s in list(map(lambda r: r.Shout, session.execute(q))):
|
|
|
|
s.stat = await ReactedStorage.get_shout_stat(s.slug)
|
2022-08-17 07:59:17 +00:00
|
|
|
for a in s.authors:
|
|
|
|
a.caption = await ShoutAuthorStorage.get_author_caption(s.slug, a.slug)
|
2022-11-15 02:36:30 +00:00
|
|
|
shouts.append(s)
|
|
|
|
if by.get("stat"):
|
|
|
|
shouts.sort(lambda s: s.stat.get(by["stat"]) or s.createdAt)
|
|
|
|
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 {}
|