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
2 changes: 2 additions & 0 deletions docs/SPEC.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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:

Expand Down
2 changes: 2 additions & 0 deletions skills/comms-cli/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ tdc thread reply <ref> "content" --close # Reply and close the thread
tdc thread reply <ref> "content" --reopen # Reply and reopen a closed thread
tdc thread reply <ref> "content" --close --notify NONE # Reply and close without notifying anyone
tdc thread reply <ref> "content" --file ./a.png # Attach a file (repeatable; content optional)
tdc thread reply <ref> "content" --no-mark-read # Reply but leave the thread unread (a reply marks it read by default)
tdc thread done <ref> # Preview thread archive (requires --yes to execute)
tdc thread done <ref> --yes # Archive thread (mark done)
tdc thread done <ref> --yes --json # Archive and return status as JSON
Expand Down Expand Up @@ -202,6 +203,7 @@ tdc conversation reply <ref> "content" # Send a message
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 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
89 changes: 89 additions & 0 deletions src/commands/conversation/conversation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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).',
)
})
})
2 changes: 1 addition & 1 deletion src/commands/conversation/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
4 changes: 4 additions & 0 deletions src/commands/conversation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ Examples:
.command('reply <conversation-ref> [content]')
.description('Send a message in a conversation')
.option('--file <path>', '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')
Expand Down
16 changes: 16 additions & 0 deletions src/commands/conversation/reply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/commands/thread/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <path>', 'Attach a file (repeatable; content optional)', collect, [])
.option('--stdin', 'Read content from stdin')
.option('--dry-run', 'Show what would be posted without posting')
Expand Down
20 changes: 20 additions & 0 deletions src/commands/thread/reply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type ReplyOptions = MutationOptions &
close?: boolean
reopen?: boolean
file?: string[]
markRead?: boolean
}

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -174,6 +176,24 @@ export async function replyToThread(
recipients,
} as Parameters<typeof client.comments.createComment>[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))
Expand Down
125 changes: 125 additions & 0 deletions src/commands/thread/thread.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
)
})
})
1 change: 1 addition & 0 deletions src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const API_SPINNER_MESSAGES: Record<string, { text: string; color?: 'blue' | 'gre
'conversations.getConversations': { text: 'Loading conversations...', color: 'blue' },
'conversations.getConversation': { text: 'Loading conversation...', color: 'blue' },
'conversations.getUnread': { text: 'Loading unread conversations...', color: 'blue' },
'conversations.markRead': { text: 'Marking conversation read...', color: 'yellow' },
'conversations.getOrCreateConversation': {
text: 'Opening conversation...',
color: 'green',
Expand Down
2 changes: 2 additions & 0 deletions src/lib/skills/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ tdc thread reply <ref> "content" --close # Reply and close the thread
tdc thread reply <ref> "content" --reopen # Reply and reopen a closed thread
tdc thread reply <ref> "content" --close --notify NONE # Reply and close without notifying anyone
tdc thread reply <ref> "content" --file ./a.png # Attach a file (repeatable; content optional)
tdc thread reply <ref> "content" --no-mark-read # Reply but leave the thread unread (a reply marks it read by default)
tdc thread done <ref> # Preview thread archive (requires --yes to execute)
tdc thread done <ref> --yes # Archive thread (mark done)
tdc thread done <ref> --yes --json # Archive and return status as JSON
Expand Down Expand Up @@ -206,6 +207,7 @@ tdc conversation reply <ref> "content" # Send a message
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 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