X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/ee7e1122d325b1807f993e4f0cacc40fbbc806fb..refs/pull/2522/head:/app/Actions/ActivityService.php diff --git a/app/Actions/ActivityService.php b/app/Actions/ActivityService.php index be9f656c3..b2a35fd2a 100644 --- a/app/Actions/ActivityService.php +++ b/app/Actions/ActivityService.php @@ -2,9 +2,10 @@ use BookStack\Auth\Permissions\PermissionService; use BookStack\Auth\User; -use BookStack\Entities\Chapter; -use BookStack\Entities\Entity; -use BookStack\Entities\Page; +use BookStack\Entities\Models\Chapter; +use BookStack\Entities\Models\Entity; +use BookStack\Entities\Models\Page; +use BookStack\Interfaces\Loggable; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Support\Facades\Log; @@ -12,14 +13,12 @@ use Illuminate\Support\Facades\Log; class ActivityService { protected $activity; - protected $user; protected $permissionService; public function __construct(Activity $activity, PermissionService $permissionService) { $this->activity = $activity; $this->permissionService = $permissionService; - $this->user = user(); } /** @@ -32,14 +31,30 @@ class ActivityService $this->setNotification($type); } + /** + * Add a generic activity event to the database. + * @param string|Loggable $detail + */ + public function add(string $type, $detail = '') + { + if ($detail instanceof Loggable) { + $detail = $detail->logDescriptor(); + } + + $activity = $this->newActivityForUser($type); + $activity->detail = $detail; + $activity->save(); + $this->setNotification($type); + } + /** * Get a new activity instance for the current user. */ protected function newActivityForUser(string $type): Activity { return $this->activity->newInstance()->forceFill([ - 'key' => strtolower($type), - 'user_id' => $this->user->id, + 'type' => strtolower($type), + 'user_id' => user()->id, ]); } @@ -51,9 +66,9 @@ class ActivityService public function removeEntity(Entity $entity) { $entity->activity()->update([ - 'extra' => $entity->name, - 'entity_id' => 0, - 'entity_type' => '', + 'detail' => $entity->name, + 'entity_id' => null, + 'entity_type' => null, ]); } @@ -150,9 +165,9 @@ class ActivityService /** * Flashes a notification message to the session if an appropriate message is available. */ - protected function setNotification(string $activityKey) + protected function setNotification(string $type) { - $notificationTextKey = 'activities.' . $activityKey . '_notification'; + $notificationTextKey = 'activities.' . $type . '_notification'; if (trans()->has($notificationTextKey)) { $message = trans($notificationTextKey); session()->flash('success', $message);