core/resolvers/zine.py

235 lines
7.3 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
from services.zine.shoutauthor import ShoutAuthorStorage
2022-09-14 09:45:31 +00:00
from services.zine.shoutscache import ShoutsCache, prepare_shouts
2022-08-11 09:09:57 +00:00
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
2022-09-04 17:20:38 +00:00
from sqlalchemy import select, desc, and_
from sqlalchemy.orm import selectinload
2022-09-03 10:50:14 +00:00
2021-08-08 12:23:12 +00:00
2022-09-08 16:46:02 +00:00
@mutation.field("incrementView")
async def increment_view(_, _info, shout):
async with ViewedStorage.lock:
return ViewedStorage.increment(shout)
2021-10-31 15:09:16 +00:00
@query.field("topViewed")
2022-09-14 09:45:31 +00:00
async def top_viewed(_, _info, offset, limit):
async with ShoutsCache.lock:
2022-09-14 09:45:31 +00:00
return ShoutsCache.top_viewed[offset : offset + limit]
2021-07-27 05:41:45 +00:00
2022-09-03 10:50:14 +00:00
2021-10-31 14:50:55 +00:00
@query.field("topMonth")
2022-09-14 09:45:31 +00:00
async def top_month(_, _info, offset, limit):
async with ShoutsCache.lock:
2022-09-14 09:45:31 +00:00
return ShoutsCache.top_month[offset : offset + limit]
2021-10-31 14:50:55 +00:00
2022-09-03 10:50:14 +00:00
2021-10-31 14:50:55 +00:00
@query.field("topOverall")
2022-09-14 09:45:31 +00:00
async def top_overall(_, _info, offset, limit):
async with ShoutsCache.lock:
2022-09-14 09:45:31 +00:00
return ShoutsCache.top_overall[offset : offset + limit]
2021-10-30 19:15:29 +00:00
2022-09-03 10:50:14 +00:00
2022-06-11 22:05:20 +00:00
@query.field("recentPublished")
2022-09-14 09:45:31 +00:00
async def recent_published(_, _info, offset, limit):
async with ShoutsCache.lock:
2022-09-14 09:45:31 +00:00
return ShoutsCache.top_overall[offset : offset + limit]
2021-09-01 16:03:00 +00:00
2022-09-03 10:50:14 +00:00
2022-06-11 22:05:20 +00:00
@query.field("recentAll")
2022-09-14 09:45:31 +00:00
async def recent_all(_, _info, offset, limit):
async with ShoutsCache.lock:
2022-09-14 09:45:31 +00:00
return ShoutsCache.recent_all[offset : offset + limit]
2022-06-11 22:05:20 +00:00
2022-09-03 10:50:14 +00:00
@query.field("recentReacted")
2022-09-14 09:45:31 +00:00
async def recent_reacted(_, _info, offset, limit):
async with ShoutsCache.lock:
2022-09-14 09:45:31 +00:00
return ShoutsCache.recent_all[offset : offset + limit]
2022-02-03 09:13:53 +00:00
2022-09-03 10:50:14 +00:00
2021-09-27 14:59:44 +00:00
@mutation.field("viewShout")
2022-09-07 17:17:25 +00:00
async def view_shout(_, _info, slug):
2022-09-07 16:19:06 +00:00
await ViewedStorage.increment(slug)
2022-09-03 10:50:14 +00:00
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-09-03 10:50:14 +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]
with local_session() as session:
2022-09-04 17:20:38 +00:00
# s = text(open("src/queries/shout-by-slug.sql", "r").read() % slug)
2022-09-03 10:50:14 +00:00
shout = (
session.query(Shout)
.options(select_options)
.filter(Shout.slug == slug)
.first()
)
2022-08-17 09:07:04 +00:00
if not shout:
print(f"shout with slug {slug} not exist")
2022-09-03 10:50:14 +00:00
return {"error": "shout not found"}
2022-08-17 09:07:04 +00:00
else:
for a in shout.authors:
a.caption = await ShoutAuthorStorage.get_author_caption(slug, a.slug)
return shout
2021-12-06 14:50:49 +00:00
2022-09-03 10:50:14 +00:00
2022-09-05 07:28:23 +00:00
@query.field("searchQuery")
2022-09-14 09:45:31 +00:00
async def get_search_results(_, _info, query, offset, limit):
2022-09-05 07:28:23 +00:00
# TODO: remove the copy of searchByTopics
# with search ranking query
with local_session() as session:
shouts = (
session.query(Shout)
.join(ShoutTopic)
.where(and_(ShoutTopic.topic.in_(query), bool(Shout.publishedAt)))
.order_by(desc(Shout.publishedAt))
2022-09-14 09:45:31 +00:00
.limit(limit)
.offset(offset)
2022-09-05 07:28:23 +00:00
)
for s in shouts:
for a in s.authors:
a.caption = await ShoutAuthorStorage.get_author_caption(s.slug, a.slug)
s.stat.search = 1 # FIXME
return shouts
2022-05-31 07:03:50 +00:00
@query.field("shoutsByTopics")
2022-09-14 09:45:31 +00:00
async def shouts_by_topics(_, _info, slugs, offset, limit):
with local_session() as session:
2022-09-14 09:45:31 +00:00
shouts = prepare_shouts(
2022-09-03 10:50:14 +00:00
session.query(Shout)
.join(ShoutTopic)
2022-09-04 17:20:38 +00:00
.where(and_(ShoutTopic.topic.in_(slugs), bool(Shout.publishedAt)))
2022-09-03 10:50:14 +00:00
.order_by(desc(Shout.publishedAt))
2022-09-14 09:45:31 +00:00
.limit(limit)
.offset(offset)
2022-09-03 10:50:14 +00:00
)
for s in shouts:
for a in s.authors:
a.caption = await ShoutAuthorStorage.get_author_caption(s.slug, a.slug)
return shouts
2022-09-03 10:50:14 +00:00
@query.field("shoutsByCollection")
2022-09-14 09:45:31 +00:00
async def shouts_by_collection(_, _info, collection, offset, limit):
with local_session() as session:
2022-09-14 09:45:31 +00:00
shouts = prepare_shouts(
2022-09-03 10:50:14 +00:00
session.query(Shout)
.join(ShoutCollection, ShoutCollection.collection == collection)
2022-09-04 17:20:38 +00:00
.where(and_(ShoutCollection.shout == Shout.slug, bool(Shout.publishedAt)))
2022-09-03 10:50:14 +00:00
.order_by(desc(Shout.publishedAt))
2022-09-14 09:45:31 +00:00
.limit(limit)
.offset(offset)
2022-09-03 10:50:14 +00:00
)
for s in shouts:
for a in s.authors:
a.caption = await ShoutAuthorStorage.get_author_caption(s.slug, a.slug)
return shouts
2022-09-03 10:50:14 +00:00
2022-05-31 07:03:50 +00:00
@query.field("shoutsByAuthors")
2022-09-14 09:45:31 +00:00
async def shouts_by_authors(_, _info, slugs, offset, limit):
with local_session() as session:
2022-09-03 10:50:14 +00:00
shouts = (
session.query(Shout)
.join(ShoutAuthor)
2022-09-04 17:20:38 +00:00
.where(and_(ShoutAuthor.user.in_(slugs), bool(Shout.publishedAt)))
2022-09-03 10:50:14 +00:00
.order_by(desc(Shout.publishedAt))
2022-09-14 09:45:31 +00:00
.limit(limit)
.offset(offset)
2022-09-03 10:50:14 +00:00
)
for s in shouts:
for a in s.authors:
a.caption = await ShoutAuthorStorage.get_author_caption(s.slug, a.slug)
return shouts
2022-09-03 10:50:14 +00:00
2022-08-17 07:59:17 +00:00
SINGLE_COMMUNITY = True
2022-09-03 10:50:14 +00:00
2022-05-31 07:03:50 +00:00
@query.field("shoutsByCommunities")
2022-09-14 09:45:31 +00:00
async def shouts_by_communities(_, info, slugs, offset, limit):
2022-09-03 10:50:14 +00:00
if SINGLE_COMMUNITY:
2022-09-14 09:45:31 +00:00
return recent_published(_, info, offset, limit)
2022-08-17 07:59:17 +00:00
else:
with local_session() as session:
2022-09-03 10:50:14 +00:00
# TODO fix postgres high load
shouts = (
session.query(Shout)
.distinct()
.join(ShoutTopic)
.where(
and_(
2022-09-04 17:20:38 +00:00
bool(Shout.publishedAt),
2022-09-03 10:50:14 +00:00
ShoutTopic.topic.in_(
select(Topic.slug).where(Topic.community.in_(slugs))
),
)
)
.order_by(desc(Shout.publishedAt))
2022-09-14 09:45:31 +00:00
.limit(limit)
.offset(offset)
2022-09-03 10:50:14 +00:00
)
2022-08-17 07:59:17 +00:00
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-09-03 10:50:14 +00:00
@mutation.field("follow")
@login_required
async def follow(_, info, what, slug):
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:
2022-09-03 10:50:14 +00:00
return {"error": str(e)}
return {}
2022-09-03 10:50:14 +00:00
@mutation.field("unfollow")
@login_required
async def unfollow(_, info, what, slug):
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:
2022-09-03 10:50:14 +00:00
return {"error": str(e)}
return {}