+
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/contracts/Persistence/Notification/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ public function createNotification(CreateStruct $createStruct): Notification;
*/
public function updateNotification(APINotification $notification, UpdateStruct $updateStruct): Notification;

/**
* @param int[] $notificationIds
*
* @return int[]
*/
public function bulkUpdateUserNotifications(
int $ownerId,
UpdateStruct $updateStruct,
bool $pendingOnly = false,
array $notificationIds = []
): array;

/**
* Count users unread Notifications.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

abstract class NotificationServiceDecorator implements NotificationService
{
/** @var \Ibexa\Contracts\Core\Repository\NotificationService */
protected $innerService;
protected NotificationService $innerService;

public function __construct(NotificationService $innerService)
{
Expand All @@ -41,6 +40,11 @@ public function getNotification(int $notificationId): Notification
return $this->innerService->getNotification($notificationId);
}

public function markUserNotificationsAsRead(array $notificationIds = []): void
{
$this->innerService->markUserNotificationsAsRead($notificationIds);
}

public function markNotificationAsRead(Notification $notification): void
{
$this->innerService->markNotificationAsRead($notification);
Expand Down
5 changes: 5 additions & 0 deletions src/contracts/Repository/NotificationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public function findNotifications(?NotificationQuery $query = null): Notificatio
*/
public function getNotification(int $notificationId): Notification;

/**
* @param int[] $notificationIds
*/
public function markUserNotificationsAsRead(array $notificationIds = []): void;

/**
* Mark notification as read so it no longer bother the user.
*
Expand Down
31 changes: 31 additions & 0 deletions src/lib/Persistence/Cache/NotificationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,37 @@ public function createNotification(CreateStruct $createStruct): Notification
return $this->persistenceHandler->notificationHandler()->createNotification($createStruct);
}

public function bulkUpdateUserNotifications(
int $ownerId,
UpdateStruct $updateStruct,
bool $pendingOnly = false,
array $notificationIds = []
): array {
$this->logger->logCall(__METHOD__, [
'userId' => $ownerId,
'notificationIds' => $notificationIds,
]);

$updatedNotificationIds = $this->persistenceHandler
->notificationHandler()
->bulkUpdateUserNotifications($ownerId, $updateStruct, $pendingOnly, $notificationIds);

$cacheKeys = array_map(
fn (int $id): string => $this->cacheIdentifierGenerator->generateKey(self::NOTIFICATION_IDENTIFIER, [$id], true),
$updatedNotificationIds
);

$cacheKeys[] = $this->cacheIdentifierGenerator->generateKey(
self::NOTIFICATION_PENDING_COUNT_IDENTIFIER,
[$ownerId],
true
);

$this->cache->deleteItems($cacheKeys);

return $updatedNotificationIds;
}

/**
* {@inheritdoc}
*/
Expand Down
13 changes: 13 additions & 0 deletions src/lib/Persistence/Legacy/Notification/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Ibexa\Contracts\Core\Persistence\Notification\CreateStruct;
use Ibexa\Contracts\Core\Persistence\Notification\Notification;
use Ibexa\Contracts\Core\Persistence\Notification\UpdateStruct;
use Ibexa\Contracts\Core\Repository\Values\Notification\Query\NotificationQuery;

abstract class Gateway
Expand Down Expand Up @@ -42,6 +43,18 @@ abstract public function getNotificationById(int $notificationId): array;
*/
abstract public function updateNotification(Notification $notification): void;

/**
* @param int[] $notificationIds
*
* @return int[]
*/
abstract public function bulkUpdateUserNotifications(
int $ownerId,
UpdateStruct $updateStruct,
bool $pendingOnly = false,
array $notificationIds = []
): array;

abstract public function countUserNotifications(int $userId, ?NotificationQuery $query = null): int;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Doctrine\DBAL\Query\QueryBuilder;
use Ibexa\Contracts\Core\Persistence\Notification\CreateStruct;
use Ibexa\Contracts\Core\Persistence\Notification\Notification;
use Ibexa\Contracts\Core\Persistence\Notification\UpdateStruct;
use Ibexa\Contracts\Core\Repository\Values\Notification\Query\CriterionInterface;
use Ibexa\Contracts\Core\Repository\Values\Notification\Query\NotificationQuery;
use Ibexa\Core\Base\Exceptions\InvalidArgumentException;
Expand Down Expand Up @@ -82,6 +83,78 @@ public function getNotificationById(int $notificationId): array
return $query->execute()->fetchAllAssociative();
}

/**
* @param int[] $notificationIds
*
* @return int[]
*/
public function bulkUpdateUserNotifications(
int $ownerId,
UpdateStruct $updateStruct,
bool $pendingOnly = false,
array $notificationIds = []
): array {
$queryBuilder = $this->connection->createQueryBuilder();
$this->applyNotificationFilters($queryBuilder, $ownerId, $pendingOnly, $notificationIds);

$idsToUpdate = $queryBuilder->execute()->fetchFirstColumn();
if (empty($idsToUpdate)) {
return [];
}

if ($updateStruct->isPending !== null) {
$this->updateNotificationsPendingStatus($idsToUpdate, $updateStruct->isPending);
}

return array_map('intval', $idsToUpdate);
}

/**
* @param int[] $notificationIds
*/
private function applyNotificationFilters(
QueryBuilder $queryBuilder,
int $ownerId,
bool $pendingOnly,
array $notificationIds
): void {
$queryBuilder
->select(self::COLUMN_ID)
->from(self::TABLE_NOTIFICATION)
->andWhere($queryBuilder->expr()->eq(self::COLUMN_OWNER_ID, ':ownerId'))
->setParameter(':ownerId', $ownerId, ParameterType::INTEGER);

if ($pendingOnly) {
$queryBuilder->andWhere($queryBuilder->expr()->eq(self::COLUMN_IS_PENDING, ':isPendingFlag'))
->setParameter(':isPendingFlag', true, ParameterType::BOOLEAN);
}

if (!empty($notificationIds)) {
$queryBuilder->andWhere(
$queryBuilder->expr()->in(self::COLUMN_ID, ':notificationIds')
);
$queryBuilder->setParameter(':notificationIds', $notificationIds, Connection::PARAM_INT_ARRAY);
}
}

/**
* @param int[] $idsToUpdate
*/
private function updateNotificationsPendingStatus(array $idsToUpdate, bool $isPending): void
{
$updateQuery = $this->connection->createQueryBuilder();
$updateQuery
->update(self::TABLE_NOTIFICATION)
->set(self::COLUMN_IS_PENDING, ':is_pending')
->andWhere(
$updateQuery->expr()->in(self::COLUMN_ID, ':ids')
)
->setParameter(':is_pending', $isPending, ParameterType::BOOLEAN)
->setParameter(':ids', $idsToUpdate, Connection::PARAM_INT_ARRAY);

$updateQuery->execute();
}

public function updateNotification(Notification $notification): void
{
if (!isset($notification->id) || !is_numeric($notification->id)) {
Expand Down Expand Up @@ -113,7 +186,7 @@ public function countUserNotifications(int $userId, ?NotificationQuery $query =
$this->applyFilters($queryBuilder, $query->getCriteria());
}

return (int)$queryBuilder->execute()->fetchColumn();
return (int)$queryBuilder->execute()->fetchOne();
}

public function countUserPendingNotifications(int $userId): int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Doctrine\DBAL\DBALException;
use Ibexa\Contracts\Core\Persistence\Notification\CreateStruct;
use Ibexa\Contracts\Core\Persistence\Notification\Notification;
use Ibexa\Contracts\Core\Persistence\Notification\UpdateStruct;
use Ibexa\Contracts\Core\Repository\Values\Notification\Query\NotificationQuery;
use Ibexa\Core\Base\Exceptions\DatabaseException;
use Ibexa\Core\Persistence\Legacy\Notification\Gateway;
Expand Down Expand Up @@ -44,6 +45,19 @@ public function getNotificationById(int $notificationId): array
}
}

public function bulkUpdateUserNotifications(
int $ownerId,
UpdateStruct $updateStruct,
bool $pendingOnly = false,
array $notificationIds = []
): array {
try {
return $this->innerGateway->bulkUpdateUserNotifications($ownerId, $updateStruct, $pendingOnly, $notificationIds);
} catch (DBALException | PDOException $e) {
throw DatabaseException::wrap($e);
}
}

public function updateNotification(Notification $notification): void
{
try {
Expand Down
9 changes: 9 additions & 0 deletions src/lib/Persistence/Legacy/Notification/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ public function getNotificationById(int $notificationId): Notification
return reset($notification);
}

public function bulkUpdateUserNotifications(
int $ownerId,
UpdateStruct $updateStruct,
bool $pendingOnly = false,
array $notificationIds = []
): array {
return $this->gateway->bulkUpdateUserNotifications($ownerId, $updateStruct, $pendingOnly, $notificationIds);
}

/**
* {@inheritdoc}
*
Expand Down
12 changes: 12 additions & 0 deletions src/lib/Repository/NotificationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ public function getNotification(int $notificationId): APINotification
return $this->buildDomainObject($notification);
}

/**
* @param int[] $notificationIds
*/
public function markUserNotificationsAsRead(array $notificationIds = []): void
{
$currentUserId = $this->getCurrentUserId();
$updateStruct = new UpdateStruct();
$updateStruct->isPending = false;

$this->persistenceHandler->bulkUpdateUserNotifications($currentUserId, $updateStruct, true, $notificationIds);
}

public function markNotificationAsRead(APINotification $notification): void
{
$currentUserId = $this->getCurrentUserId();
Expand Down
8 changes: 8 additions & 0 deletions src/lib/Repository/SiteAccessAware/NotificationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ public function getNotification(int $notificationId): Notification
return $this->service->getNotification($notificationId);
}

/**
* @param int[] $notificationIds
*/
public function markUserNotificationsAsRead(array $notificationIds = []): void
{
$this->service->markUserNotificationsAsRead($notificationIds);
}

/**
* Mark notification as read so it no longer bother the user.
*
Expand Down
Loading
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载