diff --git a/resolvers/author.py b/resolvers/author.py index 771e3de9..178073a8 100644 --- a/resolvers/author.py +++ b/resolvers/author.py @@ -303,29 +303,36 @@ async def get_author_followers(_, _info, slug: str): author_alias = aliased(Author) author_query = select(author_alias).filter(author_alias.slug == slug) result = local_session().execute(author_query).first() - if result: - [author] = result - author_id = author.id - cached = await redis.execute("GET", f"author:{author_id}:followers") - if not cached: - author_follower_alias = aliased(AuthorFollower, name="af") - q = select(Author).join( - author_follower_alias, - and_( - author_follower_alias.author == author_id, - author_follower_alias.follower == Author.id, - ), - ) - results = get_with_stat(q) - if isinstance(results, list): - for follower in results: - await cache_follower(follower.dict(), author.dict()) - logger.debug(f"@{slug} cache updated with {len(results)} followers") - return results - else: - logger.debug(f"@{slug} got followers cached") - if isinstance(cached, str): - return json.loads(cached) + + [author] = result + author_id = author.id + cached = await redis.execute("GET", f"author:{author_id}:followers") + if cached: + logger.debug(f"@{slug} got followers cached") + followers_ids = [] + followers = [] + if isinstance(cached, str): + followers_cached = json.loads(cached) + if isinstance(followers_cached, list): + for fc in followers_cached: + if fc['id'] not in followers_ids: + followers.append(fc) + return followers + + author_follower_alias = aliased(AuthorFollower, name="af") + q = select(Author).join( + author_follower_alias, + and_( + author_follower_alias.author == author_id, + author_follower_alias.follower == Author.id, + ), + ) + results = get_with_stat(q) + if isinstance(results, list): + for follower in results: + await cache_follower(follower.dict(), author.dict()) + logger.debug(f"@{slug} cache updated with {len(results)} followers") + return results except Exception as exc: import traceback