This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
from collections.abc import Collection
|
||||
from datetime import UTC
|
||||
from typing import Any
|
||||
|
||||
import orjson
|
||||
@@ -72,12 +73,26 @@ async def notify_shout(shout: dict[str, Any], action: str = "update") -> None:
|
||||
logger.error(f"Failed to publish to channel {channel_name}: {e}")
|
||||
|
||||
|
||||
async def notify_follower(follower: dict[str, Any], author_id: int, action: str = "follow") -> None:
|
||||
async def notify_follower(follower: dict[str, Any], author_id: int, action: str = "follow", subscription_id: int | None = None) -> None:
|
||||
channel_name = f"follower:{author_id}"
|
||||
try:
|
||||
# Simplify dictionary before publishing
|
||||
simplified_follower = {k: follower[k] for k in ["id", "name", "slug", "pic"]}
|
||||
data = {"payload": simplified_follower, "action": action}
|
||||
|
||||
# Формат данных для фронтенда согласно обновленной спецификации SSE
|
||||
from datetime import datetime
|
||||
|
||||
data = {
|
||||
"action": "create" if action == "follow" else "delete",
|
||||
"entity": "follower",
|
||||
"payload": {
|
||||
"id": subscription_id or 999, # ID записи подписки из БД
|
||||
"follower_id": simplified_follower["id"],
|
||||
"following_id": author_id,
|
||||
"created_at": datetime.now(UTC).isoformat()
|
||||
}
|
||||
}
|
||||
|
||||
# save in channel
|
||||
payload = data.get("payload")
|
||||
if isinstance(payload, Collection) and not isinstance(payload, str | bytes | dict):
|
||||
@@ -91,6 +106,7 @@ async def notify_follower(follower: dict[str, Any], author_id: int, action: str
|
||||
if json_data:
|
||||
# Use the 'await' keyword when publishing
|
||||
await redis.publish(channel_name, json_data)
|
||||
logger.debug(f"📡 Отправлено SSE уведомление о подписке: author_id={author_id}, follower={simplified_follower.get('name')}")
|
||||
|
||||
except (ConnectionError, TimeoutError, KeyError, ValueError) as e:
|
||||
# Log the error and re-raise it
|
||||
|
||||
Reference in New Issue
Block a user