@serifpersia/dango 3.3.9 → 3.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/client/dist/assets/{AnimeCard-URCiiy5W.js → AnimeCard-CJy1SXH3.js} +1 -1
- package/client/dist/assets/{AnimeInfo-e3z3er9t.js → AnimeInfo-BoXlVvZ-.js} +1 -1
- package/client/dist/assets/{AnimeInfoPage-B7Djlmy_.js → AnimeInfoPage-BxbA_7AV.js} +2 -2
- package/client/dist/assets/Asmr--Zc8G9zv.js +2 -0
- package/client/dist/assets/AsmrResetProgressModal-CKYm-8cp.js +1 -0
- package/client/dist/assets/AvSyncCalibrator-3yjZS7s6.js +10 -0
- package/client/dist/assets/Home-rrhOGQcg.js +1 -0
- package/client/dist/assets/Insights-Bay90Yeg.js +1 -0
- package/client/dist/assets/{LibraryListPage-B0qWqhb_.js → LibraryListPage-HzuDavbb.js} +1 -1
- package/client/dist/assets/{ListeningList-CSqD8j1C.js → ListeningList-DYcQnnsG.js} +1 -1
- package/client/dist/assets/{Manga-Cg2YuObF.js → Manga-D8CT9aO0.js} +1 -1
- package/client/dist/assets/{MangaInfoPage-DcArhEsy.js → MangaInfoPage-DGK6CYp1.js} +1 -1
- package/client/dist/assets/MangaReadPage-B9XoDKF9.js +1 -0
- package/client/dist/assets/{MangaResetProgressModal-D_yT3d1v.js → MangaResetProgressModal-QjZyIAjl.js} +1 -1
- package/client/dist/assets/Mature-CHt_-Hz5.js +1 -0
- package/client/dist/assets/MediaCard-B5584Cq9.js +1 -0
- package/client/dist/assets/Music-HeLrEZ2d.js +1 -0
- package/client/dist/assets/OptionListMenu-CJ90CdwI.js +1 -0
- package/client/dist/assets/Player-BYtEax_z.js +56 -0
- package/client/dist/assets/PlayerSettings-4-cUAY4B.js +1 -0
- package/client/dist/assets/QueueOptionsButton-Ba0dg_82.js +1 -0
- package/client/dist/assets/{QueueRail-B7f1-e6i.js → QueueRail-O7f4RqjB.js} +1 -1
- package/client/dist/assets/Radio-DPZxQnyD.js +1 -0
- package/client/dist/assets/{ReadingList-BaIem8H1.js → ReadingList-n6rr8ygy.js} +1 -1
- package/client/dist/assets/{ResetProgressModal-DES5Y-Fl.js → ResetProgressModal-CcStlbhP.js} +1 -1
- package/client/dist/assets/Search-C2wrWOdy.js +1 -0
- package/client/dist/assets/Settings-CluUYs8U.js +21 -0
- package/client/dist/assets/{SkeletonGrid-BxfgjP4i.js → SkeletonGrid-CLEPY9nP.js} +1 -1
- package/client/dist/assets/Trackers-CTGPxs0V.js +2 -0
- package/client/dist/assets/Tv-BwHlDdi8.js +4 -0
- package/client/dist/assets/Tv-ZEA45gXK.css +1 -0
- package/client/dist/assets/TvInfo-CXnrLekr.js +1 -0
- package/client/dist/assets/TvPopup-BekfiNcX.js +1 -0
- package/client/dist/assets/{TvSearch-BWDwWnHe.js → TvSearch-DuqR-z3v.js} +1 -1
- package/client/dist/assets/{TvWatchlist-D3n5_4MI.js → TvWatchlist-CBvgFrA0.js} +1 -1
- package/client/dist/assets/Watchlist-mj8mTOam.js +1 -0
- package/client/dist/assets/{hls-BO_L1WoN.js → hls-Boje3IOE.js} +1 -1
- package/client/dist/assets/index-BXwhFWB_.js +178 -0
- package/client/dist/assets/useAnimeInfoData-DJ7A4B4T.js +1 -0
- package/client/dist/assets/useAsmr-DIwIsJXF.js +1 -0
- package/client/dist/assets/{useAsmrPopup-D_o9-QlR.js → useAsmrPopup-e4zNN9ig.js} +1 -1
- package/client/dist/assets/useMangaLibrary-C9Lci5N4.js +1 -0
- package/client/dist/assets/{useMangaPopup-TFzkC-tG.js → useMangaPopup-B8nnpvyi.js} +1 -1
- package/client/dist/assets/useProviders-B5Qr4I1y.js +1 -0
- package/client/dist/assets/useTv-Dm8MCYlL.js +1 -0
- package/client/dist/assets/useTvLibrary-B7jEk2Q7.js +1 -0
- package/client/dist/assets/{vendor-query-BXRECSI6.js → vendor-query-BUTye89G.js} +1 -1
- package/client/dist/index.html +2 -2
- package/orchestrator.js +1 -2
- package/package.json +2 -6
- package/server/dist/app-auth.js +9 -29
- package/server/dist/app-hono.js +621 -0
- package/server/dist/config.js +0 -1
- package/server/dist/db/drizzle.js +32 -0
- package/server/dist/db/schema-anime.js +64 -0
- package/server/dist/db/schema-asmr.js +25 -0
- package/server/dist/db/schema-manga.js +31 -0
- package/server/dist/db/schema-tv.js +33 -0
- package/server/dist/db.js +75 -4
- package/server/dist/discord-gateway.js +1 -1
- package/server/dist/google.js +0 -7
- package/server/dist/hono/asmr.js +1 -0
- package/server/dist/hono/auth.js +56 -0
- package/server/dist/hono/music.js +419 -0
- package/server/dist/{controllers/proxy.controller.js → hono/proxy.js} +390 -394
- package/server/dist/hono/tv.js +197 -0
- package/server/dist/hono/watchlist.js +733 -0
- package/server/dist/lib/anilist.js +3 -3
- package/server/dist/lib/insights.js +185 -0
- package/server/dist/lib/ipc.js +1 -2
- package/server/dist/lib/kitsu.js +1 -28
- package/server/dist/lib/mal-import.js +291 -0
- package/server/dist/lib/migration.js +20 -34
- package/server/dist/lib/offline-db.js +36 -36
- package/server/dist/lib/tracker/manga-sync.service.js +21 -21
- package/server/dist/lib/tracker/sync.service.js +20 -20
- package/server/dist/lib/translate.js +79 -0
- package/server/dist/lib/ytmusic.js +0 -2
- package/server/dist/providers/title-matching.js +0 -1
- package/server/dist/repositories/asmr.repository.js +90 -87
- package/server/dist/repositories/insights.repository.js +24 -13
- package/server/dist/repositories/library.repository.js +33 -93
- package/server/dist/repositories/manga.repository.js +108 -116
- package/server/dist/repositories/notifications.repository.js +50 -31
- package/server/dist/repositories/queue.repository.js +36 -25
- package/server/dist/repositories/settings.repository.js +18 -8
- package/server/dist/repositories/shows-meta.repository.js +54 -41
- package/server/dist/repositories/temp-show-ids.repository.js +43 -31
- package/server/dist/repositories/tv.repository.js +98 -115
- package/server/dist/repositories/watched-episodes.repository.js +52 -20
- package/server/dist/repositories/watchlist.repository.js +36 -48
- package/server/dist/server.js +59 -151
- package/server/dist/sync.js +41 -4
- package/server/dist/trpc/context.js +7 -0
- package/server/dist/trpc/errors.js +21 -0
- package/server/dist/trpc/index.js +19 -0
- package/server/dist/trpc/router.js +49 -0
- package/server/dist/trpc/routers/asmr-library.js +122 -0
- package/server/dist/trpc/routers/asmr-progress.js +133 -0
- package/server/dist/trpc/routers/asmr.js +87 -0
- package/server/dist/trpc/routers/auth.js +247 -0
- package/server/dist/trpc/routers/continue-watching.js +83 -0
- package/server/dist/trpc/routers/data.js +1039 -0
- package/server/dist/trpc/routers/discord.js +288 -0
- package/server/dist/trpc/routers/discovery.js +15 -0
- package/server/dist/trpc/routers/insights.js +45 -0
- package/server/dist/trpc/routers/manga-library.js +280 -0
- package/server/dist/trpc/routers/manga-progress.js +181 -0
- package/server/dist/trpc/routers/manga.js +201 -0
- package/server/dist/{controllers/music.controller.js → trpc/routers/music.js} +155 -288
- package/server/dist/trpc/routers/notifications.js +80 -0
- package/server/dist/trpc/routers/progress.js +15 -0
- package/server/dist/trpc/routers/queue.js +95 -0
- package/server/dist/{routes/radio.routes.js → trpc/routers/radio.js} +30 -23
- package/server/dist/trpc/routers/settings.js +105 -0
- package/server/dist/trpc/routers/tracker.js +219 -0
- package/server/dist/trpc/routers/tv-library.js +145 -0
- package/server/dist/trpc/routers/tv-progress.js +171 -0
- package/server/dist/trpc/routers/tv.js +565 -0
- package/server/dist/trpc/routers/watchlist.js +564 -0
- package/server/dist/trpc/validation.js +159 -0
- package/server/dist/utils/security.utils.js +0 -18
- package/server/package.json +5 -10
- package/client/dist/assets/Asmr-C28TfSvX.js +0 -2
- package/client/dist/assets/AsmrResetProgressModal-6Xh71AgR.js +0 -1
- package/client/dist/assets/AvSyncCalibrator-BwCtRW0P.js +0 -10
- package/client/dist/assets/Home-BJdbHYja.js +0 -1
- package/client/dist/assets/Insights-CjRy42YE.js +0 -1
- package/client/dist/assets/MangaReadPage-YCETZRte.js +0 -1
- package/client/dist/assets/Mature-BGxRWkV5.js +0 -1
- package/client/dist/assets/MediaCard-CAzoxsRS.js +0 -1
- package/client/dist/assets/Music-Cgo-yQ7E.js +0 -1
- package/client/dist/assets/OptionListMenu--Owe9Jle.js +0 -1
- package/client/dist/assets/Player-yRXG2BrP.js +0 -56
- package/client/dist/assets/PlayerSettings-DrZLa0CC.js +0 -1
- package/client/dist/assets/QueueOptionsButton-BVZyPyKf.js +0 -1
- package/client/dist/assets/Radio-BFjxlzuF.js +0 -1
- package/client/dist/assets/Search-BOBo0Ogh.js +0 -1
- package/client/dist/assets/Settings-DOvNQ0gX.js +0 -21
- package/client/dist/assets/Trackers-DZ9KqOWv.js +0 -2
- package/client/dist/assets/Tv-5mATEU4p.js +0 -4
- package/client/dist/assets/Tv-C2SWcVzN.css +0 -1
- package/client/dist/assets/TvInfo-CF-5C5Fs.js +0 -1
- package/client/dist/assets/TvPopup-Dfqifj3s.js +0 -1
- package/client/dist/assets/Watchlist-B8MHNsAU.js +0 -1
- package/client/dist/assets/index-D2RkUJIN.js +0 -178
- package/client/dist/assets/useAnimeInfoData-BWShDHqE.js +0 -1
- package/client/dist/assets/useAsmr-FyfD9G7b.js +0 -1
- package/client/dist/assets/useMangaLibrary-BJU5x8Vz.js +0 -1
- package/client/dist/assets/useProviders-DDRxa8vl.js +0 -1
- package/client/dist/assets/useTv-Bh6WmNnZ.js +0 -1
- package/client/dist/assets/useTvLibrary-_Pqm8re1.js +0 -1
- package/server/dist/controllers/asmr-library.controller.js +0 -288
- package/server/dist/controllers/auth.controller.js +0 -225
- package/server/dist/controllers/data.controller.js +0 -767
- package/server/dist/controllers/insights.controller.js +0 -203
- package/server/dist/controllers/manga-library.controller.js +0 -350
- package/server/dist/controllers/settings.controller.js +0 -609
- package/server/dist/controllers/tv-library.controller.js +0 -314
- package/server/dist/controllers/watchlist.controller.js +0 -1069
- package/server/dist/request-logger.js +0 -16
- package/server/dist/routes/asmr-library.routes.js +0 -23
- package/server/dist/routes/asmr.routes.js +0 -81
- package/server/dist/routes/auth.routes.js +0 -25
- package/server/dist/routes/data.routes.js +0 -47
- package/server/dist/routes/discord-gateway.routes.js +0 -51
- package/server/dist/routes/insights.routes.js +0 -35
- package/server/dist/routes/lan-auth.routes.js +0 -46
- package/server/dist/routes/manga-library.routes.js +0 -25
- package/server/dist/routes/manga.routes.js +0 -141
- package/server/dist/routes/music.routes.js +0 -20
- package/server/dist/routes/providers.routes.js +0 -17
- package/server/dist/routes/proxy.routes.js +0 -12
- package/server/dist/routes/settings.routes.js +0 -24
- package/server/dist/routes/tracker.routes.js +0 -287
- package/server/dist/routes/translate.routes.js +0 -88
- package/server/dist/routes/tv-library.routes.js +0 -23
- package/server/dist/routes/tv.routes.js +0 -629
- package/server/dist/routes/watchlist.routes.js +0 -341
|
@@ -0,0 +1,564 @@
|
|
|
1
|
+
import { protectedProcedure, router } from '../index.js';
|
|
2
|
+
import { WatchlistRepository } from '../../repositories/watchlist.repository.js';
|
|
3
|
+
import { WatchedEpisodesRepository, } from '../../repositories/watched-episodes.repository.js';
|
|
4
|
+
import { ShowsMetaRepository } from '../../repositories/shows-meta.repository.js';
|
|
5
|
+
import { NotificationsRepository } from '../../repositories/notifications.repository.js';
|
|
6
|
+
import { QueueRepository } from '../../repositories/queue.repository.js';
|
|
7
|
+
import { SettingsRepository } from '../../repositories/settings.repository.js';
|
|
8
|
+
import { performWriteTransactionAsync } from '../../sync.js';
|
|
9
|
+
import { getMigratedId } from '../../lib/migration.js';
|
|
10
|
+
import { isAnilistRateLimited, searchAnilist, searchAnilistByTitle, getAnilistEpisodes, getShowMetaById, } from '../../lib/anilist.js';
|
|
11
|
+
import { kitsuSearchAnime } from '../../lib/kitsu.js';
|
|
12
|
+
import { offlineDb } from '../../lib/offline-db.js';
|
|
13
|
+
import { dbAll } from '../../utils/db-utils.js';
|
|
14
|
+
import { defineSchema, idInput, idsInput, optStr, reqObj, showIdInput, watchlistAddInput, watchlistBatchStatusInput, watchlistStatusInput, } from '../validation.js';
|
|
15
|
+
const watchlistListInput = () => defineSchema((value) => {
|
|
16
|
+
const obj = reqObj(value);
|
|
17
|
+
const out = { ...readFilterFields(obj) };
|
|
18
|
+
const status = optStr(obj, 'status');
|
|
19
|
+
if (status !== undefined)
|
|
20
|
+
out.status = status;
|
|
21
|
+
const page = optNum(obj, 'page');
|
|
22
|
+
if (page !== undefined)
|
|
23
|
+
out.page = page;
|
|
24
|
+
const limit = optNum(obj, 'limit');
|
|
25
|
+
if (limit !== undefined)
|
|
26
|
+
out.limit = limit;
|
|
27
|
+
const showMature = optStr(obj, 'showMature');
|
|
28
|
+
if (showMature !== undefined)
|
|
29
|
+
out.showMature = showMature;
|
|
30
|
+
return out;
|
|
31
|
+
});
|
|
32
|
+
const continueWatchingAllInput = () => defineSchema((value) => {
|
|
33
|
+
const obj = reqObj(value);
|
|
34
|
+
const out = { ...readFilterFields(obj) };
|
|
35
|
+
const page = optNum(obj, 'page');
|
|
36
|
+
if (page !== undefined)
|
|
37
|
+
out.page = page;
|
|
38
|
+
const limit = optNum(obj, 'limit');
|
|
39
|
+
if (limit !== undefined)
|
|
40
|
+
out.limit = limit;
|
|
41
|
+
return out;
|
|
42
|
+
});
|
|
43
|
+
export const watchlistRouter = router({
|
|
44
|
+
check: protectedProcedure.input(showIdInput()).query(async ({ ctx, input }) => {
|
|
45
|
+
const showId = await getMigratedId(ctx.db, input.showId);
|
|
46
|
+
const item = await WatchlistRepository.getById(ctx.db, showId);
|
|
47
|
+
return { inWatchlist: !!item, status: item?.status ?? null };
|
|
48
|
+
}),
|
|
49
|
+
add: protectedProcedure.input(watchlistAddInput()).mutation(async ({ ctx, input }) => {
|
|
50
|
+
const id = await getMigratedId(ctx.db, input.id);
|
|
51
|
+
await performWriteTransactionAsync(ctx.db, async (tx) => {
|
|
52
|
+
await WatchlistRepository.upsert(tx, {
|
|
53
|
+
id,
|
|
54
|
+
name: input.name,
|
|
55
|
+
thumbnail: input.thumbnail || '',
|
|
56
|
+
status: input.status || 'Watching',
|
|
57
|
+
nativeName: input.nativeName || '',
|
|
58
|
+
englishName: input.englishName || '',
|
|
59
|
+
type: input.type || 'TV',
|
|
60
|
+
});
|
|
61
|
+
if (typeof input.isAdult === 'boolean') {
|
|
62
|
+
await ShowsMetaRepository.upsert(tx, { id, isAdult: input.isAdult ? 1 : 0 });
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
await ctx.db.saveNow();
|
|
66
|
+
if (input.name && !/^\d+$/.test(id)) {
|
|
67
|
+
const db = ctx.db;
|
|
68
|
+
const name = input.name;
|
|
69
|
+
const resolveAndSave = async () => {
|
|
70
|
+
if (!isAnilistRateLimited()) {
|
|
71
|
+
const result = await searchAnilistByTitle(name);
|
|
72
|
+
if (result?.id) {
|
|
73
|
+
await ShowsMetaRepository.upsert(db, { id, anilistId: result.id });
|
|
74
|
+
db.scheduleSave();
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
try {
|
|
79
|
+
const kitsuResults = await kitsuSearchAnime({ query: name, page: 1, perPage: 3 });
|
|
80
|
+
if (kitsuResults.length > 0) {
|
|
81
|
+
const anilistId = Math.abs(kitsuResults[0].id);
|
|
82
|
+
await ShowsMetaRepository.upsert(db, { id, anilistId });
|
|
83
|
+
db.scheduleSave();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
// ignore
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
void resolveAndSave().catch(() => { });
|
|
91
|
+
}
|
|
92
|
+
return { success: true };
|
|
93
|
+
}),
|
|
94
|
+
remove: protectedProcedure.input(idInput()).mutation(async ({ ctx, input }) => {
|
|
95
|
+
const id = await getMigratedId(ctx.db, input.id);
|
|
96
|
+
await performWriteTransactionAsync(ctx.db, async (tx) => {
|
|
97
|
+
await WatchlistRepository.delete(tx, id);
|
|
98
|
+
await WatchedEpisodesRepository.deleteByShow(tx, id);
|
|
99
|
+
await NotificationsRepository.deleteByShow(tx, id);
|
|
100
|
+
});
|
|
101
|
+
return { success: true };
|
|
102
|
+
}),
|
|
103
|
+
removeMany: protectedProcedure.input(idsInput()).mutation(async ({ ctx, input }) => {
|
|
104
|
+
const ids = await Promise.all(input.ids.map((id) => getMigratedId(ctx.db, id)));
|
|
105
|
+
await performWriteTransactionAsync(ctx.db, async (tx) => {
|
|
106
|
+
await WatchlistRepository.deleteMany(tx, ids);
|
|
107
|
+
for (const id of ids) {
|
|
108
|
+
await WatchedEpisodesRepository.deleteByShow(tx, id);
|
|
109
|
+
await NotificationsRepository.deleteByShow(tx, id);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
ctx.db.scheduleSave();
|
|
113
|
+
return { success: true, removed: ids.length };
|
|
114
|
+
}),
|
|
115
|
+
setStatus: protectedProcedure.input(watchlistStatusInput()).mutation(async ({ ctx, input }) => {
|
|
116
|
+
const id = await getMigratedId(ctx.db, input.id);
|
|
117
|
+
await performWriteTransactionAsync(ctx.db, async (tx) => {
|
|
118
|
+
await WatchlistRepository.updateStatus(tx, id, input.status);
|
|
119
|
+
});
|
|
120
|
+
return { success: true };
|
|
121
|
+
}),
|
|
122
|
+
batchStatus: protectedProcedure
|
|
123
|
+
.input(watchlistBatchStatusInput())
|
|
124
|
+
.mutation(async ({ ctx, input }) => {
|
|
125
|
+
const ids = await Promise.all(input.ids.map((id) => getMigratedId(ctx.db, id)));
|
|
126
|
+
await performWriteTransactionAsync(ctx.db, async (tx) => {
|
|
127
|
+
await WatchlistRepository.updateStatusMany(tx, ids, input.status);
|
|
128
|
+
});
|
|
129
|
+
ctx.db.scheduleSave();
|
|
130
|
+
return { success: true, updated: ids.length };
|
|
131
|
+
}),
|
|
132
|
+
list: protectedProcedure.input(watchlistListInput()).query(async ({ ctx, input }) => {
|
|
133
|
+
const db = ctx.db;
|
|
134
|
+
const page = Math.max(parseInt(String(input.page ?? 1)) || 1, 1);
|
|
135
|
+
const limit = Math.min(Math.max(parseInt(String(input.limit ?? 10)) || 10, 1), 100);
|
|
136
|
+
const showMature = input.showMature;
|
|
137
|
+
const offset = (page - 1) * limit;
|
|
138
|
+
const filters = getWatchlistFilters({
|
|
139
|
+
query: input.query,
|
|
140
|
+
type: input.type,
|
|
141
|
+
season: input.season,
|
|
142
|
+
year: input.year,
|
|
143
|
+
genres: input.genres,
|
|
144
|
+
excludeGenres: input.excludeGenres,
|
|
145
|
+
sortBy: input.sortBy,
|
|
146
|
+
titlePreference: input.titlePreference,
|
|
147
|
+
});
|
|
148
|
+
const status = input.status;
|
|
149
|
+
const allRows = await WatchlistRepository.getAll(db, status);
|
|
150
|
+
let filteredRows = await filterWatchlistRows(allRows, filters, db);
|
|
151
|
+
if (showMature === 'false') {
|
|
152
|
+
const ids = filteredRows.map((r) => r.id);
|
|
153
|
+
if (ids.length > 0) {
|
|
154
|
+
const placeholders = ids.map(() => '?').join(',');
|
|
155
|
+
const adultIds = await dbAll(db, `SELECT id FROM shows_meta WHERE id IN (${placeholders}) AND isAdult = 1`, ids);
|
|
156
|
+
const adultSet = new Set(adultIds.map((r) => r.id));
|
|
157
|
+
filteredRows = filteredRows.filter((row) => !adultSet.has(row.id));
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
const rows = filteredRows.slice(offset, offset + limit);
|
|
161
|
+
for (const row of rows) {
|
|
162
|
+
if (!row.thumbnail || row.thumbnail.trim() === '') {
|
|
163
|
+
try {
|
|
164
|
+
const meta = (await ShowsMetaRepository.getById(db, row.id));
|
|
165
|
+
if (meta?.thumbnail && meta.thumbnail.trim() !== '') {
|
|
166
|
+
row.thumbnail = meta.thumbnail;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
catch {
|
|
170
|
+
// ignore
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
await backfillMissingPosters(db, rows);
|
|
175
|
+
const rowIds = rows.map((r) => r.id);
|
|
176
|
+
let adultIds = new Set();
|
|
177
|
+
if (rowIds.length > 0) {
|
|
178
|
+
const placeholders = rowIds.map(() => '?').join(',');
|
|
179
|
+
const adultRows = await dbAll(db, `SELECT id FROM shows_meta WHERE id IN (${placeholders}) AND isAdult = 1`, rowIds);
|
|
180
|
+
adultIds = new Set(adultRows.map((r) => r.id));
|
|
181
|
+
}
|
|
182
|
+
return {
|
|
183
|
+
data: rows.map((row) => ({
|
|
184
|
+
...row,
|
|
185
|
+
_id: row.id,
|
|
186
|
+
thumbnail: row.thumbnail || '',
|
|
187
|
+
isAdult: adultIds.has(row.id),
|
|
188
|
+
})),
|
|
189
|
+
total: filteredRows.length,
|
|
190
|
+
page,
|
|
191
|
+
limit,
|
|
192
|
+
};
|
|
193
|
+
}),
|
|
194
|
+
continueWatchingAll: protectedProcedure
|
|
195
|
+
.input(continueWatchingAllInput())
|
|
196
|
+
.query(async ({ ctx, input }) => {
|
|
197
|
+
const db = ctx.db;
|
|
198
|
+
const page = Math.max(parseInt(String(input.page ?? 1)) || 1, 1);
|
|
199
|
+
const limit = Math.min(Math.max(parseInt(String(input.limit ?? 10)) || 10, 1), 100);
|
|
200
|
+
const offset = (page - 1) * limit;
|
|
201
|
+
const filters = getWatchlistFilters({
|
|
202
|
+
query: input.query,
|
|
203
|
+
type: input.type,
|
|
204
|
+
season: input.season,
|
|
205
|
+
year: input.year,
|
|
206
|
+
genres: input.genres,
|
|
207
|
+
excludeGenres: input.excludeGenres,
|
|
208
|
+
sortBy: input.sortBy,
|
|
209
|
+
titlePreference: input.titlePreference,
|
|
210
|
+
});
|
|
211
|
+
const data = await filterWatchlistRows(await getContinueWatchingData(db), filters, db);
|
|
212
|
+
return {
|
|
213
|
+
data: data.slice(offset, offset + limit),
|
|
214
|
+
total: data.length,
|
|
215
|
+
page,
|
|
216
|
+
limit,
|
|
217
|
+
};
|
|
218
|
+
}),
|
|
219
|
+
queueSuggested: protectedProcedure.input(showIdInput()).query(async ({ ctx, input }) => {
|
|
220
|
+
const db = ctx.db;
|
|
221
|
+
const showId = await getMigratedId(db, input.showId);
|
|
222
|
+
const resumeProgress = await WatchedEpisodesRepository.getLatestResumeProgress(db, showId);
|
|
223
|
+
if (resumeProgress) {
|
|
224
|
+
return {
|
|
225
|
+
showId,
|
|
226
|
+
episodeNumber: resumeProgress.episodeNumber,
|
|
227
|
+
resumeTime: resumeProgress.currentTime || 0,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
const [watchedEpisodes, episodes] = await Promise.all([
|
|
231
|
+
WatchedEpisodesRepository.getByShow(db, showId),
|
|
232
|
+
resolveAvailableEpisodes(db, showId),
|
|
233
|
+
]);
|
|
234
|
+
const watchedSet = new Set(watchedEpisodes.map((ep) => ep.episodeNumber.toString()));
|
|
235
|
+
const finishedEpisodes = watchedEpisodes
|
|
236
|
+
.filter((ep) => ep.duration > 0 && ep.currentTime >= ep.duration * 0.8)
|
|
237
|
+
.map((ep) => parseFloat(ep.episodeNumber))
|
|
238
|
+
.filter((ep) => !Number.isNaN(ep));
|
|
239
|
+
const nextAfterFinished = finishedEpisodes.length > 0 ? String(Math.max(...finishedEpisodes) + 1) : undefined;
|
|
240
|
+
const episodeNumber = (nextAfterFinished &&
|
|
241
|
+
episodes.includes(nextAfterFinished) &&
|
|
242
|
+
!watchedSet.has(nextAfterFinished)
|
|
243
|
+
? nextAfterFinished
|
|
244
|
+
: episodes.find((ep) => !watchedSet.has(ep))) ||
|
|
245
|
+
episodes[0] ||
|
|
246
|
+
'1';
|
|
247
|
+
return { showId, episodeNumber, resumeTime: 0 };
|
|
248
|
+
}),
|
|
249
|
+
queueRemaining: protectedProcedure.input(showIdInput()).query(async ({ ctx, input }) => {
|
|
250
|
+
const db = ctx.db;
|
|
251
|
+
const showId = await getMigratedId(db, input.showId);
|
|
252
|
+
const [watchedEpisodes, queuedEpisodes, episodes] = await Promise.all([
|
|
253
|
+
WatchedEpisodesRepository.getByShow(db, showId),
|
|
254
|
+
QueueRepository.getByShow(db, showId),
|
|
255
|
+
resolveAvailableEpisodes(db, showId),
|
|
256
|
+
]);
|
|
257
|
+
const watchedSet = new Set(watchedEpisodes.map((ep) => ep.episodeNumber.toString()));
|
|
258
|
+
const queuedSet = new Set(queuedEpisodes.map((ep) => ep.episodeNumber.toString()));
|
|
259
|
+
const remaining = episodes.filter((ep) => !watchedSet.has(ep) && !queuedSet.has(ep));
|
|
260
|
+
return { showId, episodes: remaining };
|
|
261
|
+
}),
|
|
262
|
+
});
|
|
263
|
+
function optNum(obj, name) {
|
|
264
|
+
const value = obj[name];
|
|
265
|
+
if (value === undefined || value === null || value === '')
|
|
266
|
+
return undefined;
|
|
267
|
+
const num = Number(value);
|
|
268
|
+
return Number.isFinite(num) ? num : undefined;
|
|
269
|
+
}
|
|
270
|
+
function readFilterFields(obj) {
|
|
271
|
+
const out = {};
|
|
272
|
+
const query = optStr(obj, 'query');
|
|
273
|
+
if (query !== undefined)
|
|
274
|
+
out.query = query;
|
|
275
|
+
const type = optStr(obj, 'type');
|
|
276
|
+
if (type !== undefined)
|
|
277
|
+
out.type = type;
|
|
278
|
+
const season = optStr(obj, 'season');
|
|
279
|
+
if (season !== undefined)
|
|
280
|
+
out.season = season;
|
|
281
|
+
const year = optStr(obj, 'year');
|
|
282
|
+
if (year !== undefined)
|
|
283
|
+
out.year = year;
|
|
284
|
+
const genres = optStr(obj, 'genres');
|
|
285
|
+
if (genres !== undefined)
|
|
286
|
+
out.genres = genres;
|
|
287
|
+
const excludeGenres = optStr(obj, 'excludeGenres');
|
|
288
|
+
if (excludeGenres !== undefined)
|
|
289
|
+
out.excludeGenres = excludeGenres;
|
|
290
|
+
const sortBy = optStr(obj, 'sortBy');
|
|
291
|
+
if (sortBy !== undefined)
|
|
292
|
+
out.sortBy = sortBy;
|
|
293
|
+
const titlePreference = optStr(obj, 'titlePreference');
|
|
294
|
+
if (titlePreference !== undefined)
|
|
295
|
+
out.titlePreference = titlePreference;
|
|
296
|
+
return out;
|
|
297
|
+
}
|
|
298
|
+
function normalizeFilterValue(value) {
|
|
299
|
+
if (typeof value !== 'string')
|
|
300
|
+
return undefined;
|
|
301
|
+
const trimmed = value.trim();
|
|
302
|
+
return trimmed && trimmed !== 'ALL' ? trimmed : undefined;
|
|
303
|
+
}
|
|
304
|
+
function getWatchlistFilters(query) {
|
|
305
|
+
return {
|
|
306
|
+
query: normalizeFilterValue(query['query']),
|
|
307
|
+
type: normalizeFilterValue(query['type']),
|
|
308
|
+
season: normalizeFilterValue(query['season']),
|
|
309
|
+
year: normalizeFilterValue(query['year']),
|
|
310
|
+
genres: normalizeFilterValue(query['genres']),
|
|
311
|
+
excludeGenres: normalizeFilterValue(query['excludeGenres']),
|
|
312
|
+
sortBy: normalizeFilterValue(query['sortBy']),
|
|
313
|
+
titlePreference: ['name', 'nativeName', 'englishName'].includes(String(query['titlePreference']))
|
|
314
|
+
? query['titlePreference']
|
|
315
|
+
: 'name',
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
function matchesLocalFilters(row, filters) {
|
|
319
|
+
if (filters.query) {
|
|
320
|
+
const queryWords = new Set(filters.query
|
|
321
|
+
.toLowerCase()
|
|
322
|
+
.split(/\s+/)
|
|
323
|
+
.filter((w) => w.length >= 2));
|
|
324
|
+
const rowTitle = (row.englishName || row.name || row.nativeName || '').toLowerCase();
|
|
325
|
+
const titleWords = rowTitle.split(/\s+/);
|
|
326
|
+
const overlap = titleWords.filter((w) => queryWords.has(w)).length;
|
|
327
|
+
if (overlap < queryWords.size)
|
|
328
|
+
return false;
|
|
329
|
+
}
|
|
330
|
+
return true;
|
|
331
|
+
}
|
|
332
|
+
function sortFilteredRows(rows, filters) {
|
|
333
|
+
const getSortTitle = (row) => {
|
|
334
|
+
const preferredTitle = filters.titlePreference ? row[filters.titlePreference] : undefined;
|
|
335
|
+
return preferredTitle || row.name || '';
|
|
336
|
+
};
|
|
337
|
+
if (filters.sortBy === 'name_asc') {
|
|
338
|
+
return [...rows].sort((a, b) => getSortTitle(a).localeCompare(getSortTitle(b)));
|
|
339
|
+
}
|
|
340
|
+
if (filters.sortBy === 'name_desc') {
|
|
341
|
+
return [...rows].sort((a, b) => getSortTitle(b).localeCompare(getSortTitle(a)));
|
|
342
|
+
}
|
|
343
|
+
return rows;
|
|
344
|
+
}
|
|
345
|
+
async function getAnilistSeasonYearMatches(season, year) {
|
|
346
|
+
const matched = new Map();
|
|
347
|
+
if (!year || year === 'ALL') {
|
|
348
|
+
return matched;
|
|
349
|
+
}
|
|
350
|
+
const seasonYear = parseInt(year);
|
|
351
|
+
if (Number.isNaN(seasonYear))
|
|
352
|
+
return matched;
|
|
353
|
+
const perPage = 50;
|
|
354
|
+
let page = 1;
|
|
355
|
+
while (true) {
|
|
356
|
+
const searchVars = {
|
|
357
|
+
seasonYear,
|
|
358
|
+
page,
|
|
359
|
+
perPage,
|
|
360
|
+
};
|
|
361
|
+
if (season && season !== 'ALL') {
|
|
362
|
+
searchVars.season = season.toUpperCase();
|
|
363
|
+
}
|
|
364
|
+
const results = await searchAnilist(searchVars);
|
|
365
|
+
for (const show of results) {
|
|
366
|
+
if (show.anilistId) {
|
|
367
|
+
matched.set(show.anilistId, { title: show.names || {} });
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
if (results.length < perPage)
|
|
371
|
+
break;
|
|
372
|
+
page++;
|
|
373
|
+
if (page > 10)
|
|
374
|
+
break;
|
|
375
|
+
}
|
|
376
|
+
return matched;
|
|
377
|
+
}
|
|
378
|
+
function rowMatchesAnilistSeasonYear(row, anilistMatches) {
|
|
379
|
+
if (anilistMatches.size === 0)
|
|
380
|
+
return true;
|
|
381
|
+
if (/^\d+$/.test(row.id) && anilistMatches.has(parseInt(row.id))) {
|
|
382
|
+
return true;
|
|
383
|
+
}
|
|
384
|
+
const rowTitle = (row.englishName || row.name || row.nativeName || '').toLowerCase();
|
|
385
|
+
if (!rowTitle)
|
|
386
|
+
return false;
|
|
387
|
+
const rowWords = new Set(rowTitle.split(/\s+/).filter((w) => w.length >= 2));
|
|
388
|
+
if (rowWords.size === 0)
|
|
389
|
+
return false;
|
|
390
|
+
for (const [, media] of anilistMatches) {
|
|
391
|
+
const titles = [media.title?.romaji, media.title?.english, media.title?.native].filter(Boolean);
|
|
392
|
+
for (const title of titles) {
|
|
393
|
+
const titleWords = new Set(title
|
|
394
|
+
.toLowerCase()
|
|
395
|
+
.split(/\s+/)
|
|
396
|
+
.filter((w) => w.length >= 2));
|
|
397
|
+
const overlap = [...rowWords].filter((w) => titleWords.has(w)).length;
|
|
398
|
+
const minLen = Math.min(rowWords.size, titleWords.size);
|
|
399
|
+
if (minLen >= 2 && overlap / minLen >= 0.7)
|
|
400
|
+
return true;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
return false;
|
|
404
|
+
}
|
|
405
|
+
async function filterWatchlistRows(rows, filters, db) {
|
|
406
|
+
let filtered = rows.filter((row) => matchesLocalFilters(row, filters));
|
|
407
|
+
if (filters.type === 'ADULT' && db && filtered.length > 0) {
|
|
408
|
+
const ids = filtered.map((r) => r.id);
|
|
409
|
+
const placeholders = ids.map(() => '?').join(',');
|
|
410
|
+
const adultRows = await dbAll(db, `SELECT id FROM shows_meta WHERE id IN (${placeholders}) AND isAdult = 1`, ids);
|
|
411
|
+
const adultSet = new Set(adultRows.map((r) => r.id));
|
|
412
|
+
filtered = filtered.filter((row) => adultSet.has(row.id));
|
|
413
|
+
}
|
|
414
|
+
else if (filters.type && filters.type !== 'ALL' && filters.type !== 'ADULT') {
|
|
415
|
+
filtered = filtered.filter((row) => row.type === filters.type);
|
|
416
|
+
}
|
|
417
|
+
if ((filters.genres || filters.excludeGenres) && db) {
|
|
418
|
+
const ids = filtered.map((r) => r.id);
|
|
419
|
+
const placeholders = ids.map(() => '?').join(',');
|
|
420
|
+
const genreRows = await dbAll(db, `SELECT id, genres FROM shows_meta WHERE id IN (${placeholders})`, ids);
|
|
421
|
+
const idToGenres = new Map(genreRows.map((r) => [r.id, r.genres ? JSON.parse(r.genres) : []]));
|
|
422
|
+
const includeList = filters.genres
|
|
423
|
+
?.split(',')
|
|
424
|
+
.map((g) => g.trim())
|
|
425
|
+
.filter(Boolean) || [];
|
|
426
|
+
const excludeList = filters.excludeGenres
|
|
427
|
+
?.split(',')
|
|
428
|
+
.map((g) => g.trim())
|
|
429
|
+
.filter(Boolean) || [];
|
|
430
|
+
filtered = filtered.filter((row) => {
|
|
431
|
+
const rowGenres = idToGenres.get(row.id) || [];
|
|
432
|
+
if (includeList.length && !includeList.every((g) => rowGenres.includes(g)))
|
|
433
|
+
return false;
|
|
434
|
+
if (excludeList.length && excludeList.some((g) => rowGenres.includes(g)))
|
|
435
|
+
return false;
|
|
436
|
+
return true;
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
if (((filters.season && filters.season !== 'ALL') || (filters.year && filters.year !== 'ALL')) &&
|
|
440
|
+
filtered.length > 0) {
|
|
441
|
+
const anilistMatches = await getAnilistSeasonYearMatches(filters.season, filters.year);
|
|
442
|
+
if (anilistMatches.size > 0) {
|
|
443
|
+
filtered = filtered.filter((row) => rowMatchesAnilistSeasonYear(row, anilistMatches));
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
return sortFilteredRows(filtered, filters);
|
|
447
|
+
}
|
|
448
|
+
async function backfillMissingPosters(db, rows) {
|
|
449
|
+
const missing = rows.filter((r) => !r.thumbnail || r.thumbnail.trim() === '');
|
|
450
|
+
if (missing.length === 0)
|
|
451
|
+
return;
|
|
452
|
+
const slice = missing.slice(0, 10);
|
|
453
|
+
await Promise.allSettled(slice.map(async (row) => {
|
|
454
|
+
try {
|
|
455
|
+
const meta = await getShowMetaById(row.id);
|
|
456
|
+
const poster = meta?.thumbnail?.trim();
|
|
457
|
+
if (!poster)
|
|
458
|
+
return;
|
|
459
|
+
row.thumbnail = poster;
|
|
460
|
+
const dual = row;
|
|
461
|
+
if (dual._id !== undefined)
|
|
462
|
+
dual._id = row.id;
|
|
463
|
+
try {
|
|
464
|
+
await ShowsMetaRepository.upsert(db, {
|
|
465
|
+
id: row.id,
|
|
466
|
+
thumbnail: poster,
|
|
467
|
+
popularityScore: meta?.score ?? (meta?.averageScore != null ? meta.averageScore / 10 : undefined),
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
catch {
|
|
471
|
+
// ignore
|
|
472
|
+
}
|
|
473
|
+
try {
|
|
474
|
+
await WatchlistRepository.updateThumbnail(db, row.id, poster);
|
|
475
|
+
}
|
|
476
|
+
catch {
|
|
477
|
+
// ignore
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
catch {
|
|
481
|
+
// ignore
|
|
482
|
+
}
|
|
483
|
+
}));
|
|
484
|
+
db.scheduleSave();
|
|
485
|
+
await sweepOfflinePosters(db);
|
|
486
|
+
}
|
|
487
|
+
async function sweepOfflinePosters(db) {
|
|
488
|
+
try {
|
|
489
|
+
db.run(`UPDATE shows_meta SET name = (SELECT w.name FROM watchlist w WHERE w.id = shows_meta.id)
|
|
490
|
+
WHERE (name IS NULL OR TRIM(name) = '')
|
|
491
|
+
AND EXISTS (SELECT 1 FROM watchlist w WHERE w.id = shows_meta.id AND w.name IS NOT NULL AND TRIM(w.name) != '')`);
|
|
492
|
+
}
|
|
493
|
+
catch {
|
|
494
|
+
// ignore
|
|
495
|
+
}
|
|
496
|
+
const [wlMissing, metaMissing] = await Promise.all([
|
|
497
|
+
WatchlistRepository.getMissingThumbnails(db),
|
|
498
|
+
ShowsMetaRepository.getShowIdsMissingMeta(db),
|
|
499
|
+
]);
|
|
500
|
+
const ids = [...new Set([...wlMissing.map((r) => r.id), ...metaMissing.map((r) => r.id)])].slice(0, 500);
|
|
501
|
+
if (ids.length === 0)
|
|
502
|
+
return;
|
|
503
|
+
for (const id of ids) {
|
|
504
|
+
const poster = resolveOfflinePoster(id);
|
|
505
|
+
if (poster)
|
|
506
|
+
await applyPoster(db, id, poster);
|
|
507
|
+
}
|
|
508
|
+
db.scheduleSave();
|
|
509
|
+
}
|
|
510
|
+
function resolveOfflinePoster(id) {
|
|
511
|
+
if (/^\d+$/.test(id)) {
|
|
512
|
+
return offlineDb.getByAnilistId(Number(id))?.thumbnail?.trim() || null;
|
|
513
|
+
}
|
|
514
|
+
const mal = /^mal-(\d+)$/i.exec(id)?.[1];
|
|
515
|
+
if (mal)
|
|
516
|
+
return offlineDb.getByMalId(Number(mal))?.thumbnail?.trim() || null;
|
|
517
|
+
return null;
|
|
518
|
+
}
|
|
519
|
+
async function applyPoster(db, id, poster) {
|
|
520
|
+
try {
|
|
521
|
+
await ShowsMetaRepository.upsert(db, { id, thumbnail: poster });
|
|
522
|
+
}
|
|
523
|
+
catch {
|
|
524
|
+
// ignore
|
|
525
|
+
}
|
|
526
|
+
try {
|
|
527
|
+
await WatchlistRepository.updateThumbnail(db, id, poster);
|
|
528
|
+
}
|
|
529
|
+
catch {
|
|
530
|
+
// ignore
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
async function getContinueWatchingData(db, limit) {
|
|
534
|
+
const rows = await WatchedEpisodesRepository.getContinueWatching(db, limit);
|
|
535
|
+
const ignoreAdultRow = await SettingsRepository.getByKey(db, 'ignoreAdultContent');
|
|
536
|
+
const ignoreAdult = ignoreAdultRow ? ignoreAdultRow.value !== 'false' : true;
|
|
537
|
+
const watchlistOnlyRow = await SettingsRepository.getByKey(db, 'cwWatchlistOnly');
|
|
538
|
+
const watchlistOnly = watchlistOnlyRow
|
|
539
|
+
? watchlistOnlyRow.value === 'true' || watchlistOnlyRow.value === '1'
|
|
540
|
+
: false;
|
|
541
|
+
const enrichedRows = rows
|
|
542
|
+
.filter((show) => {
|
|
543
|
+
if (ignoreAdult && show.isAdult)
|
|
544
|
+
return false;
|
|
545
|
+
if (watchlistOnly && show.watchlistStatus !== 'Watching')
|
|
546
|
+
return false;
|
|
547
|
+
return true;
|
|
548
|
+
})
|
|
549
|
+
.map((show) => ({
|
|
550
|
+
...show,
|
|
551
|
+
episodeCount: show.episodeCount,
|
|
552
|
+
type: show.type || show.smType,
|
|
553
|
+
thumbnail: show.thumbnail ?? '',
|
|
554
|
+
}));
|
|
555
|
+
await backfillMissingPosters(db, enrichedRows);
|
|
556
|
+
return enrichedRows;
|
|
557
|
+
}
|
|
558
|
+
async function resolveAvailableEpisodes(db, showId) {
|
|
559
|
+
const episodeData = await getAnilistEpisodes(showId);
|
|
560
|
+
const episodes = Array.isArray(episodeData) && episodeData.length
|
|
561
|
+
? [...episodeData].sort((a, b) => parseFloat(a) - parseFloat(b))
|
|
562
|
+
: [];
|
|
563
|
+
return episodes;
|
|
564
|
+
}
|