notificaiton: improve NotificationHandlerInterface + mark notification

as unread if a comment is appended
This commit is contained in:
2022-01-04 19:02:03 +01:00
parent ebc6d21ba6
commit 4ef024516c
5 changed files with 40 additions and 4 deletions

View File

@@ -15,11 +15,14 @@ use Chill\MainBundle\Doctrine\Model\TrackCreationInterface;
use Chill\MainBundle\Doctrine\Model\TrackUpdateInterface;
use DateTimeImmutable;
use DateTimeInterface;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PreFlushEventArgs;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table("chill_main_notification_comment")
* @ORM\HasLifecycleCallbacks
*/
class NotificationComment implements TrackCreationInterface, TrackUpdateInterface
{
@@ -52,6 +55,13 @@ class NotificationComment implements TrackCreationInterface, TrackUpdateInterfac
*/
private ?Notification $notification = null;
/**
* Internal variable which detect if the comment is just persisted.
*
* @internal
*/
private bool $recentlyPersisted = false;
/**
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
@@ -98,6 +108,32 @@ class NotificationComment implements TrackCreationInterface, TrackUpdateInterfac
return $this->updatedBy;
}
/**
* @ORM\PreFlush
*/
public function onFlushMarkNotificationAsUnread(PreFlushEventArgs $eventArgs): void
{
if ($this->recentlyPersisted) {
foreach ($this->getNotification()->getAddressees() as $addressee) {
if ($this->getCreatedBy() !== $addressee) {
$this->getNotification()->markAsUnreadBy($addressee);
}
}
if ($this->getNotification()->getSender() !== $this->getCreatedBy()) {
$this->getNotification()->markAsUnreadBy($this->getNotification()->getSender());
}
}
}
/**
* @ORM\PrePersist
*/
public function onPrePersist(LifecycleEventArgs $eventArgs): void
{
$this->recentlyPersisted = true;
}
public function setContent(string $content): self
{
$this->content = $content;