3단계 노래 DB: 곡 스키마(장르·기준별 단계·가사·후보), 노라보세·스타맵 가져오기, 유튜브 원곡 찾기(쿠키 노래 검색 + 로그인 없는 Topic 대체), 관리자 노래·수집 화면
- 노라보세: 8,572곡(멜론 ID·연도·최고순위·장르·5단계 난이도·가사), 500 나는 묶음은 반씩 나눠 재요청, 원본 캐시로 재실행 안전 - 스타맵 803곡: 같은 곡은 합치고(363) 새 곡 440, 맵 제작자 연도(올해)는 버림 - 유튜브: 제목·가수·길이 점수로 확정/후보, 퍼가기 금지 영상 제외, 검색 방식별로 시도 기록 - 관리자: 노래 검색·장르·연도·난이도·유튜브 상태·출처 필터, 편집 패널(미리듣기·후보 선택·하이라이트·별칭·숨김), 수집 기록 - 테스트 32개, e2e에 노래 편집 단계 추가 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,9 @@ import type { Audit, Roles } from '../admin/roles';
|
||||
import { SiteSettingsSchema, type SettingsStore } from '../admin/settings';
|
||||
import type { Sessions } from '../auth/sessions';
|
||||
import { toPublicUser, type Users } from '../auth/users';
|
||||
import type { Database } from 'bun:sqlite';
|
||||
import { catalogStats, genreList, getSong, listSongs, updateSong } from '../songs/store';
|
||||
import { checkEmbed, type EmbedState } from '../collector/oembed';
|
||||
|
||||
export interface AdminDeps {
|
||||
users: Users;
|
||||
@@ -15,11 +18,43 @@ export interface AdminDeps {
|
||||
settings: SettingsStore;
|
||||
disconnectUser: (userId: string) => void;
|
||||
stats: () => Record<string, unknown>;
|
||||
db: Database;
|
||||
/** Injected for tests. */
|
||||
checkEmbed?: (videoId: string) => Promise<EmbedState>;
|
||||
}
|
||||
|
||||
type Env = { Variables: { userId: string | null; role: Role } };
|
||||
|
||||
const DISCORD_ID = /^\d{17,20}$/;
|
||||
const VIDEO_ID = /^[\w-]{11}$/;
|
||||
|
||||
/** Accepts https://youtu.be/ID, youtube.com/watch?v=ID, music.youtube.com/watch?v=ID or the bare ID. */
|
||||
export function parseVideoId(s: string): string | null {
|
||||
const t = s.trim();
|
||||
if (VIDEO_ID.test(t)) return t;
|
||||
try {
|
||||
const u = new URL(t);
|
||||
const id = u.hostname === 'youtu.be' ? u.pathname.slice(1) : u.searchParams.get('v');
|
||||
return id && VIDEO_ID.test(id) ? id : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
const SongPatchSchema = z.object({
|
||||
title: z.string().trim().min(1).max(200).optional(),
|
||||
artist: z.string().trim().min(1).max(200).optional(),
|
||||
year: z.number().int().min(1900).max(2100).nullable().optional(),
|
||||
genres: z.array(z.string().max(30)).max(20).optional(),
|
||||
level: z.number().int().min(1).max(10).nullable().optional(),
|
||||
youtube: z.string().max(200).nullable().optional(),
|
||||
highlightSec: z.number().min(0).max(3600).nullable().optional(),
|
||||
hidden: z.boolean().optional(),
|
||||
note: z.string().max(1000).nullable().optional(),
|
||||
titleAliases: z.array(z.string().max(200)).max(30).optional(),
|
||||
artistAliases: z.array(z.string().max(200)).max(30).optional(),
|
||||
lyrics: z.string().max(20000).nullable().optional(),
|
||||
});
|
||||
|
||||
export function createAdminApp(d: AdminDeps) {
|
||||
const app = new Hono<Env>();
|
||||
@@ -53,6 +88,63 @@ export function createAdminApp(d: AdminDeps) {
|
||||
return c.json({ site: parsed.data });
|
||||
});
|
||||
|
||||
// ------------------------------------------------------------ songs (docs/02 노래 관리)
|
||||
app.get('/catalog', (c) => c.json(catalogStats(d.db)));
|
||||
app.get('/genres', (c) => c.json({ genres: genreList(d.db) }));
|
||||
app.get('/songs', (c) => {
|
||||
const q = (k: string) => c.req.query(k) || undefined;
|
||||
const num = (k: string) => (q(k) ? Number(q(k)) || undefined : undefined);
|
||||
const yt = q('youtube');
|
||||
const sort = q('sort');
|
||||
return c.json(
|
||||
listSongs(d.db, {
|
||||
q: q('q')?.trim().slice(0, 100),
|
||||
genres: q('genres')?.split(',').filter(Boolean),
|
||||
yearFrom: num('yearFrom'),
|
||||
yearTo: num('yearTo'),
|
||||
level: num('level'),
|
||||
youtube: yt && ['matched', 'candidate', 'none', 'dead'].includes(yt) ? (yt as 'matched') : undefined,
|
||||
source: q('source'),
|
||||
hidden: q('hidden') === undefined ? undefined : q('hidden') === '1',
|
||||
lyrics: q('lyrics') === undefined ? undefined : q('lyrics') === '1',
|
||||
sort: sort && ['recent', 'year', 'level', 'peak', 'title'].includes(sort) ? (sort as 'recent') : undefined,
|
||||
page: num('page'),
|
||||
}),
|
||||
);
|
||||
});
|
||||
app.get('/songs/:id', (c) => {
|
||||
const r = getSong(d.db, Number(c.req.param('id')));
|
||||
return r ? c.json(r) : c.json({ error: '노래를 찾을 수 없어요.' }, 404);
|
||||
});
|
||||
app.patch('/songs/:id', async (c) => {
|
||||
const id = Number(c.req.param('id'));
|
||||
const before = getSong(d.db, id);
|
||||
if (!before) return c.json({ error: '노래를 찾을 수 없어요.' }, 404);
|
||||
const parsed = SongPatchSchema.safeParse(await body(c));
|
||||
if (!parsed.success) return c.json({ error: `입력 값이 올바르지 않아요: ${parsed.error.issues.map((i) => i.path.join('.')).join(', ')}` }, 400);
|
||||
const { youtube, ...rest } = parsed.data;
|
||||
let youtubeId: string | null | undefined;
|
||||
let embed: { author: string; title: string } | undefined;
|
||||
if (youtube !== undefined) {
|
||||
if (youtube === null || youtube.trim() === '') youtubeId = null;
|
||||
else {
|
||||
youtubeId = parseVideoId(youtube);
|
||||
if (!youtubeId) return c.json({ error: '유튜브 주소를 알아볼 수 없어요. https://youtu.be/… 형식으로 넣어 주세요.' }, 400);
|
||||
const e = await (d.checkEmbed ?? checkEmbed)(youtubeId);
|
||||
if (e.state === 'noembed') return c.json({ error: '이 영상은 다른 사이트에서 재생할 수 없게 막혀 있어요(퍼가기 금지). 다른 영상을 골라 주세요.' }, 400);
|
||||
if (e.state === 'dead') return c.json({ error: '없는 영상이에요.' }, 400);
|
||||
if (e.state === 'error') return c.json({ error: '유튜브 확인에 실패했어요. 잠시 후 다시 시도해 주세요.' }, 502);
|
||||
embed = { author: e.author, title: e.title };
|
||||
}
|
||||
}
|
||||
const genres = new Set(genreList(d.db).map((g) => g.id));
|
||||
if (rest.genres?.some((g) => !genres.has(g))) return c.json({ error: '없는 장르가 있어요.' }, 400);
|
||||
updateSong(d.db, id, { ...rest, youtubeId }, embed);
|
||||
const after = getSong(d.db, id)!;
|
||||
d.audit.log(actor(c), 'song.update', String(id), { title: before.song.title, artist: before.song.artist }, parsed.data);
|
||||
return c.json(after);
|
||||
});
|
||||
|
||||
// ------------------------------------------------------------ audit
|
||||
app.get('/audit', (c) => {
|
||||
const page = Math.max(0, Number(c.req.query('page') ?? 0) || 0);
|
||||
|
||||
Reference in New Issue
Block a user