notificaiton: improve NotificationHandlerInterface + mark notification

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

View File

@ -25,7 +25,7 @@ final class ActivityNotificationHandler implements NotificationHandlerInterface
$this->activityRepository = $activityRepository;
}
public function getTemplate(array $options = []): string
public function getTemplate(Notification $notification, array $options = []): string
{
return '@ChillActivity/Activity/showInNotification.html.twig';
}

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;

View File

@ -18,7 +18,7 @@ interface NotificationHandlerInterface
/**
* Return the template path (twig file).
*/
public function getTemplate(array $options = []): string;
public function getTemplate(Notification $notification, array $options = []): string;
/**
* Return an array which will be passed as data for the template.

View File

@ -45,7 +45,7 @@ final class NotificationHandlerManager
public function getTemplate(Notification $notification, array $options = []): string
{
return $this->getHandler($notification, $options)->getTemplate($options);
return $this->getHandler($notification, $options)->getTemplate($notification, $options);
}
public function getTemplateData(Notification $notification, array $options = []): array

View File

@ -25,7 +25,7 @@ final class AccompanyingPeriodNotificationHandler implements NotificationHandler
$this->accompanyingPeriodRepository = $accompanyingPeriodRepository;
}
public function getTemplate(array $options = []): string
public function getTemplate(Notification $notification, array $options = []): string
{
return 'ChillPersonBundle:AccompanyingPeriod:showInNotification.html.twig';
}