Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ tdc conversation view <ref> # view conversation messages
tdc conversation view <ref> --pinned # only the pinned messages
tdc conversation done <ref> --yes # archive a conversation
tdc conversation undone <ref> --yes # unarchive it
tdc conversation mark-read <ref> # mark a conversation read
tdc msg view <ref> # view a conversation message
tdc msg pin <ref> # pin a message for everyone in the chat (unpin to undo)
tdc comment pin <ref> # pin a comment for everyone in the thread (unpin to undo)
Expand Down
15 changes: 15 additions & 0 deletions docs/SPEC.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions skills/comms-cli/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ tdc conversation reply <ref> "content" --json # Send and return message as JSON
tdc conversation reply <ref> "content" --json --full # Include all message fields
tdc conversation reply <ref> "content" --file ./a.png # Attach a file (repeatable; content optional)
tdc conversation reply <ref> "content" --no-mark-read # Reply but leave the conversation unread (a reply marks it read by default)
tdc conversation mark-read <ref> # Mark a conversation read
tdc conversation mark-read <ref> <ref> --yes # Mark multiple conversations read
tdc conversation done <ref> # Preview conversation archive (requires --yes to execute)
tdc conversation done <ref> --yes # Archive conversation
tdc conversation done <ref> --yes --json # Archive and return status as JSON
Expand Down Expand Up @@ -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 <command>, 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 <command>, 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

Expand All @@ -497,7 +499,7 @@ echo "Quick reply" | tdc conversation reply <ref> --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
Expand Down
146 changes: 144 additions & 2 deletions src/commands/conversation/conversation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { tmpdir } from 'node:os'
import { join } from 'node:path'
import {
captureConsole,
captureStream,
createTestProgram,
describeEmptyMachineOutput,
mockStdin,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 },
])
})
})
23 changes: 23 additions & 0 deletions src/commands/conversation/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down Expand Up @@ -74,6 +76,27 @@ export function conversationLabel(conversation: Pick<Conversation, 'id' | 'title
: `conversation ${conversation.id}`
}

/** Conversations for the shared `mark-read` / `mark-unread` runner in `src/lib/read-state.ts`. */
export const CONVERSATION_READ_STATE: ReadStateEntity<Conversation> = {
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()
}
Expand Down
24 changes: 24 additions & 0 deletions src/commands/conversation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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 <conversation-ref>')
.description('Mute a conversation (stop notifications)')
Expand Down
39 changes: 39 additions & 0 deletions src/commands/conversation/mark-read.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
await runReadStateMutation<Conversation, MarkReadStatus>(
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
},
}),
},
)
}
2 changes: 1 addition & 1 deletion src/commands/conversation/reply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`,
)
}
}
Expand Down
Loading