From a8313d1324ac79ec8670e07875445f2ba5184536 Mon Sep 17 00:00:00 2001 From: tonyrewin Date: Mon, 19 Sep 2022 19:14:20 +0300 Subject: [PATCH] recent-commented --- resolvers/__init__.py | 5 +++++ resolvers/zine.py | 6 ++++++ schema.graphql | 3 ++- services/zine/shoutscache.py | 31 ++++++++++++++++++++++++++++++- 4 files changed, 43 insertions(+), 2 deletions(-) diff --git a/resolvers/__init__.py b/resolvers/__init__.py index 84313eba..638122c6 100644 --- a/resolvers/__init__.py +++ b/resolvers/__init__.py @@ -40,6 +40,7 @@ from resolvers.topics import ( topics_by_community, topics_all, ) + from resolvers.zine import ( get_shout_by_slug, follow, @@ -49,6 +50,8 @@ from resolvers.zine import ( top_overall, recent_published, recent_all, + recent_commented, + recent_reacted, top_viewed, shouts_by_authors, shouts_by_topics, @@ -72,6 +75,8 @@ __all__ = [ "get_top_authors", # zine "recent_published", + "recent_commented", + "recent_reacted", "recent_all", "shouts_by_topics", "shouts_by_authors", diff --git a/resolvers/zine.py b/resolvers/zine.py index 1999f02c..a0827f12 100644 --- a/resolvers/zine.py +++ b/resolvers/zine.py @@ -64,6 +64,12 @@ async def recent_reacted(_, _info, offset, limit): return ShoutsCache.recent_reacted[offset : offset + limit] +@query.field("recentCommented") +async def recent_commented(_, _info, offset, limit): + async with ShoutsCache.lock: + return ShoutsCache.recent_commented[offset : offset + limit] + + @query.field("getShoutBySlug") async def get_shout_by_slug(_, info, slug): all_fields = [ diff --git a/schema.graphql b/schema.graphql index 6705e4ee..68a7d3cd 100644 --- a/schema.graphql +++ b/schema.graphql @@ -236,7 +236,8 @@ type Query { topOverall(offset: Int!, limit: Int!): [Shout]! topCommented(offset: Int!, limit: Int!): [Shout]! recentPublished(offset: Int!, limit: Int!): [Shout]! # homepage - recentReacted(offset: Int!, limit: Int!): [Shout]! # test + recentReacted(offset: Int!, limit: Int!): [Shout]! + recentCommented(offset: Int!, limit: Int!): [Shout]! recentAll(offset: Int!, limit: Int!): [Shout]! # reactons diff --git a/services/zine/shoutscache.py b/services/zine/shoutscache.py index 2ebc20b4..fa9bbaab 100644 --- a/services/zine/shoutscache.py +++ b/services/zine/shoutscache.py @@ -36,6 +36,7 @@ class ShoutsCache: recent_published = [] recent_all = [] recent_reacted = [] + recent_commented = [] top_month = [] top_overall = [] top_viewed = [] @@ -102,7 +103,7 @@ class ShoutsCache: selectinload(Shout.authors), selectinload(Shout.topics), ) - .where(and_(bool(Shout.publishedAt), Shout.slug.in_(list(reacted_slugs)))) + .where(Shout.slug.in_(list(reacted_slugs))) .filter(not bool(Shout.deletedAt)) .group_by(Shout.slug) .order_by(Shout.publishedAt) @@ -113,6 +114,33 @@ class ShoutsCache: ShoutsCache.recent_reacted = shouts print("[zine.cache] %d recently reacted shouts " % len(shouts)) + @staticmethod + async def prepare_recent_commented(): + with local_session() as session: + reactions = session.query(Reaction).order_by(Reaction.createdAt).limit(ShoutsCache.limit) + commented_slugs = set([]) + for r in reactions: + if bool(r.body): + commented_slugs.add(r.shout) + shouts = await prepare_shouts( + session, + ( + select(Shout) + .options( + selectinload(Shout.authors), + selectinload(Shout.topics), + ) + .where(Shout.slug.in_(list(commented_slugs))) + .filter(not bool(Shout.deletedAt)) + .group_by(Shout.slug) + .order_by(Shout.publishedAt) + .limit(ShoutsCache.limit) + ) + ) + async with ShoutsCache.lock: + ShoutsCache.recent_commented = shouts + print("[zine.cache] %d recently commented shouts " % len(shouts)) + @staticmethod async def prepare_top_overall(): with local_session() as session: @@ -240,6 +268,7 @@ class ShoutsCache: await ShoutsCache.prepare_recent_published() await ShoutsCache.prepare_recent_all() await ShoutsCache.prepare_recent_reacted() + await ShoutsCache.prepare_recent_commented() await ShoutsCache.prepare_by_author() await ShoutsCache.prepare_by_topic()