src/Entity/ChatMessage.php line 105

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiFilter;
  4. use ApiPlatform\Core\Annotation\ApiProperty;
  5. use ApiPlatform\Core\Annotation\ApiResource;
  6. use ApiPlatform\Core\Annotation\ApiSubresource;
  7. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter;
  8. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  9. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  10. use App\Filter\RestrictedGroupFilter;
  11. use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
  12. use App\Repository\ChatMessageRepository;
  13. use Doctrine\Common\Collections\ArrayCollection;
  14. use Doctrine\Common\Collections\Collection;
  15. use Doctrine\ORM\Mapping as ORM;
  16. use Gedmo\Mapping\Annotation as Gedmo;
  17. use Symfony\Component\Serializer\Annotation\Groups;
  18. use Symfony\Component\Validator\Constraints as Assert;
  19. /**
  20.  * @ORM\Entity(repositoryClass=ChatMessageRepository::class)
  21.  * @ApiResource(
  22.  *      mercure={
  23.  *          "private"=true,
  24.  *          "normalization_context"={
  25.  *              "groups"={
  26.  *                  "chat_message:read:id",  
  27.  *                    "chat_message:read:content", 
  28.  *                    "chat_message:read:received",
  29.  *                    "chat_message:read:read",
  30.  *                    "chat_message:read:createdAt",
  31.  *                    "chat_message:read:active",
  32.  *                    "chat_message:read:user", 
  33.  *                        "user:read:id",
  34.  *                        "user:read:firstName",
  35.  *                         "user:read:lastName",
  36.  *                        "user:read:client",
  37.  *                            "client:read:id",
  38.  *                            "client:read:firstName",
  39.  *                            "client:read:lastName",
  40.  *                            "client:read:avatar",
  41.  *                    "chat_message:read:attachments",
  42.  *                        "media_object:read:contentUrl"
  43.  *                }
  44.  *            }
  45.  *      },
  46.  *      order={"createdAt": "DESC", "id": "DESC"},
  47.  *      normalizationContext={
  48.  *          "groups"={"chat_message:read"}
  49.  *      },
  50.  *      denormalizationContext={
  51.  *          "groups"={"chat_message:write"}
  52.  *      },
  53.  *      itemOperations={
  54.  *          "get"={
  55.  *              "security"="is_granted('CHAT_VIEW', object.getChat())"
  56.  *          },
  57.  *          "patch"={
  58.  *              "security"="is_granted('ROLE_ADMIN') or object.getUser() == user"
  59.  *          },
  60.  *          "delete"={
  61.  *              "security"="is_granted('ROLE_ADMIN') or object.getUser() == user"
  62.  *          }
  63.  *      },
  64.  *      collectionOperations={
  65.  *          "get"={
  66.  *              "security"="is_granted('ROLE_USER')"
  67.  *          }, 
  68.  *          "post"={
  69.  *              "security"="is_granted('ROLE_USER')",
  70.  *              "path"="/chat_messages",
  71.  *              "method"="POST",
  72.  *              "controller"="App\Controller\Api\ChatMessageController::post"
  73.  *          },
  74.  *          "get_by_chat"={
  75.  *             "description"="Get ChatMessages by Chat.",
  76.  *             "path"="/chats/{id}/chat_messages",
  77.  *             "method"="GET",
  78.  *             "controller"="App\Controller\Api\ChatMessageController::getChatMessagesByChat",
  79.  *             "security"="is_granted('ROLE_USER')"
  80.  *          }
  81.  *      }
  82.  * )
  83.  * @ApiFilter(SearchFilter::class, properties={
  84.  *      "chat.id", "user.id", "user.active"
  85.  * })
  86.  * @ApiFilter(DateFilter::class, properties={"createdAt"})
  87.  * @ApiFilter(OrderFilter::class)
  88.  * @ApiFilter(RestrictedGroupFilter::class,
  89.  *      arguments={
  90.  *          "parameterName": "groups",
  91.  *          "overrideDefaultGroups": true,
  92.  *          "whitelist": {"chat_message:read"}
  93.  *      }
  94.  * )
  95.  *  @ApiFilter(PropertyFilter::class, 
  96.  *      arguments={
  97.  *          "parameterName"="fields", 
  98.  *          "overrideDefaultProperties"=true
  99.  *     }
  100.  * )
  101.  */
  102. class ChatMessage
  103. {
  104.     /**
  105.      * @ORM\Id
  106.      * @ORM\GeneratedValue
  107.      * @ORM\Column(type="integer")
  108.      * @ApiProperty(identifier=true)
  109.      * @Groups({"chat_message:read", "chat_message:read:id", "chat:read"})
  110.      */
  111.     private ?int $id = null;
  112.     /**
  113.      * @ORM\ManyToOne(targetEntity=Chat::class, inversedBy="chatMessages", cascade={"persist", "refresh"})
  114.      * @Groups({"chat_message:read", "chat_message:read:chat"})
  115.      */
  116.     private ?Chat $chat = null;
  117.     /**
  118.      * @ORM\ManyToOne(targetEntity=User::class)
  119.      * @Assert\NotNull
  120.      * @Groups({"chat_message:read", "chat_message:read:user", "chat:read"})
  121.      */
  122.     private ?User $user;
  123.     /**
  124.      * @ORM\Column(name="`content`", type="text", nullable=true)
  125.      * @Groups({"chat_message:read", "chat_message:write", "chat_message:read:content", "chat:read"})
  126.      */
  127.     private ?string $content = "";
  128.     /**
  129.      * @ORM\ManyToMany(targetEntity=MediaObject::class, cascade={"persist", "refresh", "remove"})
  130.      * @Groups({"chat_message:read", "chat_message:read:attachments", "chat:read"})
  131.      */
  132.     private $attachments;
  133.     /**
  134.      * @ORM\Column(type="datetime", nullable=true)
  135.      * @Gedmo\Timestampable(on="create")
  136.      * @Groups({"chat_message:read", "chat_message:read:createdAt", "chat:read"})
  137.      */
  138.     private $createdAt;
  139.     /**
  140.      * @ORM\Column(type="datetime", nullable=true)
  141.      * @Gedmo\Timestampable(on="update")
  142.      * @Groups({"chat_message:read", "chat_message:read:updatedAt", "chat:read"})
  143.      */
  144.     private $updatedAt;
  145.     /**
  146.      * false : message masqué par la modération. Jamais servi aux clients (App\Doctrine\ActiveFilter et
  147.      * App\Doctrine\ActiveEntityExtension filtrent toute entité qui a une propriété « active »).
  148.      *
  149.      * @ORM\Column(type="boolean", options={"default": 1})
  150.      * @Groups({"chat_message:read", "chat_message:read:active"})
  151.      */
  152.     private bool $active = true;
  153.     /**
  154.      * Post Custom Field
  155.      * Permet d'envoyer un message à un autre utilisateur.
  156.      * @Groups({"chat_message:read"})
  157.      */
  158.     private $recipient;
  159.     /**
  160.      * Post Custom Field
  161.      * Permet d'envoyer un message à une Team.
  162.      * @Groups({"chat_message:read"})
  163.      */
  164.     private $team;
  165.     /**
  166.      * Post Custom Field
  167.      * @Groups({"chat_message:read", "chat_message:read:received", "chat:read"})
  168.      */
  169.     private $received = false;
  170.     /**
  171.      * Post Custom Field
  172.      * @Groups({"chat_message:read", "chat_message:read:read", "chat:read"})
  173.      */
  174.     private $read = false;
  175.     public function __construct()
  176.     {
  177.         $this->attachments = new ArrayCollection();
  178.     }
  179.     public function getId(): ?int
  180.     {
  181.         return $this->id;
  182.     }
  183.     public function getChat(): ?Chat
  184.     {
  185.         return $this->chat;
  186.     }
  187.     public function setChat(?Chat $chat): self
  188.     {
  189.         $this->chat = $chat;
  190.         return $this;
  191.     }
  192.     public function getUser(): ?User
  193.     {
  194.         return $this->user;
  195.     }
  196.     public function setUser(?User $user): self
  197.     {
  198.         $this->user = $user;
  199.         return $this;
  200.     }
  201.     public function getContent(): ?string
  202.     {
  203.         return $this->content;
  204.     }
  205.     public function setContent(?string $content): self
  206.     {
  207.         $this->content = $content;
  208.         return $this;
  209.     }
  210.     /**
  211.      * @return Collection<int, MediaObject>
  212.      */
  213.     public function getAttachments(): Collection
  214.     {
  215.         return $this->attachments;
  216.     }
  217.     public function setAttachmentFile($file): self
  218.     {
  219.         $mediaObject = new MediaObject();
  220.         $mediaObject->setFile($file);
  221.         $this->addAttachment($mediaObject);
  222.         return $this;
  223.     }
  224.     public function setAttachmentFiles(?array $files): self
  225.     {
  226.         // Supprime tous les MediaObject 
  227.         if (!$this->attachments->isEmpty()) {
  228.             foreach ($this->attachments->toArray() as $attachment) {
  229.                 $this->removeAttachment($attachment);
  230.             }
  231.         }
  232.         foreach ($files as $file) {
  233.             $this->setAttachmentFile($file);
  234.         }
  235.         return $this;
  236.     }
  237.     public function addAttachment(MediaObject $attachment): self
  238.     {
  239.         if (!$this->attachments->contains($attachment)) {
  240.             $this->attachments[] = $attachment;
  241.         }
  242.         return $this;
  243.     }
  244.     public function removeAttachment(MediaObject $attachment): self
  245.     {
  246.         $this->attachments->removeElement($attachment);
  247.         return $this;
  248.     }
  249.     public function getReceived(): ?bool
  250.     {
  251.         return $this->received;
  252.     }
  253.     /**
  254.      * @Groups({"chat_message:read", "chat_message:read:isReceived"})
  255.      */
  256.     public function isReceived(): ?bool
  257.     {
  258.         return $this->received;
  259.     }
  260.     public function setReceived(?bool $received): self
  261.     {
  262.         $this->received = $received;
  263.         return $this;
  264.     }
  265.     public function getRead(): ?bool
  266.     {
  267.         return $this->read;
  268.     }
  269.     /**
  270.      * @Groups({"chat_message:read", "chat_message:read:isRead"})
  271.      */
  272.     public function isRead(): ?bool
  273.     {
  274.         return $this->read;
  275.     }
  276.     public function setRead(?bool $read): self
  277.     {
  278.         $this->read = $read;
  279.         return $this;
  280.     }
  281.     public function getCreatedAt(): ?\DateTimeInterface
  282.     {
  283.         return $this->createdAt;
  284.     }
  285.     public function setCreatedAt(?\DateTimeInterface $createdAt): self
  286.     {
  287.         $this->createdAt = $createdAt;
  288.         return $this;
  289.     }
  290.     public function getUpdatedAt(): ?\DateTimeInterface
  291.     {
  292.         return $this->updatedAt;
  293.     }
  294.     public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
  295.     {
  296.         $this->updatedAt = $updatedAt;
  297.         return $this;
  298.     }
  299.     public function isActive(): bool
  300.     {
  301.         return $this->active;
  302.     }
  303.     public function setActive(bool $active): self
  304.     {
  305.         $this->active = $active;
  306.         return $this;
  307.     }
  308.     public function getTeam(): ?Team
  309.     {
  310.         return $this->team;
  311.     }
  312.     public function setTeam(?Team $team): self
  313.     {
  314.         $this->team = $team;
  315.         return $this;
  316.     }
  317.     public function getRecipient(): ?User
  318.     {
  319.         return $this->recipient;
  320.     }
  321.     public function setRecipient(?User $recipient): self
  322.     {
  323.         $this->recipient = $recipient;
  324.         return $this;
  325.     }
  326. }