Files
wehub-resource-sync 9b395f5cc3
Build Chrome Extension / build (push) Waiting to run
Trigger Website Rebuild (Docs Updated) / dispatch (push) Waiting to run
E2E Headed Chrome / e2e-headed (macos-15) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (ubuntu-latest) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (windows-latest) (push) Has been cancelled
CI / build (macos-latest) (push) Has been cancelled
CI / build (ubuntu-latest) (push) Has been cancelled
CI / build (windows-latest) (push) Has been cancelled
CI / unit-test (push) Has been cancelled
CI / bun-test (push) Has been cancelled
CI / adapter-test (push) Has been cancelled
CI / smoke-test (macos-latest) (push) Has been cancelled
CI / smoke-test (ubuntu-latest) (push) Has been cancelled
Security Audit / audit (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:39:48 +08:00

72 lines
2.6 KiB
JavaScript

// channel-mark.js
import { cli, Strategy } from '@jackwener/opencli/registry';
import { ArgumentError } from '@jackwener/opencli/errors';
import { buildChannelScopedSnippet } from './in-page.js';
import { dispatchEvaluateResult } from './errors.js';
import { SLOCK_SITE, SLOCK_DOMAIN, SLOCK_HOME_URL } from './shared.js';
// Three read-state operations on one channel, selected by flags:
// (default) POST /channels/:id/read-all — catch up fully
// --seq N POST /channels/:id/read {seq} — mark read up to a seq
// --unread POST /channels/:id/unread — flag as unread
cli({
site: SLOCK_SITE,
name: 'channel-mark',
access: 'write',
description: 'Mark a channel read (default), read up to --seq, or --unread.',
domain: SLOCK_DOMAIN,
strategy: Strategy.COOKIE,
browser: true,
siteSession: 'persistent',
args: [
{ name: 'channel', positional: true, required: true, help: 'channelId UUID or #name' },
{ name: 'seq', type: 'int', help: 'Mark read up to this seq (omit for read-all)' },
{ name: 'unread', type: 'bool', default: false, help: 'Mark the channel unread instead of read' },
{ name: 'server', help: 'Override active server' },
],
columns: ['channel', 'action', 'result'],
func: async (page, kwargs) => {
const channel = String(kwargs.channel ?? '').trim();
if (!channel) throw new ArgumentError('channel required');
const hasSeq = kwargs.seq !== undefined && kwargs.seq !== null && kwargs.seq !== '';
if (kwargs.unread && hasSeq) {
throw new ArgumentError('--unread and --seq are mutually exclusive');
}
let pathSuffix;
let body;
let action;
if (kwargs.unread) {
pathSuffix = '/unread';
action = 'unread';
} else if (hasSeq) {
const seq = Number(kwargs.seq);
if (!Number.isInteger(seq) || seq <= 0) throw new ArgumentError(`--seq must be a positive integer (got "${kwargs.seq}")`);
pathSuffix = '/read';
body = { seq };
action = `read-to-${seq}`;
} else {
pathSuffix = '/read-all';
action = 'read-all';
}
await page.goto(SLOCK_HOME_URL);
const snippet = buildChannelScopedSnippet({
channelInput: channel,
method: 'POST',
pathSuffix,
body,
serverIdOverride: kwargs.server,
});
const result = await page.evaluate(`(async () => { ${snippet} })()`);
const data = dispatchEvaluateResult(result);
return [{
channel,
action,
result: data?.seq !== undefined ? `seq=${data.seq}`
: data?.unreadCount !== undefined ? `unreadCount=${data.unreadCount}`
: 'ok',
}];
},
});