followers-fix
All checks were successful
Deploy on push / deploy (push) Successful in 27s

This commit is contained in:
Untone 2024-05-06 12:38:39 +03:00
parent f6b21174bf
commit c46f264c4b

View File

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