notification: store users which are unread instead of read, and in

dedicated table

The query "which are the unread notification" is much more frequent than
the read one. We then store the unread items in a dedicated table.
This commit is contained in:
2021-12-26 01:00:50 +01:00
parent f6f0786d38
commit bd3919efcb
9 changed files with 283 additions and 79 deletions

View File

@@ -13,25 +13,75 @@ namespace Chill\MainBundle\Repository;
use Chill\MainBundle\Entity\Notification;
use Chill\MainBundle\Entity\User;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ObjectRepository;
final class NotificationRepository implements ObjectRepository
{
private EntityManagerInterface $em;
private EntityRepository $repository;
public function __construct(EntityManagerInterface $entityManager)
{
$this->em = $entityManager;
$this->repository = $entityManager->getRepository(Notification::class);
}
public function countAllForAttendee(User $addressee): int // TODO passer à attendees avec S
public function countAllForAttendee(User $addressee): int
{
$query = $this->queryAllForAttendee($addressee, $countQuery = true);
return $this->queryByAddressee($addressee)
->select('count(n)')
->getQuery()
->getSingleScalarResult();
}
return $query->getSingleScalarResult();
public function countAllForSender(User $sender): int
{
return $this->queryBySender($sender)
->select('count(n)')
->getQuery()
->getSingleScalarResult();
}
public function countUnreadByUser(User $user): int
{
$sql = 'SELECT count(*) AS c FROM chill_main_notification_addresses_unread WHERE user_id = ?';
$rsm = new Query\ResultSetMapping();
$rsm->addScalarResult('c', 'c', Types::INTEGER);
$nq = $this->em->createNativeQuery($sql, $rsm);
return $nq->getSingleScalarResult();
}
public function countUnreadByUserWhereAddressee(User $user): int
{
$qb = $this->repository->createQueryBuilder('n');
$qb
->select('count(n)')
->where($qb->expr()->isMemberOf(':user', 'n.addressees'))
->andWhere($qb->expr()->isMemberOf(':user', 'n.unreadBy'))
->setParameter('user', $user);
return $qb->getQuery()->getSingleScalarResult();
}
public function countUnreadByUserWhereSender(User $user): int
{
$qb = $this->repository->createQueryBuilder('n');
$qb
->select('count(n)')
->where($qb->expr()->eq('n.sender', ':user'))
->andWhere($qb->expr()->isMemberOf(':user', 'n.unreadBy'))
->setParameter('user', $user);
return $qb->getQuery()->getSingleScalarResult();
}
public function find($id, $lockMode = null, $lockVersion = null): ?Notification
@@ -53,9 +103,9 @@ final class NotificationRepository implements ObjectRepository
*
* @return Notification[]
*/
public function findAllForAttendee(User $addressee, $limit = null, $offset = null): array // TODO passer à attendees avec S
public function findAllForAttendee(User $addressee, $limit = null, $offset = null): array
{
$query = $this->queryAllForAttendee($addressee);
$query = $this->queryByAddressee($addressee)->select('n');
if ($limit) {
$query = $query->setMaxResults($limit);
@@ -65,7 +115,22 @@ final class NotificationRepository implements ObjectRepository
$query = $query->setFirstResult($offset);
}
return $query->getResult();
return $query->getQuery()->getResult();
}
public function findAllForSender(User $sender, $limit = null, $offset = null): array
{
$query = $this->queryBySender($sender)->select('n');
if ($limit) {
$query = $query->setMaxResults($limit);
}
if ($offset) {
$query = $query->setFirstResult($offset);
}
return $query->getQuery()->getResult();
}
/**
@@ -89,22 +154,25 @@ final class NotificationRepository implements ObjectRepository
return Notification::class;
}
private function queryAllForAttendee(User $addressee, bool $countQuery = false): Query
private function queryByAddressee(User $addressee, bool $countQuery = false): QueryBuilder
{
$qb = $this->repository->createQueryBuilder('n');
$select = 'n';
if ($countQuery) {
$select = 'count(n)';
}
$qb
->select($select)
->join('n.addressees', 'a')
->where('a = :addressee')
->where($qb->expr()->isMemberOf(':addressee', 'n.addressees'))
->setParameter('addressee', $addressee);
return $qb->getQuery();
return $qb;
}
private function queryBySender(User $sender): QueryBuilder
{
$qb = $this->repository->createQueryBuilder('n');
$qb
->where($qb->expr()->eq('n.sender', ':sender'))
->setParameter('sender', $sender);
return $qb;
}
}