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_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:
author_follower_alias = aliased(AuthorFollower, name="af") logger.debug(f"@{slug} got followers cached")
q = select(Author).join( followers_ids = []
author_follower_alias, followers = []
and_( if isinstance(cached, str):
author_follower_alias.author == author_id, followers_cached = json.loads(cached)
author_follower_alias.follower == Author.id, if isinstance(followers_cached, list):
), for fc in followers_cached:
) if fc['id'] not in followers_ids:
results = get_with_stat(q) followers.append(fc)
if isinstance(results, list): return followers
for follower in results:
await cache_follower(follower.dict(), author.dict()) author_follower_alias = aliased(AuthorFollower, name="af")
logger.debug(f"@{slug} cache updated with {len(results)} followers") q = select(Author).join(
return results author_follower_alias,
else: and_(
logger.debug(f"@{slug} got followers cached") author_follower_alias.author == author_id,
if isinstance(cached, str): author_follower_alias.follower == Author.id,
return json.loads(cached) ),
)
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: except Exception as exc:
import traceback import traceback