FlowDeck CI / lint (push) Successful in 43s
FlowDeck CI / test (push) Successful in 4m2s
FlowDeck CI / lint (pull_request) Successful in 42s
FlowDeck CI / test (pull_request) Successful in 4m3s
FlowDeck CI / docker (push) Successful in 1m2s
FlowDeck CI / docker (pull_request) Successful in 35s
tests/conftest.py: mutate the settings singleton (instead of rebinding) so DB + backup dir are isolated per test -> pytest-xdist safe. Real backup tests (snapshot/prune/admin API) and OAuth mock tests (Gitea/GitHub/link) replace the previous skips. init_db() now also creates webhook_subscriptions (full schema without the FastAPI lifespan). ruff check is clean; .eslintrc.json migrated to eslint.config.mjs (flat config). CI: lint job (ruff + eslint), parallel tests (-n auto), run on every branch push. VERSION 5.11.1.
127 lines
4.2 KiB
Python
127 lines
4.2 KiB
Python
"""FlowDeck — Notifications API (v4.9.0 collaboration)."""
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
|
|
from fastapi import APIRouter, HTTPException, Request
|
|
|
|
from app.auth.session import SessionManager
|
|
from app.db import get_conn
|
|
|
|
logger = logging.getLogger(__name__)
|
|
router = APIRouter(tags=["notifications"], prefix="/api/notifications")
|
|
|
|
|
|
def _current_user(request: Request) -> dict:
|
|
user = SessionManager.decode_session(request.cookies.get("flowdeck_session", ""))
|
|
if not user or not user.get("id"):
|
|
raise HTTPException(status_code=401, detail="Authentication required")
|
|
return user
|
|
|
|
|
|
@router.get("")
|
|
async def list_notifications(request: Request, limit: int = 50):
|
|
"""List the current user's notifications, newest first."""
|
|
user = _current_user(request)
|
|
with get_conn() as conn:
|
|
rows = conn.execute(
|
|
"""SELECT n.*, a.login AS actor_login, a.full_name AS actor_name,
|
|
a.avatar_url AS actor_avatar, a.avatar_color AS actor_color
|
|
FROM notifications n
|
|
LEFT JOIN users a ON n.actor_id = a.id
|
|
WHERE n.user_id=?
|
|
ORDER BY n.created_at DESC, n.id DESC LIMIT ?""",
|
|
(user["id"], limit),
|
|
).fetchall()
|
|
unread = conn.execute(
|
|
"SELECT COUNT(*) AS c FROM notifications WHERE user_id=? AND is_read=0",
|
|
(user["id"],),
|
|
).fetchone()["c"]
|
|
return {
|
|
"notifications": [dict(r) for r in rows],
|
|
"unread": unread,
|
|
}
|
|
|
|
|
|
@router.get("/unread-count")
|
|
async def unread_count(request: Request):
|
|
"""Unread count for the topbar badge."""
|
|
user = _current_user(request)
|
|
with get_conn() as conn:
|
|
c = conn.execute(
|
|
"SELECT COUNT(*) AS c FROM notifications WHERE user_id=? AND is_read=0",
|
|
(user["id"],),
|
|
).fetchone()["c"]
|
|
return {"unread": c}
|
|
|
|
|
|
@router.post("/read")
|
|
async def mark_read(request: Request):
|
|
"""Mark one notification as read (id) or all (id omitted)."""
|
|
user = _current_user(request)
|
|
body = await request.json() if request.headers.get("content-type") else {}
|
|
nid = body.get("id")
|
|
with get_conn() as conn:
|
|
if nid:
|
|
conn.execute(
|
|
"UPDATE notifications SET is_read=1 WHERE id=? AND user_id=?",
|
|
(nid, user["id"]),
|
|
)
|
|
else:
|
|
conn.execute(
|
|
"UPDATE notifications SET is_read=1 WHERE user_id=?",
|
|
(user["id"],),
|
|
)
|
|
conn.commit()
|
|
return {"status": "ok"}
|
|
|
|
|
|
@router.post("/read-all")
|
|
async def mark_all_read(request: Request):
|
|
"""Mark all notifications as read."""
|
|
return await mark_read(request)
|
|
|
|
|
|
@router.get("/prefs")
|
|
async def get_prefs(request: Request):
|
|
"""Return the current user's notification email preferences."""
|
|
user = _current_user(request)
|
|
from app.services import notifications as notif
|
|
return {"prefs": notif.get_user_prefs(user["id"])}
|
|
|
|
|
|
@router.post("/prefs")
|
|
async def set_prefs(request: Request):
|
|
"""Update the current user's notification email preferences."""
|
|
user = _current_user(request)
|
|
from app.services import notifications as notif
|
|
body = await request.json() if request.headers.get("content-type") else {}
|
|
prefs = notif.get_user_prefs(user["id"])
|
|
for key in ("comments", "mentions"):
|
|
if key in body:
|
|
prefs[key] = bool(body[key])
|
|
notif.set_user_prefs(user["id"], prefs)
|
|
return {"status": "ok", "prefs": prefs}
|
|
|
|
|
|
@router.get("/users/search")
|
|
async def search_users(request: Request, q: str = ""):
|
|
"""User autocomplete for @mentions."""
|
|
_current_user(request)
|
|
q = (q or "").strip()
|
|
with get_conn() as conn:
|
|
if q:
|
|
like = f"%{q}%"
|
|
rows = conn.execute(
|
|
"""SELECT id, login, full_name, avatar_url, avatar_color
|
|
FROM users WHERE login LIKE ? OR full_name LIKE ?
|
|
ORDER BY (login=? OR full_name=?) DESC, login LIMIT 20""",
|
|
(like, like, q, q),
|
|
).fetchall()
|
|
else:
|
|
rows = conn.execute(
|
|
"""SELECT id, login, full_name, avatar_url, avatar_color
|
|
FROM users ORDER BY login LIMIT 20"""
|
|
).fetchall()
|
|
return {"users": [dict(r) for r in rows]}
|