diff --git a/resolvers/follower.py b/resolvers/follower.py index 26b23117..a9e075aa 100644 --- a/resolvers/follower.py +++ b/resolvers/follower.py @@ -1,7 +1,7 @@ import json from typing import List -from sqlalchemy import select, or_ +from sqlalchemy import select, or_, column from sqlalchemy.orm import aliased from sqlalchemy.sql import and_ @@ -90,19 +90,19 @@ async def unfollow(_, info, what, slug): def query_follows(user_id: str): with local_session() as session: - author = session.query(Author).filter(Author.user == user_id).first() + aliased_author = aliased(Author) + author = session.query(aliased_author).filter(aliased_author.user == user_id).first() if isinstance(author, Author): author_id = author.id - aliased_author = aliased(Author) authors_query = ( - session.query(aliased_author) + select(column('name'), column('id'), column('slug'), column('pic'), ).select_from(Author) .join(AuthorFollower, AuthorFollower.follower == author_id) - .filter(AuthorFollower.author == aliased_author.id) + .filter(AuthorFollower.author == Author.id) .all() ) topics_query = ( - session.query(Topic) + select(column('title'), column('id'), column('slug'), column('pic'), ).select_from(Author) .join(TopicFollower, TopicFollower.follower == author_id) .filter(TopicFollower.topic == Topic.id) .all() diff --git a/services/follows.py b/services/follows.py index c8666507..1eef920a 100644 --- a/services/follows.py +++ b/services/follows.py @@ -47,7 +47,7 @@ def after_author_follower_delete(mapper, connection, target: AuthorFollower): ) -async def update_follows_for_user(connection, user_id, entity_type, entity, 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: @@ -61,12 +61,10 @@ async def update_follows_for_user(connection, user_id, entity_type, entity, is_i ], } if is_insert: - follows[f"{entity_type}s"].append(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)) @@ -88,7 +86,7 @@ async def handle_author_follower_change(connection, author_id, follower_id, is_i ).first() if follower and author: await update_follows_for_user( - connection, follower.user, "author", author, is_insert + connection, follower.user, "author", author.dict(), is_insert ) @@ -110,7 +108,7 @@ async def handle_topic_follower_change(connection, topic_id, follower_id, is_ins ).first() if follower and topic: await update_follows_for_user( - connection, follower.user, "topic", topic, is_insert + connection, follower.user, "topic", topic.dict(), is_insert )