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');
65 $this->preventGuestAccess();
67 $preferences = (new UserNotificationPreferences(user()));
69 $query = Watch::query()->where('user_id', '=', user()->id);
70 $query = $permissions->restrictEntityRelationQuery($query, 'watches', 'watchable_id', 'watchable_type');
71 $watches = $query->with('watchable')->paginate(20);
73 return view('users.preferences.notifications', [
74 'preferences' => $preferences,
75 'watches' => $watches,
80 * Update the notification preferences for the current user.
82 public function updateNotifications(Request $request)
84 $this->checkPermission('receive-notifications');
85 $this->preventGuestAccess();
86 $data = $this->validate($request, [
87 'preferences' => ['required', 'array'],
88 'preferences.*' => ['required', 'string'],
91 $preferences = (new UserNotificationPreferences(user()));
92 $preferences->updateFromSettingsArray($data['preferences']);
93 $this->showSuccessNotification(trans('preferences.notifications_update_success'));
95 return redirect('/preferences/notifications');
99 * Update the preferred view format for a list view of the given type.
101 public function changeView(Request $request, string $type)
103 $valueViewTypes = ['books', 'bookshelves', 'bookshelf'];
104 if (!in_array($type, $valueViewTypes)) {
105 return redirect()->back(500);
108 $view = $request->get('view');
109 if (!in_array($view, ['grid', 'list'])) {
113 $key = $type . '_view_type';
114 setting()->putForCurrentUser($key, $view);
116 return redirect()->back(302, [], "/");
120 * Change the stored sort type for a particular view.
122 public function changeSort(Request $request, string $type)
124 $validSortTypes = ['books', 'bookshelves', 'shelf_books', 'users', 'roles', 'webhooks', 'tags', 'page_revisions'];
125 if (!in_array($type, $validSortTypes)) {
126 return redirect()->back(500);
129 $sort = substr($request->get('sort') ?: 'name', 0, 50);
130 $order = $request->get('order') === 'desc' ? 'desc' : 'asc';
132 $sortKey = $type . '_sort';
133 $orderKey = $type . '_sort_order';
134 setting()->putForCurrentUser($sortKey, $sort);
135 setting()->putForCurrentUser($orderKey, $order);
137 return redirect()->back(302, [], "/");
141 * Toggle dark mode for the current user.
143 public function toggleDarkMode()
145 $enabled = setting()->getForCurrentUser('dark-mode-enabled', false);
146 setting()->putForCurrentUser('dark-mode-enabled', $enabled ? 'false' : 'true');
148 return redirect()->back();
152 * Update the stored section expansion preference for the given user.
154 public function changeExpansion(Request $request, string $type)
156 $typeWhitelist = ['home-details'];
157 if (!in_array($type, $typeWhitelist)) {
158 return response('Invalid key', 500);
161 $newState = $request->get('expand', 'false');
162 setting()->putForCurrentUser('section_expansion#' . $type, $newState);
164 return response('', 204);
168 * Update the favorite status for a code language.
170 public function updateCodeLanguageFavourite(Request $request)
172 $validated = $this->validate($request, [
173 'language' => ['required', 'string', 'max:20'],
174 'active' => ['required', 'bool'],
177 $currentFavoritesStr = setting()->getForCurrentUser('code-language-favourites', '');
178 $currentFavorites = array_filter(explode(',', $currentFavoritesStr));
180 $isFav = in_array($validated['language'], $currentFavorites);
181 if (!$isFav && $validated['active']) {
182 $currentFavorites[] = $validated['language'];
183 } elseif ($isFav && !$validated['active']) {
184 $index = array_search($validated['language'], $currentFavorites);
185 array_splice($currentFavorites, $index, 1);
188 setting()->putForCurrentUser('code-language-favourites', implode(',', $currentFavorites));
189 return response('', 204);