diff --git a/public/js/pages/WatchPage.js b/public/js/pages/WatchPage.js index 10af703..5120cd6 100644 --- a/public/js/pages/WatchPage.js +++ b/public/js/pages/WatchPage.js @@ -1387,6 +1387,7 @@ class WatchPage { await window.API.request('POST', '/history', { id: this.content.id, type: this.content.type === 'movie' ? 'movie' : 'episode', + sourceId: this.content.sourceId, // Add sourceId at top level for resume progress, duration, data diff --git a/server/db/sqlite.js b/server/db/sqlite.js index fea2d69..c3a76e4 100644 --- a/server/db/sqlite.js +++ b/server/db/sqlite.js @@ -122,6 +122,7 @@ function initSchema() { CREATE TABLE IF NOT EXISTS watch_history ( id TEXT PRIMARY KEY, -- Composite key: user_id:item_id user_id INTEGER NOT NULL, + source_id INTEGER, -- Source ID for Xtream/M3U item_type TEXT NOT NULL, -- 'movie', 'episode' item_id TEXT NOT NULL, -- The original item ID (stream_id or composite) parent_id TEXT, -- For episodes (series ID) @@ -134,6 +135,14 @@ function initSchema() { CREATE INDEX IF NOT EXISTS idx_history_user_item ON watch_history(user_id, item_id); `); + // Migration: Add source_id column if missing (for existing databases) + try { + db.exec(`ALTER TABLE watch_history ADD COLUMN source_id INTEGER`); + console.log('[SQLite] Added source_id column to watch_history'); + } catch (e) { + // Column already exists, ignore + } + console.log('[SQLite] Schema initialized'); } diff --git a/server/routes/history.js b/server/routes/history.js index 20e7f88..1ff91d8 100644 --- a/server/routes/history.js +++ b/server/routes/history.js @@ -43,7 +43,7 @@ router.post('/', (req, res) => { try { const db = getDb(); const userId = req.user.id; - const { id, type, parentId, progress, duration, data } = req.body; + const { id, type, parentId, progress, duration, data, sourceId } = req.body; if (!id || !type) { return res.status(400).json({ error: 'Missing required fields (id, type)' }); @@ -53,9 +53,10 @@ router.post('/', (req, res) => { const timestamp = Date.now(); const stmt = db.prepare(` - INSERT INTO watch_history (id, user_id, item_type, item_id, parent_id, progress, duration, updated_at, data) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) + INSERT INTO watch_history (id, user_id, source_id, item_type, item_id, parent_id, progress, duration, updated_at, data) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT(id) DO UPDATE SET + source_id = excluded.source_id, progress = excluded.progress, duration = excluded.duration, updated_at = excluded.updated_at, @@ -65,6 +66,7 @@ router.post('/', (req, res) => { stmt.run( compositeId, userId, + sourceId || null, type, id.toString(), parentId ? parentId.toString() : null,