Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions server/lib/OPodSync/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion server/lib/OPodSync/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class DB extends \SQLite3
{
const VERSION = 20251211;
const VERSION = 20260307;

protected $statements = [];

Expand Down
4 changes: 2 additions & 2 deletions server/sql/migration_20240428.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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);
CREATE INDEX episodes_actions_link ON episodes_actions (episode);
8 changes: 8 additions & 0 deletions server/sql/migration_20260307.sql
Original file line number Diff line number Diff line change
@@ -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;
21 changes: 20 additions & 1 deletion test/tests/13_gpodder_episodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}