shoutsByLayout

This commit is contained in:
tonyrewin 2022-11-13 07:34:02 +03:00
parent 8914646f61
commit 785fbc0a2c
3 changed files with 26 additions and 0 deletions

View File

@ -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",

View File

@ -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:

View File

@ -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))