follower-notification
Some checks failed
Deploy on push / deploy (push) Failing after 11s

This commit is contained in:
2025-08-30 18:47:27 +03:00
parent 9752a470e0
commit 05b5c3defd
3 changed files with 166 additions and 3 deletions

View File

@@ -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