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
5 changes: 4 additions & 1 deletion CODEBASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ don't duplicate it here.
per-`EntityType` essential-field filtering, `formatError` / `formatErrorJson`,
`printJson` / `printNdjson` / `printEmpty`, `colors`, `pluralize`,
`closedThreadBadge` (the `(closed)` marker after a thread title), `pinnedBadge` (the
`(pinned)` marker after a pinned comment's or message's time).
`(pinned)` marker after a pinned comment's or message's time), and for deleted comments
and messages `deletedBadge` (the `(deleted)` marker), `renderEntryBody` (the body in
text output, a note in place of the API's `-`) and `clearDeletedContent` (`content: null`
in JSON output).
- **`options.ts`** — `ViewOptions`, `PaginatedViewOptions`, `MutationOptions`
(extend these rather than adding `json`/`full`/`ndjson` ad hoc).
- **`config.ts`** — `~/.config/comms-cli/config.json` I/O over cli-core
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"dependencies": {
"@pnpm/tabtab": "0.5.4",
"@todoist/cli-core": "2.1.0",
"@todoist/comms-sdk": "6.2.1",
"@todoist/comms-sdk": "6.3.1",
"chalk": "5.6.2",
"commander": "14.0.3",
"marked": "18.0.3",
Expand Down
10 changes: 2 additions & 8 deletions src/commands/comment/view.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import chalk from 'chalk'
import { getCommsClient } from '../../lib/api.js'
import { formatRelativeDate } from '../../lib/dates.js'
import { renderMarkdown } from '../../lib/markdown.js'
import type { ViewOptions } from '../../lib/options.js'
import {
clearDeletedContent,
colors,
DELETED_COMMENT_TEXT,
deletedBadge,
formatJson,
formatNdjson,
pinnedBadge,
renderEntryBody,
} from '../../lib/output.js'
import { resolveCommentId } from '../../lib/refs.js'

Expand Down Expand Up @@ -42,10 +40,6 @@ export async function viewComment(ref: string, options: ViewOptions): Promise<vo
console.log(
`${author} ${time}${pinnedBadge(comment.pinnedDate)}${deletedBadge(comment.deleted)} ${colors.timestamp(`id:${comment.id}`)}`,
)
if (comment.deleted) {
console.log(chalk.dim(DELETED_COMMENT_TEXT))
} else {
console.log(options.raw ? comment.content : await renderMarkdown(comment.content))
}
console.log(await renderEntryBody(comment, 'comment', options.raw ?? false))
console.log('')
}
68 changes: 68 additions & 0 deletions src/commands/conversation/conversation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1792,6 +1792,74 @@ describe('conversation pagination', () => {
})
})

describe('conversation view with a deleted message', () => {
beforeEach(() => {
vi.clearAllMocks()
})

it('marks the deleted message in place, and machine output drops its placeholder content', async () => {
const conversation = createConversation(42, [1], '2026-03-08T10:00:00.000Z')
const base = {
creator: 1,
conversationId: '42',
workspaceId: 1,
posted: new Date('2026-03-08T10:05:00.000Z'),
}
// The API keeps a deleted message, with "-" as its content.
const client = createClient({
activeConversations: [conversation],
messagesByConversation: {
'42': [
{ ...base, id: '98', content: '-', deleted: true },
{ ...base, id: '99', content: 'Still here', deleted: false },
],
},
})
apiMocks.getCommsClient.mockResolvedValue(client)
const consoleSpy = captureConsole('log')

await createProgram().parseAsync(['node', 'tdc', 'conversation', 'view', '42'])

const lines = consoleSpy.mock.calls.map((call) => String(call[0]))
const deletedHeaders = lines.filter((line) => line.includes('(deleted)'))
expect(deletedHeaders).toHaveLength(1)
expect(deletedHeaders[0]).toContain('id:98')
expect(lines).toContain('This message was deleted.')
expect(lines).not.toContain('-')
expect(lines).toContain('Still here')

for (const flags of [['--json'], ['--json', '--full']]) {
consoleSpy.mockClear()
await createProgram().parseAsync([
'node',
'tdc',
'conversation',
'view',
'42',
...flags,
])
const { messages } = JSON.parse(consoleSpy.mock.calls[0][0])
expect(
messages.map((m: { id: string; content: string | null; deleted: boolean }) => [
m.id,
m.content,
m.deleted,
]),
).toEqual([
['98', null, true],
['99', 'Still here', false],
])
}

consoleSpy.mockClear()
await createProgram().parseAsync(['node', 'tdc', 'conversation', 'view', '42', '--ndjson'])
const ndjsonMessage = consoleSpy.mock.calls
.map((call) => JSON.parse(String(call[0])))
.find((line) => line.id === '98')
expect(ndjsonMessage).toMatchObject({ type: 'message', content: null, deleted: true })
})
})

describe('conversation view --pinned', () => {
beforeEach(() => {
vi.clearAllMocks()
Expand Down
16 changes: 11 additions & 5 deletions src/commands/conversation/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ import chalk from 'chalk'
import { getCommsClient } from '../../lib/api.js'
import { formatRelativeDate } from '../../lib/dates.js'
import { CliError } from '../../lib/errors.js'
import { renderMarkdown } from '../../lib/markdown.js'
import { toDate } from '../../lib/options.js'
import { colors, filterEntityFields, pinnedBadge } from '../../lib/output.js'
import {
clearDeletedContent,
colors,
deletedBadge,
filterEntityFields,
pinnedBadge,
renderEntryBody,
} from '../../lib/output.js'
import { resolveConversationId } from '../../lib/refs.js'
import { buildConversationTitle, type ConversationViewOptions } from './helpers.js'

Expand Down Expand Up @@ -61,7 +67,7 @@ export async function viewConversation(
participantNames: conversation.userIds.map((id) => userMap.get(id)),
}
const messageOutput = messages.map((m) => ({
...m,
...clearDeletedContent(m),
creatorName: userMap.get(m.creator),
}))

Expand Down Expand Up @@ -103,9 +109,9 @@ export async function viewConversation(
const author = colors.author(userMap.get(message.creator) || `user:${message.creator}`)
const time = colors.timestamp(formatRelativeDate(message.posted))
console.log(
`${author} ${time}${pinnedBadge(message.pinnedDate)} ${colors.timestamp(`id:${message.id}`)}`,
`${author} ${time}${pinnedBadge(message.pinnedDate)}${deletedBadge(message.deleted)} ${colors.timestamp(`id:${message.id}`)}`,
)
console.log(options.raw ? message.content : await renderMarkdown(message.content))
console.log(await renderEntryBody(message, 'message', options.raw ?? false))
console.log('')
}
}
30 changes: 30 additions & 0 deletions src/commands/msg/msg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,36 @@ describe('msg view', () => {
})
})

describe('msg view with a deleted message', () => {
beforeEach(() => {
vi.clearAllMocks()
})

it('marks the message deleted, and machine output drops its placeholder content', async () => {
const client = createClient()
// The API keeps a deleted message, with "-" as its content.
const deleted = { ...createMessageFixture('200'), content: '-', deleted: true }
client.conversationMessages.getMessage.mockResolvedValue(deleted)
apiMocks.getCommsClient.mockResolvedValue(client)
const consoleSpy = captureConsole('log')

await createProgram().parseAsync(['node', 'tdc', 'msg', 'view', '200'])
const lines = consoleSpy.mock.calls.map((call) => String(call[0]))
expect(lines[0]).toContain('(deleted)')
expect(lines).toContain('This message was deleted.')
expect(lines).not.toContain('-')

for (const flags of [['--json'], ['--json', '--full'], ['--ndjson']]) {
consoleSpy.mockClear()
await createProgram().parseAsync(['node', 'tdc', 'msg', 'view', '200', ...flags])
expect(JSON.parse(consoleSpy.mock.calls[0][0])).toMatchObject({
content: null,
deleted: true,
})
}
})
})

describe('msg delete', () => {
beforeEach(() => {
vi.clearAllMocks()
Expand Down
19 changes: 13 additions & 6 deletions src/commands/msg/view.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { getCommsClient } from '../../lib/api.js'
import { formatRelativeDate } from '../../lib/dates.js'
import { renderMarkdown } from '../../lib/markdown.js'
import type { ViewOptions } from '../../lib/options.js'
import { colors, formatJson, formatNdjson, pinnedBadge } from '../../lib/output.js'
import {
clearDeletedContent,
colors,
deletedBadge,
formatJson,
formatNdjson,
pinnedBadge,
renderEntryBody,
} from '../../lib/output.js'
import { resolveMessageId } from '../../lib/refs.js'

export async function viewMessage(ref: string, options: ViewOptions): Promise<void> {
Expand All @@ -17,22 +24,22 @@ export async function viewMessage(ref: string, options: ViewOptions): Promise<vo
const creatorName = userResponse.fullName

if (options.json) {
const output = { ...message, creatorName }
const output = { ...clearDeletedContent(message), creatorName }
console.log(formatJson(output, options.full ? undefined : 'message', options.full))
return
}

if (options.ndjson) {
const output = { ...message, creatorName }
const output = { ...clearDeletedContent(message), creatorName }
console.log(formatNdjson([output], options.full ? undefined : 'message', options.full))
return
}

const author = colors.author(creatorName)
const time = colors.timestamp(formatRelativeDate(message.posted))
console.log(
`${author} ${time}${pinnedBadge(message.pinnedDate)} ${colors.timestamp(`id:${message.id}`)}`,
`${author} ${time}${pinnedBadge(message.pinnedDate)}${deletedBadge(message.deleted)} ${colors.timestamp(`id:${message.id}`)}`,
)
console.log(options.raw ? message.content : await renderMarkdown(message.content))
console.log(await renderEntryBody(message, 'message', options.raw ?? false))
console.log('')
}
9 changes: 2 additions & 7 deletions src/commands/thread/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import chalk from 'chalk'
import { getWorkspaceGroups, getWorkspaceUsers } from '../../lib/api.js'
import { formatRelativeDate } from '../../lib/dates.js'
import { isAccessible } from '../../lib/global-args.js'
import { renderMarkdown } from '../../lib/markdown.js'
import { colors, DELETED_COMMENT_TEXT, deletedBadge, pinnedBadge } from '../../lib/output.js'
import { colors, deletedBadge, pinnedBadge, renderEntryBody } from '../../lib/output.js'
import { assertChannelIsPublic } from '../../lib/public-channels.js'
import type { ReadStateEntity } from '../../lib/read-state.js'
import { partitionNotifyIds, resolveThreadId } from '../../lib/refs.js'
Expand Down Expand Up @@ -44,11 +43,7 @@ export async function printComment(
console.log(
`${author} ${time}${pinnedBadge(comment.pinnedDate)}${deletedBadge(comment.deleted)} ${colors.timestamp(`id:${comment.id}`)}`,
)
if (comment.deleted) {
console.log(chalk.dim(DELETED_COMMENT_TEXT))
} else {
console.log(raw ? comment.content : await renderMarkdown(comment.content))
}
console.log(await renderEntryBody(comment, 'comment', raw))
console.log('')
}

Expand Down
32 changes: 20 additions & 12 deletions src/lib/output.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { formatJson as formatJsonCore, formatNdjson as formatNdjsonCore } from '@todoist/cli-core'
import chalk from 'chalk'
import type { BaseCliError } from './errors.js'
import { renderMarkdown } from './markdown.js'
Comment thread
gnapse marked this conversation as resolved.
Resolved

type AnyCliError = BaseCliError<string>

Expand All @@ -25,24 +26,30 @@ export function pinnedBadge(pinnedDate: Date | null | undefined): string {
return pinnedDate ? chalk.dim(' (pinned)') : ''
}

/**
* Marks a deleted comment in human output. The API keeps deleted comments in place with
* "-" as their content, so they also need {@link DELETED_COMMENT_TEXT} instead of the body.
*/
// The API keeps a deleted comment or message in place, with "-" as its content and
// `deleted: true`. These helpers keep that placeholder out of every output.
type DeletableEntry = { content: string; deleted?: boolean | null }

/** Marks a deleted comment or message after its header in human output. */
export function deletedBadge(deleted: boolean | null | undefined): string {
return deleted ? chalk.dim(' (deleted)') : ''
}

export const DELETED_COMMENT_TEXT = 'This comment was deleted.'
/** The body of a comment or message in human output: a note in place of a deleted one's "-". */
export async function renderEntryBody(
entry: DeletableEntry,
kind: 'comment' | 'message',
raw: boolean,
): Promise<string> {
if (entry.deleted) return chalk.dim(`This ${kind} was deleted.`)
return raw ? entry.content : renderMarkdown(entry.content)
}

/**
* Nulls the content of a deleted comment for JSON output. The API sends "-" as a
* placeholder, which reads like real text; null says there is no content left.
*/
export function clearDeletedContent<T extends { content: string; deleted?: boolean | null }>(
comment: T,
/** Nulls a deleted entry's "-" content for JSON output; null says there is no content left. */
export function clearDeletedContent<T extends DeletableEntry>(
entry: T,
): Omit<T, 'content'> & { content: string | null } {
return comment.deleted ? { ...comment, content: null } : comment
return entry.deleted ? { ...entry, content: null } : entry
}

const THREAD_ESSENTIAL_FIELDS = [
Expand Down Expand Up @@ -94,6 +101,7 @@ const MESSAGE_ESSENTIAL_FIELDS = [
'reactions',
'pinnedDate',
'pinnedBy',
'deleted',
] as const

const WORKSPACE_ESSENTIAL_FIELDS = ['id', 'name', 'creator', 'plan'] as const
Expand Down