Skip to content

Commit

Permalink
Internal: API: Message: Allow to add attachments by create message en…
Browse files Browse the repository at this point in the history
…dpoint - refs BT#21648
  • Loading branch information
AngelFQC committed May 27, 2024
1 parent 112d4e6 commit 3712943
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 100 deletions.
55 changes: 0 additions & 55 deletions src/CoreBundle/Controller/Api/CreateMessageAttachmentAction.php

This file was deleted.

33 changes: 28 additions & 5 deletions src/CoreBundle/Entity/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,14 @@ class Message
/**
* @var Collection<int, MessageAttachment>
*/
#[Groups(['message:read'])]
#[ORM\OneToMany(mappedBy: 'message', targetEntity: MessageAttachment::class, cascade: ['remove', 'persist'])]
#[Assert\Valid]
#[Groups(['message:read', 'message:write'])]
#[ORM\OneToMany(
mappedBy: 'message',
targetEntity: MessageAttachment::class,
cascade: ['persist'],
orphanRemoval: true,
)]
protected Collection $attachments;

#[ORM\OneToMany(mappedBy: 'message', targetEntity: MessageFeedback::class, orphanRemoval: true)]
Expand Down Expand Up @@ -374,10 +380,27 @@ public function getAttachments(): Collection
return $this->attachments;
}

public function addAttachment(MessageAttachment $attachment): self
public function addAttachment(MessageAttachment $attachment): static
{
if (!$this->attachments->contains($attachment)) {
$this->attachments->add($attachment);
$attachment
->setMessage($this)
->setParent($this->sender)
->setCreator($this->sender)
;
}

return $this;
}

public function removeAttachment(MessageAttachment $attachment): static
{
$this->attachments->add($attachment);
$attachment->setMessage($this);
if ($this->attachments->removeElement($attachment)) {
if ($attachment->getMessage() === $this) {
$attachment->setMessage(null);
}
}

return $this;
}
Expand Down
65 changes: 26 additions & 39 deletions src/CoreBundle/Entity/MessageAttachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use Chamilo\CoreBundle\Controller\Api\CreateMessageAttachmentAction;
use Chamilo\CoreBundle\Repository\Node\MessageAttachmentRepository;
use Doctrine\ORM\Mapping as ORM;
use Stringable;
Expand All @@ -20,38 +17,6 @@
types: ['http://schema.org/MediaObject'],
operations: [
new Get(),
new GetCollection(),
new Post(
controller: CreateMessageAttachmentAction::class,
openapiContext: [
'requestBody' => [
'content' => [
'multipart/form-data' => [
'schema' => [
'type' => 'object',
'properties' => [
'file' => [
'type' => 'string',
'format' => 'binary',
],
'messageId' => [
'type' => 'integer',
],
],
],
],
],
],
],
security: 'is_granted(\'ROLE_USER\')',
validationContext: [
'groups' => [
'Default',
'message_attachment:create',
],
],
deserialize: false
),
],
normalizationContext: [
'groups' => ['message:read'],
Expand All @@ -68,20 +33,24 @@ class MessageAttachment extends AbstractResource implements ResourceInterface, S

#[ORM\Column(name: 'path', type: 'string', length: 255, nullable: false)]
protected string $path;
#[Groups(['message:read'])]

#[Groups(['message:read', 'message:write'])]
#[ORM\Column(name: 'comment', type: 'text', nullable: true)]
protected ?string $comment = null;

#[ORM\Column(name: 'size', type: 'integer', nullable: false)]
protected int $size;

#[ORM\ManyToOne(targetEntity: Message::class, cascade: ['persist'], inversedBy: 'attachments')]
#[ORM\ManyToOne(targetEntity: Message::class, inversedBy: 'attachments')]
#[ORM\JoinColumn(name: 'message_id', referencedColumnName: 'id', nullable: false)]
protected Message $message;

#[ORM\Column(name: 'filename', type: 'string', length: 255, nullable: false)]
protected string $filename;

#[Groups(['message:write'])]
protected ResourceFile $resourceFileToAttach;

public function __construct()
{
$this->size = 0;
Expand Down Expand Up @@ -152,12 +121,12 @@ public function setSize(int $size): self
return $this;
}

public function getMessage(): Message
public function getMessage(): ?Message
{
return $this->message;
}

public function setMessage(Message $message): self
public function setMessage(?Message $message): static
{
$this->message = $message;

Expand Down Expand Up @@ -188,4 +157,22 @@ public function setResourceName(string $name): self
{
return $this->setFilename($name);
}

public function getResourceFileToAttach(): ResourceFile
{
return $this->resourceFileToAttach;
}

public function setResourceFileToAttach(ResourceFile $resourceFileToAttach): self
{
$this
->setFilename($resourceFileToAttach->getOriginalName())
->setSize($resourceFileToAttach->getSize())
->setPath($resourceFileToAttach->getTitle())
;

$this->resourceFileToAttach = $resourceFileToAttach;

return $this;
}
}
35 changes: 34 additions & 1 deletion src/CoreBundle/State/MessageProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,21 @@
use ApiPlatform\Metadata\Post;
use ApiPlatform\State\ProcessorInterface;
use Chamilo\CoreBundle\Entity\Message;
use Chamilo\CoreBundle\Entity\MessageAttachment;
use Chamilo\CoreBundle\Entity\MessageRelUser;
use Chamilo\CoreBundle\Repository\ResourceNodeRepository;
use Doctrine\ORM\EntityManagerInterface;
use Notification;
use Vich\UploaderBundle\Storage\FlysystemStorage;

final class MessageProcessor implements ProcessorInterface
{
public function __construct(
private readonly ProcessorInterface $persistProcessor,
private readonly ProcessorInterface $removeProcessor,
private readonly FlysystemStorage $storage,
private readonly EntityManagerInterface $entityManager,
private readonly ResourceNodeRepository $resourceNodeRepository,
) {}

public function process($data, Operation $operation, array $uriVariables = [], array $context = [])
Expand All @@ -27,9 +34,20 @@ public function process($data, Operation $operation, array $uriVariables = [], a
return $this->removeProcessor->process($data, $operation, $uriVariables, $context);
}

/** @var Message $message */
$message = $this->persistProcessor->process($data, $operation, $uriVariables, $context);

\assert($message instanceof Message);
foreach ($message->getAttachments() as $attachment) {
$attachment->resourceNode->setResourceFile(
$attachment->getResourceFileToAttach()
);

foreach ($message->getReceivers() as $receiver) {
$attachment->addUserLink($receiver->getReceiver());
}
}

$this->entityManager->flush();

if ($operation instanceof Post) {
if (Message::MESSAGE_TYPE_INBOX === $message->getMsgType()) {
Expand All @@ -52,6 +70,20 @@ private function saveNotificationForInboxMessage(Message $message): void
->getValues()
;

$attachmentList = [];

/** @var MessageAttachment $messageAttachment */
foreach ($message->getAttachments() as $messageAttachment) {
$stream = $this->resourceNodeRepository->getResourceNodeFileStream(
$messageAttachment->resourceNode
);

$attachmentList[] = [
'stream' => $stream,
'filename' => $messageAttachment->getFilename(),
];
}

(new Notification())
->saveNotification(
$message->getId(),
Expand All @@ -60,6 +92,7 @@ private function saveNotificationForInboxMessage(Message $message): void
$message->getTitle(),
$message->getContent(),
$sender_info,
$attachmentList,
)
;
}
Expand Down

0 comments on commit 3712943

Please sign in to comment.