diff --git a/alembic/versions/2026_09_06_notification_project.py b/alembic/versions/2026_09_06_notification_project.py new file mode 100644 index 000000000..9ecc81667 --- /dev/null +++ b/alembic/versions/2026_09_06_notification_project.py @@ -0,0 +1,36 @@ +"""notifications: scope a notification to its project + +Revision ID: c4b8e1f70a92 +Revises: a91d47c30fb2 +Create Date: 2026-09-06 +""" + +from collections.abc import Sequence + +import sqlalchemy as sa + +from alembic import op + +revision: str = "c4b8e1f70a92" +down_revision: str | None = "a91d47c30fb2" +branch_labels: str | Sequence[str] | None = None +depends_on: str | Sequence[str] | None = None + + +def upgrade() -> None: + op.add_column("notifications", sa.Column("project_id", sa.Uuid(), nullable=True)) + op.create_index("ix_notifications_project_id", "notifications", ["project_id"]) + op.execute( + """ + UPDATE notifications n + SET project_id = s.project_id + FROM scans s + WHERE n.notification_metadata->>'scan_id' IS NOT NULL + AND s.id = (n.notification_metadata->>'scan_id')::uuid + """ + ) + + +def downgrade() -> None: + op.drop_index("ix_notifications_project_id", table_name="notifications") + op.drop_column("notifications", "project_id") diff --git a/api/app/api/v1/notifications.py b/api/app/api/v1/notifications.py index 155a976f3..0fbf7b6ce 100644 --- a/api/app/api/v1/notifications.py +++ b/api/app/api/v1/notifications.py @@ -1,6 +1,7 @@ from typing import Annotated +from uuid import UUID -from fastapi import APIRouter, Depends, HTTPException, status +from fastapi import APIRouter, Depends, HTTPException, Query, status from fastapi_pagination import Page from fastapi_pagination.ext.sqlalchemy import paginate from sqlalchemy import func, select @@ -23,17 +24,36 @@ router = APIRouter( tags=["notifications"], ) +ProjectScope = Annotated[ + UUID | None, + Query(description="Only this project's notifications, plus global ones"), +] + + +def _in_scope(query, project_id: UUID | None): + if project_id is None: + return query + return query.where( + (Notification.project_id == project_id) | Notification.project_id.is_(None) + ) + @router.get("/stats", response_model=NotificationStats) async def get_notification_stats( _current_user: CurrentUser, session: Annotated[AsyncSession, Depends(get_session)], + project_id: ProjectScope = None, ): - total_result = await session.execute(select(func.count(Notification.id))) + total_result = await session.execute( + _in_scope(select(func.count(Notification.id)), project_id) + ) total = total_result.scalar_one() unread_result = await session.execute( - select(func.count(Notification.id)).where(Notification.is_read.is_(False)) + _in_scope( + select(func.count(Notification.id)).where(Notification.is_read.is_(False)), + project_id, + ) ) unread = unread_result.scalar_one() @@ -45,8 +65,11 @@ async def get_notification_stats( async def list_notifications( _current_user: CurrentUser, session: Annotated[AsyncSession, Depends(get_session)], + project_id: ProjectScope = None, ): - query = select(Notification).order_by(Notification.created_at.desc()) + query = _in_scope(select(Notification), project_id).order_by( + Notification.created_at.desc() + ) return await paginate(session, query) @@ -54,12 +77,11 @@ async def list_notifications( async def list_unread_notifications( _current_user: CurrentUser, session: Annotated[AsyncSession, Depends(get_session)], + project_id: ProjectScope = None, ): - query = ( - select(Notification) - .where(Notification.is_read.is_(False)) - .order_by(Notification.created_at.desc()) - ) + query = _in_scope( + select(Notification).where(Notification.is_read.is_(False)), project_id + ).order_by(Notification.created_at.desc()) return await paginate(session, query) @@ -105,8 +127,11 @@ async def mark_notification_as_read( async def mark_all_notifications_as_read( _current_user: CurrentUser, session: Annotated[AsyncSession, Depends(get_session)], + project_id: ProjectScope = None, ): - count = await NotificationManager.mark_all_as_read(session=session) + count = await NotificationManager.mark_all_as_read( + session=session, project_id=project_id + ) return { "success": True, @@ -137,8 +162,9 @@ async def delete_notification( async def clear_all_notifications( _current_user: CurrentUser, session: Annotated[AsyncSession, Depends(get_session)], + project_id: ProjectScope = None, ): - await NotificationManager.clear_all(session=session) + await NotificationManager.clear_all(session=session, project_id=project_id) if settings.DEBUG: diff --git a/api/app/services/scan.py b/api/app/services/scan.py index bbdc26136..bf0a6abee 100644 --- a/api/app/services/scan.py +++ b/api/app/services/scan.py @@ -17,7 +17,6 @@ from app.services.scan_context import ScanContextService from app.services.scan_engine import ScanEngineService, stage_effects from app.services.target import TargetService from shared.config import BaseAppSettings -from shared.definitions.notifications import scan_cancelled from shared.definitions.rescan import ASSET_SEED_STAGE, rescan_label from shared.enums.api_key import APIProvider from shared.enums.scan import SCAN_LIVE_STATUSES, ScanActivityStatus, ScanStatus @@ -52,7 +51,6 @@ from shared.models.subdomain import Subdomain from shared.models.target import Target from shared.services.celery_dispatch import revoke_scan_tasks from shared.services.launch_plan import AdHocEngine, plan_label -from shared.services.notification import NotificationManager from shared.services.orchestrator.events import ScanEventPublisher from shared.services.scan_factory import build_scan_row from shared.services.scan_resolve import ( @@ -1122,19 +1120,6 @@ class ScanService: return self._to_read(scan) async def _announce_cancelled(self, scan: Scan) -> None: - target_value = (scan.execution_config or {}).get("target_value", "") - payload = scan_cancelled(str(scan.id), target_value, scan.engine_name) - try: - await NotificationManager.publish( - session=self.session, - type=payload["type"], - severity=payload["severity"], - title=payload["title"], - message=payload["message"], - metadata=payload.get("metadata"), - ) - except Exception: - logger.warning("cancel notification dispatch failed", exc_info=True) try: ScanEventPublisher( BaseAppSettings().redis_url, diff --git a/frontend/src/lib/api/notifications.ts b/frontend/src/lib/api/notifications.ts index 32d4abfdd..1f625df35 100644 --- a/frontend/src/lib/api/notifications.ts +++ b/frontend/src/lib/api/notifications.ts @@ -2,28 +2,40 @@ import { api } from './client'; import type { Notification, NotificationStats } from '$lib/types/notification'; import type { PaginatedResponse } from '$lib/types/pagination'; -export const notificationsApi = { - list: (page = 1, size = 20): Promise> => { - return api.get>(`/notifications?page=${page}&size=${size}`); - }, +const scope = (projectId?: string) => (projectId ? `&project_id=${projectId}` : ''); - listUnread: (page = 1, size = 20): Promise> => { +export const notificationsApi = { + list: (page = 1, size = 20, projectId?: string): Promise> => { return api.get>( - `/notifications/unread?page=${page}&size=${size}` + `/notifications?page=${page}&size=${size}${scope(projectId)}` ); }, - stats: (): Promise => { - return api.get('/notifications/stats'); + listUnread: ( + page = 1, + size = 20, + projectId?: string + ): Promise> => { + return api.get>( + `/notifications/unread?page=${page}&size=${size}${scope(projectId)}` + ); + }, + + stats: (projectId?: string): Promise => { + return api.get( + `/notifications/stats${projectId ? `?project_id=${projectId}` : ''}` + ); }, markAsRead: (id: number): Promise<{ success: boolean; message: string }> => { return api.patch<{ success: boolean; message: string }>(`/notifications/${id}/read`, {}); }, - markAllAsRead: (): Promise<{ success: boolean; message: string; count: number }> => { + markAllAsRead: ( + projectId?: string + ): Promise<{ success: boolean; message: string; count: number }> => { return api.post<{ success: boolean; message: string; count: number }>( - '/notifications/read-all', + `/notifications/read-all${projectId ? `?project_id=${projectId}` : ''}`, {} ); }, @@ -32,7 +44,7 @@ export const notificationsApi = { return api.delete(`/notifications/${id}`); }, - clearAll: (): Promise => { - return api.delete('/notifications'); + clearAll: (projectId?: string): Promise => { + return api.delete(`/notifications${projectId ? `?project_id=${projectId}` : ''}`); } }; diff --git a/frontend/src/lib/components/layout/notifications-menu.svelte b/frontend/src/lib/components/layout/notifications-menu.svelte index 86649ed26..192086caa 100644 --- a/frontend/src/lib/components/layout/notifications-menu.svelte +++ b/frontend/src/lib/components/layout/notifications-menu.svelte @@ -65,6 +65,14 @@ return counts; }); + const presentTypes = $derived(NOTIFICATION_TYPES.filter((t) => typeCounts[t] > 0)); + + $effect(() => { + if (selectedFilter !== 'all' && !presentTypes.includes(selectedFilter)) { + selectedFilter = 'all'; + } + }); + function onPopoverChange(open: boolean) { popoverOpen = open; if (open) tab = notificationStore.unreadCount > 0 ? 'unread' : 'all'; @@ -116,7 +124,7 @@ try { await notificationStore.deleteNotification(id); } catch { - toast.error("Couldn't delete notification — try again"); + toast.error('Notification could not be deleted'); } }; @@ -124,7 +132,7 @@ try { await notificationStore.markAllAsRead(); } catch { - toast.error("Couldn't mark notifications as read — try again"); + toast.error('Notifications could not be marked read'); } }; @@ -134,7 +142,7 @@ await notificationStore.clearAll(); clearAllOpen = false; } catch { - toast.error("Couldn't clear notifications — try again"); + toast.error('Notifications could not be cleared'); } finally { clearing = false; } @@ -156,7 +164,7 @@ goto(ROUTES.settings('notifications')); }; - const retryLoad = () => notificationStore.loadNotifications(); + const retryLoad = () => notificationStore.loadNotifications(notificationStore.projectId); {#snippet skeletonRows(count: number)} @@ -271,7 +279,7 @@ - Couldn't load notifications + Notifications could not be loaded @@ -288,12 +296,12 @@ {/if} - {tab === 'unread' ? "You're all caught up" : 'No notifications yet'} + {tab === 'unread' ? 'Nothing unread' : 'No notifications yet'} {tab === 'unread' - ? 'New alerts and scan results land here.' - : 'Scan results, findings and system events will appear here.'} + ? 'Every notification has been read.' + : 'Scan results, findings and system events appear here.'} @@ -397,16 +405,14 @@ {typeCounts.all} - {#each NOTIFICATION_TYPES as type (type)} + {#each presentTypes as type (type)} {@const TypeIcon = getTypeIcon(type)} {NOTIFICATION_TYPE_LABELS[type]} - {#if typeCounts[type] > 0} - - {typeCounts[type]} - - {/if} + + {typeCounts[type]} + {/each} @@ -431,10 +437,8 @@ - Couldn't load notifications - Something went wrong fetching your notifications. + Notifications could not be loaded + The request did not complete. Retry below.