feat(epg): order EPG sources by priority (#24)
Add persistent EPG priority ordering, source reorder validation, and priority-aware EPG matching for live channels. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
20
server/db.js
20
server/db.js
@ -2,6 +2,7 @@ const fs = require('fs/promises');
|
||||
const path = require('path');
|
||||
const { existsSync, mkdirSync } = require('fs');
|
||||
const { ensureBuiltinSources } = require('./services/builtinEpgRegistry');
|
||||
const { applyEpgOrder, nextEpgPriority, sortEpgSources } = require('./services/epgSourceOrdering');
|
||||
|
||||
// Ensure data directory exists (sync is fine for startup)
|
||||
const dataDir = path.join(__dirname, '..', 'data');
|
||||
@ -18,7 +19,8 @@ async function loadDb() {
|
||||
try {
|
||||
const fileContent = await fs.readFile(dbPath, 'utf-8');
|
||||
const data = JSON.parse(fileContent);
|
||||
return ensureBuiltinSources({
|
||||
const original = JSON.stringify(data);
|
||||
const migrated = ensureBuiltinSources({
|
||||
sources: data.sources || [],
|
||||
hiddenItems: data.hiddenItems || [],
|
||||
favorites: data.favorites || [],
|
||||
@ -26,6 +28,8 @@ async function loadDb() {
|
||||
users: data.users || [],
|
||||
nextId: data.nextId || 1
|
||||
});
|
||||
if (original !== JSON.stringify(migrated)) await saveDb(migrated);
|
||||
return migrated;
|
||||
} catch (error) {
|
||||
if (error.code === 'ENOENT') {
|
||||
// File doesn't exist, return default
|
||||
@ -137,7 +141,8 @@ async function saveDb(data) {
|
||||
const sources = {
|
||||
async getAll() {
|
||||
const db = await loadDb();
|
||||
return db.sources;
|
||||
const epg = sortEpgSources(db.sources);
|
||||
return [...db.sources.filter(source => source.type !== 'epg'), ...epg];
|
||||
},
|
||||
|
||||
async getById(id) {
|
||||
@ -147,7 +152,8 @@ const sources = {
|
||||
|
||||
async getByType(type) {
|
||||
const db = await loadDb();
|
||||
return db.sources.filter(s => s.type === type && s.enabled);
|
||||
const matching = db.sources.filter(s => s.type === type && s.enabled);
|
||||
return type === 'epg' ? sortEpgSources(matching) : matching;
|
||||
},
|
||||
|
||||
async create(source) {
|
||||
@ -155,6 +161,7 @@ const sources = {
|
||||
const newSource = {
|
||||
id: db.nextId++,
|
||||
...source,
|
||||
...(source.type === 'epg' ? { epgPriority: nextEpgPriority(db.sources) } : {}),
|
||||
enabled: true,
|
||||
created_at: new Date().toISOString(),
|
||||
updated_at: new Date().toISOString()
|
||||
@ -203,6 +210,13 @@ const sources = {
|
||||
await saveDb(db);
|
||||
}
|
||||
return source;
|
||||
},
|
||||
|
||||
async reorderEpg(orderedIds) {
|
||||
const db = await loadDb();
|
||||
const ordered = applyEpgOrder(db.sources, orderedIds);
|
||||
await saveDb(db);
|
||||
return ordered;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user