use BookStack\Activity\Models\Activity;
use BookStack\Activity\Models\Loggable;
use BookStack\Activity\Models\Webhook;
+use BookStack\Activity\Notifications\NotificationManager;
use BookStack\Entities\Models\Entity;
use BookStack\Facades\Theme;
use BookStack\Theming\ThemeEvents;
class ActivityLogger
{
+ public function __construct(
+ protected NotificationManager $notifications
+ ) {
+ $this->notifications->loadDefaultHandlers();
+ }
+
/**
* Add a generic activity event to the database.
- *
- * @param string|Loggable $detail
*/
- public function add(string $type, $detail = '')
+ public function add(string $type, string|Loggable $detail = ''): void
{
$detailToStore = ($detail instanceof Loggable) ? $detail->logDescriptor() : $detail;
$activity->detail = $detailToStore;
if ($detail instanceof Entity) {
- $activity->entity_id = $detail->id;
- $activity->entity_type = $detail->getMorphClass();
+ $activity->loggable_id = $detail->id;
+ $activity->loggable_type = $detail->getMorphClass();
}
$activity->save();
$this->setNotification($type);
$this->dispatchWebhooks($type, $detail);
+ $this->notifications->handle($activity, $detail, user());
Theme::dispatch(ThemeEvents::ACTIVITY_LOGGED, $type, $detail);
}
* and instead uses the 'extra' field with the entities name.
* Used when an entity is deleted.
*/
- public function removeEntity(Entity $entity)
+ public function removeEntity(Entity $entity): void
{
$entity->activity()->update([
- 'detail' => $entity->name,
- 'entity_id' => null,
- 'entity_type' => null,
+ 'detail' => $entity->name,
+ 'loggable_id' => null,
+ 'loggable_type' => null,
]);
}
}
}
- /**
- * @param string|Loggable $detail
- */
- protected function dispatchWebhooks(string $type, $detail): void
+ protected function dispatchWebhooks(string $type, string|Loggable $detail): void
{
$webhooks = Webhook::query()
->whereHas('trackedEvents', function (Builder $query) use ($type) {
* Log out a failed login attempt, Providing the given username
* as part of the message if the '%u' string is used.
*/
- public function logFailedLogin(string $username)
+ public function logFailedLogin(string $username): void
{
$message = config('logging.failed_login.message');
if (!$message) {