This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
from collections.abc import Collection
|
||||
from typing import Any, Union
|
||||
from typing import Any
|
||||
|
||||
import orjson
|
||||
|
||||
@@ -11,12 +11,12 @@ from services.redis import redis
|
||||
from utils.logger import root_logger as logger
|
||||
|
||||
|
||||
def save_notification(action: str, entity: str, payload: Union[dict[Any, Any], str, int, None]) -> None:
|
||||
def save_notification(action: str, entity: str, payload: dict[Any, Any] | str | int | None) -> None:
|
||||
"""Save notification with proper payload handling"""
|
||||
if payload is None:
|
||||
return
|
||||
|
||||
if isinstance(payload, (Reaction, Shout)):
|
||||
if isinstance(payload, Reaction | Shout):
|
||||
# Convert ORM objects to dict representation
|
||||
payload = {"id": payload.id}
|
||||
|
||||
@@ -26,7 +26,7 @@ def save_notification(action: str, entity: str, payload: Union[dict[Any, Any], s
|
||||
session.commit()
|
||||
|
||||
|
||||
async def notify_reaction(reaction: Union[Reaction, int], action: str = "create") -> None:
|
||||
async def notify_reaction(reaction: Reaction | int, action: str = "create") -> None:
|
||||
channel_name = "reaction"
|
||||
|
||||
# Преобразуем объект Reaction в словарь для сериализации
|
||||
@@ -56,7 +56,7 @@ async def notify_shout(shout: dict[str, Any], action: str = "update") -> None:
|
||||
data = {"payload": shout, "action": action}
|
||||
try:
|
||||
payload = data.get("payload")
|
||||
if isinstance(payload, Collection) and not isinstance(payload, (str, bytes, dict)):
|
||||
if isinstance(payload, Collection) and not isinstance(payload, str | bytes | dict):
|
||||
payload = str(payload)
|
||||
save_notification(action, channel_name, payload)
|
||||
await redis.publish(channel_name, orjson.dumps(data))
|
||||
@@ -72,7 +72,7 @@ async def notify_follower(follower: dict[str, Any], author_id: int, action: str
|
||||
data = {"payload": simplified_follower, "action": action}
|
||||
# save in channel
|
||||
payload = data.get("payload")
|
||||
if isinstance(payload, Collection) and not isinstance(payload, (str, bytes, dict)):
|
||||
if isinstance(payload, Collection) and not isinstance(payload, str | bytes | dict):
|
||||
payload = str(payload)
|
||||
save_notification(action, channel_name, payload)
|
||||
|
||||
@@ -144,7 +144,7 @@ async def notify_draft(draft_data: dict[str, Any], action: str = "publish") -> N
|
||||
|
||||
# Сохраняем уведомление
|
||||
payload = data.get("payload")
|
||||
if isinstance(payload, Collection) and not isinstance(payload, (str, bytes, dict)):
|
||||
if isinstance(payload, Collection) and not isinstance(payload, str | bytes | dict):
|
||||
payload = str(payload)
|
||||
save_notification(action, channel_name, payload)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user