diff --git a/docs/SPEC.md b/docs/SPEC.md index 8da1b51..e51c2b5 100644 --- a/docs/SPEC.md +++ b/docs/SPEC.md @@ -191,6 +191,7 @@ Options: - `--stdin` - Read content from stdin - `--dry-run` - Show what would be posted without posting +- `--no-mark-read` - Leave the thread unread. By default the reply marks it read for you, up to and including the reply; if that fails, the comment stays posted and a warning goes to stderr Output: @@ -264,6 +265,7 @@ Options: - `--stdin` - Read content from stdin - `--dry-run` - Show what would be sent without sending +- `--no-mark-read` - Leave the conversation unread. By default the reply marks it read for you, up to and including the reply; if that fails, the message stays sent and a warning goes to stderr Output: diff --git a/skills/comms-cli/SKILL.md b/skills/comms-cli/SKILL.md index b66f853..8bae255 100644 --- a/skills/comms-cli/SKILL.md +++ b/skills/comms-cli/SKILL.md @@ -117,6 +117,7 @@ tdc thread reply "content" --close # Reply and close the thread tdc thread reply "content" --reopen # Reply and reopen a closed thread tdc thread reply "content" --close --notify NONE # Reply and close without notifying anyone tdc thread reply "content" --file ./a.png # Attach a file (repeatable; content optional) +tdc thread reply "content" --no-mark-read # Reply but leave the thread unread (a reply marks it read by default) tdc thread done # Preview thread archive (requires --yes to execute) tdc thread done --yes # Archive thread (mark done) tdc thread done --yes --json # Archive and return status as JSON @@ -202,6 +203,7 @@ tdc conversation reply "content" # Send a message tdc conversation reply "content" --json # Send and return message as JSON tdc conversation reply "content" --json --full # Include all message fields tdc conversation reply "content" --file ./a.png # Attach a file (repeatable; content optional) +tdc conversation reply "content" --no-mark-read # Reply but leave the conversation unread (a reply marks it read by default) tdc conversation done # Preview conversation archive (requires --yes to execute) tdc conversation done --yes # Archive conversation tdc conversation done --yes --json # Archive and return status as JSON diff --git a/src/commands/conversation/conversation.test.ts b/src/commands/conversation/conversation.test.ts index c9fe97c..46ec5c8 100644 --- a/src/commands/conversation/conversation.test.ts +++ b/src/commands/conversation/conversation.test.ts @@ -126,6 +126,9 @@ function createClient({ ...conversationsById.get(id), mutedUntil: null, })), + markRead: vi.fn(async (_args: { id: string; messageId: string }) => ({ + status: 'ok', + })), }, conversationMessages: { getPinnedMessages: vi.fn( @@ -1884,3 +1887,89 @@ describe('conversation view --pinned', () => { ]) }) }) + +// Comms never moves the poster's own read position when they post, so a reply to an unread +// conversation left it unread, with no command to clear it. +describe('conversation reply marks the conversation read', () => { + beforeEach(() => { + vi.clearAllMocks() + }) + + it('marks the conversation read through the sent message', async () => { + const { client, program } = setupFileTest() + const logSpy = captureConsole('log') + + await program.parseAsync(['node', 'tdc', 'conversation', 'reply', '42', 'On it']) + + expect(client.conversations.markRead).toHaveBeenCalledWith({ id: '42', messageId: '999' }) + expect(logSpy).toHaveBeenCalledWith('Message sent: https://comms.todoist.com/a/1/msg/999') + }) + + it('leaves the conversation unread with --no-mark-read', async () => { + const { client, program } = setupFileTest() + captureConsole('log') + + await program.parseAsync([ + 'node', + 'tdc', + 'conversation', + 'reply', + '42', + 'On it', + '--no-mark-read', + ]) + + expect(client.conversationMessages.createMessage).toHaveBeenCalledWith( + expect.objectContaining({ conversationId: '42', content: 'On it' }), + ) + expect(client.conversations.markRead).not.toHaveBeenCalled() + }) + + it.each([ + [[], 'yes'], + [['--no-mark-read'], 'no'], + ])('marks nothing read on a dry run, and says whether it would (%j)', async (flags, shown) => { + const { client, program } = setupFileTest() + const logSpy = captureConsole('log') + + await program.parseAsync([ + 'node', + 'tdc', + 'conversation', + 'reply', + '42', + 'On it', + '--dry-run', + ...flags, + ]) + + expect(client.conversations.markRead).not.toHaveBeenCalled() + expect(logSpy).toHaveBeenCalledWith(` Mark read: ${shown}`) + }) + + it('marks nothing read when the message fails to send', async () => { + const { client, program } = setupFileTest() + client.conversationMessages.createMessage.mockRejectedValue( + new Error('Service unavailable'), + ) + + await expect( + program.parseAsync(['node', 'tdc', 'conversation', 'reply', '42', 'On it']), + ).rejects.toThrow('Service unavailable') + expect(client.conversations.markRead).not.toHaveBeenCalled() + }) + + it('still reports the sent message, with a warning, when marking it read fails', async () => { + const { client, program } = setupFileTest() + client.conversations.markRead.mockRejectedValue(new Error('Service unavailable')) + const logSpy = captureConsole('log') + const errorSpy = captureConsole('error') + + await program.parseAsync(['node', 'tdc', 'conversation', 'reply', '42', 'On it']) + + expect(logSpy).toHaveBeenCalledWith('Message sent: https://comms.todoist.com/a/1/msg/999') + expect(errorSpy).toHaveBeenCalledWith( + 'Warning: sent the message but failed to mark the conversation read (Service unavailable).', + ) + }) +}) diff --git a/src/commands/conversation/helpers.ts b/src/commands/conversation/helpers.ts index 2f036af..d7c78e7 100644 --- a/src/commands/conversation/helpers.ts +++ b/src/commands/conversation/helpers.ts @@ -47,7 +47,7 @@ export type ConversationRenderOptions = { export type ConversationState = 'active' | 'all' | 'archived' -export type ReplyOptions = MutationOptions & StdinOptions & { file?: string[] } +export type ReplyOptions = MutationOptions & StdinOptions & { file?: string[]; markRead?: boolean } export type MuteOptions = MutationOptions & { minutes?: string } diff --git a/src/commands/conversation/index.ts b/src/commands/conversation/index.ts index 4bbdd1e..5394477 100644 --- a/src/commands/conversation/index.ts +++ b/src/commands/conversation/index.ts @@ -132,6 +132,10 @@ Examples: .command('reply [content]') .description('Send a message in a conversation') .option('--file ', 'Attach a file (repeatable; content optional)', collect, []) + .option( + '--no-mark-read', + 'Leave the conversation unread for you (a reply marks it read by default)', + ) .option('--stdin', 'Read content from stdin') .option('--dry-run', 'Show what would be sent without sending') .option('--json', 'Output sent message as JSON') diff --git a/src/commands/conversation/reply.ts b/src/commands/conversation/reply.ts index 74f9557..8b6c333 100644 --- a/src/commands/conversation/reply.ts +++ b/src/commands/conversation/reply.ts @@ -42,6 +42,7 @@ export async function replyToConversation( printDryRun('send message to conversation', { Conversation: String(conversationId), Attach: hasFiles ? files.join(', ') : undefined, + 'Mark read': options.markRead === false ? 'no' : 'yes', Content: preview || undefined, }) return @@ -64,6 +65,21 @@ export async function replyToConversation( attachments, }) + // Comms leaves the poster's own unread marker where it was, so a reply to an unread + // conversation would otherwise stay unread. Marking through the reply, not the latest + // message, keeps anything sent after it unread. The message is already sent, so a failure + // warns rather than fails the command, which a script would read as a reason to send again. + if (options.markRead !== false) { + try { + await client.conversations.markRead({ id: conversationId, messageId: message.id }) + } catch (error) { + const detail = error instanceof Error ? error.message : String(error) + console.error( + `Warning: sent the message but failed to mark the conversation read (${detail}).`, + ) + } + } + if (options.json) { console.log(formatJson(message, 'message', options.full)) return diff --git a/src/commands/thread/index.ts b/src/commands/thread/index.ts index f8850f3..96355e9 100644 --- a/src/commands/thread/index.ts +++ b/src/commands/thread/index.ts @@ -59,6 +59,10 @@ Examples: ) .option('--close', 'Close the thread after replying') .option('--reopen', 'Reopen the thread after replying') + .option( + '--no-mark-read', + 'Leave the thread unread for you (a reply marks it read by default)', + ) .option('--file ', 'Attach a file (repeatable; content optional)', collect, []) .option('--stdin', 'Read content from stdin') .option('--dry-run', 'Show what would be posted without posting') diff --git a/src/commands/thread/reply.ts b/src/commands/thread/reply.ts index 03fb54c..be0784b 100644 --- a/src/commands/thread/reply.ts +++ b/src/commands/thread/reply.ts @@ -17,6 +17,7 @@ type ReplyOptions = MutationOptions & close?: boolean reopen?: boolean file?: string[] + markRead?: boolean } /** @@ -132,6 +133,7 @@ export async function replyToThread( ? formatNotifyLabel(resolved.notified.groups) : undefined, Attach: hasFiles ? files.join(', ') : undefined, + 'Mark read': options.markRead === false ? 'no' : 'yes', Content: preview || undefined, }) return @@ -174,6 +176,24 @@ export async function replyToThread( recipients, } as Parameters[0]) + // Comms leaves the poster's own unread marker where it was, so a reply to an unread thread + // would otherwise stay unread. Marking through the reply, not the thread's latest comment, + // keeps anything posted after it unread. The comment is already posted, so a failure warns + // rather than fails the command, which a script would read as a reason to post again. + if (options.markRead !== false) { + try { + if (typeof comment.objIndex !== 'number') { + throw new Error('Comms returned the comment without its position in the thread') + } + await client.threads.markRead({ id: threadId, objIndex: comment.objIndex }) + } catch (error) { + const detail = error instanceof Error ? error.message : String(error) + console.error( + `Warning: posted the comment but failed to mark the thread read (${detail}). Run \`tdc thread mark-read id:${threadId}\` to mark it read.`, + ) + } + } + if (options.json) { const output = resolved ? { ...comment, notified: resolved.notified } : comment console.log(formatJson(output, 'comment', options.full)) diff --git a/src/commands/thread/thread.test.ts b/src/commands/thread/thread.test.ts index e28fdc9..9d593bf 100644 --- a/src/commands/thread/thread.test.ts +++ b/src/commands/thread/thread.test.ts @@ -3113,3 +3113,128 @@ describe('thread view --pinned', () => { ]) }) }) + +// Comms never moves the poster's own read position when they post, so a reply to an unread +// thread left it unread until a separate mark-read. +describe('thread reply marks the thread read', () => { + beforeEach(() => { + vi.clearAllMocks() + }) + + it('marks the thread read through the posted comment', async () => { + const client = createClient() + apiMocks.getCommsClient.mockResolvedValue(client) + const logSpy = captureConsole('log') + + await createProgram().parseAsync(['node', 'tdc', 'thread', 'reply', '500', 'On it']) + + expect(client.threads.markRead).toHaveBeenCalledWith({ id: '500', objIndex: 12 }) + expect(logSpy).toHaveBeenCalledWith( + 'Comment posted: https://comms.todoist.com/a/10/ch/CH100/t/500/c/12', + ) + }) + + it.each([ + ['--close', 10], + ['--reopen', 11], + ])('marks the thread read through the comment that %s posts', async (flag, objIndex) => { + const thread = { ...createThreadFixture(500), closed: flag === '--reopen' } + const client = createClient({ thread }) + apiMocks.getCommsClient.mockResolvedValue(client) + captureConsole('log') + + await createProgram().parseAsync(['node', 'tdc', 'thread', 'reply', '500', 'Done', flag]) + + expect(client.threads.markRead).toHaveBeenCalledWith({ id: '500', objIndex }) + }) + + it('leaves the thread unread with --no-mark-read', async () => { + const client = createClient() + apiMocks.getCommsClient.mockResolvedValue(client) + captureConsole('log') + + await createProgram().parseAsync([ + 'node', + 'tdc', + 'thread', + 'reply', + '500', + 'On it', + '--no-mark-read', + ]) + + expect(client.comments.createComment).toHaveBeenCalledWith( + expect.objectContaining({ threadId: '500', content: 'On it' }), + ) + expect(client.threads.markRead).not.toHaveBeenCalled() + }) + + it.each([ + [[], 'yes'], + [['--no-mark-read'], 'no'], + ])('marks nothing read on a dry run, and says whether it would (%j)', async (flags, shown) => { + const client = createClient() + apiMocks.getCommsClient.mockResolvedValue(client) + const logSpy = captureConsole('log') + + await createProgram().parseAsync([ + 'node', + 'tdc', + 'thread', + 'reply', + '500', + 'On it', + '--dry-run', + ...flags, + ]) + + expect(client.threads.markRead).not.toHaveBeenCalled() + expect(logSpy).toHaveBeenCalledWith(` Mark read: ${shown}`) + }) + + it('marks nothing read when the reply fails', async () => { + const client = createClient() + client.comments.createComment.mockRejectedValue(new Error('Service unavailable')) + apiMocks.getCommsClient.mockResolvedValue(client) + + await expect( + createProgram().parseAsync(['node', 'tdc', 'thread', 'reply', '500', 'On it']), + ).rejects.toThrow('Service unavailable') + expect(client.threads.markRead).not.toHaveBeenCalled() + }) + + it('still reports the posted comment, with a warning, when marking it read fails', async () => { + const client = createClient() + client.threads.markRead.mockRejectedValue(new Error('Service unavailable')) + apiMocks.getCommsClient.mockResolvedValue(client) + const logSpy = captureConsole('log') + const errorSpy = captureConsole('error') + + await createProgram().parseAsync(['node', 'tdc', 'thread', 'reply', '500', 'On it']) + + expect(logSpy).toHaveBeenCalledWith( + 'Comment posted: https://comms.todoist.com/a/10/ch/CH100/t/500/c/12', + ) + expect(errorSpy).toHaveBeenCalledWith( + 'Warning: posted the comment but failed to mark the thread read (Service unavailable). Run `tdc thread mark-read id:500` to mark it read.', + ) + }) + + it('warns without calling Comms when the comment comes back without its position', async () => { + const client = createClient() + client.comments.createComment.mockResolvedValue({ + ...createComment(12, 12), + objIndex: undefined as unknown as number, + }) + apiMocks.getCommsClient.mockResolvedValue(client) + captureConsole('log') + const errorSpy = captureConsole('error') + + await createProgram().parseAsync(['node', 'tdc', 'thread', 'reply', '500', 'On it']) + + expect(client.threads.markRead).not.toHaveBeenCalled() + expect(errorSpy).toHaveBeenCalledWith( + 'Warning: posted the comment but failed to mark the thread read (Comms returned the comment without its position in the thread). Run `tdc thread mark-read id:500` to mark it read.', + ) + }) +}) diff --git a/src/lib/api.ts b/src/lib/api.ts index e2f5096..8ed7780 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -83,6 +83,7 @@ const API_SPINNER_MESSAGES: Record "content" --close # Reply and close the thread tdc thread reply "content" --reopen # Reply and reopen a closed thread tdc thread reply "content" --close --notify NONE # Reply and close without notifying anyone tdc thread reply "content" --file ./a.png # Attach a file (repeatable; content optional) +tdc thread reply "content" --no-mark-read # Reply but leave the thread unread (a reply marks it read by default) tdc thread done # Preview thread archive (requires --yes to execute) tdc thread done --yes # Archive thread (mark done) tdc thread done --yes --json # Archive and return status as JSON @@ -206,6 +207,7 @@ tdc conversation reply "content" # Send a message tdc conversation reply "content" --json # Send and return message as JSON tdc conversation reply "content" --json --full # Include all message fields tdc conversation reply "content" --file ./a.png # Attach a file (repeatable; content optional) +tdc conversation reply "content" --no-mark-read # Reply but leave the conversation unread (a reply marks it read by default) tdc conversation done # Preview conversation archive (requires --yes to execute) tdc conversation done --yes # Archive conversation tdc conversation done --yes --json # Archive and return status as JSON