+
@@ -106,22 +121,12 @@ useHead({
});
// Fetch notifications
-const notifications = ref[]>([]);
-
-async function fetchNotifications() {
- const { data } = await useFetch("/api/v1/notifications");
- notifications.value = data.value || [];
-}
-
-// Initial fetch
-await fetchNotifications();
+const notifications = useNotifications();
// Mark a notification as read
async function markAsRead(id: string) {
await $dropFetch(`/api/v1/notifications/${id}/read`, { method: "POST" });
- const notification = notifications.value.find(
- (n: SerializeObject) => n.id === id,
- );
+ const notification = notifications.value.find((n) => n.id === id);
if (notification) {
notification.read = true;
}
@@ -129,12 +134,21 @@ async function markAsRead(id: string) {
// Mark all notifications as read
async function markAllAsRead() {
- await $dropFetch("/api/v1/notifications/readall", { method: "POST" });
- notifications.value.forEach(
- (notification: SerializeObject) => {
- notification.read = true;
- },
- );
+ await $dropFetch("/api/v1/notifications/readall", {
+ method: "POST",
+ failTitle: "Failed to read all notifications",
+ });
+ notifications.value.forEach((notification) => {
+ notification.read = true;
+ });
+}
+
+async function clearNotifications() {
+ await $dropFetch("/api/v1/notifications/clear", {
+ method: "POST",
+ failTitle: "Failed to clear notifications",
+ });
+ notifications.value = [];
}
// Delete a notification
diff --git a/server/api/v1/notifications/clear.post.ts b/server/api/v1/notifications/clear.post.ts
new file mode 100644
index 0000000..091d387
--- /dev/null
+++ b/server/api/v1/notifications/clear.post.ts
@@ -0,0 +1,25 @@
+import aclManager from "~/server/internal/acls";
+import prisma from "~/server/internal/db/database";
+
+export default defineEventHandler(async (h3) => {
+ const userId = await aclManager.getUserIdACL(h3, ["notifications:mark"]);
+ if (!userId) throw createError({ statusCode: 403 });
+
+ const acls = await aclManager.fetchAllACLs(h3);
+ if (!acls)
+ throw createError({
+ statusCode: 500,
+ statusMessage: "Got userId but no ACLs - what?",
+ });
+
+ await prisma.notification.deleteMany({
+ where: {
+ userId,
+ acls: {
+ hasSome: acls,
+ },
+ },
+ });
+
+ return;
+});