From 785fbc0a2c05b9e97842c10c991b159c53cdf926 Mon Sep 17 00:00:00 2001 From: tonyrewin Date: Sun, 13 Nov 2022 07:34:02 +0300 Subject: [PATCH] shoutsByLayout --- resolvers/__init__.py | 2 ++ resolvers/zine.py | 16 ++++++++++++++++ services/zine/shoutscache.py | 8 ++++++++ 3 files changed, 26 insertions(+) diff --git a/resolvers/__init__.py b/resolvers/__init__.py index bebee337..43accd1e 100644 --- a/resolvers/__init__.py +++ b/resolvers/__init__.py @@ -57,6 +57,7 @@ from resolvers.zine import ( recent_reacted, shouts_by_authors, shouts_by_topics, + shouts_by_layout, shouts_by_communities, ) @@ -91,6 +92,7 @@ __all__ = [ "recent_reacted", "recent_all", "shouts_by_topics", + "shouts_by_layout", "shouts_by_authors", "shouts_by_communities", "get_user_reacted_shouts", diff --git a/resolvers/zine.py b/resolvers/zine.py index dc6eac8e..7fa3be87 100644 --- a/resolvers/zine.py +++ b/resolvers/zine.py @@ -125,6 +125,22 @@ async def shouts_by_authors(_, _info, slugs, offset=0, limit=100): return shouts_prepared[offset : offset + limit] +@query.field("shoutsByLayout") +async def shouts_by_layout(_, _info, layout, amount=100, offset=0): + async with ShoutsCache.lock: + shouts = {} + # for layout in ['image', 'audio', 'video', 'literature']: + shouts_by_layout = list(ShoutsCache.by_layout.get(layout, [])) + for s in shouts_by_layout: + for a in s.authors: + a.caption = await ShoutAuthorStorage.get_author_caption(s.slug, a.slug) + # if bool(s.publishedAt): + shouts[s.slug] = s + shouts_prepared = list(shouts.values()) + shouts_prepared.sort(key=lambda s: s.createdAt, reverse=True) + return shouts_prepared[offset : offset + amount] + + @query.field("shoutsByTopics") async def shouts_by_topics(_, _info, slugs, offset=0, limit=100): async with ShoutsCache.lock: diff --git a/services/zine/shoutscache.py b/services/zine/shoutscache.py index 4b851f88..442c09ab 100644 --- a/services/zine/shoutscache.py +++ b/services/zine/shoutscache.py @@ -29,6 +29,9 @@ async def prepare_shouts(session, stmt): return shouts +LAYOUTS = ['audio', 'video', 'image', 'literature'] + + class ShoutsCache: # limit = 200 period = 60 * 60 # 1 hour @@ -44,6 +47,7 @@ class ShoutsCache: by_author = {} by_topic = {} + by_layout = {} @staticmethod async def prepare_recent_published(): @@ -71,8 +75,12 @@ class ShoutsCache: for t in s.topics: ShoutsCache.by_topic[t.slug] = ShoutsCache.by_topic.get(t.slug, {}) ShoutsCache.by_topic[t.slug][s.slug] = s + if s.layout in LAYOUTS: + ShoutsCache.by_layout[s.layout] = ShoutsCache.by_layout.get(s.layout, []) + ShoutsCache.by_layout[s.layout].append(s) print("[zine.cache] indexed by %d topics " % len(ShoutsCache.by_topic.keys())) print("[zine.cache] indexed by %d authors " % len(ShoutsCache.by_author.keys())) + print("[zine.cache] indexed by %d layouts " % len(ShoutsCache.by_layout.keys())) ShoutsCache.recent_published = shouts print("[zine.cache] %d recently published shouts " % len(shouts))