diff --git a/README.md b/README.md index 633116a..3cdc841 100644 --- a/README.md +++ b/README.md @@ -147,6 +147,7 @@ tdc conversation view # view conversation messages tdc conversation view --pinned # only the pinned messages tdc conversation done --yes # archive a conversation tdc conversation undone --yes # unarchive it +tdc conversation mark-read # mark a conversation read tdc msg view # view a conversation message tdc msg pin # pin a message for everyone in the chat (unpin to undo) tdc comment pin # pin a comment for everyone in the thread (unpin to undo) diff --git a/docs/SPEC.md b/docs/SPEC.md index e51c2b5..0f86d40 100644 --- a/docs/SPEC.md +++ b/docs/SPEC.md @@ -284,6 +284,21 @@ Options: - `--yes` - Confirm archive (required to execute) - `--dry-run` - Show what would happen without executing +#### `tdc conversation mark-read [conversation-refs...]` + +Mark conversations read for the current user, through their latest message. Conversations that are already read are reported and left alone. + +Arguments: + +- `conversation-refs` - Conversation IDs or Comms URLs + +Options: + +- `--yes` - Confirm a bulk run (required to execute more than one conversation) +- `--stdin` - Read conversation refs from stdin, one per line (`#` lines are skipped) +- `--dry-run` - Show what would happen without executing +- `--json` - One `{ id, isRead }` row per conversation + --- ### Conversation Message Commands diff --git a/skills/comms-cli/SKILL.md b/skills/comms-cli/SKILL.md index 8bae255..3a12e0b 100644 --- a/skills/comms-cli/SKILL.md +++ b/skills/comms-cli/SKILL.md @@ -204,6 +204,8 @@ 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 mark-read # Mark a conversation read +tdc conversation mark-read --yes # Mark multiple conversations read 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 @@ -476,7 +478,7 @@ Mutating commands accept `--dry-run` to preview the operation without making the Run without --dry-run to execute. ``` -Destructive commands (`channel delete`, `comment delete`, `msg delete`, `groups delete`, `thread delete`, `thread done`/`undone`, `conversation done`/`undone`, and bulk `thread mark-read`/`mark-unread`) only preview without `--yes`: they print `Would …` and `Use --yes to confirm.` and change nothing. Under `--json`, a missing `--yes` (without `--dry-run`) fails with `CONFIRMATION_REQUIRED` ("Pass --yes to execute , or --dry-run to preview it.") before any API call, so a script never reads a preview as the result. `--dry-run --json` prints one `{ "status": "preview", "command", "messages" }` object instead of the text preview; bulk `thread mark-read`/`mark-unread` keep their per-thread rows with `dryRun: true`. +Destructive commands (`channel delete`, `comment delete`, `msg delete`, `groups delete`, `thread delete`, `thread done`/`undone`, `conversation done`/`undone`, bulk `thread mark-read`/`mark-unread` and bulk `conversation mark-read`) only preview without `--yes`: they print `Would …` and `Use --yes to confirm.` and change nothing. Under `--json`, a missing `--yes` (without `--dry-run`) fails with `CONFIRMATION_REQUIRED` ("Pass --yes to execute , or --dry-run to preview it.") before any API call, so a script never reads a preview as the result. `--dry-run --json` prints one `{ "status": "preview", "command", "messages" }` object instead of the text preview; bulk `mark-read`/`mark-unread` keep their per-thread or per-conversation rows with `dryRun: true`. ## Reference System @@ -497,7 +499,7 @@ echo "Quick reply" | tdc conversation reply --stdin If neither a content argument nor `--stdin` is given, the CLI opens `$EDITOR` for interactive input. In non-TTY environments (e.g. when called by an agent or in a pipeline), the editor is automatically skipped and the command fails fast with an actionable error message. Use `--non-interactive` to force this behavior even in a TTY, or `--interactive` to override auto-detection. -`tdc thread mark-read` and `tdc thread mark-unread` read thread refs from stdin with `--stdin`, one per line (`#` lines are skipped), added to any refs passed as arguments: +`tdc thread mark-read`, `tdc thread mark-unread` and `tdc conversation mark-read` read their refs from stdin with `--stdin`, one per line (`#` lines are skipped), added to any refs passed as arguments: ```bash printf "id:CbT8n2Kp4Qx6Rz9Lm3Va\nid:CbT9m4Qr7Vz2Nx8Lp5Sa\n" | tdc thread mark-read --stdin --yes diff --git a/src/commands/conversation/conversation.test.ts b/src/commands/conversation/conversation.test.ts index 46ec5c8..17b7553 100644 --- a/src/commands/conversation/conversation.test.ts +++ b/src/commands/conversation/conversation.test.ts @@ -3,6 +3,7 @@ import { tmpdir } from 'node:os' import { join } from 'node:path' import { captureConsole, + captureStream, createTestProgram, describeEmptyMachineOutput, mockStdin, @@ -1889,7 +1890,7 @@ 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. +// conversation left it unread until a separate mark-read. describe('conversation reply marks the conversation read', () => { beforeEach(() => { vi.clearAllMocks() @@ -1969,7 +1970,148 @@ describe('conversation reply marks the conversation read', () => { 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).', + 'Warning: sent the message but failed to mark the conversation read (Service unavailable). Run `tdc conversation mark-read id:42` to mark it read.', ) }) }) + +describe('conversation mark-read', () => { + beforeEach(() => { + vi.clearAllMocks() + }) + + function setupMarkRead(unreadIds: string[]) { + const client = createClient({ + activeConversations: [ + { ...createConversation(42, [1, 2], '2026-03-08T10:00:00.000Z'), lastObjIndex: 7 }, + { + ...createConversation(43, [1, 2, 3], '2026-03-08T11:00:00.000Z'), + title: 'Design sync', + lastObjIndex: 4, + }, + ], + }) + client.conversations.getUnread.mockResolvedValue({ + data: unreadIds.map((conversationId) => ({ + conversationId, + objIndex: 1, + directMention: false, + })), + version: 1, + }) + apiMocks.getCommsClient.mockResolvedValue(client) + return { client, program: createProgram() } + } + + it('marks one unread conversation read through its latest message, without --yes', async () => { + const { client, program } = setupMarkRead(['42']) + const logSpy = captureConsole('log') + + await program.parseAsync(['node', 'tdc', 'conversation', 'mark-read', '42']) + + expect(client.conversations.getUnread).toHaveBeenCalledWith(1) + expect(client.conversations.markRead).toHaveBeenCalledWith({ id: '42', objIndex: 7 }) + expect(logSpy).toHaveBeenCalledWith('Conversation 42 marked read.') + }) + + it('names a titled conversation by its title', async () => { + const { program } = setupMarkRead(['43']) + const logSpy = captureConsole('log') + + await program.parseAsync(['node', 'tdc', 'conversation', 'mark-read', '43']) + + expect(logSpy).toHaveBeenCalledWith('Conversation Design sync (43) marked read.') + }) + + it('leaves a conversation that is already read unchanged', async () => { + const { client, program } = setupMarkRead([]) + const logSpy = captureConsole('log') + + await program.parseAsync(['node', 'tdc', 'conversation', 'mark-read', '42']) + + expect(client.conversations.markRead).not.toHaveBeenCalled() + expect(logSpy).toHaveBeenCalledWith('Conversation 42 is already read.') + }) + + it('shows what it would do on a dry run', async () => { + const { client, program } = setupMarkRead(['42']) + const logSpy = captureConsole('log') + + await program.parseAsync(['node', 'tdc', 'conversation', 'mark-read', '42', '--dry-run']) + + expect(client.conversations.markRead).not.toHaveBeenCalled() + expect(logSpy).toHaveBeenCalledWith('Dry run: would mark read conversation 42.') + }) + + it('previews several conversations without --yes', async () => { + const { client, program } = setupMarkRead(['42', '43']) + const logSpy = captureConsole('log') + + await program.parseAsync(['node', 'tdc', 'conversation', 'mark-read', '42', '43']) + + expect(client.conversations.markRead).not.toHaveBeenCalled() + expect(logSpy).toHaveBeenCalledWith('Would mark read conversation 42.') + expect(logSpy).toHaveBeenCalledWith('Would mark read conversation Design sync (43).') + expect(logSpy).toHaveBeenCalledWith('Use --yes to confirm.') + }) + + it('marks several conversations read with --yes, fetching the unread list once', async () => { + const { client, program } = setupMarkRead(['42']) + const logSpy = captureConsole('log') + + await program.parseAsync(['node', 'tdc', 'conversation', 'mark-read', '42', '43', '--yes']) + + expect(client.conversations.getUnread).toHaveBeenCalledTimes(1) + expect(client.conversations.markRead).toHaveBeenCalledTimes(1) + expect(client.conversations.markRead).toHaveBeenCalledWith({ id: '42', objIndex: 7 }) + expect(logSpy).toHaveBeenCalledWith('Conversation Design sync (43) is already read.') + expect(logSpy).toHaveBeenCalledWith( + 'Summary: 1 changed conversation, 1 unchanged conversation', + ) + }) + + it('reads conversation refs from stdin with --stdin', async () => { + const { client, program } = setupMarkRead(['42', '43']) + captureConsole('log') + mockStdin('42\n# skipped\n43\n') + + await program.parseAsync(['node', 'tdc', 'conversation', 'mark-read', '--stdin', '--yes']) + + expect(client.conversations.markRead).toHaveBeenCalledWith({ id: '42', objIndex: 7 }) + expect(client.conversations.markRead).toHaveBeenCalledWith({ id: '43', objIndex: 4 }) + }) + + it('shows help instead of reading piped refs when given no refs', async () => { + const { client, program } = setupMarkRead(['42']) + const stdout = captureStream('stdout') + mockStdin('42\n') + + await program.parseAsync(['node', 'tdc', 'conversation', 'mark-read']).catch(() => {}) + + expect(stdout.mock.calls.map((args) => String(args[0])).join('')).toContain( + 'Usage: tdc conversation mark-read', + ) + expect(client.conversations.markRead).not.toHaveBeenCalled() + }) + + it('prints a row per conversation with --json', async () => { + const { program } = setupMarkRead(['42']) + const logSpy = captureConsole('log') + + await program.parseAsync([ + 'node', + 'tdc', + 'conversation', + 'mark-read', + '42', + '43', + '--yes', + '--json', + ]) + + expect(JSON.parse(String(logSpy.mock.calls.at(-1)?.[0]))).toEqual([ + { id: '42', isRead: true }, + { id: '43', isRead: true }, + ]) + }) +}) diff --git a/src/commands/conversation/helpers.ts b/src/commands/conversation/helpers.ts index d7c78e7..c043b71 100644 --- a/src/commands/conversation/helpers.ts +++ b/src/commands/conversation/helpers.ts @@ -13,6 +13,8 @@ import type { ViewOptions, } from '../../lib/options.js' import { colors, formatJson, formatNdjson } from '../../lib/output.js' +import type { ReadStateEntity } from '../../lib/read-state.js' +import { resolveConversationId } from '../../lib/refs.js' export type UnreadOptions = ViewOptions & { workspace?: string } @@ -74,6 +76,27 @@ export function conversationLabel(conversation: Pick = { + noun: 'conversation', + command: 'conversation', + resolveId: resolveConversationId, + load: (client, conversationId) => client.conversations.getConversation(conversationId), + workspaceId: (conversation) => conversation.workspaceId, + loadUnread: async (client, workspaceId) => { + const unread = await client.conversations.getUnread(workspaceId) + return new Map( + unread.data.map((unreadConversation) => [ + unreadConversation.conversationId, + unreadConversation.objIndex, + ]), + ) + }, + // The messages already say "Conversation", so an untitled one is named by its id alone. + label: (conversation) => + conversation.title ? `${conversation.title} (${conversation.id})` : conversation.id, +} + export function sortByLastActiveDescending(a: Conversation, b: Conversation): number { return new Date(b.lastActive).getTime() - new Date(a.lastActive).getTime() } diff --git a/src/commands/conversation/index.ts b/src/commands/conversation/index.ts index 5394477..575c1ea 100644 --- a/src/commands/conversation/index.ts +++ b/src/commands/conversation/index.ts @@ -3,6 +3,7 @@ import { withCaseInsensitiveChoices } from '../../lib/completion.js' import { collect } from '../../lib/options.js' import { markConversationDone, markConversationUndone } from './archive.js' import { listConversations } from './list.js' +import { markConversationRead } from './mark-read.js' import { muteConversation } from './mute.js' import { replyToConversation } from './reply.js' import { unmuteConversation } from './unmute.js' @@ -181,6 +182,29 @@ Examples: ) .action(markConversationUndone) + const markReadCmd = conversation + .command('mark-read [conversation-refs...]') + .description('Mark a conversation read for the current user') + .option('--yes', 'Skip confirmation for bulk operations') + .option('--stdin', 'Read conversation refs from stdin, one per line') + .option('--dry-run', 'Show what would happen without executing') + .option('--json', 'Output result as JSON') + .addHelpText( + 'after', + ` +Examples: + tdc conversation mark-read id:CbV8n2Kp4Qx6Rz9Lm3Va + tdc conversation mark-read id:CbV8n2Kp4Qx6Rz9Lm3Va id:CbV9m4Qr7Vz2Nx8Lp5Sa --yes + printf "id:CbV8n2Kp4Qx6Rz9Lm3Va\\nid:CbV9m4Qr7Vz2Nx8Lp5Sa\\n" | tdc conversation mark-read --stdin --yes`, + ) + .action((refs, options) => { + if (refs.length === 0 && !options.stdin) { + markReadCmd.help() + return + } + return markConversationRead(refs, options) + }) + conversation .command('mute ') .description('Mute a conversation (stop notifications)') diff --git a/src/commands/conversation/mark-read.ts b/src/commands/conversation/mark-read.ts new file mode 100644 index 0000000..32fad88 --- /dev/null +++ b/src/commands/conversation/mark-read.ts @@ -0,0 +1,39 @@ +import type { Conversation } from '@todoist/comms-sdk' +import type { MutationOptions, StdinOptions } from '../../lib/options.js' +import { runReadStateMutation } from '../../lib/read-state.js' +import { CONVERSATION_READ_STATE } from './helpers.js' + +export type MarkConversationReadOptions = MutationOptions & StdinOptions + +type MarkReadStatus = { + id: string + isRead: true +} + +export async function markConversationRead( + refs: string[], + options: MarkConversationReadOptions, +): Promise { + await runReadStateMutation( + CONVERSATION_READ_STATE, + refs, + options, + { + verb: 'read', + plan: async (_client, conversationId) => ({ + scope: '', + isUnchanged: (state) => state.unreadObjIndex === null, + status: () => ({ id: conversationId, isRead: true }), + apply: async (client, state) => { + // The exact latest position: one past it would also clear the + // unread, but reads ahead of what the conversation holds. + await client.conversations.markRead({ + id: conversationId, + objIndex: state.item.lastObjIndex, + }) + return null + }, + }), + }, + ) +} diff --git a/src/commands/conversation/reply.ts b/src/commands/conversation/reply.ts index 8b6c333..94dc53a 100644 --- a/src/commands/conversation/reply.ts +++ b/src/commands/conversation/reply.ts @@ -75,7 +75,7 @@ export async function replyToConversation( } 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}).`, + `Warning: sent the message but failed to mark the conversation read (${detail}). Run \`tdc conversation mark-read id:${conversationId}\` to mark it read.`, ) } } diff --git a/src/lib/skills/content.ts b/src/lib/skills/content.ts index d592f10..4b18206 100644 --- a/src/lib/skills/content.ts +++ b/src/lib/skills/content.ts @@ -208,6 +208,8 @@ 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 mark-read # Mark a conversation read +tdc conversation mark-read --yes # Mark multiple conversations read 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 @@ -480,7 +482,7 @@ Mutating commands accept \`--dry-run\` to preview the operation without making t Run without --dry-run to execute. \`\`\` -Destructive commands (\`channel delete\`, \`comment delete\`, \`msg delete\`, \`groups delete\`, \`thread delete\`, \`thread done\`/\`undone\`, \`conversation done\`/\`undone\`, and bulk \`thread mark-read\`/\`mark-unread\`) only preview without \`--yes\`: they print \`Would …\` and \`Use --yes to confirm.\` and change nothing. Under \`--json\`, a missing \`--yes\` (without \`--dry-run\`) fails with \`CONFIRMATION_REQUIRED\` ("Pass --yes to execute , or --dry-run to preview it.") before any API call, so a script never reads a preview as the result. \`--dry-run --json\` prints one \`{ "status": "preview", "command", "messages" }\` object instead of the text preview; bulk \`thread mark-read\`/\`mark-unread\` keep their per-thread rows with \`dryRun: true\`. +Destructive commands (\`channel delete\`, \`comment delete\`, \`msg delete\`, \`groups delete\`, \`thread delete\`, \`thread done\`/\`undone\`, \`conversation done\`/\`undone\`, bulk \`thread mark-read\`/\`mark-unread\` and bulk \`conversation mark-read\`) only preview without \`--yes\`: they print \`Would …\` and \`Use --yes to confirm.\` and change nothing. Under \`--json\`, a missing \`--yes\` (without \`--dry-run\`) fails with \`CONFIRMATION_REQUIRED\` ("Pass --yes to execute , or --dry-run to preview it.") before any API call, so a script never reads a preview as the result. \`--dry-run --json\` prints one \`{ "status": "preview", "command", "messages" }\` object instead of the text preview; bulk \`mark-read\`/\`mark-unread\` keep their per-thread or per-conversation rows with \`dryRun: true\`. ## Reference System @@ -501,7 +503,7 @@ echo "Quick reply" | tdc conversation reply --stdin If neither a content argument nor \`--stdin\` is given, the CLI opens \`$EDITOR\` for interactive input. In non-TTY environments (e.g. when called by an agent or in a pipeline), the editor is automatically skipped and the command fails fast with an actionable error message. Use \`--non-interactive\` to force this behavior even in a TTY, or \`--interactive\` to override auto-detection. -\`tdc thread mark-read\` and \`tdc thread mark-unread\` read thread refs from stdin with \`--stdin\`, one per line (\`#\` lines are skipped), added to any refs passed as arguments: +\`tdc thread mark-read\`, \`tdc thread mark-unread\` and \`tdc conversation mark-read\` read their refs from stdin with \`--stdin\`, one per line (\`#\` lines are skipped), added to any refs passed as arguments: \`\`\`bash printf "id:CbT8n2Kp4Qx6Rz9Lm3Va\\nid:CbT9m4Qr7Vz2Nx8Lp5Sa\\n" | tdc thread mark-read --stdin --yes