consent-recorder: let each consenter pick which servers to record in

The site lists only servers the bot and the user share (single-member REST
fetch, no privileged intent). Consent requires at least one server; the
choice can be changed later and removed servers stop recording at once.
Consent text mentions the server choice, so CONSENT_VERSION is bumped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
EJClaw
2026-11-11 20:53:14 +09:00
parent 3a40645b12
commit f45d349f96
8 changed files with 152 additions and 21 deletions

View File

@@ -20,6 +20,8 @@ test('봇이 들어가 정원이 찬 지금 채널의 동의자는 그대로 따
const store = new Store(':memory:');
store.consent({ id: 'u1', username: 'a' });
store.consent({ id: 'u2', username: 'b' });
store.setConsentGuilds('u1', ['g']);
store.setConsentGuilds('u2', ['g']);
const cfg = loadConfig({ PUBLIC_URL: 'http://localhost', DISCORD_CLIENT_ID: 'c', DISCORD_CLIENT_SECRET: 's' }, false);
const bot = new FollowBot(cfg, store);
const guild = fakeGuild();
@@ -30,3 +32,26 @@ test('봇이 들어가 정원이 찬 지금 채널의 동의자는 그대로 따
await bot.client.destroy();
store.close();
});
test('녹음할 서버로 고르지 않은 서버에서는 동의자라도 따라가지 않는다', async () => {
const store = new Store(':memory:');
store.consent({ id: 'u1', username: 'a' });
store.consent({ id: 'u2', username: 'b' });
store.setConsentGuilds('u1', ['g']);
store.setConsentGuilds('u2', ['other']);
const cfg = loadConfig({ PUBLIC_URL: 'http://localhost', DISCORD_CLIENT_ID: 'c', DISCORD_CLIENT_SECRET: 's' }, false);
const bot = new FollowBot(cfg, store);
const guild = fakeGuild();
(bot as any).recorders.set('g', { channelId: 'A' });
assert.deepEqual(bot.consentedPresence(guild), [{ userId: 'u1', channelId: 'A' }]);
// 서버를 빼면 진행 중인 녹음도 멈춘다
const stopped: string[] = [];
(bot as any).recorders.set('g', { channelId: 'A', stopUser: (id: string) => stopped.push(id) });
store.setConsentGuilds('u1', []);
bot.consentChanged('u1');
assert.deepEqual(stopped, ['u1']);
assert.deepEqual(bot.consentedPresence(guild), []);
(bot as any).recorders.clear();
await bot.client.destroy();
store.close();
});

View File

@@ -53,6 +53,10 @@ before(async () => {
consentChanged: (id) => changed.push(id),
discardActive: (id) => events.push(`discard:${id}:${store.recordingStats(id).count}`),
recordingNow: () => null,
userGuilds: async () => [
{ id: 'g1', name: '첫째 서버' },
{ id: 'g2', name: '둘째 서버' },
],
});
await new Promise<void>((r) => site.listen(port, '127.0.0.1', r));
});
@@ -65,7 +69,7 @@ after(() => {
});
const get = (p: string, cookie = '') => fetch(base + p, { redirect: 'manual', headers: cookie ? { cookie } : {} });
const post = (p: string, cookie: string, form: Record<string, string>) =>
const post = (p: string, cookie: string, form: Record<string, string> | string[][]) =>
fetch(base + p, { method: 'POST', redirect: 'manual', headers: { cookie, 'Content-Type': 'application/x-www-form-urlencoded' }, body: new URLSearchParams(form) });
async function login(): Promise<{ cookie: string; csrf: string }> {
@@ -116,12 +120,31 @@ test('디스코드 로그인 → 모든 항목 체크해야 동의 → 철회하
assert.equal((await post('/consent', cookie, { csrf, self: '1', model: '1', age: '1' })).status, 409);
assert.equal((await post('/consent', cookie, { csrf, version: 'old', self: '1', model: '1', age: '1' })).status, 409);
assert.equal(store.isConsented(USER.id), false);
const version = /name="version" value="([^"]+)"/.exec(await (await get('/', cookie)).text())![1];
const page0 = await (await get('/', cookie)).text();
const version = /name="version" value="([^"]+)"/.exec(page0)![1];
assert.equal(version, CONSENT_VERSION);
assert.equal((await post('/consent', cookie, { csrf, version, self: '1', model: '1', age: '1' })).status, 303);
// 봇과 함께 있는 서버만 고를 수 있고, 하나 이상 골라야 동의된다
assert.match(page0, /name="guild" value="g1"[^>]*><span>첫째 서버/);
const ok = { csrf, version, self: '1', model: '1', age: '1' };
assert.equal((await post('/consent', cookie, ok)).status, 400);
assert.equal((await post('/consent', cookie, { ...ok, guild: 'other' })).status, 400);
assert.equal(store.isConsented(USER.id), false);
assert.equal((await post('/consent', cookie, [...Object.entries(ok), ['guild', 'g1'], ['guild', 'g2']])).status, 303);
assert.equal(store.isConsented(USER.id), true);
assert.deepEqual(store.consentGuilds(USER.id), ['g1', 'g2']);
assert.deepEqual(changed, [USER.id]);
assert.match(await (await get('/', cookie)).text(), /녹음 동의함/);
assert.match(await (await get('/', cookie)).text(), /녹음 동의함[\s\S]*녹음할 서버: <b>첫째 서버, 둘째 서버/);
// 녹음할 서버 바꾸기: 뺀 서버는 녹음하지 않는다. 다 빼면 어디서도 녹음하지 않음
assert.equal((await post('/guilds', cookie, { csrf, guild: 'other' })).status, 400);
assert.equal((await post('/guilds', cookie, { csrf, guild: 'g2' })).status, 303);
assert.deepEqual(changed, [USER.id, USER.id]);
assert.equal(store.isConsentedIn(USER.id, 'g1'), false);
assert.equal(store.isConsentedIn(USER.id, 'g2'), true);
assert.equal((await post('/guilds', cookie, { csrf })).status, 303);
assert.equal(store.isConsentedIn(USER.id, 'g2'), false);
assert.match(await (await get('/', cookie)).text(), /녹음할 서버: <b>없음/);
assert.equal((await post('/guilds', cookie, { csrf, guild: 'g1' })).status, 303);
// 녹음 파일 하나 있다고 치고 철회 + 삭제
const f = path.join(tmp, 'recordings', USER.id, 'a.ogg');
@@ -139,7 +162,9 @@ test('디스코드 로그인 → 모든 항목 체크해야 동의 → 철회하
assert.equal(store.isConsented(USER.id), false);
assert.equal(fs.existsSync(f), false);
assert.equal(store.recordingStats(USER.id).count, 0);
assert.deepEqual(store.consentLog(USER.id).map((l) => l.action), ['consent', 'delete_recordings', 'revoke', 'delete_recordings']);
assert.deepEqual(store.consentLog(USER.id).map((l) => l.action), ['guilds', 'consent', 'guilds', 'guilds', 'guilds', 'delete_recordings', 'revoke', 'delete_recordings']);
// 철회한 뒤에는 서버를 바꿀 수 없다
assert.equal((await post('/guilds', cookie, { csrf, guild: 'g1' })).status, 400);
// 로그아웃하면 세션 끝
assert.equal((await post('/logout', cookie, { csrf })).status, 303);