From 5b8347ee54658daa8ea98e41f70257b317dd9801 Mon Sep 17 00:00:00 2001 From: Untone Date: Wed, 21 Feb 2024 19:11:49 +0300 Subject: [PATCH] query-fix-2 --- resolvers/author.py | 13 ++++++++++++- resolvers/follower.py | 10 +++++++--- services/follows.py | 45 ++++++++++++++++++++++++++++++++++--------- 3 files changed, 55 insertions(+), 13 deletions(-) diff --git a/resolvers/author.py b/resolvers/author.py index 5257d1e9..8733e613 100644 --- a/resolvers/author.py +++ b/resolvers/author.py @@ -204,7 +204,18 @@ async def get_author_by_user_id(user_id: str): q = select(Author).filter(Author.user == user_id) author = await load_author_with_stats(q) if author: - await redis.execute("set", redis_key, json.dumps(author.dict())) + await redis.execute( + "set", + redis_key, + json.dumps( + { + "id": author.id, + "name": author.name, + "slug": author.slug, + "pic": author.pic, + } + ), + ) return author diff --git a/resolvers/follower.py b/resolvers/follower.py index f0979818..63653b78 100644 --- a/resolvers/follower.py +++ b/resolvers/follower.py @@ -91,17 +91,21 @@ async def unfollow(_, info, what, slug): def query_follows(user_id: str): with local_session() as session: aliased_author = aliased(Author) - author = session.query(aliased_author).filter(aliased_author.user == user_id).first() + author = ( + session.query(aliased_author).filter(aliased_author.user == user_id).first() + ) if isinstance(author, Author): author_id = author.id authors_query = ( - select(column('name'), column('id'), column('slug'), column('pic'), ).select_from(Author) + select(column("name"), column("id"), column("slug"), column("pic")) + .select_from(Author) .join(AuthorFollower, AuthorFollower.follower == author_id) .filter(AuthorFollower.author == Author.id) ) topics_query = ( - select(column('title'), column('id'), column('slug'), column('pic'), ).select_from(Author) + select(column("title"), column("id"), column("slug"), column("pic")) + .select_from(Topic) .join(TopicFollower, TopicFollower.follower == author_id) .filter(TopicFollower.topic == Topic.id) ) diff --git a/services/follows.py b/services/follows.py index 1eef920a..89bebd2e 100644 --- a/services/follows.py +++ b/services/follows.py @@ -14,9 +14,22 @@ from services.viewed import ViewedStorage @event.listens_for(Author, "after_insert") @event.listens_for(Author, "after_update") -def after_author_update(mapper, connection, target: Author): - redis_key = f"user:{target.user}:author" - asyncio.create_task(redis.execute("set", redis_key, json.dumps(target.dict()))) +def after_author_update(mapper, connection, author: Author): + redis_key = f"user:{author.user}:author" + asyncio.create_task( + redis.execute( + "set", + redis_key, + json.dumps( + { + "id": author.id, + "name": author.name, + "slug": author.slug, + "pic": author.pic, + } + ), + ) + ) @event.listens_for(TopicFollower, "after_insert") @@ -47,7 +60,9 @@ def after_author_follower_delete(mapper, connection, target: AuthorFollower): ) -async def update_follows_for_user(connection, user_id, entity_type, entity: dict, is_insert): +async def update_follows_for_user( + connection, user_id, entity_type, entity: dict, is_insert +): redis_key = f"user:{user_id}:follows" follows_str = await redis.get(redis_key) if follows_str: @@ -64,7 +79,9 @@ async def update_follows_for_user(connection, user_id, entity_type, entity: dict follows[f"{entity_type}s"].append(entity) else: # Remove the entity from follows - follows[f"{entity_type}s"] = [e for e in follows[f"{entity_type}s"] if e["id"] != entity['id']] + follows[f"{entity_type}s"] = [ + e for e in follows[f"{entity_type}s"] if e["id"] != entity["id"] + ] await redis.execute("set", redis_key, json.dumps(follows)) @@ -131,11 +148,21 @@ class FollowsCached: ) @staticmethod - async def update_author_cache(author): + async def update_author_cache(author: Author): redis_key = f"user:{author.user}:author" - author_dict = author.dict() - if isinstance(author_dict, dict): - await redis.execute("set", redis_key, json.dumps(author_dict)) + if isinstance(author, Author): + await redis.execute( + "set", + redis_key, + json.dumps( + { + "id": author.id, + "name": author.name, + "slug": author.slug, + "pic": author.pic, + } + ) + ) follows = await get_author_follows(None, None, user=author.user) if isinstance(follows, dict): redis_key = f"user:{author.user}:follows"