src/Entity/Chat.php line 103

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\OrderFilter;
  8. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  9. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\ExistsFilter;
  10. use App\Filter\RestrictedGroupFilter;
  11. use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
  12. use App\Repository\ChatRepository;
  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=ChatRepository::class)
  21.  * @ApiResource(
  22.  *      mercure={
  23.  *          "private"=true,
  24.  *          "normalization_context"={
  25.  *              "groups"={
  26.  *                    "chat:read:id",
  27.  *                    "chat:read:countChatMessages", 
  28.  *                    "chat:read:unreadChatMessageCount", 
  29.  *                    "chat:read:active", 
  30.  *                    "chat:read:read", 
  31.  *                    "chat:read:lastChatMessage", 
  32.  *                    "chat_message:read:id",  
  33.  *                    "chat_message:read:content", 
  34.  *                    "chat_message:read:received",
  35.  *                    "chat_message:read:read",
  36.  *                    "chat_message:read:createdAt",
  37.  *                    "chat_message:read:user", 
  38.  *                        "user:read:id",
  39.  *                        "user:read:firstName",
  40.  *                        "user:read:lastName",
  41.  *                        "user:read:client",
  42.  *                            "client:read:id",
  43.  *                            "client:read:firstName",
  44.  *                            "client:read:lastName",
  45.  *                            "client:read:avatar",
  46.  *                    "chat_message:read:attachments",
  47.  *                        "media_object:read:contentUrl"
  48.  *                  }
  49.  *              }
  50.  *      },
  51.  *      normalizationContext={
  52.  *          "groups"={"chat:read"}
  53.  *      },
  54.  *      denormalizationContext={
  55.  *          "groups"={"chat:write"}
  56.  *      },
  57.  *      itemOperations={
  58.  *          "get"={
  59.  *              "security"="is_granted('CHAT_VIEW', object)"
  60.  *          },
  61.  *          "patch"={
  62.  *              "security"="is_granted('ROLE_ADMIN')"
  63.  *          },
  64.  *          "delete"={
  65.  *              "security"="is_granted('ROLE_ADMIN')"
  66.  *          }
  67.  *      },
  68.  *      collectionOperations={
  69.  *          "get"={
  70.  *              "security"="is_granted('ROLE_USER')"
  71.  *          },
  72.  *          "post"={
  73.  *              "security"="is_granted('ROLE_ADMIN')"
  74.  *          },
  75.  *          "get_by_user_and_recipient"={
  76.  *              "security"="is_granted('ROLE_USER')",
  77.  *              "path"="/users/{user}/teams/{team}/recipients/{recipient}/chat",
  78.  *              "method"="GET",
  79.  *              "controller"="App\Controller\Api\ChatController::getChatByTeamAndUserAndRecipient"
  80.  *          }
  81.  *      }
  82.  * )
  83.  * @ApiFilter(SearchFilter::class, properties={"team.id", "active", "company.id", "type", "users.id", "countUsers", "countChatMessages"})
  84.  * @ApiFilter(ExistsFilter::class, properties={"type", "countUsers", "team", "company", "countChatMessages"})
  85.  * @ApiFilter(OrderFilter::class)
  86.  * @ApiFilter(RestrictedGroupFilter::class,
  87.  *      arguments={
  88.  *          "parameterName": "groups",
  89.  *          "overrideDefaultGroups": true,
  90.  *          "whitelist": {"chat:read"}
  91.  *      }
  92.  * )
  93.  *  @ApiFilter(PropertyFilter::class, 
  94.  *      arguments={
  95.  *          "parameterName"="fields", 
  96.  *          "overrideDefaultProperties"=true
  97.  *     }
  98.  * )
  99.  */
  100. class Chat
  101. {
  102.     const TYPE_GLOBAL = "global";
  103.     const TYPE_TEAM = "team";
  104.     const TYPE_MAIN = "main";
  105.     const TYPES = [
  106.         self::TYPE_GLOBAL => self::TYPE_GLOBAL,
  107.         self::TYPE_TEAM => self::TYPE_TEAM,
  108.     ];
  109.     /**
  110.      * @ORM\Id
  111.      * @ORM\GeneratedValue
  112.      * @ORM\Column(type="integer")
  113.      * @ApiProperty(identifier=true)
  114.      * @Groups({"chat:read", "chat:read:id", "team:read", "team_user:read"})
  115.      */
  116.     private ?int $id = null;
  117.     /**
  118.      * @ORM\Column(type="string", length=255, nullable=true, options={"default": "global"})
  119.      * @Groups({"chat:read", "chat:read:type", "client:read", "user:read", "company:read", "team_user:read", "team:read"})
  120.      * @Assert\NotBlank
  121.      */
  122.     private ?string $type = self::TYPE_GLOBAL;
  123.     /**
  124.      * @ORM\ManyToMany(targetEntity=User::class, mappedBy="chats")
  125.      * @Groups({"chat:read", "chat:write", "chat:read:users"})
  126.      */
  127.     private $users;
  128.     /**
  129.      * @ORM\Column(type="integer", nullable=true, options={"default": 0})
  130.      * @Groups({"chat:read", "chat:read:countUsers", "team:read"})
  131.      */
  132.     private ?int $countUsers = 0;
  133.     /**
  134.      * @ORM\ManyToOne(targetEntity=Team::class)
  135.      * @Groups({"chat:read", "chat:write", "chat:read:team"})
  136.      */
  137.     private ?Team $team;
  138.     /**
  139.      * @ORM\ManyToOne(targetEntity=Company::class)
  140.      * @Groups({"chat:read", "chat:write", "chat:read:company"})
  141.      */
  142.     private ?Company $company = null;
  143.     /**
  144.      * @ORM\OneToMany(targetEntity=ChatMessage::class, mappedBy="chat", orphanRemoval=true)
  145.      * @Groups({"chat:read", "chat:read:chatMessages"})
  146.      */
  147.     private $chatMessages;
  148.     /**
  149.      * @ORM\Column(type="integer", nullable=true, options={"default": 0})
  150.      * @Groups({"chat:read", "chat:read:countChatMessages", "team:read"})
  151.      */
  152.     private ?int $countChatMessages = 0;
  153.     /**
  154.      * @ORM\Column(type="boolean", nullable=true, options={"default": 1})
  155.      * @Groups({"chat:read", "chat:write", "chat:read:active", "team:read"})
  156.      */
  157.     private $active = true;
  158.     /**
  159.      * @ORM\Column(type="datetime", nullable=true)
  160.      * @Gedmo\Timestampable(on="create")
  161.      * @Groups({"chat:read", "chat:read:createdAt"})
  162.      */
  163.     private $createdAt;
  164.     /**
  165.      * @ORM\Column(type="datetime", nullable=true)
  166.      * @Gedmo\Timestampable(on="update")
  167.      * @Groups({"chat:read", "chat:read:updatedAt"})
  168.      */
  169.     private $updatedAt;
  170.     /**
  171.      * @Groups({"chat:read", "chat:read:lastChatMessage", "team:read"})
  172.      * @var ?ChatMessage
  173.      */
  174.     private $lastChatMessage = null;
  175.     /**
  176.      * Auteurs dont le lecteur ne doit pas voir les messages (ceux qu'il a bloqués), renseignés au postLoad par
  177.      * App\Service\ChatService::assignCustomFields. Seuls des identifiants : aucune entité chargée pendant l'hydratation.
  178.      *
  179.      * @var int[]
  180.      */
  181.     private array $hiddenAuthorIds = [];
  182.     /**
  183.      * Chat Général : début du challenge en cours (#3156). Les messages plus anciens ne comptent pas pour le dernier
  184.      * message affiché dans la liste des chats. Renseigné au postLoad par App\Service\ChatService::assignCustomFields ;
  185.      * null = aucune borne (autres chats, ou aucun challenge en cours).
  186.      */
  187.     private ?\DateTimeInterface $visibleSince = null;
  188.     /**
  189.      * Custom field
  190.      * @var ?string
  191.      * @Groups({"chat:read", "chat:read:name", "team:read"})
  192.      */
  193.     public $name = null;
  194.     /**
  195.      * Custom field
  196.      * @var ?MediaObject
  197.      * @Groups({"chat:read", "chat:read:image", "team:read"})
  198.      */
  199.     public $image = null;
  200.     /**
  201.      * Post Custom Field
  202.      * @Groups({"chat:read", "chat:read:read"})
  203.      */
  204.     private bool $read = false;
  205.     /**
  206.      * Post Custom Field
  207.      * @Groups({"chat:read", "chat:read:unreadChatMessageCount"})
  208.      */
  209.     private ?int $unreadChatMessageCount = null;
  210.     public function __construct()
  211.     {
  212.         $this->users = new ArrayCollection();
  213.         $this->chatMessages = new ArrayCollection();
  214.     }
  215.     public function getId(): ?int
  216.     {
  217.         return $this->id;
  218.     }
  219.     public function getType(): ?string
  220.     {
  221.         return $this->type;
  222.     }
  223.     public function setType(?string $type): self
  224.     {
  225.         $this->type = $type;
  226.         return $this;
  227.     }
  228.     /**
  229.      * @return Collection<int, User>
  230.      */
  231.     public function getUsers(): Collection
  232.     {
  233.         return $this->users;
  234.     }
  235.     public function addUser(User $user): self
  236.     {
  237.         if (!$this->users->contains($user)) {
  238.             $this->users[] = $user;
  239.             $user->addChat($this);
  240.         }
  241.         return $this;
  242.     }
  243.     public function removeUser(User $user): self
  244.     {
  245.         if ($this->users->removeElement($user)) {
  246.             $user->removeChat($this);
  247.         }
  248.         return $this;
  249.     }
  250.     public function getCountUsers(): ?int
  251.     {
  252.         return $this->countUsers;
  253.     }
  254.     public function setCountUsers(?int $countUsers): self
  255.     {
  256.         $this->countUsers = $countUsers;
  257.         return $this;
  258.     }
  259.     public function getTeam(): ?Team
  260.     {
  261.         return $this->team;
  262.     }
  263.     public function setTeam(?Team $team): self
  264.     {
  265.         $this->team = $team;
  266.         return $this;
  267.     }
  268.     public function getCompany(): ?Company
  269.     {
  270.         return $this->company;
  271.     }
  272.     public function setCompany(?Company $company): self
  273.     {
  274.         $this->company = $company;
  275.         return $this;
  276.     }
  277.     /**
  278.      * @return Collection|ChatMessage[]
  279.      */
  280.     public function getChatMessages(): Collection
  281.     {
  282.         return $this->chatMessages;
  283.     }
  284.     public function addChatMessage(ChatMessage $chatMessage): self
  285.     {
  286.         if ($chatMessage instanceof ChatMessage) {
  287.             if (!$this->chatMessages->contains($chatMessage)) {
  288.                 $this->chatMessages[] = $chatMessage;
  289.                 $chatMessage->setChat($this);
  290.             }
  291.         }
  292.         return $this;
  293.     }
  294.     public function removeChatMessage(ChatMessage $chatMessage): self
  295.     {
  296.         if ($this->chatMessages->removeElement($chatMessage)) {
  297.             // set the owning side to null (unless already changed)
  298.             if ($chatMessage->getChat() === $this) {
  299.                 $chatMessage->setChat(null);
  300.             }
  301.         }
  302.         return $this;
  303.     }
  304.     public function getCountChatMessages(): ?int
  305.     {
  306.         return $this->countChatMessages;
  307.     }
  308.     public function setCountChatMessages(?int $countChatMessages): self
  309.     {
  310.         $this->countChatMessages = $countChatMessages;
  311.         return $this;
  312.     }
  313.     public function getActive(): ?bool
  314.     {
  315.         return $this->active;
  316.     }
  317.     public function isActive(): ?bool
  318.     {
  319.         return $this->active;
  320.     }
  321.     public function setActive(?bool $active): self
  322.     {
  323.         $this->active = $active;
  324.         return $this;
  325.     }
  326.     public function getCreatedAt(): ?\DateTimeInterface
  327.     {
  328.         return $this->createdAt;
  329.     }
  330.     public function setCreatedAt(?\DateTimeInterface $createdAt): self
  331.     {
  332.         $this->createdAt = $createdAt;
  333.         return $this;
  334.     }
  335.     public function getUpdatedAt(): ?\DateTimeInterface
  336.     {
  337.         return $this->updatedAt;
  338.     }
  339.     public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
  340.     {
  341.         $this->updatedAt = $updatedAt;
  342.         return $this;
  343.     }
  344.     /**
  345.      * Dernier message visible : ni masqué par la modération, ni écrit par un auteur que le lecteur a bloqué,
  346.      * ni antérieur à la borne du chat Général (début du challenge en cours).
  347.      */
  348.     public function getLastChatMessage(): ?ChatMessage
  349.     {
  350.         if (!$this->chatMessages->isEmpty()) {
  351.             $this->lastChatMessage = null;
  352.             foreach (array_reverse($this->chatMessages->toArray()) as $chatMessage) {
  353.                 $author = $chatMessage->getUser();
  354.                 if ($this->visibleSince !== null && $chatMessage->getCreatedAt() !== null && $chatMessage->getCreatedAt() < $this->visibleSince) {
  355.                     continue;
  356.                 }
  357.                 if ($chatMessage->isActive() && !($author instanceof User && in_array($author->getId(), $this->hiddenAuthorIds, true))) {
  358.                     $this->lastChatMessage = $chatMessage;
  359.                     break;
  360.                 }
  361.             }
  362.         }
  363.         return $this->lastChatMessage;
  364.     }
  365.     public function setLastChatMessage(?ChatMessage $lastChatMessage): self
  366.     {
  367.         $this->lastChatMessage = $lastChatMessage;
  368.         return $this;
  369.     }
  370.     /**
  371.      * @param int[] $userIds
  372.      */
  373.     public function hideMessagesFrom(array $userIds): self
  374.     {
  375.         $this->hiddenAuthorIds = $userIds;
  376.         return $this;
  377.     }
  378.     public function showMessagesSince(?\DateTimeInterface $since): self
  379.     {
  380.         $this->visibleSince = $since;
  381.         return $this;
  382.     }
  383.     public function getVisibleSince(): ?\DateTimeInterface
  384.     {
  385.         return $this->visibleSince;
  386.     }
  387.     public function getRead(): ?bool
  388.     {
  389.         return $this->read;
  390.     }
  391.     /**
  392.      * @Groups({"chat:read", "chat:read:isRead"})
  393.      */
  394.     public function isRead(): ?bool
  395.     {
  396.         return $this->read;
  397.     }
  398.     public function setRead(?bool $read): self
  399.     {
  400.         $this->read = $read;
  401.         return $this;
  402.     }
  403.     public function getUnreadChatMessageCount(): ?int
  404.     {
  405.         return $this->unreadChatMessageCount;
  406.     }
  407.     public function setUnreadChatMessageCount(?int $unreadChatMessageCount): self
  408.     {
  409.         $this->unreadChatMessageCount = $unreadChatMessageCount;
  410.         return $this;
  411.     }
  412. }