Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to dis/enable the locations feature #614

Merged
merged 4 commits into from
Sep 1, 2024
Merged
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
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`');
}
}
72 changes: 70 additions & 2 deletions public/js/settings-account-location.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ const rows = table.getElementsByTagName('tr');

reloadTable()

async function reloadTable() {
async function reloadTable(featureIsEnabled = true) {
table.getElementsByTagName('tbody')[0].innerHTML = ''

if (document.getElementById('toggleLocationsFeatureBtn').textContent === 'Enable locations') {
return
}

document.getElementById('locationsTableLoadingSpinner').classList.remove('d-none')

const response = await fetch('/settings/locations');
Expand Down Expand Up @@ -189,4 +194,67 @@ document.getElementById('updateLocationButton').addEventListener('click', async

reloadTable()
locationModal.hide()
})
})

async function toggleLocationFeature() {
let enableLocationsFeature = document.getElementById('toggleLocationsFeatureBtn').textContent === 'Enable locations'
await sendRequestToggleLocationsFeature(enableLocationsFeature)
setLocationFeatureBtnState(!enableLocationsFeature)
setLocationTableState(enableLocationsFeature)
reloadTable(enableLocationsFeature)

setLocationsAlert('Locations ' + (enableLocationsFeature === true ? 'enabled' : 'disabled'))
}

function setLocationFeatureBtnState(featureIsEnabled) {
if (featureIsEnabled === true) {
document.getElementById('toggleLocationsFeatureBtn').classList.add('btn-primary')
document.getElementById('toggleLocationsFeatureBtn').classList.remove('btn-outline-danger')
document.getElementById('toggleLocationsFeatureBtn').textContent = 'Enable locations'

return
}

document.getElementById('toggleLocationsFeatureBtn').classList.add('btn-outline-danger')
document.getElementById('toggleLocationsFeatureBtn').classList.remove('btn-primary')
document.getElementById('toggleLocationsFeatureBtn').textContent = 'Disable locations'

}

function setLocationTableState(featureIsEnabled) {
if (featureIsEnabled === true) {
document.getElementById('createLocationBtn').disabled = false

return
}

document.getElementById('createLocationBtn').disabled = true
}

async function sendRequestToggleLocationsFeature(isLocationsEnabled) {
const response = await fetch('/settings/locations/toggle-feature', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
'locationsEnabled': isLocationsEnabled,
})
})

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
}


async function sendRequestFetchIsLocationsFeatureEnabled() {
const response = await fetch('/settings/locations/toggle-feature', {method: 'GET'})

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
const data = await response.json()

return data.locationsEnabled
}
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
7 changes: 7 additions & 0 deletions src/Domain/User/UserEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ private function __construct(
private readonly ?string $country,
private readonly bool $jellyfinSyncEnabled,
private readonly bool $displayCharacterNames,
private readonly bool $hasLocationsEnabled,
) {
}

Expand Down Expand Up @@ -66,6 +67,7 @@ public static function createFromArray(array $data) : self
$data['country'],
(bool)$data['jellyfin_sync_enabled'],
(bool)$data['display_character_names'],
(bool)$data['locations_enabled'],
);
}

Expand Down Expand Up @@ -179,6 +181,11 @@ public function hasJellyfinSyncEnabled() : bool
return $this->jellyfinSyncEnabled;
}

public function hasLocationsEnabled() : bool
{
return $this->hasLocationsEnabled;
}

public function hasPlexScrobbleRatingsEnabled() : bool
{
return $this->plexScrobbleRatings;
Expand Down
Loading
Loading