3 namespace BookStack\Users\Controllers;
5 use BookStack\Activity\Models\Watch;
6 use BookStack\Http\Controller;
7 use BookStack\Permissions\PermissionApplicator;
8 use BookStack\Settings\UserNotificationPreferences;
9 use BookStack\Settings\UserShortcutMap;
10 use BookStack\Users\UserRepo;
11 use Illuminate\Http\Request;
13 class UserPreferencesController extends Controller
15 public function __construct(
16 protected UserRepo $userRepo
21 * Show the overview for user preferences.
23 public function index()
25 return view('users.preferences.index');
29 * Show the user-specific interface shortcuts.
31 public function showShortcuts()
33 $shortcuts = UserShortcutMap::fromUserPreferences();
34 $enabled = setting()->getForCurrentUser('ui-shortcuts-enabled', false);
36 return view('users.preferences.shortcuts', [
37 'shortcuts' => $shortcuts,
38 'enabled' => $enabled,
43 * Update the user-specific interface shortcuts.
45 public function updateShortcuts(Request $request)
47 $enabled = $request->get('enabled') === 'true';
48 $providedShortcuts = $request->get('shortcut', []);
49 $shortcuts = new UserShortcutMap($providedShortcuts);
51 setting()->putForCurrentUser('ui-shortcuts', $shortcuts->toJson());
52 setting()->putForCurrentUser('ui-shortcuts-enabled', $enabled);
54 $this->showSuccessNotification(trans('preferences.shortcuts_update_success'));
56 return redirect('/preferences/shortcuts');
60 * Show the notification preferences for the current user.
62 public function showNotifications(PermissionApplicator $permissions)
64 $this->checkPermission('receive-notifications');
66 $preferences = (new UserNotificationPreferences(user()));
68 $query = Watch::query()->where('user_id', '=', user()->id);
69 $query = $permissions->restrictEntityRelationQuery($query, 'watches', 'watchable_id', 'watchable_type');
70 $watches = $query->with('watchable')->paginate(20);
72 return view('users.preferences.notifications', [
73 'preferences' => $preferences,
74 'watches' => $watches,
79 * Update the notification preferences for the current user.
81 public function updateNotifications(Request $request)
83 $this->checkPermission('receive-notifications');
84 $data = $this->validate($request, [
85 'preferences' => ['required', 'array'],
86 'preferences.*' => ['required', 'string'],
89 $preferences = (new UserNotificationPreferences(user()));
90 $preferences->updateFromSettingsArray($data['preferences']);
91 $this->showSuccessNotification(trans('preferences.notifications_update_success'));
93 return redirect('/preferences/notifications');
97 * Update the preferred view format for a list view of the given type.
99 public function changeView(Request $request, string $type)
101 $valueViewTypes = ['books', 'bookshelves', 'bookshelf'];
102 if (!in_array($type, $valueViewTypes)) {
103 return redirect()->back(500);
106 $view = $request->get('view');
107 if (!in_array($view, ['grid', 'list'])) {
111 $key = $type . '_view_type';
112 setting()->putForCurrentUser($key, $view);
114 return redirect()->back(302, [], "/");
118 * Change the stored sort type for a particular view.
120 public function changeSort(Request $request, string $type)
122 $validSortTypes = ['books', 'bookshelves', 'shelf_books', 'users', 'roles', 'webhooks', 'tags', 'page_revisions'];
123 if (!in_array($type, $validSortTypes)) {
124 return redirect()->back(500);
127 $sort = substr($request->get('sort') ?: 'name', 0, 50);
128 $order = $request->get('order') === 'desc' ? 'desc' : 'asc';
130 $sortKey = $type . '_sort';
131 $orderKey = $type . '_sort_order';
132 setting()->putForCurrentUser($sortKey, $sort);
133 setting()->putForCurrentUser($orderKey, $order);
135 return redirect()->back(302, [], "/");
139 * Toggle dark mode for the current user.
141 public function toggleDarkMode()
143 $enabled = setting()->getForCurrentUser('dark-mode-enabled', false);
144 setting()->putForCurrentUser('dark-mode-enabled', $enabled ? 'false' : 'true');
146 return redirect()->back();
150 * Update the stored section expansion preference for the given user.
152 public function changeExpansion(Request $request, string $type)
154 $typeWhitelist = ['home-details'];
155 if (!in_array($type, $typeWhitelist)) {
156 return response('Invalid key', 500);
159 $newState = $request->get('expand', 'false');
160 setting()->putForCurrentUser('section_expansion#' . $type, $newState);
162 return response('', 204);
166 * Update the favorite status for a code language.
168 public function updateCodeLanguageFavourite(Request $request)
170 $validated = $this->validate($request, [
171 'language' => ['required', 'string', 'max:20'],
172 'active' => ['required', 'bool'],
175 $currentFavoritesStr = setting()->getForCurrentUser('code-language-favourites', '');
176 $currentFavorites = array_filter(explode(',', $currentFavoritesStr));
178 $isFav = in_array($validated['language'], $currentFavorites);
179 if (!$isFav && $validated['active']) {
180 $currentFavorites[] = $validated['language'];
181 } elseif ($isFav && !$validated['active']) {
182 $index = array_search($validated['language'], $currentFavorites);
183 array_splice($currentFavorites, $index, 1);
186 setting()->putForCurrentUser('code-language-favourites', implode(',', $currentFavorites));
187 return response('', 204);