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,15 +56,16 @@ 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(follower_dict) await cache_author(author_dict)
await notify_follower(follower_dict, author_id, "follow") await cache_author(follower_dict)
follows.append(author_dict) await notify_follower(follower_dict, author_id, "follow")
follows.append(author_dict)
elif what == "TOPIC": elif what == "TOPIC":
error = topic_follow(follower_id, slug) error = topic_follow(follower_id, slug)
@ -108,18 +109,17 @@ 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
for idx, item in enumerate(follows): await cache_author(author_dict)
if item["id"] == author_id: for idx, item in enumerate(follows):
await cache_author(follower_dict) if item["id"] == author_id:
await notify_follower(follower_dict, author_id, "unfollow") await cache_author(follower_dict)
follows.pop( await notify_follower(follower_dict, author_id, "unfollow")
idx follows.pop(idx)
) # Remove the author_dict from the follows list break
break
elif what == "TOPIC": elif what == "TOPIC":
error = topic_unfollow(follower_id, slug) error = topic_unfollow(follower_id, slug)