Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(email): resolve a mention's subject from a real message, not a draft (#6475)

gbirman993b6ca7324b
diff --git a/apps/web/src/features/block-email/component/Block.tsx b/apps/web/src/features/block-email/component/Block.tsx
index 22411b0364..70666747b9 100644
--- a/apps/web/src/features/block-email/component/Block.tsx
+++ b/apps/web/src/features/block-email/component/Block.tsx
@@ -11,6 +11,7 @@ import {
import { buildEntityData } from '@entity';
import { EmailDebouncedReadMarker } from '@notifications';
import { useThreadQuery } from '@queries/email/thread';
+import { representativeThreadMessage } from '@queries/email/thread-subject';
import { createMemo, Show, Suspense } from 'solid-js';
import { EmailBlockAdapter } from '../EmailBlockAdapter';
@@ -33,7 +34,9 @@ export default function BlockEmail() {
if (!thread) return undefined;
return buildEntityData({
id: thread.db_id,
- name: displaySubject(thread.messages[0]?.subject),
+ name: displaySubject(
+ representativeThreadMessage(thread.messages)?.subject
+ ),
blockName: 'email',
isRead: thread.is_read,
done: !thread.inbox_visible,
@@ -66,7 +69,9 @@ export default function BlockEmail() {
const title = () => {
const data = threadData();
if (!data || !data.thread || data.thread.messages.length === 0) return '';
- return displaySubject(data.thread.messages[0].subject);
+ return displaySubject(
+ representativeThreadMessage(data.thread.messages)?.subject
+ );
};
return (
diff --git a/apps/web/src/lib/queries/email/thread-subject.test.ts b/apps/web/src/lib/queries/email/thread-subject.test.ts
new file mode 100644
index 0000000000..d4dc280ebf
--- /dev/null
+++ b/apps/web/src/lib/queries/email/thread-subject.test.ts
@@ -0,0 +1,53 @@
+import { describe, expect, it } from 'vitest';
+import { representativeThreadMessage } from './thread-subject';
+
+describe('representativeThreadMessage', () => {
+ it('returns undefined for an empty thread', () => {
+ expect(representativeThreadMessage([])).toBeUndefined();
+ });
+
+ it('takes the subject-bearing message when the newest message is a draft with no subject', () => {
+ // Newest-first: an unsent draft reply floats to the head with no subject.
+ const messages = [
+ { subject: '', is_draft: true },
+ { subject: '$1000 in credits from Macro', is_draft: false },
+ ];
+ expect(representativeThreadMessage(messages)?.subject).toBe(
+ '$1000 in credits from Macro'
+ );
+ });
+
+ it('skips a null-subject draft', () => {
+ const messages = [
+ { subject: null, is_draft: true },
+ { subject: 'Real subject', is_draft: false },
+ ];
+ expect(representativeThreadMessage(messages)?.subject).toBe('Real subject');
+ });
+
+ it('prefers the newest non-draft with a subject over an older one', () => {
+ const messages = [
+ { subject: 'Re: Newer', is_draft: false },
+ { subject: 'Older', is_draft: false },
+ ];
+ expect(representativeThreadMessage(messages)?.subject).toBe('Re: Newer');
+ });
+
+ it('falls back to a draft subject when no sent message has one', () => {
+ const messages = [
+ { subject: ' ', is_draft: false },
+ { subject: 'Draft subject', is_draft: true },
+ ];
+ expect(representativeThreadMessage(messages)?.subject).toBe(
+ 'Draft subject'
+ );
+ });
+
+ it('falls back to the newest message when nothing has a subject', () => {
+ const messages = [
+ { subject: '', is_draft: true },
+ { subject: null, is_draft: false },
+ ];
+ expect(representativeThreadMessage(messages)).toBe(messages[0]);
+ });
+});
diff --git a/apps/web/src/lib/queries/email/thread-subject.ts b/apps/web/src/lib/queries/email/thread-subject.ts
new file mode 100644
index 0000000000..4d662de64c
--- /dev/null
+++ b/apps/web/src/lib/queries/email/thread-subject.ts
@@ -0,0 +1,30 @@
+/** A thread message reduced to the fields that decide the thread's subject. */
+type SubjectMessage = {
+ subject?: string | null;
+ is_draft?: boolean;
+};
+
+const hasSubject = (message: SubjectMessage): boolean =>
+ !!message.subject && message.subject.trim().length > 0;
+
+/**
+ * Pick the message whose subject names the thread.
+ *
+ * Threads are returned newest-first, so `messages[0]` is not a safe proxy for
+ * the subject: an unsent draft reply carries `sent_at = now` and usually no
+ * subject, so it floats to the head of the thread. Taking its (empty) subject
+ * renders the thread as "No Subject" even though a real message in it has one —
+ * and a non-owner never sees the draft at all, so a limit-1 page comes back
+ * empty for them. Prefer the newest non-draft message that has a subject, then
+ * any message with a subject, and only fall back to the newest message when the
+ * thread genuinely has no subject anywhere.
+ */
+export function representativeThreadMessage<T extends SubjectMessage>(
+ messages: ReadonlyArray<T>
+): T | undefined {
+ return (
+ messages.find((m) => !m.is_draft && hasSubject(m)) ??
+ messages.find(hasSubject) ??
+ messages[0]
+ );
+}
diff --git a/apps/web/src/lib/queries/preview/fetchers.ts b/apps/web/src/lib/queries/preview/fetchers.ts
index f88cf8fed0..aaf8789db0 100644
--- a/apps/web/src/lib/queries/preview/fetchers.ts
+++ b/apps/web/src/lib/queries/preview/fetchers.ts
@@ -3,6 +3,7 @@ import {
enableGraphqlSoup,
isFeatureEnabled,
} from '@core/constant/featureFlags';
+import { DEFAULT_THREAD_MESSAGES_LIMIT } from '@core/constant/pagination';
import { cognitionApiServiceClient } from '@service-cognition/client';
import { emailClient } from '@service-email/client';
import type { ApiThread } from '@service-email/generated/schemas';
@@ -15,6 +16,7 @@ import { normalizeMessageSender } from '../channel/message-sender';
import { queryClient } from '../client';
import { emailKeys } from '../email/keys';
import { threadQueryOptions } from '../email/thread';
+import { representativeThreadMessage } from '../email/thread-subject';
import type { ItemEntity, MessageContext, PreviewItem } from './types';
async function fetchSessionPreviews(ids: string[]): Promise<PreviewItem[]> {
@@ -351,7 +353,11 @@ async function fetchEmailPreviews(threadIds: string[]): Promise<PreviewItem[]> {
const result = await emailClient.getThread({
thread_id: threadId,
offset: 0,
- limit: 1,
+ // Fetch a page rather than a single message: the newest message can
+ // be a subjectless draft (or, for a non-owner, be filtered out
+ // entirely), so a limit-1 fetch would resolve the thread as "No
+ // Subject" even though a real message in it has one.
+ limit: DEFAULT_THREAD_MESSAGES_LIMIT,
});
if (result.isErr()) {
@@ -364,10 +370,10 @@ async function fetchEmailPreviews(threadIds: string[]): Promise<PreviewItem[]> {
thread = result.value.thread;
}
- const firstMessage = thread.messages[0];
- const subject = firstMessage?.subject ?? 'No Subject';
+ const representative = representativeThreadMessage(thread.messages);
+ const subject = representative?.subject ?? 'No Subject';
const sender =
- firstMessage?.from?.email ?? firstMessage?.from?.name ?? undefined;
+ representative?.from?.email ?? representative?.from?.name ?? undefined;
return {
...base,