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,11 +303,22 @@ async def get_author_followers(_, _info, slug: str):
author_alias = aliased(Author) author_alias = aliased(Author)
author_query = select(author_alias).filter(author_alias.slug == slug) author_query = select(author_alias).filter(author_alias.slug == slug)
result = local_session().execute(author_query).first() result = local_session().execute(author_query).first()
if result:
[author] = result [author] = result
author_id = author.id author_id = author.id
cached = await redis.execute("GET", f"author:{author_id}:followers") cached = await redis.execute("GET", f"author:{author_id}:followers")
if not cached: 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") author_follower_alias = aliased(AuthorFollower, name="af")
q = select(Author).join( q = select(Author).join(
author_follower_alias, author_follower_alias,
@ -322,10 +333,6 @@ async def get_author_followers(_, _info, slug: str):
await cache_follower(follower.dict(), author.dict()) await cache_follower(follower.dict(), author.dict())
logger.debug(f"@{slug} cache updated with {len(results)} followers") logger.debug(f"@{slug} cache updated with {len(results)} followers")
return results return results
else:
logger.debug(f"@{slug} got followers cached")
if isinstance(cached, str):
return json.loads(cached)
except Exception as exc: except Exception as exc:
import traceback import traceback