-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete.ts
More file actions
56 lines (46 loc) · 1.77 KB
/
Copy pathdelete.ts
File metadata and controls
56 lines (46 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { getCommsClient } from '../../lib/api.js'
import { CliError } from '../../lib/errors.js'
import type { MutationOptions } from '../../lib/options.js'
import { formatJson, printDryRun } from '../../lib/output.js'
import { assertChannelIsPublic } from '../../lib/public-channels.js'
import { resolveCommentId } from '../../lib/refs.js'
type DeleteOptions = MutationOptions & { yes?: boolean }
export async function deleteComment(ref: string, options: DeleteOptions): Promise<void> {
const commentId = resolveCommentId(ref)
const client = await getCommsClient()
const [comment, user] = await Promise.all([
client.comments.getComment(commentId),
client.users.getSessionUser(),
])
await assertChannelIsPublic(comment.channelId, comment.workspaceId)
if (comment.creator !== user.id) {
throw new CliError('NOT_CREATOR', 'You can only delete comments that you created.')
}
if (options.dryRun) {
const preview =
comment.content.length > 200 ? `${comment.content.slice(0, 200)}...` : comment.content
printDryRun('delete comment', {
Comment: commentId,
Thread: comment.threadId,
Content: preview,
})
return
}
if (!options.yes) {
if (options.json) {
throw new CliError(
'MISSING_YES_FLAG',
'--yes is required to execute deletion in --json mode.',
)
}
console.log(`Would delete comment ${commentId}`)
console.log('Use --yes to confirm.')
return
}
await client.comments.deleteComment(commentId)
if (options.json) {
console.log(formatJson({ id: commentId, deleted: true }))
return
}
console.log(`Comment ${commentId} deleted.`)
}