diff --git a/server/lib/OPodSync/API.php b/server/lib/OPodSync/API.php index 4bc4f4b..a494a4a 100644 --- a/server/lib/OPodSync/API.php +++ b/server/lib/OPodSync/API.php @@ -568,7 +568,7 @@ public function episodes(): array $db->exec('BEGIN;'); $timestamp = time(); - $st = $db->prepare('INSERT INTO episodes_actions (user, subscription, url, changed, action, data) VALUES (:user, :subscription, :url, :changed, :action, :data);'); + $st = $db->prepare('INSERT INTO episodes_actions (user, subscription, device, url, changed, action, data) VALUES (:user, :subscription, :device, :url, :changed, :action, :data);'); foreach ($input as $action) { if (!isset($action->podcast, $action->action, $action->episode)) { @@ -593,13 +593,19 @@ public function episodes(): array $changed = null; } + $episode = $action->episode; + $action_name = strtolower($action->action); + unset($action->action, $action->episode, $action->podcast); + $data = json_encode($action, JSON_THROW_ON_ERROR); + $device = $db->firstColumn('SELECT id FROM devices WHERE deviceid = json_extract(?, \'$.device\') AND user = ?;', $data, $this->user->id); + $st->bindValue(':user', $this->user->id); $st->bindValue(':subscription', $id); - $st->bindValue(':url', $action->episode); + $st->bindValue(':device', $device); + $st->bindValue(':url', $episode); $st->bindValue(':changed', $changed ?? $timestamp); - $st->bindValue(':action', strtolower($action->action)); - unset($action->action, $action->episode, $action->podcast); - $st->bindValue(':data', json_encode($action, JSON_THROW_ON_ERROR)); + $st->bindValue(':action', $action_name); + $st->bindValue(':data', $data); $st->execute(); $st->reset(); $st->clear(); diff --git a/server/lib/OPodSync/DB.php b/server/lib/OPodSync/DB.php index 2e495dd..352abc6 100644 --- a/server/lib/OPodSync/DB.php +++ b/server/lib/OPodSync/DB.php @@ -4,7 +4,7 @@ class DB extends \SQLite3 { - const VERSION = 20251211; + const VERSION = 20260307; protected $statements = []; diff --git a/server/sql/migration_20240428.sql b/server/sql/migration_20240428.sql index e518dc6..2e34184 100644 --- a/server/sql/migration_20240428.sql +++ b/server/sql/migration_20240428.sql @@ -81,11 +81,11 @@ INSERT INTO episodes_actions SELECT a.id, a.user, a.subscription, e.id, d.id, a.url, a.changed, a.action, a.data FROM episodes_actions_old a LEFT JOIN episodes e ON e.media_url = a.url - LEFT JOIN devices d ON d.deviceid = json_extract(a.data, '$.device'); + LEFT JOIN devices d ON d.deviceid = json_extract(a.data, '$.device') AND d.user = a.user; DROP TABLE episodes_actions_old; DROP TABLE devices_old; DROP TABLE subscriptions_old; CREATE INDEX subscription_feed ON subscriptions (feed); -CREATE INDEX episodes_actions_link ON episodes_actions (episode); \ No newline at end of file +CREATE INDEX episodes_actions_link ON episodes_actions (episode); diff --git a/server/sql/migration_20260307.sql b/server/sql/migration_20260307.sql new file mode 100644 index 0000000..661f273 --- /dev/null +++ b/server/sql/migration_20260307.sql @@ -0,0 +1,8 @@ +UPDATE episodes_actions +SET device = ( + SELECT d.id + FROM devices d + WHERE d.deviceid = json_extract(episodes_actions.data, '$.device') + AND d.user = episodes_actions.user +) +WHERE json_extract(data, '$.device') IS NOT NULL; diff --git a/test/tests/13_gpodder_episodes.php b/test/tests/13_gpodder_episodes.php index ff1db4d..ebb9a80 100644 --- a/test/tests/13_gpodder_episodes.php +++ b/test/tests/13_gpodder_episodes.php @@ -11,7 +11,26 @@ $r = $http->GET('/api/2/episodes/demo.json'); Test::equals(200, $r->status, $r); -$r = json_decode($r); +$r = json_decode($r->body); Test::assert(is_object($r)); Test::assert(isset($r->actions)); Test::assert(count($r->actions) === 2); + +$db = new SQLite3($data_root . '/data.sqlite'); +$res = $db->query('SELECT a.device, d.deviceid, a.user, d.user AS device_user + FROM episodes_actions a + LEFT JOIN devices d ON d.id = a.device + ORDER BY a.id;'); +$rows = []; + +while ($row = $res->fetchArray(SQLITE3_ASSOC)) { + $rows[] = $row; +} + +Test::assert(count($rows) === 2); + +foreach ($rows as $row) { + Test::assert(!empty($row['device'])); + Test::equals('test-device', $row['deviceid']); + Test::equals($row['user'], $row['device_user']); +}