-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Add thread mark-read command #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| import { readFile } from 'node:fs/promises' | ||
| import type { CommsApi, Thread } from '@doist/comms-sdk' | ||
| import { getCommsClient } from '../../lib/api.js' | ||
| import { CliError } from '../../lib/errors.js' | ||
| import type { MutationOptions } from '../../lib/options.js' | ||
| import { formatJson, pluralize } from '../../lib/output.js' | ||
| import { assertChannelIsPublic } from '../../lib/public-channels.js' | ||
| import { resolveThreadId } from '../../lib/refs.js' | ||
|
|
||
| export type MarkThreadReadOptions = MutationOptions & { | ||
| fromFile?: string | ||
| } | ||
|
|
||
| type LoadedThread = { | ||
| thread: Thread | ||
| isUnread: boolean | ||
| } | ||
|
|
||
| type MarkReadStatus = { | ||
| id: string | ||
| isRead: true | ||
| } | ||
|
|
||
| type TextStatus = 'changed' | 'preview' | 'unchanged' | ||
|
|
||
| export async function markThreadRead( | ||
| refs: string[], | ||
| options: MarkThreadReadOptions, | ||
| ): Promise<void> { | ||
| const rawRefs = await collectThreadRefs(refs, options.fromFile) | ||
| if (rawRefs.length === 0) { | ||
| throw new CliError( | ||
| 'INVALID_REF', | ||
| 'No thread references provided. Pass refs as arguments or via --from-file.', | ||
| ) | ||
| } | ||
|
|
||
| const needsConfirmation = rawRefs.length > 1 && !options.yes && !options.dryRun | ||
| if (options.json && needsConfirmation) { | ||
| throw new CliError( | ||
| 'MISSING_YES_FLAG', | ||
| '--yes is required to execute bulk mark-read in --json mode.', | ||
| ) | ||
| } | ||
|
|
||
| const client = await getCommsClient() | ||
| const unreadCache = new Map<number, Set<string>>() | ||
| const jsonStatuses: MarkReadStatus[] = [] | ||
| const textStatuses: TextStatus[] = [] | ||
|
|
||
| for (const rawRef of rawRefs) { | ||
| const threadId = resolveThreadId(rawRef) | ||
| const loaded = await loadThread(client, unreadCache, threadId) | ||
|
|
||
| if (!loaded.isUnread) { | ||
| jsonStatuses.push({ id: threadId, isRead: true }) | ||
| textStatuses.push('unchanged') | ||
| if (!options.json) { | ||
| console.log(`Thread ${threadLabel(loaded.thread)} is already read.`) | ||
| } | ||
| continue | ||
| } | ||
|
|
||
| if (needsConfirmation || options.dryRun) { | ||
| jsonStatuses.push({ id: threadId, isRead: true }) | ||
| textStatuses.push('preview') | ||
| if (!options.json) { | ||
| const prefix = options.dryRun ? 'Dry run: would' : 'Would' | ||
| console.log(`${prefix} mark read thread ${threadLabel(loaded.thread)}.`) | ||
| } | ||
| continue | ||
| } | ||
|
|
||
| await client.threads.markRead({ | ||
| id: threadId, | ||
| objIndex: getLatestObjIndex(loaded.thread), | ||
| }) | ||
| unreadCache.get(loaded.thread.workspaceId)?.delete(threadId) | ||
|
|
||
| jsonStatuses.push({ id: threadId, isRead: true }) | ||
| textStatuses.push('changed') | ||
| if (!options.json) { | ||
| console.log(`Thread ${threadLabel(loaded.thread)} marked read.`) | ||
| } | ||
| } | ||
|
|
||
| if (options.json && !options.dryRun) { | ||
| console.log(formatJson(jsonStatuses)) | ||
| return | ||
| } | ||
|
|
||
| if (!options.json && rawRefs.length > 1) { | ||
| printSummary(textStatuses) | ||
| } | ||
|
|
||
| if (!options.json && needsConfirmation) { | ||
| console.log('Use --yes to confirm.') | ||
| } | ||
| } | ||
|
|
||
| async function collectThreadRefs(refs: string[], fromFile: string | undefined): Promise<string[]> { | ||
| const inlineRefs = refs.map((ref) => ref.trim()).filter(Boolean) | ||
| if (!fromFile) { | ||
| return inlineRefs | ||
| } | ||
|
|
||
| let content: string | ||
| try { | ||
| content = await readFile(fromFile, 'utf8') | ||
| } catch (error) { | ||
| const message = error instanceof Error ? error.message : String(error) | ||
| throw new CliError('FILE_READ_ERROR', `Could not read refs file: ${fromFile}`, [message]) | ||
| } | ||
|
|
||
| const fileRefs = content | ||
| .split(/\r?\n/) | ||
| .map((line) => line.trim()) | ||
| .filter((line) => line !== '' && !line.startsWith('#')) | ||
|
|
||
| return [...inlineRefs, ...fileRefs] | ||
| } | ||
|
|
||
| async function loadThread( | ||
| client: CommsApi, | ||
| unreadCache: Map<number, Set<string>>, | ||
| threadId: string, | ||
| ): Promise<LoadedThread> { | ||
| const thread = await client.threads.getThread(threadId) | ||
| await assertChannelIsPublic(thread.channelId, thread.workspaceId) | ||
|
|
||
| let unreadIds = unreadCache.get(thread.workspaceId) | ||
| if (!unreadIds) { | ||
| const unread = await client.threads.getUnread(thread.workspaceId) | ||
| unreadIds = new Set(unread.data.map((unreadThread) => unreadThread.threadId)) | ||
| unreadCache.set(thread.workspaceId, unreadIds) | ||
| } | ||
|
|
||
| return { thread, isUnread: unreadIds.has(thread.id) } | ||
| } | ||
|
|
||
| function getLatestObjIndex(thread: Thread): number { | ||
| return Math.max( | ||
| ...[thread.lastComment?.objIndex, thread.lastObjIndex, thread.commentCount, 0] | ||
| .filter((value): value is number => typeof value === 'number') | ||
| .map((value) => Math.max(value, 0)), | ||
| ) | ||
| } | ||
|
|
||
| function threadLabel(thread: Thread): string { | ||
| return `${thread.title} (${thread.id})` | ||
| } | ||
|
|
||
| function printSummary(statuses: TextStatus[]): void { | ||
| const summary = [ | ||
| summarizeStatus(statuses, 'changed'), | ||
| summarizeStatus(statuses, 'unchanged'), | ||
| summarizeStatus(statuses, 'preview'), | ||
| ].filter(Boolean) | ||
|
|
||
| console.log('') | ||
| console.log(`Summary: ${summary.join(', ')}`) | ||
| } | ||
|
|
||
| function summarizeStatus(statuses: TextStatus[], status: TextStatus): string | null { | ||
| const count = statuses.filter((value) => value === status).length | ||
| if (count === 0) { | ||
| return null | ||
| } | ||
|
|
||
| const noun = status === 'preview' ? pluralize(count, 'preview') : pluralize(count, 'thread') | ||
| return status === 'preview' ? `${count} ${noun}` : `${count} ${status} ${noun}` | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.