From 037c111b1dbbf5d68a88e0f2ead1a5266e6001c3 Mon Sep 17 00:00:00 2001 From: tonyrewin Date: Wed, 5 Oct 2022 14:54:04 +0300 Subject: [PATCH] unique-fix --- resolvers/zine.py | 41 +++++++++++++++++------------------- services/zine/shoutscache.py | 6 ++++-- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/resolvers/zine.py b/resolvers/zine.py index 19082cbb..030554bc 100644 --- a/resolvers/zine.py +++ b/resolvers/zine.py @@ -111,37 +111,34 @@ async def get_search_results(_, _info, searchtext, offset, limit): @query.field("shoutsByAuthors") async def shouts_by_authors(_, _info, slugs, offset, limit): - shouts = [] async with ShoutsCache.lock: + shouts = {} for author in slugs: - shouts.extend(ShoutsCache.by_author.get(author, [])) - shouts_prepared = [] - for s in shouts: - if bool(s.publishedAt): - for a in s.authors: - a.caption = await ShoutAuthorStorage.get_author_caption(s.slug, a.slug) - if s not in shouts_prepared: - shouts_prepared.append(s) + for shouts_by_author in ShoutsCache.by_author.get(author, []): + for s in shouts_by_author: + 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 = shouts.values() shouts_prepared.sort(key=lambda s: s.publishedAt, reverse=True) return shouts_prepared[offset : offset + limit] @query.field("shoutsByTopics") -async def shouts_by_topics(_, _info, slugs, offset, limit): - shouts = [] +async def shouts_by_topics(_, _info, slugs, offset=0, limit=100): async with ShoutsCache.lock: + shouts = {} for topic in slugs: - shouts.extend(ShoutsCache.by_topic.get(topic, [])) - shouts_prepared = [] - for s in shouts: - if bool(s.publishedAt): - for a in s.authors: - a.caption = await ShoutAuthorStorage.get_author_caption(s.slug, a.slug) - if s not in shouts_prepared: - shouts_prepared.append(s) - shouts_prepared = list(set(shouts_prepared)) - shouts_prepared.sort(key=lambda s: s.publishedAt, reverse=True) - return shouts_prepared[offset : offset + limit] + for shouts_by_topic in ShoutsCache.by_topic.get(topic, []): + for s in shouts_by_topic: + 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 = shouts.values() + shouts_prepared.sort(key=lambda s: s.publishedAt, reverse=True) + return shouts_prepared[offset : offset + limit] @query.field("shoutsByCollection") diff --git a/services/zine/shoutscache.py b/services/zine/shoutscache.py index c39823b1..781a3a5b 100644 --- a/services/zine/shoutscache.py +++ b/services/zine/shoutscache.py @@ -66,10 +66,12 @@ class ShoutsCache: for s in shouts: for a in s.authors: ShoutsCache.by_author[a.slug] = ShoutsCache.by_author.get(a.slug, []) - ShoutsCache.by_author[a.slug].append(s) + if s not in ShoutsCache.by_author[a.slug]: + ShoutsCache.by_author[a.slug].append(s) for t in s.topics: ShoutsCache.by_topic[t.slug] = ShoutsCache.by_topic.get(t.slug, []) - ShoutsCache.by_topic[t.slug].append(s) + if s not in ShoutsCache.by_topic[t.slug]: + ShoutsCache.by_topic[t.slug].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())) ShoutsCache.recent_published = shouts