Skip to content

IBX-9060: Added $query parameter for filtering notifications count and loading #520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: 4.6
Choose a base branch
from
Open
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: 4 additions & 8 deletions src/contracts/Persistence/Notification/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,16 @@ public function countPendingNotifications(int $ownerId): int;
public function getNotificationById(int $notificationId): Notification;

/**
* @param int $userId
* @param int $offset
* @param int $limit
* @param string[] $query
*
* @return \Ibexa\Contracts\Core\Persistence\Notification\Notification[]
*/
public function loadUserNotifications(int $userId, int $offset, int $limit): array;
public function loadUserNotifications(int $userId, int $offset, int $limit, array $query = []): array;

/**
* @param int $currentUserId
*
* @return int
* @param string[] $query
*/
public function countNotifications(int $currentUserId): int;
public function countNotifications(int $currentUserId, array $query = []): int;

/**
* @param \Ibexa\Contracts\Core\Repository\Values\Notification\Notification $notification
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ public function __construct(NotificationService $innerService)
$this->innerService = $innerService;
}

/**
* @param string[] $query
*/
public function loadNotifications(
int $offset,
int $limit
int $limit,
array $query = []
): NotificationList {
return $this->innerService->loadNotifications($offset, $limit);
return $this->innerService->loadNotifications($offset, $limit, $query);
}

public function getNotification(int $notificationId): Notification
Expand All @@ -50,9 +54,12 @@ public function getPendingNotificationCount(): int
return $this->innerService->getPendingNotificationCount();
}

public function getNotificationCount(): int
/**
* @param string[] $query
*/
public function getNotificationCount(array $query = []): int
{
return $this->innerService->getNotificationCount();
return $this->innerService->getNotificationCount($query);
}

public function createNotification(CreateStruct $createStruct): Notification
Expand Down
15 changes: 4 additions & 11 deletions src/contracts/Repository/NotificationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,9 @@
interface NotificationService
{
/**
* Get currently logged user notifications.
*
* @param int $offset the start offset for paging
* @param int $limit the number of notifications returned
*
* @return \Ibexa\Contracts\Core\Repository\Values\Notification\NotificationList
* @param string[] $query
*/
public function loadNotifications(int $offset, int $limit): NotificationList;
public function loadNotifications(int $offset, int $limit, array $query = []): NotificationList;

/**
* Load single notification (by ID).
Expand Down Expand Up @@ -65,11 +60,9 @@ public function markNotificationAsUnread(Notification $notification): void;
public function getPendingNotificationCount(): int;

/**
* Get count of total users notifications.
*
* @return int
* @param string[] $query
*/
public function getNotificationCount(): int;
public function getNotificationCount(array $query = []): int;

/**
* Creates a new notification.
Expand Down
21 changes: 14 additions & 7 deletions src/lib/Persistence/Cache/NotificationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,17 @@ public function countPendingNotifications(int $ownerId): int
}

/**
* {@inheritdoc}
* @param string[] $query
*/
public function countNotifications(int $ownerId): int
public function countNotifications(int $ownerId, array $query = []): int
{
$cacheKeyParams = [$ownerId];
if (!empty($query)) {
$cacheKeyParams[] = json_encode($query);
}

$cacheItem = $this->cache->getItem(
$this->cacheIdentifierGenerator->generateKey(self::NOTIFICATION_COUNT_IDENTIFIER, [$ownerId], true)
$this->cacheIdentifierGenerator->generateKey(self::NOTIFICATION_COUNT_IDENTIFIER, $cacheKeyParams, true)
);

$count = $cacheItem->get();
Expand All @@ -114,9 +119,10 @@ public function countNotifications(int $ownerId): int

$this->logger->logCall(__METHOD__, [
'ownerId' => $ownerId,
'query' => $query,
]);

$count = $this->persistenceHandler->notificationHandler()->countNotifications($ownerId);
$count = $this->persistenceHandler->notificationHandler()->countNotifications($ownerId, $query);

$cacheItem->set($count);
$this->cache->save($cacheItem);
Expand Down Expand Up @@ -151,17 +157,18 @@ public function getNotificationById(int $notificationId): Notification
}

/**
* {@inheritdoc}
* @param string[] $query
*/
public function loadUserNotifications(int $userId, int $offset, int $limit): array
public function loadUserNotifications(int $userId, int $offset, int $limit, array $query = []): array
{
$this->logger->logCall(__METHOD__, [
'ownerId' => $userId,
'offset' => $offset,
'limit' => $limit,
'query' => $query,
]);

return $this->persistenceHandler->notificationHandler()->loadUserNotifications($userId, $offset, $limit);
return $this->persistenceHandler->notificationHandler()->loadUserNotifications($userId, $offset, $limit, $query);
}
}

Expand Down
19 changes: 9 additions & 10 deletions src/lib/Persistence/Legacy/Notification/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,9 @@ abstract public function getNotificationById(int $notificationId): array;
abstract public function updateNotification(Notification $notification): void;

/**
* @param int $userId
*
* @return int
* @param string[] $query
*/
abstract public function countUserNotifications(int $userId): int;
abstract public function countUserNotifications(int $userId, array $query = []): int;

/**
* Count users unread Notifications.
Expand All @@ -58,13 +56,14 @@ abstract public function countUserNotifications(int $userId): int;
abstract public function countUserPendingNotifications(int $userId): int;

/**
* @param int $userId
* @param int $offset
* @param int $limit
*
* @return array
* @param string[] $query
*/
abstract public function loadUserNotifications(int $userId, int $offset = 0, int $limit = -1): array;
abstract public function loadUserNotifications(
int $userId,
int $offset = 0,
int $limit = -1,
array $query = []
): array;

/**
* @param int $notificationId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Ibexa\Core\Persistence\Legacy\Notification\Gateway;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Query\QueryBuilder;
use Ibexa\Contracts\Core\Persistence\Notification\CreateStruct;
use Ibexa\Contracts\Core\Persistence\Notification\Notification;
use Ibexa\Core\Base\Exceptions\InvalidArgumentException;
Expand All @@ -25,20 +26,13 @@ class DoctrineDatabase extends Gateway
public const COLUMN_CREATED = 'created';
public const COLUMN_DATA = 'data';

/** @var \Doctrine\DBAL\Connection */
private $connection;
private Connection $connection;

/**
* @param \Doctrine\DBAL\Connection $connection
*/
public function __construct(Connection $connection)
{
$this->connection = $connection;
}

/**
* {@inheritdoc}
*/
public function insert(CreateStruct $createStruct): int
{
$query = $this->connection->createQueryBuilder();
Expand All @@ -62,9 +56,6 @@ public function insert(CreateStruct $createStruct): int
return (int) $this->connection->lastInsertId();
}

/**
* {@inheritdoc}
*/
public function getNotificationById(int $notificationId): array
{
$query = $this->connection->createQueryBuilder();
Expand All @@ -78,9 +69,6 @@ public function getNotificationById(int $notificationId): array
return $query->execute()->fetchAll(PDO::FETCH_ASSOC);
}

/**
* {@inheritdoc}
*/
public function updateNotification(Notification $notification): void
{
if (!isset($notification->id) || !is_numeric($notification->id)) {
Expand All @@ -100,23 +88,57 @@ public function updateNotification(Notification $notification): void
}

/**
* {@inheritdoc}
* @param string[] $query
*/
public function countUserNotifications(int $userId): int
private function applyFilters(QueryBuilder $queryBuilder, array $query): void
{
$query = $this->connection->createQueryBuilder();
$query
if (isset($query['type'])) {
$queryBuilder
->andWhere($queryBuilder->expr()->like(self::COLUMN_TYPE, ':type'))
->setParameter(':type', '%' . $query['type'] . '%');
}

if (isset($query['status']) && is_array($query['status'])) {
if (in_array('read', $query['status'])) {
$queryBuilder->andWhere($queryBuilder->expr()->eq(self::COLUMN_IS_PENDING, ':status_read'))
->setParameter(':status_read', false);
}
if (in_array('unread', $query['status'])) {
$queryBuilder->andWhere($queryBuilder->expr()->eq(self::COLUMN_IS_PENDING, ':status_unread'))
->setParameter(':status_unread', true);
}
}

if (isset($query['created_from'])) {
$queryBuilder->andWhere($queryBuilder->expr()->gte(self::COLUMN_CREATED, ':created_from'))
->setParameter(':created_from', $query['created_from'], PDO::PARAM_INT);
}

if (isset($query['created_to'])) {
$queryBuilder->andWhere($queryBuilder->expr()->lte(self::COLUMN_CREATED, ':created_to'))
->setParameter(':created_to', $query['created_to'], PDO::PARAM_INT);
}
}

/**
* @param string[] $query
*/
public function countUserNotifications(int $userId, array $query = []): int
{
$queryBuilder = $this->connection->createQueryBuilder();
$queryBuilder
->select('COUNT(' . self::COLUMN_ID . ')')
->from(self::TABLE_NOTIFICATION)
->where($query->expr()->eq(self::COLUMN_OWNER_ID, ':user_id'))
->where($queryBuilder->expr()->eq(self::COLUMN_OWNER_ID, ':user_id'))
->setParameter(':user_id', $userId, PDO::PARAM_INT);

return (int)$query->execute()->fetchColumn();
if (!empty($query)) {
$this->applyFilters($queryBuilder, $query);
}

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

/**
* {@inheritdoc}
*/
public function countUserPendingNotifications(int $userId): int
{
$query = $this->connection->createQueryBuilder();
Expand All @@ -133,30 +155,31 @@ public function countUserPendingNotifications(int $userId): int
}

/**
* {@inheritdoc}
* @param string[] $query
*/
public function loadUserNotifications(int $userId, int $offset = 0, int $limit = -1): array
public function loadUserNotifications(int $userId, int $offset = 0, int $limit = -1, array $query = []): array
{
$query = $this->connection->createQueryBuilder();
$query
$queryBuilder = $this->connection->createQueryBuilder();
$queryBuilder
->select(...$this->getColumns())
->from(self::TABLE_NOTIFICATION)
->where($query->expr()->eq(self::COLUMN_OWNER_ID, ':user_id'))
->where($queryBuilder->expr()->eq(self::COLUMN_OWNER_ID, ':user_id'))
->setFirstResult($offset);

if (!empty($query)) {
$this->applyFilters($queryBuilder, $query);
}

if ($limit > 0) {
$query->setMaxResults($limit);
$queryBuilder->setMaxResults($limit);
}

$query->orderBy(self::COLUMN_ID, 'DESC');
$query->setParameter(':user_id', $userId, PDO::PARAM_INT);
$queryBuilder->orderBy(self::COLUMN_ID, 'DESC');
$queryBuilder->setParameter(':user_id', $userId, PDO::PARAM_INT);

return $query->execute()->fetchAll(PDO::FETCH_ASSOC);
return $queryBuilder->execute()->fetchAll(PDO::FETCH_ASSOC);
}

/**
* {@inheritdoc}
*/
public function delete(int $notificationId): void
{
$query = $this->connection->createQueryBuilder();
Expand All @@ -168,9 +191,6 @@ public function delete(int $notificationId): void
$query->execute();
}

/**
* @return array
*/
private function getColumns(): array
{
return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@ public function updateNotification(Notification $notification): void
}
}

public function countUserNotifications(int $userId): int
/**
* @param string[] $query
*/
public function countUserNotifications(int $userId, array $query = []): int
{
try {
return $this->innerGateway->countUserNotifications($userId);
return $this->innerGateway->countUserNotifications($userId, $query);
} catch (DBALException | PDOException $e) {
throw DatabaseException::wrap($e);
}
Expand All @@ -70,10 +73,13 @@ public function countUserPendingNotifications(int $userId): int
}
}

public function loadUserNotifications(int $userId, int $offset = 0, int $limit = -1): array
/**
* @param string[] $query
*/
public function loadUserNotifications(int $userId, int $offset = 0, int $limit = -1, array $query = []): array
{
try {
return $this->innerGateway->loadUserNotifications($userId, $offset, $limit);
return $this->innerGateway->loadUserNotifications($userId, $offset, $limit, $query);
} catch (DBALException | PDOException $e) {
throw DatabaseException::wrap($e);
}
Expand Down
Loading
Loading