mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 22:53:49 +00:00
fix loading of unread addressees in notification
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user