-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreply.ts
More file actions
76 lines (68 loc) · 2.42 KB
/
reply.ts
File metadata and controls
76 lines (68 loc) · 2.42 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { getTwistClient } from '../../lib/api.js'
import { openEditor, readStdin } from '../../lib/input.js'
import type { MutationOptions } from '../../lib/options.js'
import { formatJson } from '../../lib/output.js'
import { assertChannelIsPublic } from '../../lib/public-channels.js'
import { extractId, resolveThreadId } from '../../lib/refs.js'
type ReplyOptions = MutationOptions & {
notify?: string
}
export async function replyToThread(
ref: string,
content: string | undefined,
options: ReplyOptions,
): Promise<void> {
const threadId = resolveThreadId(ref)
let replyContent = await readStdin()
if (!replyContent && content) {
replyContent = content
}
if (!replyContent) {
replyContent = await openEditor()
}
if (!replyContent || replyContent.trim() === '') {
console.error('No content provided.')
process.exit(1)
}
const notifyValue = options.notify ?? 'EVERYONE_IN_THREAD'
let recipients: string | number[]
if (notifyValue === 'EVERYONE' || notifyValue === 'EVERYONE_IN_THREAD') {
recipients = notifyValue
} else {
recipients = notifyValue.split(',').map((userRef) => {
const trimmed = userRef.trim()
if (!trimmed) {
console.error('Invalid user reference list: found empty value')
process.exit(1)
return 0
}
try {
return extractId(trimmed)
} catch {
console.error(`Invalid user reference: ${trimmed}. Use 123 or id:123`)
process.exit(1)
return 0
}
})
}
if (options.dryRun) {
console.log('Dry run: would post comment to thread', threadId)
console.log(`Notify: ${Array.isArray(recipients) ? recipients.join(', ') : recipients}`)
console.log('')
console.log(replyContent)
return
}
const client = await getTwistClient()
const thread = await client.threads.getThread(threadId)
await assertChannelIsPublic(thread.channelId, thread.workspaceId)
const comment = await client.comments.createComment({
threadId,
content: replyContent,
recipients,
} as Parameters<typeof client.comments.createComment>[0])
if (options.json) {
console.log(formatJson(comment, 'comment', options.full))
return
}
console.log(`Comment posted: ${comment.url}`)
}