]> BookStack Code Mirror - bookstack/blobdiff - app/Http/Controllers/WebhookController.php
Aligned notification capitalisation
[bookstack] / app / Http / Controllers / WebhookController.php
index 15a31f312fd02a185af5dc52781150e7971320de..588b256a35156ae92c11c806319445805e1c5f36 100644 (file)
@@ -20,8 +20,11 @@ class WebhookController extends Controller
      */
     public function index()
     {
-        // TODO - Get and pass webhooks
-        return view('settings.webhooks.index');
+        $webhooks = Webhook::query()
+            ->orderBy('name', 'desc')
+            ->with('trackedEvents')
+            ->get();
+        return view('settings.webhooks.index', ['webhooks' => $webhooks]);
     }
 
     /**
@@ -37,7 +40,18 @@ class WebhookController extends Controller
      */
     public function store(Request $request)
     {
-        // TODO - Create webhook
+        $validated = $this->validate($request, [
+            'name' => ['required', 'max:150'],
+            'endpoint' => ['required', 'url', 'max:500'],
+            'events' => ['required', 'array'],
+            'active' => ['required'],
+        ]);
+
+        $webhook = new Webhook($validated);
+        $webhook->active = $validated['active'] === 'true';
+        $webhook->save();
+        $webhook->updateTrackedEvents(array_values($validated['events']));
+
         $this->logActivity(ActivityType::WEBHOOK_CREATE, $webhook);
         return redirect('/settings/webhooks');
     }
@@ -48,7 +62,9 @@ class WebhookController extends Controller
     public function edit(string $id)
     {
         /** @var Webhook $webhook */
-        $webhook = Webhook::query()->findOrFail($id);
+        $webhook = Webhook::query()
+            ->with('trackedEvents')
+            ->findOrFail($id);
 
         return view('settings.webhooks.edit', ['webhook' => $webhook]);
     }
@@ -58,10 +74,19 @@ class WebhookController extends Controller
      */
     public function update(Request $request, string $id)
     {
+        $validated = $this->validate($request, [
+            'name' => ['required', 'max:150'],
+            'endpoint' => ['required', 'url', 'max:500'],
+            'events' => ['required', 'array'],
+            'active' => ['required'],
+        ]);
+
         /** @var Webhook $webhook */
         $webhook = Webhook::query()->findOrFail($id);
 
-        // TODO - Update
+        $webhook->active = $validated['active'] === 'true';
+        $webhook->fill($validated)->save();
+        $webhook->updateTrackedEvents($validated['events']);
 
         $this->logActivity(ActivityType::WEBHOOK_UPDATE, $webhook);
         return redirect('/settings/webhooks');
@@ -85,7 +110,7 @@ class WebhookController extends Controller
         /** @var Webhook $webhook */
         $webhook = Webhook::query()->findOrFail($id);
 
-        // TODO - Delete event type relations
+        $webhook->trackedEvents()->delete();
         $webhook->delete();
 
         $this->logActivity(ActivityType::WEBHOOK_DELETE, $webhook);