src/EventSubscriber/UserSubscriber.php line 244

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Campaign;
  4. use App\Entity\SystemEmail;
  5. use App\Entity\User;
  6. use App\Repository\SystemEmailRepository;
  7. use App\Repository\UserRepository;
  8. use App\Service\FirebaseCloudeMessagingService;
  9. use App\Service\FrontAPIService;
  10. use App\Service\MessageService;
  11. use App\Service\NotificationService;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use App\Enum\AdminMail;
  14. use App\Enum\Notification;
  15. use App\Event\User\PasswordResetEmailEvent;
  16. use App\Event\User\UserMentionnedEvent;
  17. use App\Event\User\SendEmailBeforeDeletedEvent;
  18. use App\Event\User\SendEmailOneMonthBeforeDeletedEvent;
  19. use App\Event\User\UserLoginWithoutPasswordEvent;
  20. use App\Event\User\UserReactionEvent;
  21. use App\Repository\ChatNotificationRepository;
  22. use App\Service\ShortcodeService;
  23. use Symfony\Component\Mime\Address;
  24. use App\Repository\FileMessageRepository;
  25. use App\Repository\MessageRepository;
  26. use App\Service\GoogleStorageService;
  27. use Twig\Environment;
  28. use App\Entity\DiscussionGroup;
  29. class UserSubscriber implements EventSubscriberInterface
  30. {
  31.     public function __construct(
  32.         private FrontAPIService $frontAPIService,
  33.         private SystemEmailRepository $systemEmailRepository,
  34.         private UserRepository $userRepository,
  35.         private ShortcodeService $shortcodeService,
  36.         private NotificationService $notificationService,
  37.         private ChatNotificationRepository $chatNotificationRepository,
  38.         private FileMessageRepository $fileMessageRepository,
  39.         private MessageRepository $messageRepository,
  40.         private GoogleStorageService $googleStorageService
  41.         private MessageService $messageService
  42.         private Environment $twig,
  43.         private FirebaseCloudeMessagingService $firebaseCloudeMessagingService,
  44.     ){}
  45.     public static function getSubscribedEvents()
  46.     {
  47.         return [
  48.             UserMentionnedEvent::NAME => "onUserMentionnedInTchat",
  49.             UserReactionEvent::NAME=>"onUserReactionInMessage",
  50.             PasswordResetEmailEvent::NAME => 'onUserResetPassword',
  51.             SendEmailBeforeDeletedEvent::NAME => 'onSendEmailBeforeDeletedEvent',
  52.             SendEmailOneMonthBeforeDeletedEvent::NAME => 'onSendEmailOneMonthBeforeDeletedEvent',
  53.             UserLoginWithoutPasswordEvent::NAME => 'onUserLoginWithoutPasswordEvent'
  54.         ];
  55.     }
  56.     public function onUserLoginWithoutPasswordEvent(UserLoginWithoutPasswordEvent $event){
  57.         $email $this->systemEmailRepository->findOneBy(['code' => SystemEmail::LOGIN_WITHOUT_PASSWORD]);
  58.         $user $event->getUser(); 
  59.         $this->notificationService->create($email$user$user);
  60.         
  61.     }
  62.     public function onUserResetPassword(PasswordResetEmailEvent $event){
  63.         $email $this->systemEmailRepository->findOneBy(['code' => SystemEmail::RESET_PASSWORD]);
  64.         $user $event->getUser(); 
  65.         $this->notificationService->create($email$user$user);
  66.     }
  67.     public function onUserReactionInMessage(UserReactionEvent $event){
  68.         $messageCampaingId $event->getCampaign()->getId();
  69.         $to $event->getTo();
  70.         $from $event->getFrom();
  71.         $emoji $event->getEmoji();
  72.         $message $event->getMessage();
  73.         $reaction $event->getReaction(); 
  74.         $attachments null;
  75.         $linkAttachments null;
  76.         $filelinksUrl null;
  77.         $displayCodeToAddInMissionInMailHeader true;
  78.         $mission $message->getMission(); 
  79.         foreach ($message->getFileMessages() as $fileMessage) {
  80.             if ($fileMessage->isIsNew()) {
  81.                 $nameOfFileinBucket "Campaigns/{$mission->getCampaign()->getId()}/{$fileMessage->getName()}";
  82.                 $nameOfBucket "company-{$mission->getCampaign()->getCompany()->getId()}";
  83.                 $link $this->googleStorageService->getUrlBucket(strtolower($nameOfBucket),$nameOfFileinBucket);
  84.             }
  85.             else {
  86.                 $link "{$this->getParameter('back_website_url')}/{$this->messageService->getFileMissionUrl($mission$fileMessage->getName())}";
  87.             } 
  88.         
  89.             $linksFileMessages[]=[
  90.                 'link'=>$link
  91.                 'name'=>$fileMessage->getName()
  92.             ];
  93.         }
  94.         // $content = "<p>Bonjour {$to->getFullName()}</p>
  95.         //             <div>{$from->getFullName()} a réagi à votre message : {$message->getContent() } 
  96.         //                     <p>(Réaction : $emoji)</p>
  97.         //             </div>";        
  98.         $render =  $this->twig->render('emails/metion_in_message.html.twig',[
  99.             'message'=>  $message,
  100.             'reaction'=>$reaction,
  101.             'mission'=> $mission,
  102.             'sendTo'=>  $to
  103.             'user_second_color'=>'#08085c',
  104.             'user_primary_color'=> '#08085c'
  105.             ]
  106.         ); 
  107.         $this->notificationService->sendEmail(
  108.             new Address('caroline.b@my-flow.fr''myFlow'),
  109.             new Address($to->getEmail()),
  110.             "{$from->getFullName()} a réagi à votre  message",
  111.             $render,
  112.             $attachments,
  113.             $linkAttachments,
  114.             $filelinksUrl,
  115.             $displayCodeToAddInMissionInMailHeader,
  116.             $messageCampaingId
  117.         );
  118.     }
  119.     public function onUserMentionnedInTchat(UserMentionnedEvent $event) {
  120.         $mission $event->getMission();
  121.         $campaign =  $mission->getCampaign();
  122.         $user $event->getUser();
  123.         $message $event->getMessage();
  124.         $messageId $event->getMessageId();
  125.         $destinationMail $event->getUserEmail();
  126.         $subject ="Vous avez été mentionné(e) !"
  127.         $content "Votre nom a été cité dans un message. Cliquez pour voir où !"
  128.         $messageObj $this->messageRepository->findOneBy(['id'=>$messageId]);
  129.         $groupId $messageObj->getDiscussionGroup()!=null $messageObj->getDiscussionGroup() : FirebaseCloudeMessagingService::GROUP
  130.         $linksFileMessages =[];
  131.         $render =  $this->twig->render('emails/new_message.html.twig',[
  132.             'message'=>  $messageObj,
  133.             'sendTo'=> $user
  134.             'user_second_color'=>'#08085c',
  135.             'user_primary_color'=> '#08085c'
  136.             ]
  137.         ); 
  138.         foreach ($messageObj->getFileMessages() as $fileMessage) {
  139.             if ($fileMessage->isIsNew()) {
  140.                 $nameOfFileinBucket "Campaigns/{$mission->getCampaign()->getId()}/{$fileMessage->getName()}";
  141.                 $nameOfBucket "company-{$mission->getCampaign()->getCompany()->getId()}";
  142.                 $link $this->googleStorageService->getUrlBucket(strtolower($nameOfBucket),$nameOfFileinBucket);
  143.             }
  144.             else {
  145.                 $link "{$this->getParameter('back_website_url')}/{$this->messageService->getFileMissionUrl($mission$fileMessage->getName())}";
  146.             } 
  147.         
  148.             $linksFileMessages[]=[
  149.                 'link'=>$link
  150.                 'name'=>$fileMessage->getName()
  151.             ];
  152.         }
  153.         
  154.         if ($user !=null &&  $this->canSendEmailToUser($user)) {
  155.             $email $this->systemEmailRepository->findOneBy(['code' => SystemEmail::USER_MENTIONNED]);
  156.             $user $this->setEmailForContactProject(user:$user);
  157.             // $emailParsed = $this->shortcodeService->parse($email->getContent(), $user, $campaign->getCompany(),null, $campaign, null, null,null, null, null, null, $message);
  158.            
  159.             $this->notificationService->sendEmail(
  160.                 from: new Address('operation@my-flow.fr'),
  161.                 to: new Address($destinationMail), 
  162.                 subject$email->getSubject(), 
  163.                 content$render
  164.                 attachmentsnull
  165.                 linkAttachmentsnull,
  166.                 filelinksUrl$linksFileMessages,
  167.                 displayCodeToAddInMissionInMailHeader true,
  168.                 campaignId$campaign->getId(),
  169.                 usernull,
  170.                 messageId$messageId,
  171.             );
  172.             
  173.             if ($groupId instanceof DiscussionGroup) {
  174.                 $groupId $groupId->getId();
  175.             }
  176.             $this->removePreviewNotificationToSendTo($user$campaign);
  177.             $this->firebaseCloudeMessagingService->createPushNotificationByMission($user$mission,$subject$contentFirebaseCloudeMessagingService::MESSAGES$groupId); 
  178.         }
  179.         else {
  180.             
  181.             $email $this->systemEmailRepository->findOneBy(['code' => SystemEmail::ALL_USER_MENTIONNED]);
  182.             foreach ($this->getUserParticipants($campaign,$messageObj?->getDiscussionGroup()) as $user) {
  183.                 
  184.                 if ($this->canSendEmailToUser($user)) {
  185.                     $this->removePreviewNotificationToSendTo($user$campaign);
  186.                     $user $this->setEmailForContactProject(user:$user);
  187.                     // $emailParsed = $this->shortcodeService->parse($email->getContent(), $user, $campaign->getCompany(),null, $campaign, null, null,null, null, null, null, $message);
  188.                     
  189.                     $this->notificationService->sendEmail(
  190.                         from: new Address('operation@my-flow.fr'),
  191.                         to: new Address($user->getEmail()), 
  192.                         subject$email->getSubject(), 
  193.                         content:  $render
  194.                         attachmentsnull
  195.                         linkAttachmentsnull,
  196.                         filelinksUrl$linksFileMessages,
  197.                         displayCodeToAddInMissionInMailHeader true,
  198.                         campaignId$campaign->getId(),
  199.                     );
  200.                     $this->firebaseCloudeMessagingService->createPushNotificationByMission($user$mission,$subject$contentFirebaseCloudeMessagingService::MESSAGES$groupId); 
  201.                 }
  202.             } 
  203.         }
  204.     }
  205.     public function onSendEmailBeforeDeletedEvent(SendEmailBeforeDeletedEvent $event){
  206.         $user $event->getUser();
  207.         $email $this->systemEmailRepository->findOneBy(['code' => SystemEmail::EMAIL_BEFORE_DELETED_USER]);
  208.         $this->notificationService->create($email$user$user);
  209.     }
  210.     public function onSendEmailOneMonthBeforeDeletedEvent(SendEmailOneMonthBeforeDeletedEvent $event){
  211.         $user $event->getUser();
  212.         $email $this->systemEmailRepository->findOneBy(['code' => SystemEmail::EMAIL_ONE_MONTH_BEFORE_DELETED_USER]);
  213.         $this->notificationService->create($email$user$user);
  214.        
  215.     }
  216.     private function getUserParticipants(Campaign $campaign,$groupedMessage null){
  217.        $ids = [];
  218.        $allParticipantWithAdmin = [];
  219.        if (!is_null($groupedMessage)) {
  220.            foreach ($groupedMessage->getUsers() as $userInGroup) {
  221.                if (!in_array($userInGroup->getId(), $ids) and !$userInGroup->getDeleted() and $userInGroup->isEnabled()) {
  222.                     $ids[] = $userInGroup->getId();            
  223.                     $allParticipantWithAdmin[] = $userInGroup;            
  224.                 }
  225.            }
  226.        }else{
  227.             foreach ($campaign->getMissions() as $mission) {
  228.                 foreach ($mission->getParticipants() as $participant) {
  229.                     if (!in_array($participant->getUser()->getId(), $ids) and !$participant->getUser()->getDeleted() and $participant->getUser()->isEnabled()) {
  230.                         $ids[] = $participant->getUser()->getId();            
  231.                         $allParticipantWithAdmin[] = $participant->getUser();            
  232.                     }
  233.                 }
  234.             }
  235.        }
  236.        return $allParticipantWithAdmin ;
  237.     }
  238.     private function setEmailForContactProject(User $user){
  239.         if ($user->getId()=="Kb8bh3rBpsRukYgsMoYM9v") {
  240.              $adminEmail $this->userRepository->findOneBy(['email'=>"michael.lh@my-flow.fr"]) ; 
  241.              return  $adminEmail != null $adminEmail $user;
  242.         }
  243.         if ($user->getId()=="L15WDFhbcUcwtNFtBB2r5Q") {
  244.              $adminEmail $this->userRepository->findOneBy(['email'=>"thomas.s@my-flow.fr"]);
  245.              return  $adminEmail != null $adminEmail $user;
  246.              
  247.         }
  248.         return $user;
  249.     }
  250.     private function canSendEmailToUser(User $user){
  251.         // return (($user->getLastActivityInMinute() > 10 || !$user->isLogged()) && !is_null($user->getFreqNotificationEverytime()) && in_array(Notification::MENTIONNED_TO_ME->value, $user->getFreqNotificationEverytime())) ? true : false;
  252.         return (!is_null($user->getFreqNotificationEverytime()) && in_array(Notification::MENTIONNED_TO_ME->value$user->getFreqNotificationEverytime())) ? true false;
  253.     }
  254.     private function removePreviewNotificationToSendTo(User $user,Campaign $campaign){
  255.     
  256.            $chatNotifications =  $this->chatNotificationRepository->findNextNotificationToSend($user,$campaign);
  257.     }
  258. }