Skip to content

Commit

Permalink
Add basic get and post endpoints for feature toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
leepeuker committed Sep 1, 2024
1 parent 562560d commit e701e2a
Show file tree
Hide file tree
Showing 7 changed files with 306 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function up() : void
ALTER TABLE `movie_user_watch_dates`
ADD COLUMN `location_id` INT(10) UNSIGNED DEFAULT NULL AFTER `position`,
ADD CONSTRAINT `fk_movie_user_watch_dates_location_id` FOREIGN KEY (`location_id`) REFERENCES `location` (`id`) ON DELETE CASCADE;
SQL
SQL
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class AddLocationsFeatureFlagToUserTable extends AbstractMigration
{
public function down() : void
{
$this->execute(
<<<SQL
ALTER TABLE user DROP COLUMN locations_enabled;
SQL,
);
}

public function up() : void
{
$this->execute(
<<<SQL
ALTER TABLE user ADD COLUMN locations_enabled TINYINT(1) DEFAULT 1 AFTER display_character_names;
SQL,
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
<?php declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class AddLocationsFeatureFlagToUserTable extends AbstractMigration
{
public function down() : void
{
$this->execute(
<<<SQL
CREATE TABLE `tmp_user` (
`id` INTEGER,
`email` TEXT NOT NULL,
`name` TEXT NOT NULL,
`password` TEXT NOT NULL,
`totp_uri` TEXT DEFAULT NULL,
`is_admin` TINYINT(1) DEFAULT 0,
`dashboard_visible_rows` TEXT DEFAULT NULL,
`dashboard_extended_rows` TEXT DEFAULT NULL,
`dashboard_order_rows` TEXT DEFAULT NULL,
`jellyfin_access_token` TEXT DEFAULT NULL,
`jellyfin_user_id` TEXT DEFAULT NULL,
`jellyfin_server_url` TEXT DEFAULT NULL,
`jellyfin_sync_enabled` TINYINT(1) DEFAULT 0,
`privacy_level` INTEGER DEFAULT 1,
`date_format_id` INTEGER DEFAULT 0,
`trakt_user_name` TEXT,
`plex_webhook_uuid` TEXT,
`jellyfin_webhook_uuid` TEXT,
`emby_webhook_uuid` TEXT,
`trakt_client_id` TEXT,
`plex_client_id` TEXT DEFAULT NULL,
`plex_client_temporary_code` TEXT DEFAULT NULL,
`plex_access_token` TEXT DEFAULT NULL,
`plex_account_id` TEXT DEFAULT NULL,
`plex_server_url` TEXT DEFAULT NULL,
`jellyfin_scrobble_views` INTEGER DEFAULT 1,
`emby_scrobble_views` INTEGER DEFAULT 1,
`plex_scrobble_views` INTEGER DEFAULT 1,
`plex_scrobble_ratings` INTEGER DEFAULT 0,
`radarr_feed_uuid` TEXT DEFAULT NULL,
`watchlist_automatic_removal_enabled` INTEGER DEFAULT 1,
`country` TEXT DEFAULT NULL,
`display_character_names` INTEGER DEFAULT 1,
`core_account_changes_disabled` INTEGER DEFAULT 0,
`created_at` TEXT NOT NULL,
PRIMARY KEY (`id`),
UNIQUE (`email`),
UNIQUE (`name`)
)
SQL,
);
$this->execute(
'INSERT INTO `tmp_user` (
`id`,
`email`,
`name`,
`password`,
`totp_uri`,
`is_admin`,
`dashboard_visible_rows`,
`dashboard_extended_rows`,
`dashboard_order_rows`,
`jellyfin_access_token`,
`jellyfin_user_id`,
`jellyfin_server_url`,
`privacy_level`,
`date_format_id`,
`trakt_user_name`,
`plex_webhook_uuid`,
`jellyfin_webhook_uuid`,
`emby_webhook_uuid`,
`trakt_client_id`,
`plex_client_id`,
`plex_client_temporary_code`,
`plex_access_token`,
`plex_account_id`,
`plex_server_url`,
`jellyfin_scrobble_views`,
`emby_scrobble_views`,
`plex_scrobble_views`,
`plex_scrobble_ratings`,
`radarr_feed_uuid`,
`watchlist_automatic_removal_enabled`,
`country`,
`display_character_names`,
`core_account_changes_disabled`,
`created_at`
) SELECT
`id`,
`email`,
`name`,
`password`,
`totp_uri`,
`is_admin`,
`dashboard_visible_rows`,
`dashboard_extended_rows`,
`dashboard_order_rows`,
`jellyfin_access_token`,
`jellyfin_user_id`,
`jellyfin_server_url`,
`privacy_level`,
`date_format_id`,
`trakt_user_name`,
`plex_webhook_uuid`,
`jellyfin_webhook_uuid`,
`emby_webhook_uuid`,
`trakt_client_id`,
`plex_client_id`,
`plex_client_temporary_code`,
`plex_access_token`,
`plex_account_id`,
`plex_server_url`,
`jellyfin_scrobble_views`,
`emby_scrobble_views`,
`plex_scrobble_views`,
`plex_scrobble_ratings`,
`radarr_feed_uuid`,
`watchlist_automatic_removal_enabled`,
`country`,
`display_character_names`,
`core_account_changes_disabled`,
`created_at` FROM user',
);
$this->execute('DROP TABLE `user`');
$this->execute('ALTER TABLE `tmp_user` RENAME TO `user`');
}

public function up() : void
{
$this->execute(
<<<SQL
CREATE TABLE `tmp_user` (
`id` INTEGER,
`email` TEXT NOT NULL,
`name` TEXT NOT NULL,
`password` TEXT NOT NULL,
`totp_uri` TEXT DEFAULT NULL,
`is_admin` TINYINT(1) DEFAULT 0,
`dashboard_visible_rows` TEXT DEFAULT NULL,
`dashboard_extended_rows` TEXT DEFAULT NULL,
`dashboard_order_rows` TEXT DEFAULT NULL,
`jellyfin_access_token` TEXT DEFAULT NULL,
`jellyfin_user_id` TEXT DEFAULT NULL,
`jellyfin_server_url` TEXT DEFAULT NULL,
`jellyfin_sync_enabled` TINYINT(1) DEFAULT 0,
`privacy_level` INTEGER DEFAULT 1,
`date_format_id` INTEGER DEFAULT 0,
`trakt_user_name` TEXT,
`plex_webhook_uuid` TEXT,
`jellyfin_webhook_uuid` TEXT,
`emby_webhook_uuid` TEXT,
`trakt_client_id` TEXT,
`plex_client_id` TEXT DEFAULT NULL,
`plex_client_temporary_code` TEXT DEFAULT NULL,
`plex_access_token` TEXT DEFAULT NULL,
`plex_account_id` TEXT DEFAULT NULL,
`plex_server_url` TEXT DEFAULT NULL,
`jellyfin_scrobble_views` INTEGER DEFAULT 1,
`emby_scrobble_views` INTEGER DEFAULT 1,
`plex_scrobble_views` INTEGER DEFAULT 1,
`plex_scrobble_ratings` INTEGER DEFAULT 0,
`radarr_feed_uuid` TEXT DEFAULT NULL,
`watchlist_automatic_removal_enabled` INTEGER DEFAULT 1,
`country` TEXT DEFAULT NULL,
`display_character_names` INTEGER DEFAULT 1,
`locations_enabled` INTEGER DEFAULT 1,
`core_account_changes_disabled` INTEGER DEFAULT 0,
`created_at` TEXT NOT NULL,
PRIMARY KEY (`id`),
UNIQUE (`email`),
UNIQUE (`name`)
)
SQL,
);
$this->execute(
'INSERT INTO `tmp_user` (
`id`,
`email`,
`name`,
`password`,
`totp_uri`,
`is_admin`,
`dashboard_visible_rows`,
`dashboard_extended_rows`,
`dashboard_order_rows`,
`jellyfin_access_token`,
`jellyfin_user_id`,
`jellyfin_server_url`,
`jellyfin_sync_enabled`,
`privacy_level`,
`date_format_id`,
`trakt_user_name`,
`plex_webhook_uuid`,
`jellyfin_webhook_uuid`,
`emby_webhook_uuid`,
`trakt_client_id`,
`plex_client_id`,
`plex_client_temporary_code`,
`plex_access_token`,
`plex_account_id`,
`plex_server_url`,
`jellyfin_scrobble_views`,
`emby_scrobble_views`,
`plex_scrobble_views`,
`plex_scrobble_ratings`,
`radarr_feed_uuid`,
`watchlist_automatic_removal_enabled`,
`country`,
`display_character_names`,
`core_account_changes_disabled`,
`created_at`
) SELECT * FROM user',
);
$this->execute('DROP TABLE `user`');
$this->execute('ALTER TABLE `tmp_user` RENAME TO `user`');
}
}
2 changes: 2 additions & 0 deletions settings/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ function addWebRoutes(RouterService $routerService, FastRoute\RouteCollector $ro
$routes->add('POST', '/settings/locations', [Web\LocationController::class, 'createLocation'], [Web\Middleware\UserIsAuthenticated::class]);
$routes->add('PUT', '/settings/locations/{locationId:\d+}', [Web\LocationController::class, 'updateLocation'], [Web\Middleware\UserIsAuthenticated::class]);
$routes->add('DELETE', '/settings/locations/{locationId:\d+}', [Web\LocationController::class, 'deleteLocation'], [Web\Middleware\UserIsAuthenticated::class]);
$routes->add('GET', '/settings/locations/toggle-feature', [Web\LocationController::class, 'fetchToggleFeature'], [Web\Middleware\UserIsAuthenticated::class]);
$routes->add('POST', '/settings/locations/toggle-feature', [Web\LocationController::class, 'updateToggleFeature'], [Web\Middleware\UserIsAuthenticated::class]);

$routes->add('GET', '/settings/integrations/radarr', [Web\SettingsController::class, 'renderRadarrPage'], [Web\Middleware\UserIsAuthenticated::class]);
$routes->add('PUT', '/settings/radarr/feed', [RadarrController::class, 'regenerateRadarrFeedUrl'], [Web\Middleware\UserIsAuthenticated::class]);
Expand Down
10 changes: 10 additions & 0 deletions src/Domain/User/UserApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ public function hasUsers() : bool
return $this->repository->getCountOfUsers() > 0;
}

public function isLocationsEnabled(int $userId) : bool
{
return $this->repository->isLocationsEnabled($userId);
}

public function isValidPassword(int $userId, string $password) : bool
{
$passwordHash = $this->repository->findUserById($userId)?->getPasswordHash();
Expand Down Expand Up @@ -372,6 +377,11 @@ public function updateJellyfinSyncEnabled(int $userId, bool $enabledSync) : void
$this->repository->updateJellyfinSyncEnabled($userId, $enabledSync);
}

public function updateLocationsEnabled(int $userId, bool $locationsEnabled) : void
{
$this->repository->updateLocationsEnabled($userId, $locationsEnabled);
}

public function updateName(int $userId, string $name) : void
{
$this->userValidator->ensureNameFormatIsValid($name);
Expand Down
23 changes: 23 additions & 0 deletions src/Domain/User/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,16 @@ public function hasHiddenPerson(int $userId, int $personId) : bool
return (bool)$userPersonSettings[0]['is_hidden_in_top_lists'];
}

public function isLocationsEnabled(int $userId) : bool
{
$userPersonSettings = $this->dbConnection->fetchAllAssociative(
'SELECT locations_enabled FROM user WHERE id = ?',
[$userId],
);

return (bool)$userPersonSettings[0]['locations_enabled'];
}

public function setEmbyWebhookId(int $userId, ?string $embyWebhookId) : void
{
$this->dbConnection->update(
Expand Down Expand Up @@ -657,6 +667,19 @@ public function updateJellyfinSyncEnabled(int $userId, bool $enabledSync) : void
);
}

public function updateLocationsEnabled(int $userId, bool $locationsEnabled) : void
{
$this->dbConnection->update(
'user',
[
'locations_enabled' => (int)$locationsEnabled,
],
[
'id' => $userId,
],
);
}

public function updateName(int $userId, string $name) : void
{
$this->dbConnection->update(
Expand Down
28 changes: 28 additions & 0 deletions src/HttpController/Web/LocationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Movary\Domain\Movie\History\Location\MovieHistoryLocationApi;
use Movary\Domain\User\Service\Authentication;
use Movary\Domain\User\UserApi;
use Movary\Util\Json;
use Movary\ValueObject\Http\Request;
use Movary\ValueObject\Http\Response;
Expand All @@ -13,6 +14,7 @@ class LocationController
public function __construct(
private readonly Authentication $authenticationService,
private readonly MovieHistoryLocationApi $locationApi,
private readonly UserApi $userApi,
) {
}

Expand Down Expand Up @@ -58,6 +60,19 @@ public function fetchLocations() : Response
return Response::createJson(Json::encode($locations));
}

public function fetchToggleFeature() : Response
{
$currentUser = $this->authenticationService->getCurrentUser();

$isLocationsEnabled = $this->userApi->isLocationsEnabled($currentUser->getId());

return Response::createJson(
Json::encode(
['locationsEnabled' => $isLocationsEnabled],
),
);
}

public function updateLocation(Request $request) : Response
{
$currentUser = $this->authenticationService->getCurrentUser();
Expand All @@ -81,4 +96,17 @@ public function updateLocation(Request $request) : Response

return Response::createOk();
}

public function updateToggleFeature(Request $request) : Response
{
$currentUser = $this->authenticationService->getCurrentUser();
$requestData = Json::decode($request->getBody());

$this->userApi->updateLocationsEnabled(
$currentUser->getId(),
$requestData['locationsEnabled'],
);

return Response::createNoContent();
}
}

0 comments on commit e701e2a

Please sign in to comment.