-
Notifications
You must be signed in to change notification settings - Fork 514
Message: Fix message status update on deletion for sender and receiver - refs BT#21887 #5695
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
use ApiPlatform\Metadata\Delete; | ||
use ApiPlatform\Metadata\Get; | ||
use ApiPlatform\Metadata\GetCollection; | ||
use ApiPlatform\Metadata\Patch; | ||
use ApiPlatform\Metadata\Post; | ||
use ApiPlatform\Metadata\Put; | ||
use Chamilo\CoreBundle\Filter\SearchOrFilter; | ||
|
@@ -51,6 +52,20 @@ | |
provider: MessageByGroupStateProvider::class | ||
), | ||
new Post(securityPostDenormalize: "is_granted('CREATE', object)"), | ||
new Patch( | ||
uriTemplate: '/messages/{id}/delete-for-user', | ||
inputFormats: ['json' => ['application/json']], | ||
security: "is_granted('ROLE_USER')", | ||
output: false, | ||
processor: MessageProcessor::class, | ||
), | ||
new Patch( | ||
uriTemplate: '/messages/{id}/check-and-update-status', | ||
inputFormats: ['json' => ['application/json']], | ||
security: "is_granted('ROLE_USER')", | ||
output: false, | ||
processor: MessageProcessor::class, | ||
), | ||
], | ||
normalizationContext: [ | ||
'groups' => ['message:read'], | ||
|
@@ -62,17 +77,15 @@ | |
processor: MessageProcessor::class, | ||
)] | ||
#[ApiFilter(filterClass: OrderFilter::class, properties: ['title', 'sendDate'])] | ||
#[ApiFilter( | ||
filterClass: SearchFilter::class, | ||
properties: [ | ||
'msgType' => 'exact', | ||
'status' => 'exact', | ||
'sender' => 'exact', | ||
'receivers.receiver' => 'exact', | ||
'receivers.tags.tag' => 'exact', | ||
'parent' => 'exact', | ||
] | ||
)] | ||
#[ApiFilter(SearchFilter::class, properties: [ | ||
'msgType' => 'exact', | ||
'status' => 'exact', | ||
'sender' => 'exact', | ||
'receivers.receiver' => 'exact', | ||
'receivers.receiverType' => 'exact', | ||
'receivers.tags.tag' => 'exact', | ||
'parent' => 'exact', | ||
])] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line indented incorrectly; expected at least 4 spaces, found 0 |
||
#[ApiFilter( | ||
BooleanFilter::class, | ||
properties: ['receivers.read'] | ||
|
@@ -85,7 +98,7 @@ class Message | |
public const MESSAGE_TYPE_INVITATION = 6; | ||
public const MESSAGE_TYPE_CONVERSATION = 7; | ||
// status | ||
public const MESSAGE_STATUS_DELETED = 3; | ||
public const MESSAGE_STATUS_SENDER_DELETED = 3; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line indented incorrectly; expected 8 spaces, found 4 |
||
public const MESSAGE_STATUS_DRAFT = 4; | ||
public const MESSAGE_STATUS_INVITATION_PENDING = 5; | ||
public const MESSAGE_STATUS_INVITATION_ACCEPTED = 6; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,7 @@ class MessageRelUser | |
{ | ||
public const TYPE_TO = 1; | ||
public const TYPE_CC = 2; | ||
public const TYPE_SENDER = 8; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line indented incorrectly; expected 8 spaces, found 4 |
||
|
||
#[Groups(['message_rel_user:read'])] | ||
#[ORM\Column(name: 'id', type: 'integer')] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
|
||
use ApiPlatform\Metadata\DeleteOperationInterface; | ||
use ApiPlatform\Metadata\Operation; | ||
use ApiPlatform\Metadata\Patch; | ||
use ApiPlatform\Metadata\Post; | ||
use ApiPlatform\State\ProcessorInterface; | ||
use Chamilo\CoreBundle\Entity\Message; | ||
|
@@ -16,6 +17,8 @@ | |
use Chamilo\CoreBundle\Repository\ResourceNodeRepository; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Notification; | ||
use Symfony\Bundle\SecurityBundle\Security; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Vich\UploaderBundle\Storage\FlysystemStorage; | ||
|
||
final class MessageProcessor implements ProcessorInterface | ||
|
@@ -26,6 +29,8 @@ public function __construct( | |
private readonly FlysystemStorage $storage, | ||
private readonly EntityManagerInterface $entityManager, | ||
private readonly ResourceNodeRepository $resourceNodeRepository, | ||
private readonly Security $security, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line indented incorrectly; expected 4 spaces, found 8 |
||
private readonly RequestStack $requestStack | ||
) {} | ||
|
||
public function process($data, Operation $operation, array $uriVariables = [], array $context = []) | ||
|
@@ -34,6 +39,14 @@ public function process($data, Operation $operation, array $uriVariables = [], a | |
return $this->removeProcessor->process($data, $operation, $uriVariables, $context); | ||
} | ||
|
||
if ($operation instanceof Patch && str_contains($operation->getUriTemplate(), 'delete-for-user')) { | ||
return $this->processDeleteForUser($data); | ||
} | ||
|
||
if ($operation instanceof Patch && str_contains($operation->getUriTemplate(), 'check-and-update-status')) { | ||
return $this->checkAndUpdateMessageStatus($data); | ||
} | ||
|
||
/** @var Message $message */ | ||
$message = $this->persistProcessor->process($data, $operation, $uriVariables, $context); | ||
|
||
|
@@ -47,12 +60,66 @@ public function process($data, Operation $operation, array $uriVariables = [], a | |
} | ||
} | ||
|
||
$user = $this->security->getUser(); | ||
if (!$user) { | ||
throw new \LogicException('User not found.'); | ||
} | ||
$messageRelUser = new MessageRelUser(); | ||
$messageRelUser->setMessage($message); | ||
$messageRelUser->setReceiver($user); | ||
$messageRelUser->setReceiverType(MessageRelUser::TYPE_SENDER); | ||
$this->entityManager->persist($messageRelUser); | ||
|
||
if ($message->getMsgType() === Message::MESSAGE_TYPE_INBOX) { | ||
$this->saveNotificationForInboxMessage($message); | ||
} | ||
|
||
$this->entityManager->flush(); | ||
|
||
if ($operation instanceof Post) { | ||
if (Message::MESSAGE_TYPE_INBOX === $message->getMsgType()) { | ||
$this->saveNotificationForInboxMessage($message); | ||
} | ||
return $message; | ||
} | ||
|
||
private function processDeleteForUser($data): Message | ||
{ | ||
/** @var Message $message */ | ||
$message = $data; | ||
|
||
$request = $this->requestStack->getCurrentRequest(); | ||
if (!$request) { | ||
throw new \LogicException('Cannot get current request'); | ||
} | ||
|
||
$requestData = json_decode($request->getContent(), true); | ||
if (!isset($requestData['userId'])) { | ||
throw new \InvalidArgumentException('The field userId is required.'); | ||
} | ||
|
||
$userId = $requestData['userId']; | ||
$messageRelUserRepository = $this->entityManager->getRepository(MessageRelUser::class); | ||
$messageRelUser = $messageRelUserRepository->findOneBy([ | ||
'message' => $message, | ||
'receiver' => $userId, | ||
]); | ||
|
||
if ($messageRelUser) { | ||
$this->entityManager->remove($messageRelUser); | ||
$this->entityManager->flush(); | ||
} | ||
|
||
return $message; | ||
} | ||
|
||
private function checkAndUpdateMessageStatus($data): Message | ||
{ | ||
/** @var Message $message */ | ||
$message = $data; | ||
|
||
$messageRelUserRepository = $this->entityManager->getRepository(MessageRelUser::class); | ||
$remainingReceivers = $messageRelUserRepository->count(['message' => $message]); | ||
|
||
if ($remainingReceivers === 0) { | ||
$message->setStatus(Message::MESSAGE_STATUS_SENDER_DELETED); | ||
$this->entityManager->flush(); | ||
} | ||
|
||
return $message; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.