mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-11-23 04:46:08 -08:00
feat(stats): group library seasons, movies and OVAs into franchise cards (#336)
This commit is contained in:
1 parent
9d2057cc30
commit
71251b8d7b
41 files changed
+2335
-109
No files matched your search
@@ -11,6 +11,7 @@ import {
|
||||
enrichSessionsWithKnownWordMetrics,
|
||||
isPositiveSafeInteger,
|
||||
loadKnownWordsSet,
|
||||
parseAnimeGroupOverrideBody,
|
||||
parseBooleanQuery,
|
||||
parseDuplicateLineCleanupBody,
|
||||
parseExcludedWordsBody,
|
||||
@@ -190,6 +191,11 @@ export function registerStatsLibraryRoutes(
|
||||
return c.json(statsJson('animeLibrary', rows));
|
||||
});
|
||||
|
||||
// Registered before `/api/stats/anime/:animeId` so `groups` is not read as an id.
|
||||
app.get('/api/stats/anime/groups', async (c) => {
|
||||
return c.json(statsJson('animeLibraryGroups', await tracker.getAnimeLibraryGroups()));
|
||||
});
|
||||
|
||||
app.get('/api/stats/anime/merge-recommendations', async (c) => {
|
||||
const recommendations = await tracker.getAnimeMergeRecommendations();
|
||||
return c.json(statsJson('animeMergeRecommendations', { recommendations }));
|
||||
@@ -208,11 +214,12 @@ export function registerStatsLibraryRoutes(
|
||||
if (animeId === null) return c.body(null, 400);
|
||||
const detail = await tracker.getAnimeDetail(animeId);
|
||||
if (!detail) return c.body(null, 404);
|
||||
const [episodes, anilistEntries] = await Promise.all([
|
||||
const [episodes, anilistEntries, group] = await Promise.all([
|
||||
tracker.getAnimeEpisodes(animeId),
|
||||
tracker.getAnimeAnilistEntries(animeId),
|
||||
tracker.getAnimeGroupPlacement(animeId),
|
||||
]);
|
||||
return c.json(statsJson('animeDetail', { detail, episodes, anilistEntries }));
|
||||
return c.json(statsJson('animeDetail', { detail, episodes, anilistEntries, group }));
|
||||
});
|
||||
|
||||
app.get('/api/stats/anime/:animeId/words', async (c) => {
|
||||
@@ -267,6 +274,22 @@ export function registerStatsLibraryRoutes(
|
||||
return c.json(statsJson('deleteAnime', { ok: true }));
|
||||
});
|
||||
|
||||
app.put('/api/stats/anime/:animeId/group', async (c) => {
|
||||
const animeId = parsePositiveId(c.req.param('animeId'));
|
||||
if (animeId === null) return c.body(null, 400);
|
||||
const request = parseAnimeGroupOverrideBody(await c.req.json().catch(() => null));
|
||||
if (!request) return c.body(null, 400);
|
||||
if (request.mode === 'join' && request.targetAnimeId === animeId) return c.body(null, 400);
|
||||
const result = await tracker.setAnimeGroupOverride(animeId, request);
|
||||
if (result.ok) return c.json(statsJson('setAnimeGroupOverride', { ok: true }));
|
||||
if (result.reason === 'not-found') return c.body(null, 404);
|
||||
const error =
|
||||
result.reason === 'unsupported-kind'
|
||||
? 'Only anime entries can be grouped.'
|
||||
: 'That title has no AniList entry to group with. Link it to AniList or merge the entries instead.';
|
||||
return c.json(statsJson('error', { error }), 409);
|
||||
});
|
||||
|
||||
app.post('/api/stats/anime/:animeId/merge', async (c) => {
|
||||
const animeId = parsePositiveId(c.req.param('animeId'));
|
||||
if (animeId === null) return c.body(null, 400);
|
||||
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
import { createLogger } from '../../../logger.js';
|
||||
import type { AnkiConnectConfig } from '../../../types.js';
|
||||
import type {
|
||||
StatsAnimeGroupOverrideRequest,
|
||||
StatsDayRange,
|
||||
StatsDictionaryWordQuery,
|
||||
StatsDictionaryWordSort,
|
||||
@@ -59,6 +60,15 @@ export function isPositiveSafeInteger(value: unknown): value is number {
|
||||
return typeof value === 'number' && Number.isSafeInteger(value) && value > 0;
|
||||
}
|
||||
|
||||
/** Validates a `PUT /api/stats/anime/:animeId/group` body; null for anything malformed. */
|
||||
export function parseAnimeGroupOverrideBody(body: unknown): StatsAnimeGroupOverrideRequest | null {
|
||||
if (!body || typeof body !== 'object') return null;
|
||||
const { mode, targetAnimeId } = body as Record<string, unknown>;
|
||||
if (mode === 'standalone' || mode === 'anilist') return { mode };
|
||||
if (mode === 'join' && isPositiveSafeInteger(targetAnimeId)) return { mode, targetAnimeId };
|
||||
return null;
|
||||
}
|
||||
|
||||
export function parsePositiveId(raw: string | undefined): number | null {
|
||||
if (raw === undefined) return null;
|
||||
const value = Number(raw);
|
||||
|
||||
Reference in new issue
Block a user