diff --git a/config/services.yaml b/config/services.yaml index fd1400ec32..4547030e61 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -91,6 +91,11 @@ services: $persistProcessor: '@api_platform.doctrine.orm.state.persist_processor' $removeProcessor: '@api_platform.doctrine.orm.state.remove_processor' + Chamilo\CoreBundle\State\MessageProcessor: + bind: + $persistProcessor: '@api_platform.doctrine.orm.state.persist_processor' + $removeProcessor: '@api_platform.doctrine.orm.state.remove_processor' + Chamilo\CoreBundle\EventSubscriber\AnonymousUserSubscriber: tags: - name: kernel.event_subscriber diff --git a/src/CoreBundle/Entity/Message.php b/src/CoreBundle/Entity/Message.php index 241be2e640..efbbc4bd91 100644 --- a/src/CoreBundle/Entity/Message.php +++ b/src/CoreBundle/Entity/Message.php @@ -18,6 +18,7 @@ use ApiPlatform\Metadata\Post; use ApiPlatform\Metadata\Put; use Chamilo\CoreBundle\Repository\MessageRepository; use Chamilo\CoreBundle\State\MessageByGroupStateProvider; +use Chamilo\CoreBundle\State\MessageProcessor; use DateTime; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; @@ -56,7 +57,8 @@ use Symfony\Component\Validator\Constraints as Assert; denormalizationContext: [ 'groups' => ['message:write'], ], - security: "is_granted('ROLE_USER')" + security: "is_granted('ROLE_USER')", + processor: MessageProcessor::class, )] #[ApiFilter(filterClass: OrderFilter::class, properties: ['title', 'sendDate'])] #[ApiFilter( diff --git a/src/CoreBundle/State/MessageProcessor.php b/src/CoreBundle/State/MessageProcessor.php new file mode 100644 index 0000000000..dd222907fd --- /dev/null +++ b/src/CoreBundle/State/MessageProcessor.php @@ -0,0 +1,63 @@ +removeProcessor->process($data, $operation, $uriVariables, $context); + } + + $message = $this->persistProcessor->process($data, $operation, $uriVariables, $context); + + assert($message instanceof Message); + + if ($operation instanceof Post) { + if (Message::MESSAGE_TYPE_INBOX === $message->getMsgType()) { + $this->saveNotificationForInboxMessage($message); + } + } + + return $message; + } + + private function saveNotificationForInboxMessage(Message $message): void + { + $sender_info = api_get_user_info( + $message->getSender()->getId() + ); + + foreach ($message->getReceivers() as $receiver) { + $user = $receiver->getReceiver(); + + (new Notification()) + ->saveNotification( + $message->getId(), + Notification::NOTIFICATION_TYPE_MESSAGE, + [$user->getId()], + $message->getTitle(), + $message->getContent(), + $sender_info, + ) + ; + } + } +}