Internal: Message: Send mail when saving inbox message - refs BT#21613

pull/5462/head
Angel Fernando Quiroz Campos 2 years ago
parent f1cf9a60fa
commit bce4d95069
  1. 5
      config/services.yaml
  2. 4
      src/CoreBundle/Entity/Message.php
  3. 63
      src/CoreBundle/State/MessageProcessor.php

@ -91,6 +91,11 @@ services:
$persistProcessor: '@api_platform.doctrine.orm.state.persist_processor' $persistProcessor: '@api_platform.doctrine.orm.state.persist_processor'
$removeProcessor: '@api_platform.doctrine.orm.state.remove_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: Chamilo\CoreBundle\EventSubscriber\AnonymousUserSubscriber:
tags: tags:
- name: kernel.event_subscriber - name: kernel.event_subscriber

@ -18,6 +18,7 @@ use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put; use ApiPlatform\Metadata\Put;
use Chamilo\CoreBundle\Repository\MessageRepository; use Chamilo\CoreBundle\Repository\MessageRepository;
use Chamilo\CoreBundle\State\MessageByGroupStateProvider; use Chamilo\CoreBundle\State\MessageByGroupStateProvider;
use Chamilo\CoreBundle\State\MessageProcessor;
use DateTime; use DateTime;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
@ -56,7 +57,8 @@ use Symfony\Component\Validator\Constraints as Assert;
denormalizationContext: [ denormalizationContext: [
'groups' => ['message:write'], 'groups' => ['message:write'],
], ],
security: "is_granted('ROLE_USER')" security: "is_granted('ROLE_USER')",
processor: MessageProcessor::class,
)] )]
#[ApiFilter(filterClass: OrderFilter::class, properties: ['title', 'sendDate'])] #[ApiFilter(filterClass: OrderFilter::class, properties: ['title', 'sendDate'])]
#[ApiFilter( #[ApiFilter(

@ -0,0 +1,63 @@
<?php
/* For licensing terms, see /license.txt */
declare(strict_types=1);
namespace Chamilo\CoreBundle\State;
use ApiPlatform\Metadata\DeleteOperationInterface;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Post;
use ApiPlatform\State\ProcessorInterface;
use Chamilo\CoreBundle\Entity\Message;
use Notification;
final class MessageProcessor implements ProcessorInterface
{
public function __construct(
private readonly ProcessorInterface $persistProcessor,
private readonly ProcessorInterface $removeProcessor,
) {}
public function process($data, Operation $operation, array $uriVariables = [], array $context = [])
{
if ($operation instanceof DeleteOperationInterface) {
return $this->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,
)
;
}
}
}
Loading…
Cancel
Save