Fix tenant photo moderation and guest updates
This commit is contained in:
@@ -525,13 +525,13 @@ class PhotoController extends Controller
|
||||
]);
|
||||
|
||||
// Only tenant admins can moderate
|
||||
if (isset($validated['status']) && ! $this->tokenHasScope($request, 'tenant:write')) {
|
||||
if (isset($validated['status']) && ! $this->tokenHasScope($request, 'tenant-admin')) {
|
||||
return ApiError::response(
|
||||
'insufficient_scope',
|
||||
'Insufficient Scopes',
|
||||
'You are not allowed to moderate photos for this event.',
|
||||
Response::HTTP_FORBIDDEN,
|
||||
['required_scope' => 'tenant:write']
|
||||
['required_scope' => 'tenant-admin']
|
||||
);
|
||||
}
|
||||
|
||||
@@ -823,6 +823,11 @@ class PhotoController extends Controller
|
||||
|
||||
private function tokenHasScope(Request $request, string $scope): bool
|
||||
{
|
||||
$accessToken = $request->user()?->currentAccessToken();
|
||||
if ($accessToken && $accessToken->can($scope)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$scopes = $request->user()->scopes ?? ($request->attributes->get('decoded_token')['scopes'] ?? []);
|
||||
|
||||
if (! is_array($scopes)) {
|
||||
|
||||
@@ -5,11 +5,19 @@ namespace App\Listeners\GuestNotifications;
|
||||
use App\Enums\GuestNotificationAudience;
|
||||
use App\Enums\GuestNotificationType;
|
||||
use App\Events\GuestPhotoUploaded;
|
||||
use App\Models\GuestNotification;
|
||||
use App\Models\Photo;
|
||||
use App\Services\GuestNotificationService;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
class SendPhotoUploadedNotification
|
||||
{
|
||||
private const DEDUPE_WINDOW_SECONDS = 30;
|
||||
|
||||
private const GROUP_WINDOW_MINUTES = 10;
|
||||
|
||||
private const MAX_GROUP_PHOTOS = 6;
|
||||
|
||||
/**
|
||||
* @param int[] $milestones
|
||||
*/
|
||||
@@ -25,7 +33,20 @@ class SendPhotoUploadedNotification
|
||||
? sprintf('%s hat gerade ein Foto gemacht 🎉', $guestLabel)
|
||||
: 'Es gibt neue Fotos!';
|
||||
|
||||
$this->notifications->createNotification(
|
||||
$recent = $this->findRecentPhotoNotification($event->event->id);
|
||||
if ($recent) {
|
||||
if ($this->shouldSkipDuplicate($recent, $event->photoId, $title)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$notification = $this->updateGroupedNotification($recent, $event->photoId);
|
||||
$this->markUploaderRead($notification, $event->guestIdentifier);
|
||||
$this->maybeCreateMilestoneNotification($event, $guestLabel);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$notification = $this->notifications->createNotification(
|
||||
$event->event,
|
||||
GuestNotificationType::PHOTO_ACTIVITY,
|
||||
$title,
|
||||
@@ -34,11 +55,15 @@ class SendPhotoUploadedNotification
|
||||
'audience_scope' => GuestNotificationAudience::ALL,
|
||||
'payload' => [
|
||||
'photo_id' => $event->photoId,
|
||||
'photo_ids' => [$event->photoId],
|
||||
'count' => 1,
|
||||
],
|
||||
'expires_at' => now()->addHours(3),
|
||||
]
|
||||
);
|
||||
|
||||
$this->markUploaderRead($notification, $event->guestIdentifier);
|
||||
|
||||
$this->maybeCreateMilestoneNotification($event, $guestLabel);
|
||||
}
|
||||
|
||||
@@ -87,4 +112,94 @@ class SendPhotoUploadedNotification
|
||||
|
||||
return $guestIdentifier;
|
||||
}
|
||||
|
||||
private function findRecentPhotoNotification(int $eventId): ?GuestNotification
|
||||
{
|
||||
$cutoff = Carbon::now()->subMinutes(self::GROUP_WINDOW_MINUTES);
|
||||
|
||||
return GuestNotification::query()
|
||||
->where('event_id', $eventId)
|
||||
->where('type', GuestNotificationType::PHOTO_ACTIVITY)
|
||||
->active()
|
||||
->notExpired()
|
||||
->where('created_at', '>=', $cutoff)
|
||||
->orderByDesc('id')
|
||||
->first();
|
||||
}
|
||||
|
||||
private function shouldSkipDuplicate(GuestNotification $notification, int $photoId, string $title): bool
|
||||
{
|
||||
$payload = $notification->payload;
|
||||
if (is_array($payload)) {
|
||||
$payloadIds = array_filter(
|
||||
array_map(
|
||||
fn ($value) => is_numeric($value) ? (int) $value : null,
|
||||
(array) ($payload['photo_ids'] ?? [])
|
||||
),
|
||||
fn ($value) => $value !== null && $value > 0
|
||||
);
|
||||
if (in_array($photoId, $payloadIds, true)) {
|
||||
return true;
|
||||
}
|
||||
if (is_numeric($payload['photo_id'] ?? null) && (int) $payload['photo_id'] === $photoId) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
$cutoff = Carbon::now()->subSeconds(self::DEDUPE_WINDOW_SECONDS);
|
||||
if ($notification->created_at instanceof Carbon && $notification->created_at->greaterThanOrEqualTo($cutoff)) {
|
||||
return $notification->title === $title;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function updateGroupedNotification(GuestNotification $notification, int $photoId): GuestNotification
|
||||
{
|
||||
$payload = is_array($notification->payload) ? $notification->payload : [];
|
||||
$photoIds = array_filter(
|
||||
array_map(
|
||||
fn ($value) => is_numeric($value) ? (int) $value : null,
|
||||
(array) ($payload['photo_ids'] ?? [])
|
||||
),
|
||||
fn ($value) => $value !== null && $value > 0
|
||||
);
|
||||
$photoIds[] = $photoId;
|
||||
$photoIds = array_values(array_unique($photoIds));
|
||||
$photoIds = array_slice($photoIds, 0, self::MAX_GROUP_PHOTOS);
|
||||
|
||||
$existingCount = is_numeric($payload['count'] ?? null)
|
||||
? max(1, (int) $payload['count'])
|
||||
: max(1, count($photoIds) - 1);
|
||||
$newCount = $existingCount + 1;
|
||||
|
||||
$notification->forceFill([
|
||||
'title' => $this->buildGroupedTitle($newCount),
|
||||
'payload' => [
|
||||
'count' => $newCount,
|
||||
'photo_ids' => $photoIds,
|
||||
],
|
||||
])->save();
|
||||
|
||||
return $notification;
|
||||
}
|
||||
|
||||
private function buildGroupedTitle(int $count): string
|
||||
{
|
||||
if ($count <= 1) {
|
||||
return 'Es gibt neue Fotos!';
|
||||
}
|
||||
|
||||
return sprintf('Es gibt %d neue Fotos!', $count);
|
||||
}
|
||||
|
||||
private function markUploaderRead(GuestNotification $notification, string $guestIdentifier): void
|
||||
{
|
||||
$guestIdentifier = trim($guestIdentifier);
|
||||
if ($guestIdentifier === '' || $guestIdentifier === 'anonymous') {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->notifications->markAsRead($notification, $guestIdentifier);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,6 +126,36 @@ class GuestNotificationService
|
||||
return null;
|
||||
}
|
||||
|
||||
$photoId = Arr::get($payload, 'photo_id');
|
||||
if (is_numeric($photoId)) {
|
||||
$photoId = max(1, (int) $photoId);
|
||||
} else {
|
||||
$photoId = null;
|
||||
}
|
||||
|
||||
$photoIds = Arr::get($payload, 'photo_ids');
|
||||
if (is_array($photoIds)) {
|
||||
$photoIds = array_values(array_unique(array_filter(array_map(function ($value) {
|
||||
if (! is_numeric($value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$int = (int) $value;
|
||||
|
||||
return $int > 0 ? $int : null;
|
||||
}, $photoIds))));
|
||||
$photoIds = array_slice($photoIds, 0, 10);
|
||||
} else {
|
||||
$photoIds = [];
|
||||
}
|
||||
|
||||
$count = Arr::get($payload, 'count');
|
||||
if (is_numeric($count)) {
|
||||
$count = max(1, min(9999, (int) $count));
|
||||
} else {
|
||||
$count = null;
|
||||
}
|
||||
|
||||
$cta = Arr::get($payload, 'cta');
|
||||
if (is_array($cta)) {
|
||||
$cta = [
|
||||
@@ -142,6 +172,9 @@ class GuestNotificationService
|
||||
|
||||
$clean = array_filter([
|
||||
'cta' => $cta,
|
||||
'photo_id' => $photoId,
|
||||
'photo_ids' => $photoIds,
|
||||
'count' => $count,
|
||||
]);
|
||||
|
||||
return $clean === [] ? null : $clean;
|
||||
|
||||
Reference in New Issue
Block a user