query-fix-2

This commit is contained in:
Untone 2024-02-21 19:11:49 +03:00
parent 2e07219732
commit 5b8347ee54
3 changed files with 55 additions and 13 deletions

View File

@ -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

View File

@ -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)
)

View File

@ -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"