diff --git a/app/Auth/UserRepo.php b/app/Auth/UserRepo.php index c589fd964..78bcb978e 100644 --- a/app/Auth/UserRepo.php +++ b/app/Auth/UserRepo.php @@ -158,6 +158,9 @@ class UserRepo // Delete user profile images $this->userAvatar->destroyAllForUser($user); + // Delete related activities + setting()->deleteUserSettings($user->id); + if (!empty($newOwnerId)) { $newOwner = User::query()->find($newOwnerId); if (!is_null($newOwner)) { diff --git a/app/Http/Controllers/UserPreferencesController.php b/app/Http/Controllers/UserPreferencesController.php index c42be0484..e5ac69818 100644 --- a/app/Http/Controllers/UserPreferencesController.php +++ b/app/Http/Controllers/UserPreferencesController.php @@ -38,8 +38,8 @@ class UserPreferencesController extends Controller $providedShortcuts = $request->get('shortcut', []); $shortcuts = new UserShortcutMap($providedShortcuts); - setting()->putUser(user(), 'ui-shortcuts', $shortcuts->toJson()); - setting()->putUser(user(), 'ui-shortcuts-enabled', $enabled); + setting()->putForCurrentUser('ui-shortcuts', $shortcuts->toJson()); + setting()->putForCurrentUser('ui-shortcuts-enabled', $enabled); $this->showSuccessNotification('Shortcut preferences have been updated!'); @@ -47,70 +47,45 @@ class UserPreferencesController extends Controller } /** - * Update the user's preferred book-list display setting. + * Update the preferred view format for a list view of the given type. */ - public function switchBooksView(Request $request, int $id) + public function changeView(Request $request, string $type) { - return $this->switchViewType($id, $request, 'books'); - } - - /** - * Update the user's preferred shelf-list display setting. - */ - public function switchShelvesView(Request $request, int $id) - { - return $this->switchViewType($id, $request, 'bookshelves'); - } - - /** - * Update the user's preferred shelf-view book list display setting. - */ - public function switchShelfView(Request $request, int $id) - { - return $this->switchViewType($id, $request, 'bookshelf'); - } - - /** - * For a type of list, switch with stored view type for a user. - */ - protected function switchViewType(int $userId, Request $request, string $listName) - { - $this->checkPermissionOrCurrentUser('users-manage', $userId); - - $viewType = $request->get('view_type'); - if (!in_array($viewType, ['grid', 'list'])) { - $viewType = 'list'; + $valueViewTypes = ['books', 'bookshelves', 'bookshelf']; + if (!in_array($type, $valueViewTypes)) { + return redirect()->back(500); } - $user = $this->userRepo->getById($userId); - $key = $listName . '_view_type'; - setting()->putUser($user, $key, $viewType); + $view = $request->get('view'); + if (!in_array($view, ['grid', 'list'])) { + $view = 'list'; + } - return redirect()->back(302, [], "/settings/users/$userId"); + $key = $type . '_view_type'; + setting()->putForCurrentUser($key, $view); + + return redirect()->back(302, [], "/"); } /** * Change the stored sort type for a particular view. */ - public function changeSort(Request $request, string $id, string $type) + public function changeSort(Request $request, string $type) { $validSortTypes = ['books', 'bookshelves', 'shelf_books', 'users', 'roles', 'webhooks', 'tags', 'page_revisions']; if (!in_array($type, $validSortTypes)) { return redirect()->back(500); } - $this->checkPermissionOrCurrentUser('users-manage', $id); - $sort = substr($request->get('sort') ?: 'name', 0, 50); $order = $request->get('order') === 'desc' ? 'desc' : 'asc'; - $user = $this->userRepo->getById($id); $sortKey = $type . '_sort'; $orderKey = $type . '_sort_order'; - setting()->putUser($user, $sortKey, $sort); - setting()->putUser($user, $orderKey, $order); + setting()->putForCurrentUser($sortKey, $sort); + setting()->putForCurrentUser($orderKey, $order); - return redirect()->back(302, [], "/settings/users/{$id}"); + return redirect()->back(302, [], "/"); } /** @@ -119,7 +94,7 @@ class UserPreferencesController extends Controller public function toggleDarkMode() { $enabled = setting()->getForCurrentUser('dark-mode-enabled', false); - setting()->putUser(user(), 'dark-mode-enabled', $enabled ? 'false' : 'true'); + setting()->putForCurrentUser('dark-mode-enabled', $enabled ? 'false' : 'true'); return redirect()->back(); } @@ -127,18 +102,15 @@ class UserPreferencesController extends Controller /** * Update the stored section expansion preference for the given user. */ - public function updateExpansionPreference(Request $request, string $id, string $key) + public function changeExpansion(Request $request, string $type) { - $this->checkPermissionOrCurrentUser('users-manage', $id); - $keyWhitelist = ['home-details']; - if (!in_array($key, $keyWhitelist)) { + $typeWhitelist = ['home-details']; + if (!in_array($type, $typeWhitelist)) { return response('Invalid key', 500); } $newState = $request->get('expand', 'false'); - - $user = $this->userRepo->getById($id); - setting()->putUser($user, 'section_expansion#' . $key, $newState); + setting()->putForCurrentUser('section_expansion#' . $type, $newState); return response('', 204); } @@ -161,6 +133,6 @@ class UserPreferencesController extends Controller array_splice($currentFavorites, $index, 1); } - setting()->putUser(user(), 'code-language-favourites', implode(',', $currentFavorites)); + setting()->putForCurrentUser('code-language-favourites', implode(',', $currentFavorites)); } } diff --git a/app/Settings/SettingService.php b/app/Settings/SettingService.php index f2c4c8305..9f0a41ea2 100644 --- a/app/Settings/SettingService.php +++ b/app/Settings/SettingService.php @@ -194,6 +194,8 @@ class SettingService /** * Put a user-specific setting into the database. + * Can only take string value types since this may use + * the session which is less flexible to data types. */ public function putUser(User $user, string $key, string $value): bool { @@ -206,6 +208,16 @@ class SettingService return $this->put($this->userKey($user->id, $key), $value); } + /** + * Put a user-specific setting into the database for the current access user. + * Can only take string value types since this may use + * the session which is less flexible to data types. + */ + public function putForCurrentUser(string $key, string $value) + { + return $this->putUser(user(), $key, $value); + } + /** * Convert a setting key into a user-specific key. */ diff --git a/resources/js/components/code-editor.js b/resources/js/components/code-editor.js index 2d8031205..d0c6c432a 100644 --- a/resources/js/components/code-editor.js +++ b/resources/js/components/code-editor.js @@ -73,7 +73,7 @@ class CodeEditor { isFavorite ? this.favourites.add(language) : this.favourites.delete(language); button.setAttribute('data-favourite', isFavorite ? 'true' : 'false'); - window.$http.patch('/settings/users/update-code-language-favourite', { + window.$http.patch('/preferences/update-code-language-favourite', { language: language, active: isFavorite }); diff --git a/resources/js/components/shortcuts.js b/resources/js/components/shortcuts.js index cec8684c8..4efa3d42b 100644 --- a/resources/js/components/shortcuts.js +++ b/resources/js/components/shortcuts.js @@ -70,7 +70,6 @@ class Shortcuts { */ runShortcut(id) { const el = this.container.querySelector(`[data-shortcut="${id}"]`); - console.info('Shortcut run', el); if (!el) { return false; } diff --git a/resources/views/common/dark-mode-toggle.blade.php b/resources/views/common/dark-mode-toggle.blade.php index 0812e487a..d6ecbc4d6 100644 --- a/resources/views/common/dark-mode-toggle.blade.php +++ b/resources/views/common/dark-mode-toggle.blade.php @@ -1,4 +1,4 @@ -