From dabc1fb5de786bc66cb50a7a135d46faa5bda9b5 Mon Sep 17 00:00:00 2001 From: Andreas Molnar <75847494+andreasmolnardev@users.noreply.github.com> Date: Thu, 22 Oct 2026 11:02:11 +0000 Subject: [PATCH 1/2] fix: scope notification read updates to owner --- .../src/lib/data/notifications/items.test.ts | 63 +++++++++++++++++++ .../src/lib/data/notifications/items.ts | 36 ++++++----- 2 files changed, 82 insertions(+), 17 deletions(-) create mode 100644 apps/backend/src/lib/data/notifications/items.test.ts diff --git a/apps/backend/src/lib/data/notifications/items.test.ts b/apps/backend/src/lib/data/notifications/items.test.ts new file mode 100644 index 000000000..b6986c606 --- /dev/null +++ b/apps/backend/src/lib/data/notifications/items.test.ts @@ -0,0 +1,63 @@ +import { describe, expect, mock, test } from "bun:test"; + +const notifications = [ + { id: "own-item", topicId: "own-topic", status: "received" }, + { id: "foreign-item", topicId: "foreign-topic", status: "received" }, +]; +const updates: Array<{ id: string; data: Record }> = []; + +const fakePB = { + collection(name: string) { + return { + async getFullList({ filter }: { filter: string }) { + if (name === "notificationTopics") { + return [{ id: "own-topic", userId: "user-a" }]; + } + if (name === "notificationItems") { + const topicIds = [...filter.matchAll(/topicId="([^"]+)"/g)].map((match) => match[1]); + return notifications.filter((item) => topicIds.includes(item.topicId)); + } + throw new Error(`Unexpected collection: ${name}`); + }, + async update(id: string, data: Record) { + updates.push({ id, data }); + const item = notifications.find((record) => record.id === id); + if (item) item.status = String(data.status); + return item; + }, + }; + }, +}; + +mock.module("../../pb/pocketbase", () => ({ + getSuperuserPB: async () => fakePB, +})); +mock.module("./publish", () => ({ queueNotificationForForwarding: async () => {} })); + +const { markNotificationsAsRead } = await import("./items"); + +describe("markNotificationsAsRead", () => { + test("marks a user's requested notification read without changing a foreign notification", async () => { + updates.length = 0; + notifications[0].status = "received"; + notifications[1].status = "received"; + + await markNotificationsAsRead("user-a", ["own-item", "foreign-item"]); + + expect(updates).toEqual([{ id: "own-item", data: { status: "read" } }]); + expect(notifications[0].status).toBe("read"); + expect(notifications[1].status).toBe("received"); + }); + + test("with no IDs, marks all unread notifications in the user's topics read", async () => { + updates.length = 0; + notifications[0].status = "received"; + notifications[1].status = "received"; + + await markNotificationsAsRead("user-a", []); + + expect(updates).toEqual([{ id: "own-item", data: { status: "read" } }]); + expect(notifications[0].status).toBe("read"); + expect(notifications[1].status).toBe("received"); + }); +}); diff --git a/apps/backend/src/lib/data/notifications/items.ts b/apps/backend/src/lib/data/notifications/items.ts index 3c51bad05..8aa726da6 100644 --- a/apps/backend/src/lib/data/notifications/items.ts +++ b/apps/backend/src/lib/data/notifications/items.ts @@ -158,25 +158,27 @@ export async function sendTestNotification(userId: string, topicId: string) { export async function markNotificationsAsRead(userId: string, ids: string[]) { const pb = await getSuperuserPB(); - let targetIds = ids; + const topics = (await pb.collection("notificationTopics").getFullList({ + filter: `userId="${userId}"`, + })) as Array; + const topicIds = topics.map((topic) => topic.id); + + if (topicIds.length === 0) { + return null; + } + + // Scope records to the user's topics before considering caller-supplied IDs. + // The superuser client bypasses PocketBase rules, so IDs alone are not an + // authorization check. + const topicFilter = topicIds.map((id) => `topicId="${id}"`).join(" || "); + const items = (await pb.collection("notificationItems").getFullList({ filter: topicFilter })) as Array; + const requestedIds = new Set(ids); + const targetIds = items + .filter((item) => ids.length === 0 ? item.status !== "read" : requestedIds.has(item.id)) + .map((item) => item.id); if (targetIds.length === 0) { - const topics = (await pb.collection("notificationTopics").getFullList({ - filter: `userId="${userId}"`, - })) as Array; - const topicIds = topics.map((topic) => topic.id); - - if (topicIds.length === 0) { - return null; - } - - const filter = topicIds.map((id) => `topicId="${id}"`).join(" || "); - const items = (await pb.collection("notificationItems").getFullList({ filter, sort: "-created" })) as Array; - targetIds = items.filter((item) => item.status !== "read").map((item) => item.id); - - if (targetIds.length === 0) { - return null; - } + return null; } await Promise.allSettled( From 9fff1016dea5c8f9f21f4e7e2cc3459fb62fd2d0 Mon Sep 17 00:00:00 2001 From: Andreas Molnar <75847494+andreasmolnardev@users.noreply.github.com> Date: Thu, 22 Oct 2026 11:03:18 +0000 Subject: [PATCH 2/2] fix: unwrap CalDAV integration lookup --- .../src/routes/integrations.caldav.test.ts | 43 +++++++++++++++++++ apps/backend/src/routes/integrations.route.ts | 32 +++++++++++--- 2 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 apps/backend/src/routes/integrations.caldav.test.ts diff --git a/apps/backend/src/routes/integrations.caldav.test.ts b/apps/backend/src/routes/integrations.caldav.test.ts new file mode 100644 index 000000000..d9bf3be51 --- /dev/null +++ b/apps/backend/src/routes/integrations.caldav.test.ts @@ -0,0 +1,43 @@ +import { describe, expect, test } from "bun:test"; + +import { getCalDavEventsForIntegration } from "./integrations.route"; + +describe("CalDAV integration events", () => { + test("uses the integration wrapper and persists local data through the authenticated session", async () => { + const environment = { CALDAV_URL: "https://calendar.example/dav" }; + const localData = { calendarUrl: "/calendar/" }; + const updatedLocalData = { ...localData, syncToken: "next-token" }; + const updates: Array<{ id: string; body: unknown }> = []; + const pb = { + collection(name: string) { + expect(name).toBe("integrations"); + return { + async update(id: string, body: unknown) { + updates.push({ id, body }); + }, + }; + }, + }; + const eventCalls: unknown[][] = []; + const getEvents = async (...args: any[]) => { + eventCalls.push(args); + await args[2](updatedLocalData); + return [{ id: "event-1" }]; + }; + + const events = await getCalDavEventsForIntegration( + { integration: { environment, localData } }, + "owned-integration-id", + pb, + getEvents as any, + ); + + expect(eventCalls[0]?.[0]).toBe(environment); + expect(eventCalls[0]?.[1]).toBe(localData); + expect(events).toEqual([{ id: "event-1" }]); + expect(updates).toEqual([{ + id: "owned-integration-id", + body: { localData: updatedLocalData }, + }]); + }); +}); diff --git a/apps/backend/src/routes/integrations.route.ts b/apps/backend/src/routes/integrations.route.ts index f95278c8e..1fed25f79 100644 --- a/apps/backend/src/routes/integrations.route.ts +++ b/apps/backend/src/routes/integrations.route.ts @@ -388,17 +388,13 @@ integrationsRoute ) => pb.collection("integrations").update(id, { localData }); if (integrationId) { - const integration = await getIntegration(userId, integrationId); - if (!integration) { + const result = await getIntegration(userId, integrationId); + if (!result?.integration) { throw new ApiActionError("Integration not found", 404, { error: "Not found", }); } - const events = await getUpcomingEvents( - (integration as any).environment, - (integration as any).localData, - (ld) => updateLocalData(integrationId, ld).then(() => undefined), - ); + const events = await getCalDavEventsForIntegration(result, integrationId, pb); return { events }; } @@ -425,6 +421,28 @@ integrationsRoute export default integrationsRoute; +export async function getCalDavEventsForIntegration( + result: { + integration: { + environment: Record; + localData?: Record; + }; + }, + integrationId: string, + pb: any, + getEvents: typeof getUpcomingEvents = getUpcomingEvents, +) { + const integration = result.integration; + return getEvents( + integration.environment, + integration.localData, + (localData) => + pb.collection("integrations") + .update(integrationId, { localData }) + .then(() => undefined), + ); +} + // --- Types --- type ResolveConsumerDataOpts = {