一分钟搭建影视站,支持Docker等部署方式
fix(sources): 订阅同步保留勾选状态、消除跨订阅标记污染并先去重后截断
bestZweide6fa062eeff
diff --git a/package.json b/package.jsonindex 1314ee4..fa6ed57 100644--- a/package.json+++ b/package.json@@ -1,6 +1,6 @@{"name": "libretv",- "version": "2.5.0",+ "version": "2.5.1","private": true,"description": "免费在线视频搜索与观看平台(Next.js 重构版)","license": "AGPL-3.0-or-later",diff --git a/src/components/source-manager.tsx b/src/components/source-manager.tsxindex aee8613..2ba7f66 100644--- a/src/components/source-manager.tsx+++ b/src/components/source-manager.tsx@@ -367,8 +367,16 @@ function SourceSubscriptions() {}const vodCount = store.applySubscriptionSources(url, sources);const liveCount = store.applySubscriptionLive(url, liveSources);- // 记录直播源同步时间;未实际导入的源在 store 中不存在,写入会被忽略- for (const s of liveSources) store.markLiveSynced(s.url, s.name, s.epg);+ // 仅对本订阅实际导入的直播源记录同步时间:+ // 被其他订阅/手动源占用而未导入的 M3U,其 name/epg/lastSync 不得被本订阅覆盖+ const importedLiveUrls = new Set(+ useAppStore.getState().liveSubscriptions+ .filter((s) => s.fromSubscription === url)+ .map((s) => s.url)+ );+ for (const s of liveSources) {+ if (importedLiveUrls.has(s.url)) store.markLiveSynced(s.url, s.name, s.epg);+ }store.addSubscription(url, name);store.markSubscriptionSynced(url, name);toast(`已同步 ${vodCount} 个点播源、${liveCount} 个直播源`, 'success');diff --git a/src/lib/source-list.test.ts b/src/lib/source-list.test.tsindex 7e4a32d..5f1fb6e 100644--- a/src/lib/source-list.test.ts+++ b/src/lib/source-list.test.ts@@ -119,6 +119,18 @@ describe('parseSourceListPayload', () => {expect(result.liveSources).toHaveLength(MAX_LIVE_SOURCES);});+ it('截断前先去重:前段重复不挤占上限名额', () => {+ const sources = [+ ...Array.from({ length: 60 }, () => ({ url: 'https://dup.example.com/vod' })),+ ...Array.from({ length: MAX_VOD_SOURCES }, (_, i) => ({ url: `https://vod${i}.example.com/vod` })),+ ];++ const result = parseSourceListPayload({ sources });++ expect(result.sources).toHaveLength(MAX_VOD_SOURCES);+ expect(result.sources[0].url).toBe('https://dup.example.com/vod');+ });+it('既无点播也无直播时抛错', () => {expect(() => parseSourceListPayload({ name: '空的' })).toThrow(/格式不正确/);expect(() => parseSourceListPayload({ sources: [] })).toThrow(/格式不正确/);diff --git a/src/lib/source-list.ts b/src/lib/source-list.tsindex 0483f19..fd3ba45 100644--- a/src/lib/source-list.ts+++ b/src/lib/source-list.ts@@ -66,35 +66,36 @@ export function parseSourceListPayload(json: unknown): SourceListPayload {throw new Error('订阅内容格式不正确(缺少 sources / liveSources 数组)');}+ // 先去重再截断:上限按去重后的有效条目计,前段重复不挤占名额const seenVod = new Set<string>();- const sources = rawVod.slice(0, MAX_VOD_SOURCES).flatMap((s) => {+ const sources: SourceListPayload['sources'] = [];+ for (const s of rawVod) {+ if (sources.length >= MAX_VOD_SOURCES) break;const url = normalizeUrl(s?.url, true);- if (!url || seenVod.has(url)) return [];+ if (!url || seenVod.has(url)) continue;seenVod.add(url);- return [- {- name: optionalString(s?.name) || hostnameOf(url),- url,- detail: optionalString(s?.detail),- isAdult: s?.isAdult === true,- },- ];- });+ sources.push({+ name: optionalString(s?.name) || hostnameOf(url),+ url,+ detail: optionalString(s?.detail),+ isAdult: s?.isAdult === true,+ });+ }const seenLive = new Set<string>();- const liveSources = rawLive.slice(0, MAX_LIVE_SOURCES).flatMap((s) => {+ const liveSources: SourceListPayload['liveSources'] = [];+ for (const s of rawLive) {+ if (liveSources.length >= MAX_LIVE_SOURCES) break;const url = normalizeUrl(s?.url, false);- if (!url || seenLive.has(url)) return [];+ if (!url || seenLive.has(url)) continue;seenLive.add(url);const epg = normalizeUrl(s?.epg, false);- return [- {- name: optionalString(s?.name) || hostnameOf(url),- url,- epg,- },- ];- });+ liveSources.push({+ name: optionalString(s?.name) || hostnameOf(url),+ url,+ epg,+ });+ }const name = asObject ? optionalString(asObject.name) : undefined;diff --git a/src/lib/store.ts b/src/lib/store.tsindex 00cbe0b..c06535e 100644--- a/src/lib/store.ts+++ b/src/lib/store.ts@@ -259,13 +259,24 @@ export const useAppStore = create<AppState>()(const keptCustom = get().customAPIs.filter((a) => !a.key.startsWith(prefix) && !subUrls.has(a.url.replace(/\/+$/, '')));+ const prevOwned = get().customAPIs.filter((a) => a.key.startsWith(prefix));+ // key 按序号重生成,勾选状态需按 url 对齐保留;用户停用的源不会被同步反复勾回+ const prevSelectedUrls = new Set(+ prevOwned+ .filter((a) => get().selectedKeys.includes(a.key))+ .map((a) => a.url.replace(/\/+$/, ''))+ );+ const prevUrlSet = new Set(prevOwned.map((a) => a.url.replace(/\/+$/, '')));const incoming: SourceConfig[] = list.map((s, i) => ({...s,key: `${prefix}_${i}`,}));- // 自动勾选(成人过滤开启时跳过成人源)+ // 新源自动勾选(成人过滤开启时跳过成人源),已有源维持原勾选状态const toSelect = incoming- .filter((s) => !s.isAdult || !get().yellowFilter)+ .filter((s) => {+ const u = s.url.replace(/\/+$/, '');+ return prevUrlSet.has(u) ? prevSelectedUrls.has(u) : !s.isAdult || !get().yellowFilter;+ }).map((s) => s.key);set({customAPIs: [...keptCustom, ...incoming],@@ -287,13 +298,16 @@ export const useAppStore = create<AppState>()(// 本次订阅里已消失的旧源:其最近观看记录一并清掉,收藏保留const stillPresent = new Set(list.map((s) => s.url));const droppedUrls = new Set([...ownedUrls].filter((u) => !stillPresent.has(u)));+ // 仅新导入的源自动启用;已有源维持用户的勾选状态(停用不会被同步反复勾回),+ // 因此只从勾选中移除本次已消失的源+ const freshUrls = incoming.filter((s) => !ownedUrls.has(s.url)).map((s) => s.url);set({liveSubscriptions: [...kept, ...incoming],liveSelectedUrls: [...new Set([- ...get().liveSelectedUrls.filter((u) => !ownedUrls.has(u)),- ...incoming.map((s) => s.url),+ ...get().liveSelectedUrls.filter((u) => !droppedUrls.has(u)),+ ...freshUrls,]),],liveRecent: get().liveRecent.filter((r) => !r.sourceUrl || !droppedUrls.has(r.sourceUrl)),