This commit is contained in:
parent
cfcb858bba
commit
69409f92e1
|
@ -305,43 +305,44 @@ 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()
|
||||||
|
followers = []
|
||||||
|
if result:
|
||||||
|
[author] = result
|
||||||
|
author_id = author.id
|
||||||
|
cached = await redis.execute("GET", f"author:{author_id}:followers")
|
||||||
|
if cached:
|
||||||
|
followers_ids = []
|
||||||
|
followers = []
|
||||||
|
if isinstance(cached, str):
|
||||||
|
followers_cached = json.loads(cached)
|
||||||
|
if isinstance(followers_cached, list):
|
||||||
|
logger.debug(
|
||||||
|
f"@{slug} got {len(followers_cached)} followers cached"
|
||||||
|
)
|
||||||
|
for fc in followers_cached:
|
||||||
|
if fc["id"] not in followers_ids and fc["id"] != author_id:
|
||||||
|
followers.append(fc)
|
||||||
|
followers_ids.append(fc["id"])
|
||||||
|
return followers
|
||||||
|
|
||||||
[author] = result
|
author_follower_alias = aliased(AuthorFollower, name="af")
|
||||||
author_id = author.id
|
q = select(Author).join(
|
||||||
cached = await redis.execute("GET", f"author:{author_id}:followers")
|
author_follower_alias,
|
||||||
if cached:
|
and_(
|
||||||
followers_ids = []
|
author_follower_alias.author == author_id,
|
||||||
followers = []
|
author_follower_alias.follower == Author.id,
|
||||||
if isinstance(cached, str):
|
Author.id != author_id, # exclude the author from the followers
|
||||||
followers_cached = json.loads(cached)
|
),
|
||||||
if isinstance(followers_cached, list):
|
)
|
||||||
logger.debug(
|
followers = get_with_stat(q)
|
||||||
f"@{slug} got {len(followers_cached)} followers cached"
|
if isinstance(followers, list):
|
||||||
)
|
followers_ids = [r.id for r in followers]
|
||||||
for fc in followers_cached:
|
for follower in followers:
|
||||||
if fc["id"] not in followers_ids and fc["id"] != author_id:
|
if follower.id not in followers_ids:
|
||||||
followers.append(fc)
|
await cache_follow_author_change(follower.dict(), author.dict())
|
||||||
followers_ids.append(fc["id"])
|
followers_ids.append(follower.id)
|
||||||
return followers
|
logger.debug(f"@{slug} cache updated with {len(followers)} followers")
|
||||||
|
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,
|
|
||||||
Author.id != author_id, # exclude the author from the followers
|
|
||||||
),
|
|
||||||
)
|
|
||||||
results = get_with_stat(q)
|
|
||||||
if isinstance(results, list):
|
|
||||||
followers_ids = [r.id for r in results]
|
|
||||||
for follower in results:
|
|
||||||
if follower.id not in followers_ids:
|
|
||||||
await cache_follow_author_change(follower.dict(), author.dict())
|
|
||||||
followers_ids.append(follower.id)
|
|
||||||
logger.debug(f"@{slug} cache updated with {len(results)} followers")
|
|
||||||
return results
|
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
|
|
|
@ -164,7 +164,6 @@ async def cache_topic(topic_dict: dict):
|
||||||
follower_follows_topics.append(topic_dict)
|
follower_follows_topics.append(topic_dict)
|
||||||
|
|
||||||
await redis.execute(
|
await redis.execute(
|
||||||
"SET",
|
|
||||||
"SET",
|
"SET",
|
||||||
f"author:{follower_id}:follows-topics",
|
f"author:{follower_id}:follows-topics",
|
||||||
json.dumps(follower_follows_topics, cls=CustomJSONEncoder),
|
json.dumps(follower_follows_topics, cls=CustomJSONEncoder),
|
||||||
|
|
Loading…
Reference in New Issue
Block a user