dictify
All checks were successful
Deploy to core / deploy (push) Successful in 2m31s

This commit is contained in:
Untone 2024-02-21 18:51:37 +03:00
parent 3f361b1af7
commit b7cbef01a3
2 changed files with 11 additions and 13 deletions

View File

@ -1,7 +1,7 @@
import json import json
from typing import List from typing import List
from sqlalchemy import select, or_ from sqlalchemy import select, or_, column
from sqlalchemy.orm import aliased from sqlalchemy.orm import aliased
from sqlalchemy.sql import and_ from sqlalchemy.sql import and_
@ -90,19 +90,19 @@ async def unfollow(_, info, what, slug):
def query_follows(user_id: str): def query_follows(user_id: str):
with local_session() as session: 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): if isinstance(author, Author):
author_id = author.id author_id = author.id
aliased_author = aliased(Author)
authors_query = ( authors_query = (
session.query(aliased_author) select(column('name'), column('id'), column('slug'), column('pic'), ).select_from(Author)
.join(AuthorFollower, AuthorFollower.follower == author_id) .join(AuthorFollower, AuthorFollower.follower == author_id)
.filter(AuthorFollower.author == aliased_author.id) .filter(AuthorFollower.author == Author.id)
.all() .all()
) )
topics_query = ( topics_query = (
session.query(Topic) select(column('title'), column('id'), column('slug'), column('pic'), ).select_from(Author)
.join(TopicFollower, TopicFollower.follower == author_id) .join(TopicFollower, TopicFollower.follower == author_id)
.filter(TopicFollower.topic == Topic.id) .filter(TopicFollower.topic == Topic.id)
.all() .all()

View File

@ -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" redis_key = f"user:{user_id}:follows"
follows_str = await redis.get(redis_key) follows_str = await redis.get(redis_key)
if follows_str: if follows_str:
@ -61,12 +61,10 @@ async def update_follows_for_user(connection, user_id, entity_type, entity, is_i
], ],
} }
if is_insert: if is_insert:
follows[f"{entity_type}s"].append(entity.dict()) follows[f"{entity_type}s"].append(entity)
else: else:
# Remove the entity from follows # Remove the entity from follows
follows[f"{entity_type}s"] = [ follows[f"{entity_type}s"] = [e for e in follows[f"{entity_type}s"] if e["id"] != entity['id']]
e for e in follows[f"{entity_type}s"] if e["id"] != entity.id
]
await redis.execute("set", redis_key, json.dumps(follows)) 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() ).first()
if follower and author: if follower and author:
await update_follows_for_user( 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() ).first()
if follower and topic: if follower and topic:
await update_follows_for_user( await update_follows_for_user(
connection, follower.user, "topic", topic, is_insert connection, follower.user, "topic", topic.dict(), is_insert
) )