]> BookStack Code Mirror - bookstack/blobdiff - app/Users/Controllers/UserPreferencesController.php
Notifications: added user preference UI & logic
[bookstack] / app / Users / Controllers / UserPreferencesController.php
index b20a8aa37220678e496fa6855a216c076a2ccab7..faa99629b349db365b1f39918f7d91944657bc7c 100644 (file)
@@ -3,17 +3,16 @@
 namespace BookStack\Users\Controllers;
 
 use BookStack\Http\Controller;
+use BookStack\Settings\UserNotificationPreferences;
 use BookStack\Settings\UserShortcutMap;
 use BookStack\Users\UserRepo;
 use Illuminate\Http\Request;
 
 class UserPreferencesController extends Controller
 {
-    protected UserRepo $userRepo;
-
-    public function __construct(UserRepo $userRepo)
-    {
-        $this->userRepo = $userRepo;
+    public function __construct(
+        protected UserRepo $userRepo
+    ) {
     }
 
     /**
@@ -47,6 +46,35 @@ class UserPreferencesController extends Controller
         return redirect('/preferences/shortcuts');
     }
 
+    /**
+     * Show the notification preferences for the current user.
+     */
+    public function showNotifications()
+    {
+        $preferences = (new UserNotificationPreferences(user()));
+
+        return view('users.preferences.notifications', [
+            'preferences' => $preferences,
+        ]);
+    }
+
+    /**
+     * Update the notification preferences for the current user.
+     */
+    public function updateNotifications(Request $request)
+    {
+        $data = $this->validate($request, [
+           'preferences' => ['required', 'array'],
+           'preferences.*' => ['required', 'string'],
+        ]);
+
+        $preferences = (new UserNotificationPreferences(user()));
+        $preferences->updateFromSettingsArray($data['preferences']);
+        $this->showSuccessNotification(trans('preferences.notifications_update_success'));
+
+        return redirect('/preferences/notifications');
+    }
+
     /**
      * Update the preferred view format for a list view of the given type.
      */
@@ -123,7 +151,7 @@ class UserPreferencesController extends Controller
     {
         $validated = $this->validate($request, [
             'language' => ['required', 'string', 'max:20'],
-            'active'   => ['required', 'bool'],
+            'active' => ['required', 'bool'],
         ]);
 
         $currentFavoritesStr = setting()->getForCurrentUser('code-language-favourites', '');