cacheWatchedService->fetchAllUniqueTraktIds($userId);
}
- public function fetchUserMovieHistoryByMovieId(string $clientId, TraktId $traktId) : User\Movie\History\DtoList
+ public function fetchUserMovieHistoryByMovieId(string $clientId, string $username, TraktId $traktId) : User\Movie\History\DtoList
{
- $responseData = $this->client->get($clientId, sprintf('/users/%s/history/movies/%d', $this->username, $traktId->asInt()));
+ $responseData = $this->client->get($clientId, sprintf('/users/%s/history/movies/%d', $username, $traktId->asInt()));
return User\Movie\History\DtoList::createFromArray($responseData);
}
- public function fetchUserMoviesRatings(string $clientId,) : User\Movie\Rating\DtoList
+ public function fetchUserMoviesRatings(string $clientId, string $username) : User\Movie\Rating\DtoList
{
- $responseData = $this->client->get($clientId, sprintf('/users/%s/ratings/movies', $this->username));
+ $responseData = $this->client->get($clientId, sprintf('/users/%s/ratings/movies', $username));
return User\Movie\Rating\DtoList::createFromArray($responseData);
}
- public function fetchUserMoviesWatched(string $clientId) : User\Movie\Watched\DtoList
+ public function fetchUserMoviesWatched(string $clientId, string $username) : User\Movie\Watched\DtoList
{
- $responseData = $this->client->get($clientId, sprintf('/users/%s/watched/movies', $this->username));
+ $responseData = $this->client->get($clientId, sprintf('/users/%s/watched/movies', $username));
return User\Movie\Watched\DtoList::createFromArray($responseData);
}
diff --git a/src/Application/Service/Trakt/Exception/TraktUserNameNotSet.php b/src/Application/Service/Trakt/Exception/TraktUserNameNotSet.php
new file mode 100644
index 00000000..7ca604fd
--- /dev/null
+++ b/src/Application/Service/Trakt/Exception/TraktUserNameNotSet.php
@@ -0,0 +1,7 @@
+traktApi->fetchUserMovieHistoryByMovieId($traktClientId, $traktId) as $movieHistoryEntry) {
+ foreach ($this->traktApi->fetchUserMovieHistoryByMovieId($traktClientId, $username, $traktId) as $movieHistoryEntry) {
$watchDate = Date::createFromDateTime($movieHistoryEntry->getWatchedAt());
$playsPerDates->incrementPlaysForDate($watchDate);
diff --git a/src/Application/Service/Trakt/SyncRatings.php b/src/Application/Service/Trakt/SyncRatings.php
index b688fc40..588c6075 100644
--- a/src/Application/Service/Trakt/SyncRatings.php
+++ b/src/Application/Service/Trakt/SyncRatings.php
@@ -5,6 +5,7 @@
use Movary\Api;
use Movary\Application;
use Movary\Application\Service\Trakt\Exception\TraktClientIdNotSet;
+use Movary\Application\Service\Trakt\Exception\TraktUserNameNotSet;
use Movary\ValueObject\PersonalRating;
class SyncRatings
@@ -25,7 +26,12 @@ public function execute(int $userId, bool $overwriteExistingData = false) : void
throw new TraktClientIdNotSet();
}
- $this->traktApiCacheUserMovieRatingService->set($userId, $this->traktApi->fetchUserMoviesRatings($traktClientId));
+ $traktUserName = $this->userApi->findTraktUserName($userId);
+ if ($traktUserName === null) {
+ throw new TraktUserNameNotSet();
+ }
+
+ $this->traktApiCacheUserMovieRatingService->set($userId, $this->traktApi->fetchUserMoviesRatings($traktClientId, $traktUserName));
foreach ($this->movieApi->fetchAll() as $movie) {
$traktId = $movie->getTraktId();
diff --git a/src/Application/Service/Trakt/SyncWatchedMovies.php b/src/Application/Service/Trakt/SyncWatchedMovies.php
index 07f18151..79c98ffc 100644
--- a/src/Application/Service/Trakt/SyncWatchedMovies.php
+++ b/src/Application/Service/Trakt/SyncWatchedMovies.php
@@ -6,6 +6,7 @@
use Movary\Api\Trakt\ValueObject\Movie\TraktId;
use Movary\Application;
use Movary\Application\Service\Trakt\Exception\TraktClientIdNotSet;
+use Movary\Application\Service\Trakt\Exception\TraktUserNameNotSet;
use Movary\ValueObject\Date;
use Psr\Log\LoggerInterface;
@@ -30,7 +31,12 @@ public function execute(int $userId, bool $overwriteExistingData = false, bool $
throw new TraktClientIdNotSet();
}
- $watchedMovies = $this->traktApi->fetchUserMoviesWatched($traktClientId);
+ $traktUserName = $this->userApi->findTraktUserName($userId);
+ if ($traktUserName === null) {
+ throw new TraktUserNameNotSet();
+ }
+
+ $watchedMovies = $this->traktApi->fetchUserMoviesWatched($traktClientId, $traktUserName);
foreach ($watchedMovies as $watchedMovie) {
$traktId = $watchedMovie->getMovie()->getTraktId();
@@ -41,7 +47,7 @@ public function execute(int $userId, bool $overwriteExistingData = false, bool $
continue;
}
- $this->syncMovieHistory($traktClientId, $userId, $traktId, $movie, $overwriteExistingData);
+ $this->syncMovieHistory($traktClientId, $traktUserName, $userId, $traktId, $movie, $overwriteExistingData);
$this->traktApiCacheUserMovieWatchedService->setOne($userId, $traktId, $watchedMovie->getLastUpdated());
}
@@ -95,9 +101,15 @@ private function isWatchedCacheUpToDate(int $userId, Api\Trakt\ValueObject\User\
return $cacheLastUpdated !== null && $watchedMovie->getLastUpdated()->isEqual($cacheLastUpdated) === true;
}
- private function syncMovieHistory(string $traktClientId, int $userId, TraktId $traktId, Application\Movie\Entity $movie, bool $overwriteExistingData) : void
- {
- $traktHistoryEntries = $this->playsPerDateFetcher->fetchTraktPlaysPerDate($traktClientId, $traktId);
+ private function syncMovieHistory(
+ string $traktClientId,
+ string $traktUserName,
+ int $userId,
+ TraktId $traktId,
+ Application\Movie\Entity $movie,
+ bool $overwriteExistingData
+ ) : void {
+ $traktHistoryEntries = $this->playsPerDateFetcher->fetchTraktPlaysPerDate($traktClientId, $traktUserName, $traktId);
foreach ($this->movieApi->fetchHistoryByMovieId($movie->getId(), $userId) as $localHistoryEntry) {
$localHistoryEntryDate = Date::createFromString($localHistoryEntry['watched_at']);
diff --git a/src/Application/User/Api.php b/src/Application/User/Api.php
index 2984c86f..ac68d7db 100644
--- a/src/Application/User/Api.php
+++ b/src/Application/User/Api.php
@@ -30,6 +30,11 @@ public function findTraktClientId(int $userId) : ?string
return $this->repository->findTraktClientId($userId);
}
+ public function findTraktUserName(int $userId) : ?string
+ {
+ return $this->repository->findTraktUserName($userId);
+ }
+
public function findUserIdByPlexWebhookId(string $webhookId) : ?int
{
return $this->repository->findUserIdByPlexWebhookId($webhookId);
@@ -59,4 +64,9 @@ public function updateTraktClientId(int $userId, ?string $traktClientId) : void
{
$this->repository->updateTraktClientId($userId, $traktClientId);
}
+
+ public function updateTraktUserName(int $userId, ?string $traktUserName) : void
+ {
+ $this->repository->updateTraktUserName($userId, $traktUserName);
+ }
}
diff --git a/src/Application/User/Repository.php b/src/Application/User/Repository.php
index 28cc0988..77f87bdd 100644
--- a/src/Application/User/Repository.php
+++ b/src/Application/User/Repository.php
@@ -78,6 +78,17 @@ public function findTraktClientId(int $userId) : ?string
return $plexWebhookId;
}
+ public function findTraktUserName(int $userId) : ?string
+ {
+ $plexWebhookId = $this->dbConnection->fetchOne('SELECT `trakt_user_name` FROM `user` WHERE `id` = ?', [$userId]);
+
+ if ($plexWebhookId === false) {
+ return null;
+ }
+
+ return $plexWebhookId;
+ }
+
public function findUserByEmail(string $email) : ?Entity
{
$data = $this->dbConnection->fetchAssociative('SELECT * FROM `user` WHERE `email` = ?', [$email]);
@@ -160,4 +171,17 @@ public function updateTraktClientId(int $userId, ?string $traktClientId) : void
]
);
}
+
+ public function updateTraktUserName(int $userId, ?string $traktUserName) : void
+ {
+ $this->dbConnection->update(
+ 'user',
+ [
+ 'trakt_user_name' => $traktUserName,
+ ],
+ [
+ 'id' => $userId,
+ ]
+ );
+ }
}
diff --git a/src/Command/ChangeUserTraktId.php b/src/Command/ChangeUserTraktClientId.php
similarity index 97%
rename from src/Command/ChangeUserTraktId.php
rename to src/Command/ChangeUserTraktClientId.php
index 739ea7fa..069eed1a 100644
--- a/src/Command/ChangeUserTraktId.php
+++ b/src/Command/ChangeUserTraktClientId.php
@@ -8,7 +8,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
-class ChangeUserTraktId extends Command
+class ChangeUserTraktClientId extends Command
{
protected static $defaultName = self::COMMAND_BASE_NAME . ':user:change-trakt-client-id';
diff --git a/src/Command/ChangeUserTraktUserName.php b/src/Command/ChangeUserTraktUserName.php
new file mode 100644
index 00000000..d209ba72
--- /dev/null
+++ b/src/Command/ChangeUserTraktUserName.php
@@ -0,0 +1,53 @@
+setDescription('Change user trakt client id.')
+ ->addArgument('userId', InputArgument::REQUIRED, 'ID of user')
+ ->addArgument('traktUserName', InputArgument::REQUIRED, 'New trakt username for user');
+ }
+
+ // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
+ protected function execute(InputInterface $input, OutputInterface $output) : int
+ {
+ $userId = (int)$input->getArgument('userId');
+ $traktUserName = $input->getArgument('traktUserName');
+
+ if (empty($traktUserName) === true) {
+ $traktUserName = null;
+ }
+
+ try {
+ $this->userApi->updateTraktUserName($userId, $traktUserName);
+ } catch (\Throwable $t) {
+ $this->logger->error('Could not change trakt username.', ['exception' => $t]);
+
+ $this->generateOutput($output, 'Could not update trakt username.');
+
+ return Command::FAILURE;
+ }
+
+ $this->generateOutput($output, 'Updated trakt username.');
+ return Command::SUCCESS;
+ }
+}
diff --git a/src/Command/SyncTrakt.php b/src/Command/SyncTrakt.php
index fb10b13c..ea69655e 100644
--- a/src/Command/SyncTrakt.php
+++ b/src/Command/SyncTrakt.php
@@ -3,6 +3,7 @@
namespace Movary\Command;
use Movary\Application\Service\Trakt\Exception\TraktClientIdNotSet;
+use Movary\Application\Service\Trakt\Exception\TraktUserNameNotSet;
use Movary\Application\Service\Trakt\SyncRatings;
use Movary\Application\Service\Trakt\SyncWatchedMovies;
use Psr\Log\LoggerInterface;
@@ -71,6 +72,10 @@ protected function execute(InputInterface $input, OutputInterface $output) : int
} catch (TraktClientIdNotSet $t) {
$this->generateOutput($output, 'ERROR: User as no trakt client id set.');
+ return Command::FAILURE;
+ } catch (TraktUserNameNotSet $t) {
+ $this->generateOutput($output, 'ERROR: User as no trakt user name set.');
+
return Command::FAILURE;
} catch (\Throwable $t) {
$this->generateOutput($output, 'ERROR: Could not complete trakt sync.');
diff --git a/src/Factory.php b/src/Factory.php
index 3b3fd653..2c1508b2 100644
--- a/src/Factory.php
+++ b/src/Factory.php
@@ -108,11 +108,10 @@ public static function createTmdbApiClient(ContainerInterface $container, Config
);
}
- public static function createTraktApi(ContainerInterface $container, Config $config) : Trakt\Api
+ public static function createTraktApi(ContainerInterface $container) : Trakt\Api
{
return new Trakt\Api(
$container->get(Trakt\Client::class),
- $config->getAsString('TRAKT_USERNAME'),
$container->get(Watched\Service::class),
);
}
diff --git a/src/HttpController/SettingsController.php b/src/HttpController/SettingsController.php
index 6dccd492..d71beb62 100644
--- a/src/HttpController/SettingsController.php
+++ b/src/HttpController/SettingsController.php
@@ -35,6 +35,7 @@ public function render() : Response
$this->twig->render('page/settings.html.twig', [
'plexWebhookUrl' => $this->userApi->findPlexWebhookId($userId) ?? '-',
'traktClientId' => $this->userApi->findTraktClientId($userId),
+ 'traktUserName' => $this->userApi->findTraktUserName($userId),
'applicationVersion' => $this->applicationVersion ?? '-',
'lastSyncTrakt' => $this->syncLogRepository->findLastTraktSync() ?? '-',
'lastSyncTmdb' => $this->syncLogRepository->findLastTmdbSync() ?? '-',
@@ -50,13 +51,18 @@ public function updateTrakt(Request $request) : Response
}
$traktClientId = $request->getPostParameters()['traktClientId'];
+ $traktUserName = $request->getPostParameters()['traktUserName'];
$userId = $this->authenticationService->getCurrentUserId();
if (empty($traktClientId) === true) {
$traktClientId = null;
}
+ if (empty($traktUserName) === true) {
+ $traktUserName = null;
+ }
$this->userApi->updateTraktClientId($userId, $traktClientId);
+ $this->userApi->updateTraktUserName($userId, $traktUserName);
return Response::create(
StatusCode::createSeeOther(),
diff --git a/templates/page/settings.html.twig b/templates/page/settings.html.twig
index c0300902..12ea15f4 100644
--- a/templates/page/settings.html.twig
+++ b/templates/page/settings.html.twig
@@ -23,11 +23,16 @@
To generate a client id visit this url here.