Skip to content

Commit 9fee1ca

Browse files
authored
Merge pull request #587 from arabcoders/dev
Implements progress sync for all users via state:sync
2 parents b347cc8 + a65f8db commit 9fee1ca

File tree

13 files changed

+432
-425
lines changed

13 files changed

+432
-425
lines changed

composer.lock

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/servers.spec.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,6 @@
161161
'visible' => false,
162162
'description' => 'The http version to use when making requests to the backend.',
163163
],
164-
[
165-
'key' => 'options.use_old_progress_endpoint',
166-
'type' => 'bool',
167-
'visible' => false,
168-
'description' => 'Whether to use the old progress endpoint for plex progress sync.',
169-
],
170164
[
171165
'key' => 'options.is_limited_token',
172166
'type' => 'bool',

frontend/yarn.lock

Lines changed: 175 additions & 175 deletions
Large diffs are not rendered by default.

src/Backends/Plex/Action/Progress.php

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -258,26 +258,17 @@ private function action(
258258
}
259259

260260
try {
261-
if (true === (bool)ag($context->options, Options::PLEX_USE_OLD_PROGRESS_ENDPOINT, false)) {
262-
$url = $context->backendUrl->withPath('/:/progress/')->withQuery(http_build_query([
263-
'key' => $logContext['remote']['id'],
264-
'identifier' => 'com.plexapp.plugins.library',
265-
'state' => 'stopped',
266-
'time' => $entity->getPlayProgress(),
267-
]));
268-
} else {
269-
// -- it seems /:/timeline/ allow us to update external user progress, while /:/progress/ does not.
270-
$url = $context->backendUrl->withPath('/:/timeline/')->withQuery(http_build_query([
271-
'ratingKey' => $logContext['remote']['id'],
272-
'key' => '/library/metadata/' . $logContext['remote']['id'],
273-
'identifier' => 'com.plexapp.plugins.library',
274-
'state' => 'stopped',
275-
'time' => $entity->getPlayProgress(),
276-
// -- Without duration & client identifier plex ignore watch progress update.
277-
'duration' => ag($remoteData, 'duration', 0),
278-
'X-Plex-Client-Identifier' => md5('WatchState/' . getAppVersion())
279-
]));
280-
}
261+
// -- it seems /:/timeline/ allow us to update external user progress, while /:/progress/ does not.
262+
$url = $context->backendUrl->withPath('/:/timeline/')->withQuery(http_build_query([
263+
'ratingKey' => $logContext['remote']['id'],
264+
'key' => '/library/metadata/' . $logContext['remote']['id'],
265+
'identifier' => 'com.plexapp.plugins.library',
266+
'state' => 'stopped',
267+
'time' => $entity->getPlayProgress(),
268+
// -- Without duration & client identifier plex ignore watch progress update.
269+
'duration' => ag($remoteData, 'duration', 0),
270+
'X-Plex-Client-Identifier' => md5('WatchState/' . getAppVersion())
271+
]));
281272

282273
$logContext['remote']['url'] = (string)$url;
283274

src/Backends/Plex/PlexClient.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -669,10 +669,6 @@ public function fromRequest(array $config, iRequest $request): array
669669
$config = ag_set($config, 'options.' . Options::PLEX_USER_PIN, $val);
670670
}
671671

672-
if (null !== ($val = $params->get('options.' . Options::PLEX_USE_OLD_PROGRESS_ENDPOINT))) {
673-
$config = ag_set($config, 'options.' . Options::PLEX_USE_OLD_PROGRESS_ENDPOINT, (bool)$val);
674-
}
675-
676672
if (null !== ($userId = ag($config, 'user')) && !is_int($userId)) {
677673
$config = ag_set($config, 'user', (int)$userId);
678674
}

src/Commands/State/SyncCommand.php

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,9 @@ protected function process(iInput $input, iOutput $output): int
292292

293293
unset($backend);
294294

295-
$this->logger->notice("SYSTEM: Getting users list from '{backends}'.", [
295+
$this->logger->notice(
296+
"SYSTEM: Getting users list from '{backends}'.",
297+
[
296298
'backends' => join(', ', array_map(fn($backend) => $backend['name'], $backends))
297299
]
298300
);
@@ -309,6 +311,7 @@ protected function process(iInput $input, iOutput $output): int
309311

310312
try {
311313
foreach ($client->getUsersList(['tokens' => true]) as $user) {
314+
/** @var array $info */
312315
$info = $backend;
313316
$info['token'] = ag($user, 'token', ag($backend, 'token'));
314317
$info['user'] = ag($user, 'id', ag($info, 'user'));
@@ -370,9 +373,12 @@ protected function process(iInput $input, iOutput $output): int
370373
'memory' => getMemoryUsage(),
371374
]);
372375
$perUserCache = perUserCacheAdapter($userName);
373-
$perUserMapper = perUserMapper($this->mapper, $userName)
376+
$perUserMapper = $this->mapper->withDB(perUserDb($userName))
374377
->withCache($perUserCache)
375-
->withLogger($this->logger)->loadData();
378+
->withLogger($this->logger)
379+
->withOptions(array_replace_recursive($this->mapper->getOptions(), [Options::ALT_NAME => $userName]))
380+
->loadData();
381+
376382
$this->logger->info("SYSTEM: loading of '{user}' mapper data completed using '{memory}' of memory.", [
377383
'user' => $userName,
378384
'memory' => getMemoryUsage(),
@@ -432,21 +438,20 @@ protected function process(iInput $input, iOutput $output): int
432438

433439
foreach ($changes as $b => $changed) {
434440
$count = count($changed);
435-
$this->logger->notice("SYSTEM: Changes detected for '{name}: {backend}' are '{changes}'.", [
436-
'name' => $displayName,
437-
'backend' => $b,
438-
'changes' => $count,
439-
'items' => array_map(
440-
fn(iState $i) => [
441-
'title' => $i->getName(),
442-
'state' => $i->isWatched() ? 'played' : 'unplayed',
443-
'meta' => $i->isSynced(array_keys($list)),
444-
],
445-
$changed
446-
)
447-
]);
448-
449441
if ($count >= 1) {
442+
$this->logger->notice("SYSTEM: Changes detected for '{name}: {backend}' are '{changes}'.", [
443+
'name' => $displayName,
444+
'backend' => $b,
445+
'changes' => $count,
446+
'items' => array_map(
447+
fn(iState $i) => [
448+
'title' => $i->getName(),
449+
'state' => $i->isWatched() ? 'played' : 'unplayed',
450+
'meta' => $i->isSynced(array_keys($list)),
451+
],
452+
$changed
453+
)
454+
]);
450455
/** @var iClient $client */
451456
$client = $list[$b]['class'];
452457
$client->updateState($changed, $this->queue);
@@ -470,13 +475,13 @@ protected function process(iInput $input, iOutput $output): int
470475
],
471476
]);
472477

473-
// -- commit changes
478+
// -- Release memory.
474479
if (false === $input->getOption('dry-run')) {
475480
$perUserMapper->commit();
481+
} else {
482+
$perUserMapper->reset();
476483
}
477484

478-
// -- Release memory.
479-
$perUserMapper->reset();
480485
$this->logger->info("SYSTEM: Memory usage after reset '{memory}'.", [
481486
'memory' => getMemoryUsage(),
482487
]);
@@ -791,9 +796,7 @@ private function generate_users_list(array $users, bool $includeMainUser, array
791796
}
792797

793798
// Ensure $matchedUser['client_data']['options'] is an array
794-
if (!isset($matchedUser['client_data']['options']) || !is_array(
795-
$matchedUser['client_data']['options']
796-
)) {
799+
if (!isset($matchedUser['client_data']['options']) || !is_array($matchedUser['client_data']['options'])) {
797800
$matchedUser['client_data']['options'] = [];
798801
}
799802

0 commit comments

Comments
 (0)