2025-03-20 08:55:21 +00:00
|
|
|
|
import orjson
|
2023-12-17 20:30:20 +00:00
|
|
|
|
|
2024-03-04 07:35:33 +00:00
|
|
|
|
from orm.notification import Notification
|
|
|
|
|
from services.db import local_session
|
2024-08-07 06:51:09 +00:00
|
|
|
|
from services.redis import redis
|
2024-08-09 06:37:06 +00:00
|
|
|
|
from utils.logger import root_logger as logger
|
2023-10-05 18:46:18 +00:00
|
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
|
|
2024-03-04 07:35:33 +00:00
|
|
|
|
def save_notification(action: str, entity: str, payload):
|
|
|
|
|
with local_session() as session:
|
|
|
|
|
n = Notification(action=action, entity=entity, payload=payload)
|
|
|
|
|
session.add(n)
|
|
|
|
|
session.commit()
|
|
|
|
|
|
|
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
|
async def notify_reaction(reaction, action: str = "create"):
|
|
|
|
|
channel_name = "reaction"
|
|
|
|
|
data = {"payload": reaction, "action": action}
|
2023-10-05 18:46:18 +00:00
|
|
|
|
try:
|
2024-04-17 15:32:23 +00:00
|
|
|
|
save_notification(action, channel_name, data.get("payload"))
|
2025-03-20 08:55:21 +00:00
|
|
|
|
await redis.publish(channel_name, orjson.dumps(data))
|
2023-10-05 18:46:18 +00:00
|
|
|
|
except Exception as e:
|
2024-04-17 15:32:23 +00:00
|
|
|
|
logger.error(f"Failed to publish to channel {channel_name}: {e}")
|
2023-10-05 18:46:18 +00:00
|
|
|
|
|
|
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
|
async def notify_shout(shout, action: str = "update"):
|
|
|
|
|
channel_name = "shout"
|
|
|
|
|
data = {"payload": shout, "action": action}
|
2023-10-05 18:46:18 +00:00
|
|
|
|
try:
|
2024-04-17 15:32:23 +00:00
|
|
|
|
save_notification(action, channel_name, data.get("payload"))
|
2025-03-20 08:55:21 +00:00
|
|
|
|
await redis.publish(channel_name, orjson.dumps(data))
|
2023-10-05 18:46:18 +00:00
|
|
|
|
except Exception as e:
|
2024-04-17 15:32:23 +00:00
|
|
|
|
logger.error(f"Failed to publish to channel {channel_name}: {e}")
|
2023-10-05 18:46:18 +00:00
|
|
|
|
|
|
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
|
async def notify_follower(follower: dict, author_id: int, action: str = "follow"):
|
|
|
|
|
channel_name = f"follower:{author_id}"
|
2023-10-05 18:46:18 +00:00
|
|
|
|
try:
|
2024-01-22 23:47:23 +00:00
|
|
|
|
# Simplify dictionary before publishing
|
2024-04-17 15:32:23 +00:00
|
|
|
|
simplified_follower = {k: follower[k] for k in ["id", "name", "slug", "pic"]}
|
|
|
|
|
data = {"payload": simplified_follower, "action": action}
|
2024-04-09 10:46:27 +00:00
|
|
|
|
# save in channel
|
2024-04-17 15:32:23 +00:00
|
|
|
|
save_notification(action, channel_name, data.get("payload"))
|
2024-01-22 23:47:23 +00:00
|
|
|
|
|
|
|
|
|
# Convert data to JSON string
|
2025-03-20 08:55:21 +00:00
|
|
|
|
json_data = orjson.dumps(data)
|
2024-01-22 23:47:23 +00:00
|
|
|
|
|
|
|
|
|
# Ensure the data is not empty before publishing
|
2024-04-17 15:32:23 +00:00
|
|
|
|
if json_data:
|
2024-04-09 10:46:27 +00:00
|
|
|
|
# Use the 'await' keyword when publishing
|
|
|
|
|
await redis.publish(channel_name, json_data)
|
2024-03-04 07:35:33 +00:00
|
|
|
|
|
2023-10-05 18:46:18 +00:00
|
|
|
|
except Exception as e:
|
2024-01-22 23:47:23 +00:00
|
|
|
|
# Log the error and re-raise it
|
2024-04-17 15:32:23 +00:00
|
|
|
|
logger.error(f"Failed to publish to channel {channel_name}: {e}")
|
2025-04-24 09:12:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def notify_draft(draft_data, action: str = "publish"):
|
|
|
|
|
"""
|
|
|
|
|
Отправляет уведомление о публикации или обновлении черновика.
|
2025-05-16 06:11:39 +00:00
|
|
|
|
|
2025-04-24 09:12:48 +00:00
|
|
|
|
Функция гарантирует, что данные черновика сериализуются корректно, включая
|
|
|
|
|
связанные атрибуты (topics, authors).
|
2025-05-16 06:11:39 +00:00
|
|
|
|
|
2025-04-24 09:12:48 +00:00
|
|
|
|
Args:
|
|
|
|
|
draft_data (dict): Словарь с данными черновика. Должен содержать минимум id и title
|
|
|
|
|
action (str, optional): Действие ("publish", "update"). По умолчанию "publish"
|
2025-05-16 06:11:39 +00:00
|
|
|
|
|
2025-04-24 09:12:48 +00:00
|
|
|
|
Returns:
|
|
|
|
|
None
|
2025-05-16 06:11:39 +00:00
|
|
|
|
|
2025-04-24 09:12:48 +00:00
|
|
|
|
Examples:
|
|
|
|
|
>>> draft = {"id": 1, "title": "Тестовый черновик", "slug": "test-draft"}
|
|
|
|
|
>>> await notify_draft(draft, "publish")
|
|
|
|
|
"""
|
|
|
|
|
channel_name = "draft"
|
|
|
|
|
try:
|
|
|
|
|
# Убеждаемся, что все необходимые данные присутствуют
|
|
|
|
|
# и объект не требует доступа к отсоединенным атрибутам
|
|
|
|
|
if isinstance(draft_data, dict):
|
|
|
|
|
draft_payload = draft_data
|
|
|
|
|
else:
|
|
|
|
|
# Если это ORM объект, преобразуем его в словарь с нужными атрибутами
|
|
|
|
|
draft_payload = {
|
|
|
|
|
"id": getattr(draft_data, "id", None),
|
|
|
|
|
"slug": getattr(draft_data, "slug", None),
|
|
|
|
|
"title": getattr(draft_data, "title", None),
|
|
|
|
|
"subtitle": getattr(draft_data, "subtitle", None),
|
|
|
|
|
"media": getattr(draft_data, "media", None),
|
|
|
|
|
"created_at": getattr(draft_data, "created_at", None),
|
2025-05-16 06:11:39 +00:00
|
|
|
|
"updated_at": getattr(draft_data, "updated_at", None),
|
2025-04-24 09:12:48 +00:00
|
|
|
|
}
|
2025-05-16 06:11:39 +00:00
|
|
|
|
|
2025-04-24 09:12:48 +00:00
|
|
|
|
# Если переданы связанные атрибуты, добавим их
|
|
|
|
|
if hasattr(draft_data, "topics") and draft_data.topics is not None:
|
|
|
|
|
draft_payload["topics"] = [
|
2025-05-16 06:23:48 +00:00
|
|
|
|
{"id": t.id, "name": t.name, "slug": t.slug} for t in draft_data.topics
|
2025-04-24 09:12:48 +00:00
|
|
|
|
]
|
2025-05-16 06:11:39 +00:00
|
|
|
|
|
2025-04-24 09:12:48 +00:00
|
|
|
|
if hasattr(draft_data, "authors") and draft_data.authors is not None:
|
|
|
|
|
draft_payload["authors"] = [
|
2025-05-16 06:11:39 +00:00
|
|
|
|
{
|
|
|
|
|
"id": a.id,
|
|
|
|
|
"name": a.name,
|
|
|
|
|
"slug": a.slug,
|
|
|
|
|
"pic": getattr(a, "pic", None),
|
|
|
|
|
}
|
2025-04-24 09:12:48 +00:00
|
|
|
|
for a in draft_data.authors
|
|
|
|
|
]
|
2025-05-16 06:11:39 +00:00
|
|
|
|
|
2025-04-24 09:12:48 +00:00
|
|
|
|
data = {"payload": draft_payload, "action": action}
|
2025-05-16 06:11:39 +00:00
|
|
|
|
|
2025-04-24 09:12:48 +00:00
|
|
|
|
# Сохраняем уведомление
|
|
|
|
|
save_notification(action, channel_name, data.get("payload"))
|
2025-05-16 06:11:39 +00:00
|
|
|
|
|
2025-04-24 09:12:48 +00:00
|
|
|
|
# Публикуем в Redis
|
|
|
|
|
json_data = orjson.dumps(data)
|
|
|
|
|
if json_data:
|
|
|
|
|
await redis.publish(channel_name, json_data)
|
2025-05-16 06:11:39 +00:00
|
|
|
|
|
2025-04-24 09:12:48 +00:00
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Failed to publish to channel {channel_name}: {e}")
|