follow/unfollow-cache-fix
All checks were successful
Deploy on push / deploy (push) Successful in 23s

This commit is contained in:
Untone 2024-05-05 21:04:38 +03:00
parent 13d144f838
commit 2b5fb704ba

View File

@ -56,12 +56,13 @@ async def follow(_, info, what, slug):
follower_id = int(follower_id) follower_id = int(follower_id)
error = author_follow(follower_id, slug) error = author_follow(follower_id, slug)
if not error: if not error:
author_dict = await cache_by_slug(what, slug) [author] = get_with_stat(select(Author).filter(Author.slug == slug))
if isinstance(author_dict, dict): if author:
author_id = author_dict.get("id") author_dict = author.dict()
if author_id: author_id = author.id
follows_ids = [a.get("id") for a in follows] follows_ids = [a.get("id") for a in follows]
if author_id not in follows_ids: if author_id not in follows_ids:
await cache_author(author_dict)
await cache_author(follower_dict) await cache_author(follower_dict)
await notify_follower(follower_dict, author_id, "follow") await notify_follower(follower_dict, author_id, "follow")
follows.append(author_dict) follows.append(author_dict)
@ -108,17 +109,16 @@ async def unfollow(_, info, what, slug):
# NOTE: after triggers should update cached stats # NOTE: after triggers should update cached stats
if not error: if not error:
logger.info(f"@{follower_dict.get('slug')} unfollowed @{slug}") logger.info(f"@{follower_dict.get('slug')} unfollowed @{slug}")
author_dict = await cache_by_slug(what, slug) [author] = get_with_stat(select(Author).filter(Author.slug == slug))
if isinstance(author_dict, dict): if author:
author_id = author_dict.get("id") author_dict = author.dict()
if author_id: author_id = author.id
await cache_author(author_dict)
for idx, item in enumerate(follows): for idx, item in enumerate(follows):
if item["id"] == author_id: if item["id"] == author_id:
await cache_author(follower_dict) await cache_author(follower_dict)
await notify_follower(follower_dict, author_id, "unfollow") await notify_follower(follower_dict, author_id, "unfollow")
follows.pop( follows.pop(idx)
idx
) # Remove the author_dict from the follows list
break break
elif what == "TOPIC": elif what == "TOPIC":