2023-07-19 06:03:05 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Activity\Notifications\Handlers;
|
|
|
|
|
2023-08-15 09:39:39 -04:00
|
|
|
use BookStack\Activity\ActivityType;
|
|
|
|
use BookStack\Activity\Models\Activity;
|
2023-07-19 06:03:05 -04:00
|
|
|
use BookStack\Activity\Models\Loggable;
|
2023-08-05 09:19:23 -04:00
|
|
|
use BookStack\Activity\Notifications\Messages\PageUpdateNotification;
|
|
|
|
use BookStack\Activity\Tools\EntityWatchers;
|
|
|
|
use BookStack\Activity\WatchLevels;
|
|
|
|
use BookStack\Entities\Models\Page;
|
|
|
|
use BookStack\Settings\UserNotificationPreferences;
|
2023-08-04 07:27:29 -04:00
|
|
|
use BookStack\Users\Models\User;
|
2023-07-19 06:03:05 -04:00
|
|
|
|
2023-08-05 09:19:23 -04:00
|
|
|
class PageUpdateNotificationHandler extends BaseNotificationHandler
|
2023-07-19 06:03:05 -04:00
|
|
|
{
|
2023-08-15 09:39:39 -04:00
|
|
|
public function handle(Activity $activity, Loggable|string $detail, User $user): void
|
2023-07-19 06:03:05 -04:00
|
|
|
{
|
2023-08-05 09:19:23 -04:00
|
|
|
if (!($detail instanceof Page)) {
|
|
|
|
throw new \InvalidArgumentException("Detail for page update notifications must be a page");
|
|
|
|
}
|
|
|
|
|
2023-08-15 09:39:39 -04:00
|
|
|
// Get last update from activity
|
|
|
|
$lastUpdate = $detail->activity()
|
|
|
|
->where('type', '=', ActivityType::PAGE_UPDATE)
|
|
|
|
->where('id', '!=', $activity->id)
|
|
|
|
->latest('created_at')
|
|
|
|
->first();
|
|
|
|
|
|
|
|
// Return if the same user has already updated the page in the last 15 mins
|
|
|
|
if ($lastUpdate && $lastUpdate->user_id === $user->id) {
|
|
|
|
if ($lastUpdate->created_at->gt(now()->subMinutes(15))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get active watchers
|
2023-08-05 09:19:23 -04:00
|
|
|
$watchers = new EntityWatchers($detail, WatchLevels::UPDATES);
|
|
|
|
$watcherIds = $watchers->getWatcherUserIds();
|
|
|
|
|
2023-08-15 09:39:39 -04:00
|
|
|
// Add page owner if preferences allow
|
2023-08-05 09:19:23 -04:00
|
|
|
if (!$watchers->isUserIgnoring($detail->owned_by) && $detail->ownedBy) {
|
|
|
|
$userNotificationPrefs = new UserNotificationPreferences($detail->ownedBy);
|
|
|
|
if ($userNotificationPrefs->notifyOnOwnPageChanges()) {
|
|
|
|
$watcherIds[] = $detail->owned_by;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-15 09:39:39 -04:00
|
|
|
$this->sendNotificationToUserIds(PageUpdateNotification::class, $watcherIds, $user, $detail, $detail);
|
2023-07-19 06:03:05 -04:00
|
|
|
}
|
|
|
|
}
|