async-events-fix
Some checks failed
Deploy to core / deploy (push) Failing after 1m35s

This commit is contained in:
2024-02-21 14:21:04 +03:00
parent 9da452c2f0
commit a40eb878be

View File

@@ -1,4 +1,5 @@
import time
import asyncio
from sqlalchemy import JSON, Boolean, Column, ForeignKey, Integer, String
from sqlalchemy import event, select
@@ -46,9 +47,9 @@ class Author(Base):
@event.listens_for(Author, "after_insert")
@event.listens_for(Author, "after_update")
async def after_author_update(mapper, connection, target):
def after_author_update(mapper, connection, target):
redis_key = f"user:{target.user}:author"
await redis.execute("HSET", redis_key, **vars(target))
asyncio.create_task(redis.execute("HSET", redis_key, **vars(target)))
async def update_follows_for_user(connection, user_id, entity_type, entity, is_insert):
@@ -74,14 +75,13 @@ async def update_follows_for_user(connection, user_id, entity_type, entity, is_i
async def handle_author_follower_change(connection, author_id, follower_id, is_insert):
author = connection.execute(select(Author).filter(Author.id == author_id)).first()
follower = connection.execute(
select(Author).filter(Author.id == follower_id)
).first()
if follower and author:
await update_follows_for_user(
connection, follower.user, "author", author, is_insert
)
async with connection.begin() as conn:
author = await conn.execute(select(Author).filter(Author.id == author_id)).first()
follower = await conn.execute(select(Author).filter(Author.id == follower_id)).first()
if follower and author:
await update_follows_for_user(
connection, follower.user, "author", author, is_insert
)
async def handle_topic_follower_change(connection, topic_id, follower_id, is_insert):
@@ -96,24 +96,24 @@ async def handle_topic_follower_change(connection, topic_id, follower_id, is_ins
@event.listens_for(TopicFollower, "after_insert")
async def after_topic_follower_insert(mapper, connection, target):
await handle_topic_follower_change(connection, target.topic, target.follower, True)
def after_topic_follower_insert(mapper, connection, target):
asyncio.create_task(handle_topic_follower_change(connection, target.topic, target.follower, True))
@event.listens_for(TopicFollower, "after_delete")
async def after_topic_follower_delete(mapper, connection, target):
await handle_topic_follower_change(connection, target.topic, target.follower, False)
def after_topic_follower_delete(mapper, connection, target):
asyncio.create_task(handle_topic_follower_change(connection, target.topic, target.follower, False))
@event.listens_for(AuthorFollower, "after_insert")
async def after_author_follower_insert(mapper, connection, target):
await handle_author_follower_change(
def after_author_follower_insert(mapper, connection, target):
asyncio.create_task(handle_author_follower_change(
connection, target.author, target.follower, True
)
))
@event.listens_for(AuthorFollower, "after_delete")
async def after_author_follower_delete(mapper, connection, target):
await handle_author_follower_change(
def after_author_follower_delete(mapper, connection, target):
asyncio.create_task(handle_author_follower_change(
connection, target.author, target.follower, False
)
))