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 user-specific interface shortcuts.
23 public function showShortcuts()
25 $shortcuts = UserShortcutMap::fromUserPreferences();
26 $enabled = setting()->getForCurrentUser('ui-shortcuts-enabled', false);
28 return view('users.preferences.shortcuts', [
29 'shortcuts' => $shortcuts,
30 'enabled' => $enabled,
35 * Update the user-specific interface shortcuts.
37 public function updateShortcuts(Request $request)
39 $enabled = $request->get('enabled') === 'true';
40 $providedShortcuts = $request->get('shortcut', []);
41 $shortcuts = new UserShortcutMap($providedShortcuts);
43 setting()->putForCurrentUser('ui-shortcuts', $shortcuts->toJson());
44 setting()->putForCurrentUser('ui-shortcuts-enabled', $enabled);
46 $this->showSuccessNotification(trans('preferences.shortcuts_update_success'));
48 return redirect('/preferences/shortcuts');
52 * Show the notification preferences for the current user.
54 public function showNotifications(PermissionApplicator $permissions)
56 $preferences = (new UserNotificationPreferences(user()));
58 $query = Watch::query()->where('user_id', '=', user()->id);
59 $query = $permissions->restrictEntityRelationQuery($query, 'watches', 'watchable_id', 'watchable_type');
60 $watches = $query->with('watchable')->paginate(20);
62 return view('users.preferences.notifications', [
63 'preferences' => $preferences,
64 'watches' => $watches,
69 * Update the notification preferences for the current user.
71 public function updateNotifications(Request $request)
73 $data = $this->validate($request, [
74 'preferences' => ['required', 'array'],
75 'preferences.*' => ['required', 'string'],
78 $preferences = (new UserNotificationPreferences(user()));
79 $preferences->updateFromSettingsArray($data['preferences']);
80 $this->showSuccessNotification(trans('preferences.notifications_update_success'));
82 return redirect('/preferences/notifications');
86 * Update the preferred view format for a list view of the given type.
88 public function changeView(Request $request, string $type)
90 $valueViewTypes = ['books', 'bookshelves', 'bookshelf'];
91 if (!in_array($type, $valueViewTypes)) {
92 return redirect()->back(500);
95 $view = $request->get('view');
96 if (!in_array($view, ['grid', 'list'])) {
100 $key = $type . '_view_type';
101 setting()->putForCurrentUser($key, $view);
103 return redirect()->back(302, [], "/");
107 * Change the stored sort type for a particular view.
109 public function changeSort(Request $request, string $type)
111 $validSortTypes = ['books', 'bookshelves', 'shelf_books', 'users', 'roles', 'webhooks', 'tags', 'page_revisions'];
112 if (!in_array($type, $validSortTypes)) {
113 return redirect()->back(500);
116 $sort = substr($request->get('sort') ?: 'name', 0, 50);
117 $order = $request->get('order') === 'desc' ? 'desc' : 'asc';
119 $sortKey = $type . '_sort';
120 $orderKey = $type . '_sort_order';
121 setting()->putForCurrentUser($sortKey, $sort);
122 setting()->putForCurrentUser($orderKey, $order);
124 return redirect()->back(302, [], "/");
128 * Toggle dark mode for the current user.
130 public function toggleDarkMode()
132 $enabled = setting()->getForCurrentUser('dark-mode-enabled', false);
133 setting()->putForCurrentUser('dark-mode-enabled', $enabled ? 'false' : 'true');
135 return redirect()->back();
139 * Update the stored section expansion preference for the given user.
141 public function changeExpansion(Request $request, string $type)
143 $typeWhitelist = ['home-details'];
144 if (!in_array($type, $typeWhitelist)) {
145 return response('Invalid key', 500);
148 $newState = $request->get('expand', 'false');
149 setting()->putForCurrentUser('section_expansion#' . $type, $newState);
151 return response('', 204);
155 * Update the favorite status for a code language.
157 public function updateCodeLanguageFavourite(Request $request)
159 $validated = $this->validate($request, [
160 'language' => ['required', 'string', 'max:20'],
161 'active' => ['required', 'bool'],
164 $currentFavoritesStr = setting()->getForCurrentUser('code-language-favourites', '');
165 $currentFavorites = array_filter(explode(',', $currentFavoritesStr));
167 $isFav = in_array($validated['language'], $currentFavorites);
168 if (!$isFav && $validated['active']) {
169 $currentFavorites[] = $validated['language'];
170 } elseif ($isFav && !$validated['active']) {
171 $index = array_search($validated['language'], $currentFavorites);
172 array_splice($currentFavorites, $index, 1);
175 setting()->putForCurrentUser('code-language-favourites', implode(',', $currentFavorites));
176 return response('', 204);