fix loading of unread addressees in notification

This commit is contained in:
2021-12-28 19:40:38 +01:00
parent d5d64cdf65
commit b323ddae05
3 changed files with 166 additions and 10 deletions

View File

@@ -20,19 +20,21 @@ use Doctrine\ORM\Mapping as ORM;
* @ORM\Entity
* @ORM\Table(
* name="chill_main_notification",
* uniqueConstraints={
* @ORM\UniqueConstraint(columns={"relatedEntityClass", "relatedEntityId"})
* }
* )
* @ORM\HasLifecycleCallbacks
*/
class Notification
{
private array $addedAddresses = [];
/**
* @ORM\ManyToMany(targetEntity=User::class)
* @ORM\JoinTable(name="chill_main_notification_addresses_user")
*/
private Collection $addressees;
private ?ArrayCollection $addressesOnLoad = null;
/**
* @ORM\Column(type="datetime_immutable")
*/
@@ -60,6 +62,8 @@ class Notification
*/
private int $relatedEntityId;
private array $removedAddresses = [];
/**
* @ORM\ManyToOne(targetEntity=User::class)
* @ORM\JoinColumn(nullable=false)
@@ -83,7 +87,7 @@ class Notification
{
if (!$this->addressees->contains($addressee)) {
$this->addressees[] = $addressee;
$this->addUnreadBy($addressee);
$this->addedAddresses[] = $addressee;
}
return $this;
@@ -103,6 +107,11 @@ class Notification
*/
public function getAddressees(): Collection
{
// keep a copy to compute changes later
if (null === $this->addressesOnLoad) {
$this->addressesOnLoad = new ArrayCollection($this->addressees->toArray());
}
return $this->addressees;
}
@@ -156,10 +165,29 @@ class Notification
return $this->addUnreadBy($user);
}
/**
* @ORM\PreFlush
*/
public function registerUnread()
{
foreach ($this->addedAddresses as $addressee) {
$this->addUnreadBy($addressee);
}
if (null !== $this->addressesOnLoad) {
foreach ($this->addressees as $existingAddresse) {
if (!$this->addressesOnLoad->contains($existingAddresse)) {
$this->addUnreadBy($existingAddresse);
}
}
}
}
public function removeAddressee(User $addressee): self
{
$this->addressees->removeElement($addressee);
$this->removeUnreadBy($addressee);
if ($this->addressees->removeElement($addressee)) {
$this->removedAddresses[] = $addressee;
}
return $this;
}