Files
jackwener__opencli/clis/reddit/subscribe.ts
jackwener 314bc6f231 refactor: simplify core modules — remove root shims, consolidate error classification, streamline cascade/interceptor, clean up synthesize
1. Remove root-level shim files (errors.ts, logger.ts, registry.ts, types.ts, utils.ts, launcher.ts) — update all ~840 adapter imports to reference src/ directly
2. Consolidate interceptor: reuse shared DISGUISE_FN in tap interceptor instead of reimplementing
3. Unify error classification: single ClassifiedError type with icon/exitCode/hint lookup table, eliminating duplicated pattern matching between resolveExitCode and renderError
4. Simplify cascade probe: replace repetitive switch cases with PROBE_OPTIONS lookup map
12. Clean up synthesize.ts: remove deprecated snake_case field aliases (recommended_args, recommended_columns, recommendedColumnsLegacy) and unnecessary constant aliases

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-05 03:06:47 +08:00

54 lines
1.9 KiB
TypeScript

import { CommandExecutionError } from '../../src/errors.js';
import { cli, Strategy } from '../../src/registry.js';
cli({
site: 'reddit',
name: 'subscribe',
description: 'Subscribe or unsubscribe to a subreddit',
domain: 'reddit.com',
strategy: Strategy.COOKIE,
browser: true,
args: [
{ name: 'subreddit', type: 'string', required: true, positional: true, help: 'Subreddit name (e.g. python)' },
{ name: 'undo', type: 'boolean', default: false, help: 'Unsubscribe instead of subscribe' },
],
columns: ['status', 'message'],
func: async (page, kwargs) => {
if (!page) throw new CommandExecutionError('Browser session required');
await page.goto('https://www.reddit.com');
const result = await page.evaluate(`(async () => {
try {
let sub = ${JSON.stringify(kwargs.subreddit)};
if (sub.startsWith('r/')) sub = sub.slice(2);
const undo = ${kwargs.undo ? 'true' : 'false'};
const action = undo ? 'unsub' : 'sub';
// Get modhash
const meRes = await fetch('/api/me.json', { credentials: 'include' });
const me = await meRes.json();
const modhash = me?.data?.modhash || '';
const res = await fetch('/api/subscribe', {
method: 'POST',
credentials: 'include',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: 'sr_name=' + encodeURIComponent(sub)
+ '&action=' + action
+ (modhash ? '&uh=' + encodeURIComponent(modhash) : ''),
});
if (!res.ok) return { ok: false, message: 'HTTP ' + res.status };
const label = undo ? 'Unsubscribed from' : 'Subscribed to';
return { ok: true, message: label + ' r/' + sub };
} catch (e) {
return { ok: false, message: e.toString() };
}
})()`);
return [{ status: result.ok ? 'success' : 'failed', message: result.message }];
}
});