Skip to content

⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.

fix(api): drop inbox notifications from projects the user left (#310)

Crofffcc477b86915
diff --git a/apps/api/src/modules/notifications/__tests__/integration/notifications.test.ts b/apps/api/src/modules/notifications/__tests__/integration/notifications.test.ts
index 61fc6cc7..647d44eb 100644
--- a/apps/api/src/modules/notifications/__tests__/integration/notifications.test.ts
+++ b/apps/api/src/modules/notifications/__tests__/integration/notifications.test.ts
@@ -201,6 +201,18 @@ describe('notifications', () => {
expect(unreadOnly.data!.items).toHaveLength(0);
});
+ it('drops the notifications of a project the user was removed from', async () => {
+ const { owner, columnId } = await setup();
+ const member = await addMember(owner);
+ await createIssue(owner.api, columnId, { assigneeUserId: member.userId });
+ expect((await member.api.notifications.get({ query: {} })).data!.items).toHaveLength(1);
+
+ await owner.api.projects({ projectKey: 'MKT' }).members({ userId: member.userId }).delete();
+
+ expect((await member.api.notifications.get({ query: {} })).data!.items).toHaveLength(0);
+ expect((await member.api.notifications.unread.get({ query: {} })).data!.unread).toBe(0);
+ });
+
it("a member cannot read or mutate another user's notification", async () => {
const { owner, columnId } = await setup();
const member = await addMember(owner);
diff --git a/apps/api/src/modules/notifications/service.ts b/apps/api/src/modules/notifications/service.ts
index ebced77a..f78b9f72 100644
--- a/apps/api/src/modules/notifications/service.ts
+++ b/apps/api/src/modules/notifications/service.ts
@@ -275,9 +275,11 @@ function mapRow(r: {
}
// One page of a user's inbox, newest first, keyset-paged on (created_at, id).
-// includeRead defaults to true (the inbox shows read notifications too); a snoozed
-// notification (snoozed_until still in the future) is hidden unless includeSnoozed.
-// limit is clamped to 1..100.
+// Only projects the user is still a member of: the row carries the issue title, its
+// current state and the project name read at query time, so a removed member must
+// not reach them. includeRead defaults to true (the inbox shows read notifications
+// too); a snoozed notification (snoozed_until still in the future) is hidden unless
+// includeSnoozed. limit is clamped to 1..100.
export async function listNotifications(
userId: string,
opts: { before?: NotificationCursor | null; limit?: number; filters?: NotificationFilters } = {},
@@ -317,6 +319,10 @@ export async function listNotifications(
payload: issueActivity.payload,
})
.from(notification)
+ .innerJoin(
+ projectMember,
+ and(eq(projectMember.projectId, notification.projectId), eq(projectMember.userId, userId)),
+ )
.innerJoin(issue, eq(issue.id, notification.issueId))
.innerJoin(project, eq(project.id, notification.projectId))
.innerJoin(projectColumn, eq(projectColumn.id, issue.columnId))
@@ -335,7 +341,7 @@ export async function listNotifications(
}
// The number of unread, non-snoozed notifications for the inbox badge, optionally
-// scoped to one project.
+// scoped to one project. Counts only what listNotifications shows.
export async function unreadCount(userId: string, projectId?: number): Promise<number> {
const conds = [
eq(notification.userId, userId),
@@ -346,6 +352,10 @@ export async function unreadCount(userId: string, projectId?: number): Promise<n
const [row] = await db
.select({ n: sql<number>`count(*)::int` })
.from(notification)
+ .innerJoin(
+ projectMember,
+ and(eq(projectMember.projectId, notification.projectId), eq(projectMember.userId, userId)),
+ )
.where(and(...conds));
return row?.n ?? 0;
}