이지선다 조회수 자동: 빈 양식에서 조회수 열을 빼고, 올린 뒤 서버가 곡마다 토픽 음원 + MV 조회수를 불러와 더함. 짝은 유튜브 뮤직 노래↔동영상 전환(watch 응답의 counterpart, 쿠키 필요), 조회수는 로그인 없이 player videoDetails.viewCount(안 되면 watch 화면 숫자). quizzes/duel-views.ts가 pending 곡을 한 곡씩 처리(재시작해도 이어서), 쿠키 만료면 남은 곡 모두 이유와 함께 failed. 짝이 없는 곡은 있는 영상 하나만 쓰고 비고 표시. 파일·편집기에 적은 숫자는 그대로 쓰고 다시 불러오기에서 빠짐, 칸을 비우고 저장하면 자동. 편집기: 조회수(토픽·MV)·비고 칸, 불러오는 중 표시와 2초 폴링(저장 안 됨으로 안 잡힘), 조회수 다시 불러오기(전체/곡), 저장 때 편집기의 옛 사본이 서버가 불러온 값을 덮지 않음. 내보내기는 직접 넣은 숫자만 조회수 열에. 파일로 만들기는 조회수 열이 없으면 고른 종류 유지. 014 마이그레이션, 단위·API 테스트, e2e duel 단계, docs 04·10·12
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -15,6 +15,7 @@ import { audioFile, type AudioLinks } from '../songs/audio';
|
||||
import { getImportJob, startImport, type Lavalink } from '../quizzes/playlist';
|
||||
import { applyReview, listReviews, pendingCount, rejectReview, syncReviews } from '../quizzes/review';
|
||||
import { checkEmbed, type EmbedState } from '../collector/oembed';
|
||||
import { DuelViewsJob, type ViewsSource } from '../quizzes/duel-views';
|
||||
|
||||
type Env = { Variables: { userId: string | null; role: Role } };
|
||||
|
||||
@@ -33,6 +34,9 @@ export interface QuizAdminDeps {
|
||||
importDelayMs?: number;
|
||||
videoOf: (url: string) => Promise<{ id: string; author: string; title: string } | string>;
|
||||
onChange?: () => void;
|
||||
/** 이지선다 조회수 자동: where pairs and view counts come from (tests inject a fake) and the pause between songs. */
|
||||
viewsSource?: () => Promise<ViewsSource>;
|
||||
viewsDelayMs?: number;
|
||||
}
|
||||
|
||||
let searcher: Promise<(q: string) => Promise<YtmSong[]>> | null = null;
|
||||
@@ -46,6 +50,9 @@ function defaultSearch(q: string): Promise<YtmSong[]> {
|
||||
}
|
||||
|
||||
export function mountQuizAdmin(app: Hono<Env>, d: QuizAdminDeps, h: { actor: (c: Context<Env>) => string; body: (c: Context<Env>) => Promise<unknown> }) {
|
||||
// 이지선다 조회수 자동: songs left waiting by a restart go on right away
|
||||
const views = new DuelViewsJob(d.db, d.viewsSource, { delayMs: d.viewsDelayMs, retryMs: d.viewsDelayMs, onChange: d.onChange });
|
||||
views.kick();
|
||||
const mine = (c: Context<Env>) => {
|
||||
const q = d.quizzes.row(Number(c.req.param('id')));
|
||||
if (!q) return null;
|
||||
@@ -71,18 +78,19 @@ export function mountQuizAdmin(app: Hono<Env>, d: QuizAdminDeps, h: { actor: (c:
|
||||
if (!p.success) return c.json({ error: `만들 내용이 올바르지 않아요: ${p.error.issues.slice(0, 3).map((i) => i.path.join('.') || i.message).join(', ')}` }, 400);
|
||||
const duel = p.data.kind === 'duel';
|
||||
if (duel && p.data.items.length < 2) return c.json({ error: '이지선다 퀴즈에는 곡이 2곡 이상 있어야 해요.' }, 400);
|
||||
if (duel && p.data.items.some((it) => it.views === null)) return c.json({ error: '이지선다 퀴즈의 곡에는 모두 조회수가 있어야 해요.' }, 400);
|
||||
const limits = d.limits();
|
||||
if (limits.maxSongs && p.data.items.length > limits.maxSongs) return c.json({ error: `퀴즈 하나에는 곡을 ${limits.maxSongs}곡까지 넣을 수 있어요.` }, 400);
|
||||
const id = d.quizzes.create(h.actor(c), p.data.name, limits, Date.now(), p.data.kind);
|
||||
if (typeof id === 'string') return c.json({ error: id }, 400);
|
||||
const items = p.data.items.map((it) => ({ ...it, id: undefined, songId: null, fromFile: true, ...(duel ? {} : { views: null }) }));
|
||||
// 이지선다: a 조회수 written in the file is used as is, the others are fetched (토픽 + MV) after saving
|
||||
const items = p.data.items.map((it) => ({ ...it, id: undefined, songId: null, fromFile: true, viewsAuto: null, ...(duel ? {} : { views: null }) }));
|
||||
// songs with their own hints: hints come by vote unless the maker changes it in 설정
|
||||
const settings = { ...DEFAULT_QUIZ_SETTINGS, ...(items.some((it) => it.hints.length) ? { hint: 'manual' as const } : {}) };
|
||||
const saved = d.quizzes.save(id, { name: p.data.name, description: '', visibility: 'private', settings, items }, limits);
|
||||
if (typeof saved === 'string') return c.json({ error: saved }, 400);
|
||||
d.audit.log(h.actor(c), 'quiz.create', String(id), null, { name: p.data.name, songs: items.length, from: 'file', kind: p.data.kind });
|
||||
d.onChange?.();
|
||||
if (duel) views.kick();
|
||||
return c.json({ id });
|
||||
});
|
||||
|
||||
@@ -255,9 +263,30 @@ export function mountQuizAdmin(app: Hono<Env>, d: QuizAdminDeps, h: { actor: (c:
|
||||
const reviews = syncReviews(d.db, q.id);
|
||||
d.audit.log(h.actor(c), 'quiz.save', String(q.id), { name: q.name, songs: q.songs, visibility: q.visibility }, { name: saved.name, songs: saved.items.length, visibility: saved.visibility });
|
||||
d.onChange?.();
|
||||
if (q.kind === 'duel') views.kick();
|
||||
return c.json({ quiz: saved, reviews });
|
||||
});
|
||||
|
||||
/** 이지선다 조회수 자동: where each fetched song stands (the editor polls this while songs are waiting). */
|
||||
app.get('/quizzes/:id/views', (c) => {
|
||||
const q = mine(c);
|
||||
if (!q) return c.json({ error: '퀴즈를 찾을 수 없어요.' }, 404);
|
||||
const items = d.quizzes.items(q.id).flatMap((it) => (it.viewsAuto ? [{ id: it.id!, videoId: it.videoId, views: it.views, viewsAuto: it.viewsAuto }] : []));
|
||||
return c.json({ items });
|
||||
});
|
||||
|
||||
/** 조회수 다시 불러오기: every fetched song of the saved quiz, or the songs named (which then switch from a written number to 자동). */
|
||||
app.post('/quizzes/:id/views', async (c) => {
|
||||
const q = mine(c);
|
||||
if (!q) return c.json({ error: '퀴즈를 찾을 수 없어요.' }, 404);
|
||||
if (q.kind !== 'duel') return c.json({ error: '이지선다 퀴즈에서만 조회수를 불러와요.' }, 400);
|
||||
const p = z.object({ itemIds: z.array(z.number().int().positive()).max(5000).optional() }).safeParse(await h.body(c));
|
||||
if (!p.success) return c.json({ error: '입력 값이 올바르지 않아요.' }, 400);
|
||||
const queued = views.refresh(q.id, p.data.itemIds);
|
||||
d.audit.log(h.actor(c), 'quiz.views', String(q.id), null, { queued, items: p.data.itemIds?.length ?? 'all' });
|
||||
return c.json({ queued });
|
||||
});
|
||||
|
||||
app.delete('/quizzes/:id', (c) => {
|
||||
const q = mine(c);
|
||||
if (!q) return c.json({ error: '퀴즈를 찾을 수 없어요.' }, 404);
|
||||
|
||||
Reference in New Issue
Block a user