This commit is contained in:
parent
886ca8c0ff
commit
33a59a4acc
|
@ -1,6 +1,6 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
from sqlalchemy import select, event, or_, exists, and_
|
from sqlalchemy import select, event
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from orm.author import Author, AuthorFollower
|
from orm.author import Author, AuthorFollower
|
||||||
|
@ -50,31 +50,22 @@ async def update_follows_authors_cache(follows, author_id: int, ttl=25 * 60 * 60
|
||||||
@event.listens_for(Shout, 'after_insert')
|
@event.listens_for(Shout, 'after_insert')
|
||||||
@event.listens_for(Shout, 'after_update')
|
@event.listens_for(Shout, 'after_update')
|
||||||
def after_shouts_update(mapper, connection, shout: Shout):
|
def after_shouts_update(mapper, connection, shout: Shout):
|
||||||
# Создаем подзапрос для проверки наличия авторов в списке shout.authors
|
# Main query to get authors associated with the shout through ShoutAuthor
|
||||||
subquery = select(1).where(
|
|
||||||
or_(
|
|
||||||
Author.id == shout.created_by,
|
|
||||||
and_(
|
|
||||||
Shout.id == shout.id,
|
|
||||||
ShoutAuthor.shout == Shout.id,
|
|
||||||
ShoutAuthor.author == Author.id,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
# Основной запрос с использованием объединения и подзапроса exists
|
|
||||||
authors_query = (
|
authors_query = (
|
||||||
select(Author)
|
select(Author)
|
||||||
.select_from(Author)
|
.select_from(ShoutAuthor) # Select from ShoutAuthor
|
||||||
.join(ShoutAuthor, Author.id == ShoutAuthor.author)
|
.join(Author, Author.id == ShoutAuthor.author) # Join with Author
|
||||||
.where(ShoutAuthor.shout == shout.id)
|
.where(ShoutAuthor.shout == shout.id) # Filter by shout.id
|
||||||
.union(select(Author).where(exists(subquery)))
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Execute the query
|
||||||
authors = get_with_stat(authors_query)
|
authors = get_with_stat(authors_query)
|
||||||
|
|
||||||
for author in authors:
|
for author in authors:
|
||||||
asyncio.create_task(update_author_cache(author.dict()))
|
asyncio.create_task(update_author_cache(author.dict()))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@event.listens_for(Reaction, 'after_insert')
|
@event.listens_for(Reaction, 'after_insert')
|
||||||
def after_reaction_insert(mapper, connection, reaction: Reaction):
|
def after_reaction_insert(mapper, connection, reaction: Reaction):
|
||||||
author_subquery = select(Author).where(Author.id == reaction.created_by)
|
author_subquery = select(Author).where(Author.id == reaction.created_by)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user