3 namespace BookStack\Activity\Controllers;
5 use BookStack\Activity\Models\Watch;
6 use BookStack\Activity\Tools\UserEntityWatchOptions;
7 use BookStack\App\Model;
8 use BookStack\Entities\Models\Entity;
9 use BookStack\Http\Controller;
11 use Illuminate\Http\Request;
12 use Illuminate\Validation\ValidationException;
14 class WatchController extends Controller
16 public function update(Request $request)
18 // TODO - Require notification permission
19 $requestData = $this->validate($request, [
20 'level' => ['required', 'string'],
23 $watchable = $this->getValidatedModelFromRequest($request);
24 $watchOptions = new UserEntityWatchOptions(user(), $watchable);
25 $watchOptions->updateWatchLevel($requestData['level']);
27 $this->showSuccessNotification(trans('activities.watch_update_level_notification'));
29 return redirect()->back();
33 * @throws ValidationException
36 protected function getValidatedModelFromRequest(Request $request): Entity
38 $modelInfo = $this->validate($request, [
39 'type' => ['required', 'string'],
40 'id' => ['required', 'integer'],
43 if (!class_exists($modelInfo['type'])) {
44 throw new Exception('Model not found');
47 /** @var Model $model */
48 $model = new $modelInfo['type']();
49 if (!$model instanceof Entity) {
50 throw new Exception('Model not an entity');
53 $modelInstance = $model->newQuery()
54 ->where('id', '=', $modelInfo['id'])
55 ->first(['id', 'name', 'owned_by']);
57 $inaccessibleEntity = ($modelInstance instanceof Entity && !userCan('view', $modelInstance));
58 if (is_null($modelInstance) || $inaccessibleEntity) {
59 throw new Exception('Model instance not found');
62 return $modelInstance;