2022-08-13 09:48:07 +00:00
|
|
|
from orm.collection import ShoutCollection
|
2022-07-21 11:58:50 +00:00
|
|
|
from orm.shout import Shout, ShoutAuthor, ShoutTopic
|
|
|
|
from orm.topic import Topic
|
2022-08-11 05:53:14 +00:00
|
|
|
from base.orm import local_session
|
|
|
|
from base.resolvers import mutation, query
|
2022-08-14 12:48:35 +00:00
|
|
|
from services.zine.shoutauthor import ShoutAuthorStorage
|
2022-08-11 09:09:57 +00:00
|
|
|
from services.zine.shoutscache import ShoutsCache
|
|
|
|
from services.stat.viewed import ViewedStorage
|
2022-07-21 11:58:50 +00:00
|
|
|
from resolvers.profile import author_follow, author_unfollow
|
|
|
|
from resolvers.topics import topic_follow, topic_unfollow
|
|
|
|
from resolvers.community import community_follow, community_unfollow
|
|
|
|
from resolvers.reactions import reactions_follow, reactions_unfollow
|
2021-08-07 16:14:20 +00:00
|
|
|
from auth.authenticate import login_required
|
2022-08-14 12:48:35 +00:00
|
|
|
from sqlalchemy import select, desc, and_, text
|
|
|
|
from sqlalchemy.orm import selectinload
|
|
|
|
from sqlalchemy.dialects import postgresql
|
2021-08-08 12:23:12 +00:00
|
|
|
|
2021-10-31 15:09:16 +00:00
|
|
|
@query.field("topViewed")
|
2021-12-17 18:14:31 +00:00
|
|
|
async def top_viewed(_, info, page, size):
|
2022-08-14 12:48:35 +00:00
|
|
|
async with ShoutsCache.lock:
|
|
|
|
return ShoutsCache.top_viewed[(page - 1) * size : page * size]
|
2021-07-27 05:41:45 +00:00
|
|
|
|
2021-10-31 14:50:55 +00:00
|
|
|
@query.field("topMonth")
|
2021-12-17 18:14:31 +00:00
|
|
|
async def top_month(_, info, page, size):
|
2022-08-14 12:48:35 +00:00
|
|
|
async with ShoutsCache.lock:
|
|
|
|
return ShoutsCache.top_month[(page - 1) * size : page * size]
|
2021-10-31 14:50:55 +00:00
|
|
|
|
|
|
|
@query.field("topOverall")
|
2021-12-17 18:14:31 +00:00
|
|
|
async def top_overall(_, info, page, size):
|
2022-08-14 12:48:35 +00:00
|
|
|
async with ShoutsCache.lock:
|
|
|
|
return ShoutsCache.top_overall[(page - 1) * size : page * size]
|
2021-10-30 19:15:29 +00:00
|
|
|
|
2022-06-11 22:05:20 +00:00
|
|
|
@query.field("recentPublished")
|
2022-06-12 06:10:00 +00:00
|
|
|
async def recent_published(_, info, page, size):
|
2022-08-14 12:48:35 +00:00
|
|
|
async with ShoutsCache.lock:
|
|
|
|
return ShoutsCache.recent_published[(page - 1) * size : page * size]
|
2021-09-01 16:03:00 +00:00
|
|
|
|
2022-06-11 22:05:20 +00:00
|
|
|
@query.field("recentAll")
|
|
|
|
async def recent_all(_, info, page, size):
|
2022-08-14 12:48:35 +00:00
|
|
|
async with ShoutsCache.lock:
|
|
|
|
return ShoutsCache.recent_all[(page - 1) * size : page * size]
|
2022-06-11 22:05:20 +00:00
|
|
|
|
2022-07-21 11:58:50 +00:00
|
|
|
@query.field("recentReacted")
|
|
|
|
async def recent_reacted(_, info, page, size):
|
2022-08-14 12:48:35 +00:00
|
|
|
async with ShoutsCache.lock:
|
|
|
|
return ShoutsCache.recent_reacted[(page - 1) * size : page * size]
|
2022-02-03 09:13:53 +00:00
|
|
|
|
2021-09-27 14:59:44 +00:00
|
|
|
@mutation.field("viewShout")
|
2021-12-13 07:50:33 +00:00
|
|
|
async def view_shout(_, info, slug):
|
2022-08-14 12:48:35 +00:00
|
|
|
await ViewedStorage.inc_shout(slug)
|
|
|
|
return {"error" : ""}
|
2021-09-27 14:59:44 +00:00
|
|
|
|
2021-09-24 14:39:37 +00:00
|
|
|
@query.field("getShoutBySlug")
|
2021-09-05 07:16:28 +00:00
|
|
|
async def get_shout_by_slug(_, info, slug):
|
2022-08-14 12:48:35 +00:00
|
|
|
all_fields = [node.name.value for node in info.field_nodes[0].selection_set.selections]
|
|
|
|
selected_fields = set(["authors", "topics"]).intersection(all_fields)
|
|
|
|
select_options = [selectinload(getattr(Shout, field)) for field in selected_fields]
|
2022-08-17 09:07:04 +00:00
|
|
|
shout = {}
|
2022-08-14 12:48:35 +00:00
|
|
|
with local_session() as session:
|
|
|
|
try: s = text(open('src/queries/shout-by-slug.sql', 'r').read() % slug)
|
|
|
|
except: pass
|
2022-08-17 09:07:04 +00:00
|
|
|
shout = session.query(Shout).\
|
2022-08-14 12:48:35 +00:00
|
|
|
options(select_options).\
|
2022-08-17 09:07:04 +00:00
|
|
|
filter(Shout.slug == slug).first()
|
2022-08-14 12:48:35 +00:00
|
|
|
|
2022-08-17 09:07:04 +00:00
|
|
|
if not shout:
|
|
|
|
print(f"shout with slug {slug} not exist")
|
|
|
|
return {"error" : "shout not found"}
|
|
|
|
else:
|
|
|
|
for a in shout.authors:
|
|
|
|
a.caption = await ShoutAuthorStorage.get_author_caption(slug, a.slug)
|
2022-08-14 12:48:35 +00:00
|
|
|
return shout
|
2021-12-06 14:50:49 +00:00
|
|
|
|
2022-05-31 07:03:50 +00:00
|
|
|
@query.field("shoutsByTopics")
|
|
|
|
async def shouts_by_topics(_, info, slugs, page, size):
|
2022-08-14 12:48:35 +00:00
|
|
|
page = page - 1
|
|
|
|
with local_session() as session:
|
|
|
|
shouts = session.query(Shout).\
|
|
|
|
join(ShoutTopic).\
|
|
|
|
where(and_(ShoutTopic.topic.in_(slugs), Shout.publishedAt != None)).\
|
|
|
|
order_by(desc(Shout.publishedAt)).\
|
|
|
|
limit(size).\
|
|
|
|
offset(page * size)
|
|
|
|
|
|
|
|
for s in shouts:
|
|
|
|
for a in s.authors:
|
|
|
|
a.caption = await ShoutAuthorStorage.get_author_caption(s.slug, a.slug)
|
|
|
|
return shouts
|
2021-12-15 17:24:04 +00:00
|
|
|
|
2022-08-13 09:48:07 +00:00
|
|
|
@query.field("shoutsByCollection")
|
|
|
|
async def shouts_by_topics(_, info, collection, page, size):
|
2022-08-14 12:48:35 +00:00
|
|
|
page = page - 1
|
|
|
|
shouts = []
|
|
|
|
with local_session() as session:
|
|
|
|
shouts = session.query(Shout).\
|
|
|
|
join(ShoutCollection, ShoutCollection.collection == collection).\
|
|
|
|
where(and_(ShoutCollection.shout == Shout.slug, Shout.publishedAt != None)).\
|
|
|
|
order_by(desc(Shout.publishedAt)).\
|
|
|
|
limit(size).\
|
|
|
|
offset(page * size)
|
|
|
|
for s in shouts:
|
|
|
|
for a in s.authors:
|
|
|
|
a.caption = await ShoutAuthorStorage.get_author_caption(s.slug, a.slug)
|
|
|
|
return shouts
|
2022-08-13 09:48:07 +00:00
|
|
|
|
2022-05-31 07:03:50 +00:00
|
|
|
@query.field("shoutsByAuthors")
|
|
|
|
async def shouts_by_authors(_, info, slugs, page, size):
|
2022-08-14 12:48:35 +00:00
|
|
|
page = page - 1
|
|
|
|
with local_session() as session:
|
|
|
|
|
|
|
|
shouts = session.query(Shout).\
|
|
|
|
join(ShoutAuthor).\
|
|
|
|
where(and_(ShoutAuthor.user.in_(slugs), Shout.publishedAt != None)).\
|
|
|
|
order_by(desc(Shout.publishedAt)).\
|
|
|
|
limit(size).\
|
|
|
|
offset(page * size)
|
|
|
|
|
|
|
|
for s in shouts:
|
|
|
|
for a in s.authors:
|
|
|
|
a.caption = await ShoutAuthorStorage.get_author_caption(s.slug, a.slug)
|
|
|
|
return shouts
|
2021-12-15 17:24:04 +00:00
|
|
|
|
2022-08-17 07:59:17 +00:00
|
|
|
SINGLE_COMMUNITY = True
|
|
|
|
|
2022-05-31 07:03:50 +00:00
|
|
|
@query.field("shoutsByCommunities")
|
|
|
|
async def shouts_by_communities(_, info, slugs, page, size):
|
2022-08-17 07:59:17 +00:00
|
|
|
if SINGLE_COMMUNITY:
|
|
|
|
return recent_published(_, info, page, size)
|
|
|
|
else:
|
|
|
|
page = page - 1
|
|
|
|
with local_session() as session:
|
|
|
|
#TODO fix postgres high load
|
|
|
|
shouts = session.query(Shout).distinct().\
|
|
|
|
join(ShoutTopic).\
|
|
|
|
where(and_(Shout.publishedAt != None,\
|
|
|
|
ShoutTopic.topic.in_(\
|
|
|
|
select(Topic.slug).where(Topic.community.in_(slugs))\
|
|
|
|
))).\
|
|
|
|
order_by(desc(Shout.publishedAt)).\
|
|
|
|
limit(size).\
|
|
|
|
offset(page * size)
|
|
|
|
|
|
|
|
for s in shouts:
|
|
|
|
for a in s.authors:
|
|
|
|
a.caption = await ShoutAuthorStorage.get_author_caption(s.slug, a.slug)
|
|
|
|
return shouts
|
2022-01-30 11:28:27 +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":
|
|
|
|
community_follow(user, slug)
|
|
|
|
elif what == "REACTIONS":
|
|
|
|
reactions_follow(user, slug)
|
|
|
|
except Exception as e:
|
|
|
|
return {"error" : str(e)}
|
|
|
|
|
|
|
|
return {}
|
2022-01-31 11:34:43 +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":
|
|
|
|
community_unfollow(user, slug)
|
|
|
|
elif what == "REACTIONS":
|
|
|
|
reactions_unfollow(user, slug)
|
|
|
|
except Exception as e:
|
|
|
|
return {"error" : str(e)}
|
|
|
|
|
|
|
|
return {}
|