clear notification by toggling them off and back on

This commit is contained in:
KingKDot
2026-05-15 18:57:23 -05:00
parent 5aa3a304a8
commit b68aa4a4c7
6 changed files with 72 additions and 4 deletions
+6
View File
@@ -1945,6 +1945,12 @@ export function clearNotifications() {
console.log("[db] cleared all notifications and screenshots");
}
export function deleteNotificationsForClient(clientId: string): number {
db.run(`DELETE FROM notification_screenshots WHERE client_id = ?`, clientId);
const result = db.run(`DELETE FROM notifications WHERE client_id = ?`, clientId);
return (result as any)?.changes || 0;
}
export function getClientEnrollmentStatus(id: string): string | null {
const row = db
.query<{ enrollment_status: string }>(
+1
View File
@@ -512,6 +512,7 @@ async function startServer() {
CORS_HEADERS,
pendingScripts,
pendingCommandReplies,
broadcastNotificationsCleared: notificationPluginHandlers.broadcastNotificationsCleared,
},
wsUpgrade: {
isAuthorizedAgentRequest: isAuthorizedAgent,
@@ -25,6 +25,7 @@ import {
updateGroup,
deleteGroup,
setClientGroup,
deleteNotificationsForClient,
} from "../../db";
import { metrics } from "../../metrics";
import { encodeMessage } from "../../protocol";
@@ -58,6 +59,7 @@ type ClientRouteDeps = {
CORS_HEADERS: Record<string, string>;
pendingScripts: Map<string, PendingScript>;
pendingCommandReplies: Map<string, PendingCommandReply>;
broadcastNotificationsCleared: (clientId: string) => void;
};
export async function handleClientRoutes(
@@ -496,15 +498,18 @@ export async function handleClientRoutes(
const muted = !!body?.muted;
let updated = 0;
let cleared = 0;
for (const cid of clientIds) {
if (!canUserAccessClient(user.userId, user.role, cid)) continue;
if (setClientNotificationsMuted(cid, muted)) updated++;
cleared += deleteNotificationsForClient(cid);
deps.broadcastNotificationsCleared(cid);
}
notifyDashboardViewers();
const ip = server.requestIP(req)?.address || "unknown";
logAudit({ timestamp: Date.now(), username: user.username, ip, action: AuditAction.COMMAND, details: `bulk_set_notifications_muted:${muted}:${updated}/${clientIds.length}`, success: true });
return Response.json({ ok: true, updated, total: clientIds.length, muted }, { headers: deps.CORS_HEADERS });
logAudit({ timestamp: Date.now(), username: user.username, ip, action: AuditAction.COMMAND, details: `bulk_set_notifications_muted:${muted}:${updated}/${clientIds.length} (cleared ${cleared})`, success: true });
return Response.json({ ok: true, updated, total: clientIds.length, muted, cleared }, { headers: deps.CORS_HEADERS });
}
const muteMatch = url.pathname.match(/^\/api\/clients\/([^/]+)\/notifications-muted$/);
@@ -534,6 +539,8 @@ export async function handleClientRoutes(
if (!updated) {
return Response.json({ error: "Client not found" }, { status: 404 });
}
const cleared = deleteNotificationsForClient(targetId);
deps.broadcastNotificationsCleared(targetId);
notifyDashboardViewers();
const ip = server.requestIP(req)?.address || "unknown";
@@ -543,11 +550,11 @@ export async function handleClientRoutes(
ip,
action: AuditAction.COMMAND,
targetClientId: targetId,
details: muted ? "mute_notifications" : "unmute_notifications",
details: `${muted ? "mute_notifications" : "unmute_notifications"} (cleared ${cleared})`,
success: true,
});
return Response.json({ ok: true, muted }, { headers: deps.CORS_HEADERS });
return Response.json({ ok: true, muted, cleared }, { headers: deps.CORS_HEADERS });
}
const bookmarkMatch = url.pathname.match(/^\/api\/clients\/([^/]+)\/bookmark$/);
@@ -362,6 +362,25 @@ export function createNotificationPluginHandlers(deps: CreateDeps) {
}
},
broadcastNotificationsCleared(clientId: string) {
for (const key of Array.from(deps.notificationRate.keys())) {
if (key === clientId || key.startsWith(`${clientId}:`)) {
deps.notificationRate.delete(key);
}
}
const item = { type: "notifications_cleared", clientId, ts: Date.now() };
for (const session of sessionManager.getAllNotificationSessions().values()) {
const sRole = session.userRole ?? session.viewer.data.userRole ?? "";
const sUserId = session.userId ?? session.viewer.data.userId;
if (sRole !== "admin") {
if (sUserId === undefined || !deps.canUserAccessClient(sUserId, sRole, clientId)) {
continue;
}
}
safeSendViewer(session.viewer, item);
}
},
broadcastClientLifecycleEvent(
event: "client_online" | "client_offline" | "client_purgatory",
info: { id: string; host?: string; user?: string; os?: string; ip?: string; country?: string },