85 lines
2.6 KiB
Python
85 lines
2.6 KiB
Python
from flask import Blueprint, jsonify, request, session
|
|
|
|
from app.routes.auth_decorators import require_auth
|
|
from app.services.notification_center_service import (
|
|
create_notification,
|
|
get_notification,
|
|
list_notifications,
|
|
mark_all_read,
|
|
mark_read,
|
|
unread_count,
|
|
)
|
|
|
|
bp = Blueprint("notifications", __name__, url_prefix="/api/notifications")
|
|
|
|
|
|
@bp.get("")
|
|
@require_auth
|
|
def notifications_list():
|
|
if request.args.get("summary") in ("1", "true", "yes"):
|
|
return jsonify(unread_count()), 200
|
|
sort = request.args.get("sort", "newest")
|
|
category = request.args.get("category") or None
|
|
unread_only = request.args.get("unread") in ("1", "true", "yes")
|
|
day = request.args.get("date") or None
|
|
return jsonify(
|
|
list_notifications(
|
|
date=day,
|
|
category=category,
|
|
unread_only=unread_only,
|
|
sort=sort,
|
|
)
|
|
), 200
|
|
|
|
|
|
@bp.get("/<string:notification_id>")
|
|
@require_auth
|
|
def notifications_get(notification_id: str):
|
|
row = get_notification(notification_id)
|
|
if row is None:
|
|
return jsonify({"error": True, "message": "Уведомление не найдено"}), 404
|
|
from app.services.notification_center_service import _serialize
|
|
|
|
return jsonify(_serialize(row)), 200
|
|
|
|
|
|
@bp.post("")
|
|
@require_auth
|
|
def notifications_create():
|
|
data = request.get_json(silent=True) or {}
|
|
title = data.get("title") or ""
|
|
detail = data.get("detail") or title
|
|
if not str(title).strip() and not str(detail).strip():
|
|
return jsonify({"error": True, "message": "Пустое уведомление"}), 400
|
|
row = create_notification(
|
|
title=str(title),
|
|
detail=str(detail),
|
|
kind=data.get("kind") or data.get("type") or "info",
|
|
category=data.get("category") or "general",
|
|
page=data.get("page"),
|
|
link_kind=data.get("linkKind") or data.get("link_kind"),
|
|
link_id=data.get("linkId") or data.get("link_id"),
|
|
user_login=session.get("login"),
|
|
)
|
|
from app.services.notification_center_service import _serialize
|
|
|
|
return jsonify(_serialize(row)), 201
|
|
|
|
|
|
@bp.patch("/read-all")
|
|
@require_auth
|
|
def notifications_read_all():
|
|
count = mark_all_read()
|
|
return jsonify({"success": True, "marked": count}), 200
|
|
|
|
|
|
@bp.patch("/<string:notification_id>/read")
|
|
@require_auth
|
|
def notifications_mark_read(notification_id: str):
|
|
row = mark_read(notification_id)
|
|
if row is None:
|
|
return jsonify({"error": True, "message": "Уведомление не найдено"}), 404
|
|
from app.services.notification_center_service import _serialize
|
|
|
|
return jsonify(_serialize(row)), 200
|