사이트: 모르는 유저 다시 받기가 한 번 실패하면 영영 안 받던 문제

받는 중 중복만 막고, 실패하면 다음 이벤트에서 다시 받는다 (성공한 아이디만 기억).
lib/unknown-users.ts 로 빼고 테스트 추가, 목 서버에 /mock/users-fail · latejoin?again=1

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
EJClaw
2026-10-16 22:56:45 +09:00
parent 0b8789c895
commit a4511a8eaf
5 changed files with 98 additions and 17 deletions

View File

@@ -6,7 +6,8 @@
// GET /mock/device?registered=0|1 등록 안 된 상태 흉내
// GET /mock/live?on=0|1 주기적 가짜 실시간 발화 켜기/끄기
// GET /mock/emit 가짜 실시간 발화 하나 즉시 만들기
// GET /mock/latejoin 녹음 중에 새 사람이 들어와 말함 (v1.4.1 이하 앱처럼 users.updated 없이 발화만 보냄)
// GET /mock/latejoin 녹음 중에 새 사람이 들어와 말함 (v1.4.1 이하 앱처럼 users.updated 없이 발화만 보냄). ?again=1 이면 마지막에 들어온 사람이 또 말함
// GET /mock/users-fail?n=1 다음 n 번 /api/users 를 500 으로 실패
// GET /auth/discord?as=notmember 길드 멤버가 아닌 계정으로 로그인 흉내 (403 not_member)
import http from 'node:http';
import fs from 'node:fs';
@@ -167,10 +168,14 @@ async function handle(req, res) {
return json(res, 200, { live: state.live });
}
if (p === '/mock/emit') return json(res, 200, emitLive() ?? { error: 'device_offline' });
if (p === '/mock/users-fail') {
state.usersFail = Number(q.get('n') ?? 1);
return json(res, 200, { usersFail: state.usersFail });
}
if (p === '/mock/latejoin') {
const n = db.users.length;
const u = { id: `41000000000000${String(n).padStart(4, '0')}`, name: `늦게온사람${n}`, color: '#d4537e', avatar: null, excluded: false, riot: [] };
db.users.push(u);
const u = q.get('again') === '1' ? db.users[n - 1] : { id: `41000000000000${String(n).padStart(4, '0')}`, name: `늦게온사람${n}`, color: '#d4537e', avatar: null, excluded: false, riot: [] };
if (q.get('again') !== '1') db.users.push(u);
const it = db.addLiveUtterance(u, '나 지금 들어왔어');
broadcast({ type: 'utterance.created', data: it });
return json(res, 200, { user: u, utterance: it.id });
@@ -243,7 +248,13 @@ async function handle(req, res) {
version: '1.0.0',
});
}
if (p === '/api/users') return json(res, 200, db.users);
if (p === '/api/users') {
if (state.usersFail > 0) {
state.usersFail--;
return json(res, 500, { error: 'mock_fail' });
}
return json(res, 200, db.users);
}
let m;
if ((m = p.match(/^\/api\/users\/([^/]+)$/))) {
const prof = userProfile(decodeURIComponent(m[1]));

View File

@@ -0,0 +1,24 @@
/**
* 처음 보는 유저 아이디가 오면 유저 목록을 다시 받아 온다 (v1.4.1 이하 앱은 녹음 중에 들어온 사람을 알려 주지 않아 "알 수 없음"으로 뜸).
* - 받는 중에는 또 부르지 않는다
* - 받기에 성공한 아이디는 다시 묻지 않는다 (목록에 없어도 매 발화마다 부르지 않게)
* - 실패하면 기억하지 않아 다음 이벤트에서 다시 받는다
*/
export function makeUnknownUserCheck(load: () => Promise<unknown>) {
const done = new Set<string>();
let busy = false;
return (ids: string[], known: (id: string) => boolean): void => {
if (busy) return;
const fresh = ids.filter((id) => id && !known(id) && !done.has(id));
if (!fresh.length) return;
busy = true;
load()
.then(
() => fresh.forEach((id) => done.add(id)),
() => {},
)
.finally(() => {
busy = false;
});
};
}

View File

@@ -2,6 +2,7 @@ import { createContext, useCallback, useContext, useEffect, useMemo, useRef, use
import type { DeviceStatus, LiveEvent, Overview, User } from '@gvr/shared';
import { api, onApiSignal, type Me } from '../lib/api';
import { live, type LiveConn } from '../lib/live';
import { makeUnknownUserCheck } from '../lib/unknown-users';
// ── 로그인 ───────────────────────────────────────────
type AuthState = { status: 'loading' } | { status: 'anon' } | { status: 'not_member' } | { status: 'ok'; me: Me };
@@ -139,22 +140,14 @@ export function DataProvider({ children }: { children: ReactNode }) {
alive = false;
};
}, [epoch, locked]);
const reloadUsers = useCallback(() => {
api
.users()
.then((users) => setOverview((o) => (o ? { ...o, users } : o)))
.catch(() => {});
}, []);
const fetchUsers = useCallback(() => api.users().then((users) => setOverview((o) => (o ? { ...o, users } : o))), []);
const reloadUsers = useCallback(() => void fetchUsers().catch(() => {}), [fetchUsers]);
const users = overview?.users ?? [];
const map = useMemo(() => new Map(users.map((u) => [u.id, u])), [users]);
// 처음 보는 사람 아이디는 한 번만 유저 목록을 다시 받아 온다 (v1.4.1 이하 앱은 녹음 중에 들어온 사람을 알려 주지 않아 "알 수 없음"으로 뜸)
const asked = useRef(new Set<string>());
// 처음 보는 사람 아이디가 오면 유저 목록을 다시 받아 온다 (실패하면 다음 이벤트에서 다시)
const unknownCheck = useRef(makeUnknownUserCheck(fetchUsers));
const checkUnknown = (ids: string[]) => {
if (!overview) return;
const fresh = ids.filter((id) => id && !map.has(id) && !asked.current.has(id));
if (!fresh.length) return;
fresh.forEach((id) => asked.current.add(id));
reloadUsers();
if (overview) unknownCheck.current(ids, (id) => map.has(id));
};
useLive((e) => {
if (e.type === 'users.updated') {