src/Entity/ChatThread.php line 62

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Filter\ChatThreadFilter;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use App\Repository\ChatThreadRepository;
  6. use ApiPlatform\Core\Annotation\ApiFilter;
  7. use App\Entity\Traits\TimestampableEntity;
  8. use Doctrine\Common\Collections\Collection;
  9. use ApiPlatform\Core\Annotation\ApiProperty;
  10. use ApiPlatform\Core\Annotation\ApiResource;
  11. use App\Entity\Interfaces\OwnerMappedInterface;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. use App\Entity\Interfaces\ContainerMappedInterface;
  14. use Symfony\Component\Serializer\Annotation\Groups;
  15. use Symfony\Component\Validator\Constraints as Assert;
  16. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  17. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  18. /**
  19.  * @ApiResource(
  20.  *      normalizationContext={"groups"={"ChatThread:Read"}, "skip_null_values"=false},
  21.  *      denormalizationContext={"groups"={"ChatThread:Write"}},
  22.  *      collectionOperations={
  23.  *          "get"={
  24.  *              "security"="is_granted('IS_AUTHENTICATED_FULLY')",
  25.  *              "normalization_context"={"groups"={"ChatThread:Read", "ChatThread:MuteNotification"}, "skip_null_values"=false}
  26.  *          },
  27.  *          "get_my"={
  28.  *              "path"="/chat_threads/get-my",
  29.  *              "method"="GET",
  30.  *              "normalization_context"={"groups"={"ChatThread:Read", "ChatThread:MuteNotification"}, "skip_null_values"=false},
  31.  *              "security"="is_granted('IS_AUTHENTICATED_FULLY')"
  32.  *          },
  33.  *          "post"={"security"="is_granted('IS_AUTHENTICATED_FULLY')"}
  34.  *      },
  35.  *      itemOperations={
  36.  *          "get"={"security"="is_granted('IS_CO_ANY_SPE_CHATTHREAD', object)"},
  37.  *          "put"={"security"="is_granted('IS_CO_ANY_SPE_CHATTHREAD', object)"},
  38.  *          "patch"={"security"="is_granted('IS_CO_ANY_SPE_CHATTHREAD', object)"},
  39.  *          "patch_mute"={
  40.  *              "route_name"="api_chat_threads_mute",
  41.  *              "method"="PATCH",
  42.  *              "security"="is_granted('IS_CO_ANY_SPE_CHATTHREAD', object)",
  43.  *              "denormalization_context"={"groups"={"ChatThread:Mute"}}
  44.  *          },
  45.  *          "patch_leave_group"={
  46.  *              "route_name"="api_chat_threads_leave_group",
  47.  *              "method"="PATCH",
  48.  *              "security"="is_granted('IS_CO_ANY_SPE_CHATTHREAD', object)",
  49.  *              "denormalization_context"={"groups"={"ChatThread:LeaveGroup"}}
  50.  *          },
  51.  *          "delete"={"security"="is_granted('IS_CO_ANY_SPE_CHATTHREAD', object)"}
  52.  *      }
  53.  * )
  54.  * @ApiFilter(SearchFilter::class, properties={"container.id": "exact", "name": "partial", "status": "exact", "threadType": "exact", "owner.id", "exact"})
  55.  * @ApiFilter(OrderFilter::class, properties={"id": "ASC", "lastMessageAt", "createdAt", "updatedAt"})
  56.  * @ApiFilter(ChatThreadFilter::class, properties={"search": "partial"})
  57.  * @ORM\Entity(repositoryClass=ChatThreadRepository::class)
  58.  */
  59. class ChatThread implements ContainerMappedInterface
  60. {
  61.     /**
  62.      * Hook timestampable behavior
  63.      * updates createdAt, updatedAt fields
  64.      */
  65.     use TimestampableEntity;
  66.     /**
  67.      * @ORM\Id
  68.      * @ORM\GeneratedValue
  69.      * @ORM\Column(type="integer")
  70.      * @Groups({"ChatThread:Read"})
  71.      */
  72.     private $id;
  73.     /**
  74.      * @ORM\Column(type="string", length=128)
  75.      * @Groups({"ChatThread:Read", "ChatThread:Write"})
  76.      *
  77.      * @Assert\NotBlank(message="validation.chatThread:name.notBlank")
  78.      * @Assert\Length(max=128, maxMessage="validation.chatThread:name.max")
  79.      */
  80.     private $name;
  81.     /**
  82.      * @ORM\Column(type="string", length=32)
  83.      * @ApiProperty(
  84.      *     attributes={
  85.      *         "openapi_context"={
  86.      *             "type"="string",
  87.      *             "enum"={self::THREAD_TYPE_O2O, self::THREAD_TYPE_GROUP}
  88.      *         }
  89.      *     }
  90.      * )
  91.      * @Groups({"ChatThread:Read", "ChatThread:Write"})
  92.      *
  93.      * @Assert\NotBlank(message="validation.chatThread:threadType.notBlank")
  94.      * @Assert\Length(max=32, maxMessage="validation.chatThread:threadType.max")
  95.      * @Assert\Choice({self::THREAD_TYPE_O2O, self::THREAD_TYPE_GROUP})
  96.      */
  97.     public $threadType;
  98.     public const THREAD_TYPE_O2O 'O2O';
  99.     public const THREAD_TYPE_GROUP 'GROUP';
  100.     /**
  101.      * @ORM\Column(type="string", length=32)
  102.      * @ApiProperty(
  103.      *     attributes={
  104.      *         "openapi_context"={
  105.      *             "type"="string",
  106.      *             "enum"={self::STATUS_NEW, self::STATUS_ARCHIVED}
  107.      *         }
  108.      *     }
  109.      * )
  110.      * @Groups({"ChatThread:Read", "ChatThread:Write"})
  111.      *
  112.      * @Assert\NotBlank(message="validation.chatThread:status.notBlank")
  113.      * @Assert\Length(max=32, maxMessage="validation.chatThread:status.max")
  114.      * @Assert\Choice({self::STATUS_NEW, self::STATUS_ARCHIVED})
  115.      */
  116.     private $status;
  117.     public const STATUS_NEW 'NEW';
  118.     public const STATUS_ARCHIVED 'ARCHIVED';
  119.     /**
  120.      * @ORM\ManyToOne(targetEntity=Container::class)
  121.      * @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
  122.      * @Groups({"ChatThread:Read", "ChatThread:Write"})
  123.      */
  124.     private $container;
  125.     /**
  126.      * @ORM\ManyToMany(targetEntity=User::class, inversedBy="chatThreads")
  127.      * @Groups({"ChatThread:Read", "ChatThread:Write"})
  128.      */
  129.     private $users;
  130.     /**
  131.      * @ORM\OneToMany(targetEntity=ChatMessage::class, mappedBy="chatThread")
  132.      */
  133.     private $chatMessages;
  134.     /**
  135.      * @ORM\Column(type="datetime", nullable=true)
  136.      * @Groups({"ChatThread:Read"})
  137.      */
  138.     private $lastMessageAt;
  139.     /**
  140.      * @ORM\ManyToOne(targetEntity=User::class)
  141.      * @ORM\JoinColumn(nullable=true, onDelete="CASCADE")
  142.      * @Groups({"ChatThread:Read", "ChatThread:Write"})
  143.      */
  144.     public $owner;
  145.     /**
  146.      * @ORM\OneToMany(targetEntity=MuteThreadNotification::class, mappedBy="chatThread", orphanRemoval=true, cascade={"persist"})
  147.      * @Groups({"ChatThread:MuteNotification"})
  148.      */
  149.     private $muteThreadNotifications;
  150.     private $newUsers;
  151.     public function __construct()
  152.     {
  153.         $this->users = new ArrayCollection();
  154.         $this->chatMessages = new ArrayCollection();
  155.         $this->muteThreadNotifications = new ArrayCollection();
  156.     }
  157.     public function getId(): ?int
  158.     {
  159.         return $this->id;
  160.     }
  161.     public function getName()
  162.     {
  163.         return $this->name;
  164.     }
  165.     public function setName($name)
  166.     {
  167.         $this->name $name;
  168.         return $this;
  169.     }
  170.     public function getThreadType()
  171.     {
  172.         return $this->threadType;
  173.     }
  174.     public function setThreadType($threadType)
  175.     {
  176.         $this->threadType $threadType;
  177.         return $this;
  178.     }
  179.     public function getStatus(): ?string
  180.     {
  181.         return $this->status;
  182.     }
  183.     public function setStatus(string $status): self
  184.     {
  185.         $this->status $status;
  186.         return $this;
  187.     }
  188.     public function getContainer(): ?Container
  189.     {
  190.         return $this->container;
  191.     }
  192.     public function setContainer(?Container $container): self
  193.     {
  194.         $this->container $container;
  195.         return $this;
  196.     }
  197.     /**
  198.      * @return Collection|User[]
  199.      */
  200.     public function getUsers(): Collection
  201.     {
  202.         return $this->users;
  203.     }
  204.     public function addUser(User $user): self
  205.     {
  206.         $nu = [];
  207.         if (!$this->users->contains($user)) {
  208.             $this->users[] = $user;
  209.             $nu[] = $user->getId();
  210.         }
  211.         $this->setNewUsers($nu);
  212.         return $this;
  213.     }
  214.     public function removeUser(User $user): self
  215.     {
  216.         $this->users->removeElement($user);
  217.         return $this;
  218.     }
  219.     /**
  220.      * @return Collection|ChatMessage[]
  221.      */
  222.     public function getChatMessages(): Collection
  223.     {
  224.         return $this->chatMessages;
  225.     }
  226.     public function addChatMessage(ChatMessage $chatMessage): self
  227.     {
  228.         if (!$this->chatMessages->contains($chatMessage)) {
  229.             $this->chatMessages[] = $chatMessage;
  230.             $chatMessage->setSession($this);
  231.         }
  232.         return $this;
  233.     }
  234.     public function removeChatMessage(ChatMessage $chatMessage): self
  235.     {
  236.         if ($this->chatMessages->removeElement($chatMessage)) {
  237.             // set the owning side to null (unless already changed)
  238.             if ($chatMessage->getSession() === $this) {
  239.                 $chatMessage->setSession(null);
  240.             }
  241.         }
  242.         return $this;
  243.     }
  244.     public function getLastMessageAt(): ?\DateTimeInterface
  245.     {
  246.         return $this->lastMessageAt;
  247.     }
  248.     public function setLastMessageAt(?\DateTimeInterface $lastMessageAt): self
  249.     {
  250.         $this->lastMessageAt $lastMessageAt;
  251.         return $this;
  252.     }
  253.     public function getOwner(): ?User
  254.     {
  255.         return $this->owner;
  256.     }
  257.     public function setOwner(?User $owner): self
  258.     {
  259.         $this->owner $owner;
  260.         return $this;
  261.     }
  262.     /**
  263.      * @return Collection<int, MuteThreadNotification>
  264.      */
  265.     public function getMuteThreadNotifications(): Collection
  266.     {
  267.         return $this->muteThreadNotifications;
  268.     }
  269.     public function addMuteThreadNotification(MuteThreadNotification $muteThreadNotification): self
  270.     {
  271.         if (!$this->muteThreadNotifications->contains($muteThreadNotification)) {
  272.             $this->muteThreadNotifications[] = $muteThreadNotification;
  273.             $muteThreadNotification->setChatThread($this);
  274.         }
  275.         return $this;
  276.     }
  277.     public function removeMuteThreadNotification(MuteThreadNotification $muteThreadNotification): self
  278.     {
  279.         if ($this->muteThreadNotifications->removeElement($muteThreadNotification)) {
  280.             // set the owning side to null (unless already changed)
  281.             if ($muteThreadNotification->getChatThread() === $this) {
  282.                 $muteThreadNotification->setChatThread(null);
  283.             }
  284.         }
  285.         return $this;
  286.     }
  287.     public function isUserMuteThreadNotification(User $user): bool
  288.     {
  289.         foreach ($this->muteThreadNotifications as $mn) {
  290.             if ($mn->getUser()->getId() === $user->getId()) {
  291.                 return true;
  292.             }
  293.         }
  294.         return false;
  295.     }
  296.     public function getNewUsers()
  297.     {
  298.         return $this->newUsers;
  299.     }
  300.     public function setNewUsers($newUsers)
  301.     {
  302.         $this->newUsers $newUsers;
  303.         return $this;
  304.     }
  305. }