2026-03-19 22:09:44 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MintyPHP\Module\Notifications\Service;
|
|
|
|
|
|
|
|
|
|
use MintyPHP\Module\Notifications\Repository\NotificationRepositoryInterface;
|
2026-03-20 00:05:03 +01:00
|
|
|
use MintyPHP\Repository\Tenant\UserTenantRepositoryInterface;
|
2026-03-19 22:09:44 +01:00
|
|
|
|
|
|
|
|
class NotificationService
|
|
|
|
|
{
|
2026-03-20 00:05:03 +01:00
|
|
|
private const DEDUPE_WINDOW_MINUTES = 30;
|
|
|
|
|
private const DEDUPE_TYPES = [
|
|
|
|
|
'user.created' => true,
|
|
|
|
|
'user.deleted' => true,
|
|
|
|
|
'user.activated' => true,
|
|
|
|
|
'user.deactivated' => true,
|
|
|
|
|
'user.assignment_changed' => true,
|
|
|
|
|
];
|
|
|
|
|
|
2026-03-19 22:09:44 +01:00
|
|
|
public function __construct(
|
2026-03-20 00:05:03 +01:00
|
|
|
private readonly NotificationRepositoryInterface $notificationRepository,
|
|
|
|
|
private readonly UserTenantRepositoryInterface $userTenantRepository
|
2026-03-19 22:09:44 +01:00
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @return list<array<string, mixed>> */
|
2026-03-20 00:05:03 +01:00
|
|
|
public function listForUser(int $userId, ?int $tenantId = null, int $limit = 50): array
|
2026-03-19 22:09:44 +01:00
|
|
|
{
|
|
|
|
|
if ($userId <= 0) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-20 00:05:03 +01:00
|
|
|
return $this->notificationRepository->listByUser($userId, $tenantId, $limit, 0);
|
2026-03-19 22:09:44 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-20 00:05:03 +01:00
|
|
|
public function unreadCount(int $userId, ?int $tenantId = null): int
|
2026-03-19 22:09:44 +01:00
|
|
|
{
|
|
|
|
|
if ($userId <= 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-20 00:05:03 +01:00
|
|
|
return $this->notificationRepository->countUnreadByUser($userId, $tenantId);
|
2026-03-19 22:09:44 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-20 00:05:03 +01:00
|
|
|
public function markAsRead(int $userId, int $notificationId, ?int $tenantId = null): bool
|
2026-03-19 22:09:44 +01:00
|
|
|
{
|
|
|
|
|
if ($userId <= 0 || $notificationId <= 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-20 00:05:03 +01:00
|
|
|
return $this->notificationRepository->markRead($notificationId, $userId, $tenantId);
|
2026-03-19 22:09:44 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-20 00:05:03 +01:00
|
|
|
public function markAllAsRead(int $userId, ?int $tenantId = null): int
|
2026-03-19 22:09:44 +01:00
|
|
|
{
|
|
|
|
|
if ($userId <= 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-20 00:05:03 +01:00
|
|
|
return $this->notificationRepository->markAllReadByUser($userId, $tenantId);
|
2026-03-19 22:09:44 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-20 00:05:03 +01:00
|
|
|
public function dismiss(int $userId, int $notificationId, ?int $tenantId = null): bool
|
2026-03-19 22:09:44 +01:00
|
|
|
{
|
|
|
|
|
if ($userId <= 0 || $notificationId <= 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-20 00:05:03 +01:00
|
|
|
return $this->notificationRepository->delete($notificationId, $userId, $tenantId);
|
2026-03-19 22:09:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function createForUser(
|
|
|
|
|
int $recipientUserId,
|
|
|
|
|
?int $tenantId,
|
|
|
|
|
string $type,
|
|
|
|
|
string $title,
|
|
|
|
|
?string $body,
|
|
|
|
|
?string $link,
|
|
|
|
|
array $data
|
|
|
|
|
): int|false {
|
|
|
|
|
if ($recipientUserId <= 0 || $type === '' || $title === '') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-20 00:05:03 +01:00
|
|
|
$dedupeMeta = $this->buildDedupeMeta($recipientUserId, $tenantId, $type, $data);
|
|
|
|
|
|
2026-03-19 22:09:44 +01:00
|
|
|
return $this->notificationRepository->create([
|
|
|
|
|
'recipient_user_id' => $recipientUserId,
|
|
|
|
|
'tenant_id' => $tenantId,
|
|
|
|
|
'type' => $type,
|
|
|
|
|
'title' => $title,
|
|
|
|
|
'body' => $body,
|
|
|
|
|
'link' => $link,
|
|
|
|
|
'data' => $data ?: null,
|
2026-03-20 00:05:03 +01:00
|
|
|
'dedupe_fingerprint' => $dedupeMeta['fingerprint'] ?? null,
|
|
|
|
|
'dedupe_bucket' => $dedupeMeta['bucket'] ?? null,
|
2026-03-19 22:09:44 +01:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fan-out: create notification for all active users in a tenant, optionally excluding actor.
|
|
|
|
|
*
|
|
|
|
|
* @return int Number of notifications created
|
|
|
|
|
*/
|
|
|
|
|
public function createForTenantUsers(
|
|
|
|
|
int $tenantId,
|
|
|
|
|
string $type,
|
|
|
|
|
string $title,
|
|
|
|
|
?string $body,
|
|
|
|
|
?string $link,
|
|
|
|
|
array $data,
|
|
|
|
|
?int $excludeUserId
|
|
|
|
|
): int {
|
|
|
|
|
if ($tenantId <= 0 || $type === '' || $title === '') {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-20 00:05:03 +01:00
|
|
|
$userIds = $this->userTenantRepository->listActiveUserIdsByTenantId($tenantId);
|
2026-03-19 22:09:44 +01:00
|
|
|
$created = 0;
|
|
|
|
|
|
|
|
|
|
foreach ($userIds as $userId) {
|
|
|
|
|
if ($excludeUserId !== null && $userId === $excludeUserId) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$result = $this->createForUser($userId, $tenantId, $type, $title, $body, $link, $data);
|
|
|
|
|
if ($result !== false) {
|
|
|
|
|
$created++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $created;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fan-out: create notification only for tenant users whose ID is in $allowedUserIds.
|
|
|
|
|
*
|
|
|
|
|
* @param array<int, mixed> $allowedUserIds Flipped array (user ID as key) for fast lookup
|
|
|
|
|
* @return int Number of notifications created
|
|
|
|
|
*/
|
|
|
|
|
public function createForTenantAdminUsers(
|
|
|
|
|
int $tenantId,
|
|
|
|
|
string $type,
|
|
|
|
|
string $title,
|
|
|
|
|
?string $body,
|
|
|
|
|
?string $link,
|
|
|
|
|
array $data,
|
|
|
|
|
?int $excludeUserId,
|
|
|
|
|
array $allowedUserIds
|
|
|
|
|
): int {
|
|
|
|
|
if ($tenantId <= 0 || $type === '' || $title === '' || $allowedUserIds === []) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-20 00:05:03 +01:00
|
|
|
$userIds = $this->userTenantRepository->listActiveUserIdsByTenantId($tenantId);
|
2026-03-19 22:09:44 +01:00
|
|
|
$created = 0;
|
|
|
|
|
|
|
|
|
|
foreach ($userIds as $userId) {
|
|
|
|
|
if (!isset($allowedUserIds[$userId])) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if ($excludeUserId !== null && $userId === $excludeUserId) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$result = $this->createForUser($userId, $tenantId, $type, $title, $body, $link, $data);
|
|
|
|
|
if ($result !== false) {
|
|
|
|
|
$created++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $created;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function purgeOldRead(int $retentionDays = 90): int
|
|
|
|
|
{
|
|
|
|
|
return $this->notificationRepository->purgeReadOlderThanDays($retentionDays);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function deleteAllForUser(int $userId): int
|
|
|
|
|
{
|
|
|
|
|
if ($userId <= 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->notificationRepository->deleteAllByUser($userId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-03-20 00:05:03 +01:00
|
|
|
* @param array<string, mixed> $data
|
|
|
|
|
* @return array{fingerprint: string, bucket: int}|array{}
|
2026-03-19 22:09:44 +01:00
|
|
|
*/
|
2026-03-20 00:05:03 +01:00
|
|
|
private function buildDedupeMeta(int $recipientUserId, ?int $tenantId, string $type, array $data): array
|
2026-03-19 22:09:44 +01:00
|
|
|
{
|
2026-03-20 00:05:03 +01:00
|
|
|
if (!isset(self::DEDUPE_TYPES[$type])) {
|
2026-03-19 22:09:44 +01:00
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-20 00:05:03 +01:00
|
|
|
$target = $this->dedupeTargetFromData($data);
|
|
|
|
|
if ($target === '') {
|
2026-03-19 22:09:44 +01:00
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-20 00:05:03 +01:00
|
|
|
$tenantScope = $tenantId !== null && $tenantId > 0 ? $tenantId : 0;
|
|
|
|
|
$raw = implode('|', [
|
|
|
|
|
'recipient:' . $recipientUserId,
|
|
|
|
|
'tenant:' . $tenantScope,
|
|
|
|
|
'type:' . $type,
|
|
|
|
|
'target:' . $target,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'fingerprint' => hash('sha256', $raw),
|
|
|
|
|
'bucket' => intdiv(time(), self::DEDUPE_WINDOW_MINUTES * 60),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array<string, mixed> $data
|
|
|
|
|
*/
|
|
|
|
|
private function dedupeTargetFromData(array $data): string
|
|
|
|
|
{
|
|
|
|
|
$explicit = trim((string) ($data['dedupe_target'] ?? ''));
|
|
|
|
|
if ($explicit !== '') {
|
|
|
|
|
return $explicit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$targetUserId = (int) ($data['user_id'] ?? 0);
|
|
|
|
|
if ($targetUserId > 0) {
|
|
|
|
|
return 'user:' . $targetUserId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$targetUuid = trim((string) ($data['uuid'] ?? ''));
|
|
|
|
|
if ($targetUuid !== '') {
|
|
|
|
|
return 'uuid:' . $targetUuid;
|
2026-03-19 22:09:44 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-20 00:05:03 +01:00
|
|
|
return '';
|
2026-03-19 22:09:44 +01:00
|
|
|
}
|
2026-03-20 00:05:03 +01:00
|
|
|
|
2026-03-19 22:09:44 +01:00
|
|
|
}
|