authors-subquery-json-fix

This commit is contained in:
Untone 2024-02-27 12:47:42 +03:00
parent ef2f8dca82
commit cbd8ba6b68
2 changed files with 27 additions and 18 deletions

View File

@ -1,6 +1,6 @@
import asyncio
from sqlalchemy import select, event
from sqlalchemy import select, event, cast, String
import json
from orm.author import Author, AuthorFollower
@ -65,9 +65,9 @@ def after_shouts_update(mapper, connection, shout: Shout):
asyncio.create_task(update_author_cache(author.dict()))
@event.listens_for(Reaction, 'after_insert')
def after_reaction_insert(mapper, connection, reaction: Reaction):
try:
author_subquery = select(Author).where(Author.id == reaction.created_by)
replied_author_subquery = (
select(Author)
@ -75,8 +75,15 @@ def after_reaction_insert(mapper, connection, reaction: Reaction):
.where(Reaction.id == reaction.reply_to)
)
author_query = select(author_subquery.subquery()).select_from(author_subquery.subquery()).union(
select(replied_author_subquery.subquery()).select_from(replied_author_subquery.subquery())
author_query = select(
author_subquery.subquery().c.id,
cast(author_subquery.subquery().c.links, String).label('links')
).select_from(author_subquery.subquery()).union(
select(
replied_author_subquery.subquery().c.id,
cast(replied_author_subquery.subquery().c.links, String).label('links'),
)
.select_from(replied_author_subquery.subquery())
)
authors = get_with_stat(author_query)
@ -86,6 +93,8 @@ def after_reaction_insert(mapper, connection, reaction: Reaction):
shout = connection.execute(select(Shout).select_from(Shout).where(Shout.id == reaction.shout)).first()
if shout:
after_shouts_update(mapper, connection, shout)
except Exception as exc:
logger.error(exc)
@event.listens_for(Author, 'after_insert')