Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(queue-worker): add conversation context to push notifications (#806)

Generb1ea24a7f737
diff --git a/src/queue-worker/src/mobile-push.test.ts b/src/queue-worker/src/mobile-push.test.ts
index c50b7dbae..7614bad4b 100644
--- a/src/queue-worker/src/mobile-push.test.ts
+++ b/src/queue-worker/src/mobile-push.test.ts
@@ -50,6 +50,10 @@ const target = {
channelId: "channel-1",
authorName: "Alice",
content: "private message body",
+ conversationKind: "dm" as const,
+ serverName: null,
+ channelName: null,
+ parentChannelName: null,
attachmentContentTypes: [],
}
diff --git a/src/queue-worker/src/notification-payload.test.ts b/src/queue-worker/src/notification-payload.test.ts
index f3da980d1..dbce77adc 100644
--- a/src/queue-worker/src/notification-payload.test.ts
+++ b/src/queue-worker/src/notification-payload.test.ts
@@ -9,6 +9,10 @@ function target(overrides: Partial<{
channelId: string
authorName: string
content: string
+ conversationKind: "dm" | "channel" | "thread"
+ serverName: string | null
+ channelName: string | null
+ parentChannelName: string | null
attachmentContentTypes: Array<string | null>
}> = {}) {
return {
@@ -16,6 +20,10 @@ function target(overrides: Partial<{
channelId: "channel-1",
authorName: "Alice",
content: "**Hello** [there](https://example.test)\nnext line",
+ conversationKind: "dm" as const,
+ serverName: null,
+ channelName: null,
+ parentChannelName: null,
attachmentContentTypes: [],
...overrides,
}
@@ -32,7 +40,7 @@ describe("mobile notification payload", () => {
expect(otherUser).not.toBe(first)
})
- it("emits a minimal plain-text preview and opaque route", async () => {
+ it("uses the sender title and preview-only body for a DM", async () => {
const payload = await buildPushNotificationPayload(target(), "user-1")
expect(payload).toEqual({
@@ -48,6 +56,62 @@ describe("mobile notification payload", () => {
expect(JSON.stringify(payload)).not.toContain("https://example.test")
})
+ it.each([
+ [
+ "top-level channel",
+ target({
+ conversationKind: "channel",
+ serverName: "Studio",
+ channelName: "general",
+ }),
+ "Studio · #general",
+ ],
+ [
+ "text-channel thread",
+ target({
+ conversationKind: "thread",
+ serverName: "Studio",
+ channelName: "Focused work",
+ parentChannelName: "general",
+ }),
+ "Studio · #general · Focused work",
+ ],
+ [
+ "forum post",
+ target({
+ conversationKind: "thread",
+ serverName: "Studio",
+ channelName: "Release notes",
+ parentChannelName: "announcements",
+ }),
+ "Studio · #announcements · Release notes",
+ ],
+ ])("identifies the %s conversation in the title", async (_case, input, title) => {
+ const payload = await buildPushNotificationPayload(input, "user-1")
+
+ expect(payload.title).toBe(title)
+ expect(payload.body).toBe("Alice: Hello there next line")
+ expect(payload.route).toEqual({
+ notificationId: payload.notificationId,
+ messageId: "message-1",
+ targetId: "channel-1",
+ })
+ })
+
+ it("uses stable non-empty display fallbacks without changing the route", async () => {
+ const payload = await buildPushNotificationPayload(target({
+ authorName: " ",
+ conversationKind: "thread",
+ serverName: " ",
+ channelName: null,
+ parentChannelName: " ",
+ }), "user-1")
+
+ expect(payload.title).toBe("Server · #Channel · Thread")
+ expect(payload.body).toBe("Alook: Hello there next line")
+ expect(payload.route.targetId).toBe("channel-1")
+ })
+
it.each([
[["image/png"], "Photo"],
[["video/mp4"], "Video"],
@@ -62,10 +126,27 @@ describe("mobile notification payload", () => {
expect(payload.body).toBe(expected)
})
- it("caps the body without splitting surrogate pairs", async () => {
+ it("prefixes server attachment fallbacks with the author", async () => {
+ const payload = await buildPushNotificationPayload(target({
+ content: "",
+ conversationKind: "channel",
+ serverName: "Studio",
+ channelName: "general",
+ attachmentContentTypes: ["image/png"],
+ }), "user-1")
+
+ expect(payload.body).toBe("Alice: Photo")
+ })
+
+ it("caps the complete author-prefixed body without splitting surrogate pairs", async () => {
const payload = await buildPushNotificationPayload(target({
+ conversationKind: "channel",
+ serverName: "Studio",
+ channelName: "general",
content: `${"x".repeat(118)}😀tail`,
}), "user-1")
+ expect(payload.body.startsWith("Alice: ")).toBe(true)
+ expect(payload.body.length).toBeLessThanOrEqual(120)
expect(payload.body).not.toContain("�")
expect(payload.body.endsWith("…")).toBe(true)
})
diff --git a/src/queue-worker/src/notification-payload.ts b/src/queue-worker/src/notification-payload.ts
index 2e7fe2fba..9d2584be7 100644
--- a/src/queue-worker/src/notification-payload.ts
+++ b/src/queue-worker/src/notification-payload.ts
@@ -50,6 +50,26 @@ function attachmentFallback(contentTypes: Array<string | null>): string {
return "New message"
}
+function displayName(value: string | null, fallback: string): string {
+ return value?.trim() || fallback
+}
+
+function notificationTitle(
+ target: queries.communityNotificationTarget.PushNotificationTarget,
+ authorName: string,
+): string {
+ if (target.conversationKind === "dm") return authorName
+
+ const serverName = displayName(target.serverName, "Server")
+ if (target.conversationKind === "thread") {
+ const parentChannelName = displayName(target.parentChannelName, "Channel")
+ const threadName = displayName(target.channelName, "Thread")
+ return `${serverName} · #${parentChannelName} · ${threadName}`
+ }
+
+ return `${serverName} · #${displayName(target.channelName, "Channel")}`
+}
+
export async function buildPushNotificationPayload(
target: queries.communityNotificationTarget.PushNotificationTarget,
userId: string,
@@ -61,13 +81,16 @@ export async function buildPushNotificationPayload(
const plainText = stripInlineMarkup(target.content)
.replace(/\s+/gu, " ")
.trim()
+ const authorName = target.authorName.trim() || "Alook"
+ const preview = plainText || attachmentFallback(target.attachmentContentTypes)
+ const body = target.conversationKind === "dm"
+ ? preview
+ : `${authorName}: ${preview}`
return {
notificationId,
- title: target.authorName.trim() || "Alook",
- body: plainText
- ? truncateMessagePreview(plainText)
- : attachmentFallback(target.attachmentContentTypes),
+ title: notificationTitle(target, authorName),
+ body: truncateMessagePreview(body),
route: {
notificationId,
messageId: target.messageId,
diff --git a/src/queue-worker/src/providers/apns.test.ts b/src/queue-worker/src/providers/apns.test.ts
index 82ec81da1..1e8b5a603 100644
--- a/src/queue-worker/src/providers/apns.test.ts
+++ b/src/queue-worker/src/providers/apns.test.ts
@@ -7,8 +7,8 @@ import {
const payload = {
notificationId: "4cb8126e-4842-5c26-8d3f-02031c3d014b",
- title: "Alice",
- body: "Hello",
+ title: "Studio · #announcements · Release notes",
+ body: "Alice: Hello",
route: {
notificationId: "4cb8126e-4842-5c26-8d3f-02031c3d014b",
messageId: "message-1",
@@ -111,7 +111,10 @@ describe("APNs adapter", () => {
})
expect(JSON.parse(init?.body as string)).toEqual({
aps: {
- alert: { title: "Alice", body: "Hello" },
+ alert: {
+ title: "Studio · #announcements · Release notes",
+ body: "Alice: Hello",
+ },
sound: "default",
"thread-id": "channel-1",
},
diff --git a/src/queue-worker/src/providers/fcm.test.ts b/src/queue-worker/src/providers/fcm.test.ts
index 16ee4969b..2778908c6 100644
--- a/src/queue-worker/src/providers/fcm.test.ts
+++ b/src/queue-worker/src/providers/fcm.test.ts
@@ -3,8 +3,8 @@ import { createFcmAccessToken, sendFcmNotification } from "./fcm"
const payload = {
notificationId: "4cb8126e-4842-5c26-8d3f-02031c3d014b",
- title: "Alice",
- body: "Hello",
+ title: "Studio · #announcements · Release notes",
+ body: "Alice: Hello",
route: {
notificationId: "4cb8126e-4842-5c26-8d3f-02031c3d014b",
messageId: "message-1",
@@ -173,7 +173,10 @@ describe("FCM HTTP v1 adapter", () => {
expect(JSON.parse(init?.body as string)).toEqual({
message: {
token: "provider-token",
- notification: { title: "Alice", body: "Hello" },
+ notification: {
+ title: "Studio · #announcements · Release notes",
+ body: "Alice: Hello",
+ },
data: payload.route,
android: {
collapse_key: payload.notificationId,
diff --git a/src/shared/src/db/queries/community/notification-target.ts b/src/shared/src/db/queries/community/notification-target.ts
index 89f28a1c3..4ff0006bf 100644
--- a/src/shared/src/db/queries/community/notification-target.ts
+++ b/src/shared/src/db/queries/community/notification-target.ts
@@ -1,7 +1,9 @@
-import { asc, eq } from "drizzle-orm";
+import { aliasedTable, asc, eq } from "drizzle-orm";
import {
communityAttachment,
+ communityChannel,
communityMessage,
+ communityServer,
} from "../../community-schema";
import { user } from "../../schema";
import type { Database } from "../../index";
@@ -11,6 +13,10 @@ export interface PushNotificationTarget {
channelId: string;
authorName: string;
content: string;
+ conversationKind: "dm" | "channel" | "thread";
+ serverName: string | null;
+ channelName: string | null;
+ parentChannelName: string | null;
attachmentContentTypes: Array<string | null>;
}
@@ -18,15 +24,36 @@ export async function getPushNotificationTarget(
db: Database,
messageId: string,
): Promise<PushNotificationTarget | null> {
+ const parentChannel = aliasedTable(
+ communityChannel,
+ "push_notification_parent_channel",
+ );
const rows = await db
.select({
messageId: communityMessage.id,
channelId: communityMessage.channelId,
authorName: user.name,
content: communityMessage.content,
+ channelType: communityChannel.type,
+ channelName: communityChannel.name,
+ parentChannelId: communityChannel.parentChannelId,
+ parentChannelName: parentChannel.name,
+ serverName: communityServer.name,
})
.from(communityMessage)
.innerJoin(user, eq(user.id, communityMessage.authorId))
+ .innerJoin(
+ communityChannel,
+ eq(communityChannel.id, communityMessage.channelId),
+ )
+ .leftJoin(
+ parentChannel,
+ eq(parentChannel.id, communityChannel.parentChannelId),
+ )
+ .leftJoin(
+ communityServer,
+ eq(communityServer.id, communityChannel.serverId),
+ )
.where(eq(communityMessage.id, messageId))
.limit(1);
const message = rows[0];
@@ -42,7 +69,18 @@ export async function getPushNotificationTarget(
);
return {
- ...message,
+ messageId: message.messageId,
+ channelId: message.channelId,
+ authorName: message.authorName,
+ content: message.content,
+ conversationKind: message.channelType === "dm"
+ ? "dm"
+ : message.parentChannelId
+ ? "thread"
+ : "channel",
+ serverName: message.serverName,
+ channelName: message.channelName,
+ parentChannelName: message.parentChannelName,
attachmentContentTypes: attachments.map((row) => row.contentType),
};
}
diff --git a/src/shared/test/queries/community-notification-target.test.ts b/src/shared/test/queries/community-notification-target.test.ts
index 9942bf41f..203ca59dd 100644
--- a/src/shared/test/queries/community-notification-target.test.ts
+++ b/src/shared/test/queries/community-notification-target.test.ts
@@ -16,6 +16,17 @@ describe("community notification target query", () => {
sqlite = new Sqlite(":memory:");
sqlite.exec(`
CREATE TABLE user (id TEXT PRIMARY KEY NOT NULL, name TEXT NOT NULL);
+ CREATE TABLE community_server (
+ id TEXT PRIMARY KEY NOT NULL,
+ name TEXT NOT NULL
+ );
+ CREATE TABLE community_channel (
+ id TEXT PRIMARY KEY NOT NULL,
+ server_id TEXT,
+ name TEXT,
+ type TEXT NOT NULL,
+ parent_channel_id TEXT
+ );
CREATE TABLE community_message (
id TEXT PRIMARY KEY NOT NULL,
author_id TEXT NOT NULL,
@@ -29,30 +40,117 @@ describe("community notification target query", () => {
position INTEGER,
created_at TEXT NOT NULL
);
- INSERT INTO user (id, name) VALUES ('author-1', 'Alice');
- INSERT INTO community_message (id, author_id, content, channel_id)
- VALUES ('message-1', 'author-1', '**hello**', 'channel-1');
+ INSERT INTO user (id, name) VALUES
+ ('author-1', 'Alice'),
+ ('author-blank', ' ');
+ INSERT INTO community_server (id, name) VALUES
+ ('server-1', 'Studio'),
+ ('server-blank', ' ');
+ INSERT INTO community_channel
+ (id, server_id, name, type, parent_channel_id)
+ VALUES
+ ('dm-1', NULL, NULL, 'dm', NULL),
+ ('channel-1', 'server-1', 'general', 'text', NULL),
+ ('forum-1', 'server-1', 'announcements', 'forum', NULL),
+ ('post-1', 'server-1', 'Release notes', 'thread', 'forum-1'),
+ ('thread-1', 'server-1', 'Focused work', 'thread', 'channel-1'),
+ ('channel-blank', 'server-blank', ' ', 'text', NULL),
+ ('thread-blank', 'server-blank', NULL, 'thread', 'channel-blank'),
+ ('thread-orphan', 'server-missing', 'Still pushable', 'thread', 'parent-missing');
+ INSERT INTO community_message (id, author_id, content, channel_id) VALUES
+ ('message-dm', 'author-1', '**hello**', 'dm-1'),
+ ('message-channel', 'author-1', '**hello**', 'channel-1'),
+ ('message-thread', 'author-1', '**hello**', 'thread-1'),
+ ('message-post', 'author-1', '**hello**', 'post-1'),
+ ('message-fallback', 'author-blank', '**hello**', 'thread-blank'),
+ ('message-orphan', 'author-1', '**hello**', 'thread-orphan');
INSERT INTO community_attachment
(id, message_id, content_type, position, created_at)
VALUES
- ('attachment-2', 'message-1', 'application/pdf', 1, '2026-09-12T00:00:01.000Z'),
- ('attachment-1', 'message-1', 'image/png', 0, '2026-09-12T00:00:00.000Z');
+ ('attachment-2', 'message-channel', 'application/pdf', 1, '2026-09-12T00:00:01.000Z'),
+ ('attachment-1', 'message-channel', 'image/png', 0, '2026-09-12T00:00:00.000Z');
`);
db = drizzle(sqlite, { schema }) as unknown as Database;
});
afterEach(() => sqlite.close());
- it("loads minimal display content only after the caller has gated eligibility", async () => {
- await expect(getPushNotificationTarget(db, "message-1")).resolves.toEqual({
- messageId: "message-1",
+ it("loads a DM target without server presentation fields", async () => {
+ await expect(getPushNotificationTarget(db, "message-dm")).resolves.toEqual({
+ messageId: "message-dm",
+ channelId: "dm-1",
+ authorName: "Alice",
+ content: "**hello**",
+ conversationKind: "dm",
+ serverName: null,
+ channelName: null,
+ parentChannelName: null,
+ attachmentContentTypes: [],
+ });
+ });
+
+ it("loads a top-level server channel and ordered attachment types", async () => {
+ await expect(getPushNotificationTarget(db, "message-channel")).resolves.toEqual({
+ messageId: "message-channel",
channelId: "channel-1",
authorName: "Alice",
content: "**hello**",
+ conversationKind: "channel",
+ serverName: "Studio",
+ channelName: "general",
+ parentChannelName: null,
attachmentContentTypes: ["image/png", "application/pdf"],
});
});
+ it.each([
+ ["message-thread", "thread-1", "Focused work", "general"],
+ ["message-post", "post-1", "Release notes", "announcements"],
+ ])(
+ "loads parent and child names for %s",
+ async (messageId, channelId, channelName, parentChannelName) => {
+ await expect(getPushNotificationTarget(db, messageId)).resolves.toEqual({
+ messageId,
+ channelId,
+ authorName: "Alice",
+ content: "**hello**",
+ conversationKind: "thread",
+ serverName: "Studio",
+ channelName,
+ parentChannelName,
+ attachmentContentTypes: [],
+ });
+ },
+ );
+
+ it("preserves blank and missing names for deterministic payload fallbacks", async () => {
+ await expect(getPushNotificationTarget(db, "message-fallback")).resolves.toEqual({
+ messageId: "message-fallback",
+ channelId: "thread-blank",
+ authorName: " ",
+ content: "**hello**",
+ conversationKind: "thread",
+ serverName: " ",
+ channelName: null,
+ parentChannelName: " ",
+ attachmentContentTypes: [],
+ });
+ });
+
+ it("does not filter an eligible message when parent and server rows are missing", async () => {
+ await expect(getPushNotificationTarget(db, "message-orphan")).resolves.toEqual({
+ messageId: "message-orphan",
+ channelId: "thread-orphan",
+ authorName: "Alice",
+ content: "**hello**",
+ conversationKind: "thread",
+ serverName: null,
+ channelName: "Still pushable",
+ parentChannelName: null,
+ attachmentContentTypes: [],
+ });
+ });
+
it("returns null for a deleted or missing message", async () => {
await expect(getPushNotificationTarget(db, "missing")).resolves.toBeNull();
});