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

@@ -50,11 +50,6 @@ class Notification
*/
private string $message = '';
/**
* @ORM\Column(type="json")
*/
private array $read = [];
/**
* @ORM\Column(type="string", length=255)
*/
@@ -71,9 +66,16 @@ class Notification
*/
private User $sender;
/**
* @ORM\ManyToMany(targetEntity=User::class)
* @ORM\JoinTable(name="chill_main_notification_addresses_unread")
*/
private Collection $unreadBy;
public function __construct()
{
$this->addressees = new ArrayCollection();
$this->unreadBy = new ArrayCollection();
$this->setDate(new DateTimeImmutable());
}
@@ -81,6 +83,16 @@ class Notification
{
if (!$this->addressees->contains($addressee)) {
$this->addressees[] = $addressee;
$this->addUnreadBy($addressee);
}
return $this;
}
public function addUnreadBy(User $user): self
{
if (!$this->unreadBy->contains($user)) {
$this->unreadBy->add($user);
}
return $this;
@@ -109,11 +121,6 @@ class Notification
return $this->message;
}
public function getRead(): array
{
return $this->read;
}
public function getRelatedEntityClass(): ?string
{
return $this->relatedEntityClass;
@@ -129,9 +136,32 @@ class Notification
return $this->sender;
}
public function isReadBy(User $user): bool
{
return !$this->unreadBy->contains($user);
}
public function markAsReadBy(User $user): self
{
return $this->removeUnreadBy($user);
}
public function markAsUnreadBy(User $user): self
{
return $this->addUnreadBy($user);
}
public function removeAddressee(User $addressee): self
{
$this->addressees->removeElement($addressee);
$this->unreadBy->removeElement($addressee);
return $this;
}
public function removeUnreadBy(User $user): self
{
$this->unreadBy->removeElement($user);
return $this;
}
@@ -150,13 +180,6 @@ class Notification
return $this;
}
public function setRead(array $read): self
{
$this->read = $read;
return $this;
}
public function setRelatedEntityClass(string $relatedEntityClass): self
{
$this->relatedEntityClass = $relatedEntityClass;