|
| 1 | +import type { SetStatusArgs } from '@doist/comms-sdk' |
| 2 | +import { getCommsClient } from '../../lib/api.js' |
| 3 | +import { formatDateTime, parseLocalDate } from '../../lib/dates.js' |
| 4 | +import { parseDuration } from '../../lib/duration.js' |
| 5 | +import { CliError } from '../../lib/errors.js' |
| 6 | +import type { MutationOptions } from '../../lib/options.js' |
| 7 | +import { printDryRun } from '../../lib/output.js' |
| 8 | +import { formatStatus } from '../../lib/status.js' |
| 9 | +import { formatStatusJson } from './helpers.js' |
| 10 | + |
| 11 | +const MAX_MESSAGE_LENGTH = 80 |
| 12 | + |
| 13 | +type SetStatusOptions = MutationOptions & { |
| 14 | + emoji?: string |
| 15 | + until?: string |
| 16 | + for?: string |
| 17 | + pauseNotifications?: boolean |
| 18 | +} |
| 19 | + |
| 20 | +function resolveMessage(message: string): string { |
| 21 | + const trimmed = message.trim() |
| 22 | + const length = [...trimmed].length |
| 23 | + if (length < 1 || length > MAX_MESSAGE_LENGTH) { |
| 24 | + throw new CliError( |
| 25 | + 'INVALID_VALUE', |
| 26 | + `The message must be 1 to ${MAX_MESSAGE_LENGTH} characters`, |
| 27 | + ) |
| 28 | + } |
| 29 | + return trimmed |
| 30 | +} |
| 31 | + |
| 32 | +function resolveEmoji(emoji: string | undefined): string | undefined { |
| 33 | + if (emoji === undefined) { |
| 34 | + return undefined |
| 35 | + } |
| 36 | + const trimmed = emoji.trim() |
| 37 | + if (trimmed === '') { |
| 38 | + throw new CliError('INVALID_VALUE', '--emoji cannot be empty') |
| 39 | + } |
| 40 | + if (trimmed.startsWith('-')) { |
| 41 | + throw new CliError('INVALID_VALUE', `--emoji needs an emoji, got ${trimmed}`, [ |
| 42 | + 'A flag after --emoji was read as its value; put the emoji right after --emoji', |
| 43 | + ]) |
| 44 | + } |
| 45 | + return trimmed |
| 46 | +} |
| 47 | + |
| 48 | +function resolveExpiry(options: SetStatusOptions): Date | undefined { |
| 49 | + if (options.until !== undefined && options.for !== undefined) { |
| 50 | + throw new CliError('CONFLICTING_OPTIONS', 'Cannot use --until and --for together') |
| 51 | + } |
| 52 | + |
| 53 | + let expiry: Date | undefined |
| 54 | + if (options.until !== undefined) { |
| 55 | + expiry = parseLocalDate(options.until) ?? undefined |
| 56 | + if (!expiry) { |
| 57 | + throw new CliError('INVALID_DATE', `Invalid --until date: ${options.until}`, [ |
| 58 | + 'Use a real date as YYYY-MM-DD or YYYY-MM-DDTHH:MM (local time), or add Z or an offset such as +02:00', |
| 59 | + ]) |
| 60 | + } |
| 61 | + } else if (options.for !== undefined) { |
| 62 | + const duration = parseDuration(options.for) |
| 63 | + expiry = duration === null ? undefined : new Date(Date.now() + duration) |
| 64 | + if (!expiry || Number.isNaN(expiry.getTime())) { |
| 65 | + throw new CliError('INVALID_VALUE', `Invalid --for duration: ${options.for}`, [ |
| 66 | + 'Use minutes, hours or days, e.g. 30m, 2h, 1d or 1h30m', |
| 67 | + ]) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + if (expiry && expiry.getTime() <= Date.now()) { |
| 72 | + const dateOnly = |
| 73 | + options.until !== undefined && /^\d{4}-\d{2}-\d{2}$/.test(options.until.trim()) |
| 74 | + throw new CliError('INVALID_DATE', `The status must end in the future: ${options.until}`, [ |
| 75 | + dateOnly |
| 76 | + ? 'A date with no time means midnight at its start, so add a time or use a later date' |
| 77 | + : 'Use a date or time in the future', |
| 78 | + ]) |
| 79 | + } |
| 80 | + return expiry |
| 81 | +} |
| 82 | + |
| 83 | +export async function setStatus(message: string, options: SetStatusOptions): Promise<void> { |
| 84 | + const text = resolveMessage(message) |
| 85 | + const emoji = resolveEmoji(options.emoji) |
| 86 | + const activeUntil = resolveExpiry(options) |
| 87 | + |
| 88 | + const args: SetStatusArgs = { |
| 89 | + message: text, |
| 90 | + ...(emoji ? { emoji } : {}), |
| 91 | + ...(activeUntil ? { activeUntil } : {}), |
| 92 | + ...(options.pauseNotifications ? { effects: { pauseNotifications: {} } } : {}), |
| 93 | + } |
| 94 | + |
| 95 | + if (options.dryRun) { |
| 96 | + printDryRun('set status', { |
| 97 | + Message: text, |
| 98 | + Emoji: emoji, |
| 99 | + Until: activeUntil ? formatDateTime(activeUntil) : undefined, |
| 100 | + 'Pause notifications': options.pauseNotifications ? 'yes' : undefined, |
| 101 | + }) |
| 102 | + return |
| 103 | + } |
| 104 | + |
| 105 | + const client = await getCommsClient() |
| 106 | + const user = await client.users.setStatus(args) |
| 107 | + |
| 108 | + // The server accepted the call but reports nothing active, e.g. an expiry that has already passed. |
| 109 | + if (!user.status) { |
| 110 | + throw new CliError( |
| 111 | + 'INVALID_STATE', |
| 112 | + 'Nothing changed: Comms accepted the request but there is no active status, usually because the expiry has already passed', |
| 113 | + [ |
| 114 | + 'Run `tdc status` to see the current status', |
| 115 | + 'To keep a status active, set a later --until or --for', |
| 116 | + ], |
| 117 | + ) |
| 118 | + } |
| 119 | + |
| 120 | + console.log( |
| 121 | + options.json |
| 122 | + ? formatStatusJson(user, options.full) |
| 123 | + : `Status set: ${formatStatus(user.status)}`, |
| 124 | + ) |
| 125 | +} |
0 commit comments