core/resolvers/zine.py

162 lines
4.8 KiB
Python
Raw Normal View History

from orm.collection import ShoutCollection
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-11 09:09:57 +00:00
from services.zine.shoutscache import ShoutsCache
from services.stat.viewed import ViewedStorage
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
from sqlalchemy import select, desc, and_
2022-08-09 02:29:30 +00:00
from sqlalchemy.orm import selectinload, joinedload
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):
2021-10-31 17:55:59 +00:00
async with ShoutsCache.lock:
2022-01-08 09:09:06 +00:00
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):
2021-10-31 17:55:59 +00:00
async with ShoutsCache.lock:
2022-01-08 09:09:06 +00:00
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):
2021-10-31 17:55:59 +00:00
async with ShoutsCache.lock:
2022-01-08 09:09:06 +00:00
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):
2021-10-31 17:55:59 +00:00
async with ShoutsCache.lock:
2022-06-12 06:10:00 +00:00
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):
async with ShoutsCache.lock:
return ShoutsCache.recent_all[(page - 1) * size : page * size]
@query.field("recentReacted")
async def recent_reacted(_, info, page, size):
2022-02-03 09:13:53 +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):
await ViewedStorage.inc_shout(slug)
2021-09-27 14:59:44 +00:00
return {"error" : ""}
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-09 02:29:30 +00:00
shout = None
2022-08-11 11:22:10 +00:00
# FIXME: append captions anyhow
2021-09-03 07:16:19 +00:00
with local_session() as session:
2022-08-13 16:19:16 +00:00
shout = session.query(Shout, ShoutAuthor.caption.label("author_caption")).\
2022-08-09 02:29:30 +00:00
options([
selectinload(Shout.topics),
selectinload(Shout.reactions),
2022-08-13 16:19:16 +00:00
joinedload(Shout.authors),
2022-08-09 10:17:31 +00:00
selectinload(ShoutAuthor.caption)
2022-08-09 02:29:30 +00:00
]).\
2022-08-13 16:19:16 +00:00
join(ShoutAuthor.shout == slug ).\
2021-09-24 14:39:37 +00:00
filter(Shout.slug == slug).first()
if not shout:
print(f"[resolvers.zine] error: shout with slug {slug} not exist")
2022-08-11 11:22:10 +00:00
return {"error" : "shout not found"}
2021-09-05 08:56:15 +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-01-08 09:09:06 +00:00
page = page - 1
with local_session() as session:
shouts = session.query(Shout).\
join(ShoutTopic).\
2022-05-31 07:03:50 +00:00
where(and_(ShoutTopic.topic.in_(slugs), Shout.publishedAt != None)).\
2021-12-17 14:56:05 +00:00
order_by(desc(Shout.publishedAt)).\
limit(size).\
offset(page * size)
return shouts
@query.field("shoutsByCollection")
async def shouts_by_topics(_, info, collection, page, size):
page = page - 1
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)
return shouts
2022-05-31 07:03:50 +00:00
@query.field("shoutsByAuthors")
async def shouts_by_authors(_, info, slugs, page, size):
2022-01-08 09:09:06 +00:00
page = page - 1
with local_session() as session:
shouts = session.query(Shout).\
join(ShoutAuthor).\
2022-05-31 07:03:50 +00:00
where(and_(ShoutAuthor.user.in_(slugs), Shout.publishedAt != None)).\
2021-12-17 14:56:05 +00:00
order_by(desc(Shout.publishedAt)).\
limit(size).\
offset(page * size)
return shouts
2022-05-31 07:03:50 +00:00
@query.field("shoutsByCommunities")
async def shouts_by_communities(_, info, slugs, page, size):
2022-01-08 09:09:06 +00:00
page = page - 1
with local_session() as session:
2021-12-17 14:27:16 +00:00
#TODO fix postgres high load
shouts = session.query(Shout).distinct().\
join(ShoutTopic).\
2021-12-17 14:56:05 +00:00
where(and_(Shout.publishedAt != None,\
ShoutTopic.topic.in_(\
2022-05-31 07:03:50 +00:00
select(Topic.slug).where(Topic.community.in_(slugs))\
2021-12-17 14:56:05 +00:00
))).\
order_by(desc(Shout.publishedAt)).\
limit(size).\
offset(page * size)
return shouts
2022-01-30 11:28:27 +00:00
@mutation.field("follow")
@login_required
async def follow(_, info, what, slug):
user = info.context["request"].user
2022-06-19 17:54:39 +00:00
try:
2022-06-29 10:47:53 +00:00
if what == "AUTHOR":
author_follow(user, slug)
2022-06-29 10:47:53 +00:00
elif what == "TOPIC":
topic_follow(user, slug)
2022-06-29 10:47:53 +00:00
elif what == "COMMUNITY":
community_follow(user, slug)
elif what == "REACTIONS":
reactions_follow(user, slug)
2022-06-19 17:54:39 +00:00
except Exception as e:
2022-06-29 12:22:14 +00:00
return {"error" : str(e)}
2022-06-19 17:54:39 +00:00
return {}
@mutation.field("unfollow")
@login_required
async def unfollow(_, info, what, slug):
user = info.context["request"].user
2022-01-30 11:28:27 +00:00
2022-06-19 17:54:39 +00:00
try:
2022-06-29 10:47:53 +00:00
if what == "AUTHOR":
author_unfollow(user, slug)
2022-06-29 10:47:53 +00:00
elif what == "TOPIC":
topic_unfollow(user, slug)
2022-06-29 10:47:53 +00:00
elif what == "COMMUNITY":
community_unfollow(user, slug)
elif what == "REACTIONS":
reactions_unfollow(user, slug)
2022-06-19 17:54:39 +00:00
except Exception as e:
2022-06-29 12:22:14 +00:00
return {"error" : str(e)}
2022-01-31 09:16:46 +00:00
2022-06-19 17:54:39 +00:00
return {}