-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds ability to add multiple participants to chat / clearly state tha…
…t users where added or removed (related to #464)
- Loading branch information
Showing
6 changed files
with
161 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace App\Doctrine; | ||
|
||
use App\Converter\UserStringConverter; | ||
use App\Entity\Chat; | ||
use App\Entity\ChatMessage; | ||
use App\Entity\User; | ||
use App\EventSubscriber\DoctrineEntityCollector; | ||
use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener; | ||
use Doctrine\ORM\Event\OnFlushEventArgs; | ||
use Doctrine\ORM\Event\PreUpdateEventArgs; | ||
use Doctrine\ORM\Events; | ||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
|
||
#[AsDoctrineListener(event: Events::preUpdate)] | ||
class ChatMessageRecipientsSubscriber { | ||
|
||
public function __construct(private readonly TokenStorageInterface $tokenStorage, | ||
private readonly TranslatorInterface $translator, | ||
private readonly DoctrineEntityCollector $entityCollector, | ||
private readonly UserStringConverter $userStringConverter) { | ||
|
||
} | ||
|
||
public function preUpdate(PreUpdateEventArgs $args): void { | ||
$currentUser = $this->tokenStorage->getToken()?->getUser(); | ||
|
||
if(!$currentUser instanceof User) { | ||
return; | ||
} | ||
|
||
$uow = $args->getObjectManager()->getUnitOfWork(); | ||
|
||
foreach($uow->getScheduledCollectionUpdates() as $collectionUpdate) { | ||
if(!$collectionUpdate->getOwner() instanceof Chat) { | ||
continue; | ||
} | ||
|
||
$chat = $collectionUpdate->getOwner(); | ||
assert($chat instanceof Chat); | ||
|
||
$addedParticipants = [ ]; | ||
$removedParticipants = [ ]; | ||
|
||
foreach($collectionUpdate->getInsertDiff() as $user) { | ||
$addedParticipants[] = $user; | ||
} | ||
|
||
foreach($collectionUpdate->getDeleteDiff() as $user) { | ||
$removedParticipants[] = $user; | ||
} | ||
|
||
$content = ''; | ||
|
||
if(count($addedParticipants) > 0) { | ||
$content = $this->translator->trans('chat.participants.success.added', [ | ||
'%users%' => implode(', ', array_map(fn(User $user) => $this->userStringConverter->convert($user, includeUsername: false), $addedParticipants)), | ||
]); | ||
} | ||
|
||
if(count($removedParticipants) > 0) { | ||
$content .= "\n\n" . $this->translator->trans('chat.participants.success.remove', [ | ||
'%users%' => implode(', ', array_map(fn(User $user) => $this->userStringConverter->convert($user, includeUsername: false), $removedParticipants)), | ||
]); | ||
} | ||
|
||
$chatMessage = (new ChatMessage()) | ||
->setChat($chat) | ||
->setContent(trim($content)) | ||
->setCreatedBy($currentUser); | ||
|
||
$this->entityCollector->collectForPersist($chatMessage); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace App\EventSubscriber; | ||
|
||
use Doctrine\Common\EventArgs; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Symfony\Component\Console\Event\ConsoleTerminateEvent; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpKernel\Event\TerminateEvent; | ||
|
||
/** | ||
* This collector collects entities to be persisted into the database. This | ||
* is useful for entities that need to be persisted during a Doctrine event | ||
* listener. | ||
* | ||
* The documentation states that using EntityManager::flush() is highly | ||
* discouraged during any Doctrine listener. | ||
*/ | ||
class DoctrineEntityCollector implements EventSubscriberInterface { | ||
|
||
private array $collectedForPersist = [ ]; | ||
|
||
private array $collectedForRemoval = [ ]; | ||
|
||
public function __construct(private readonly EntityManagerInterface $em) { | ||
|
||
} | ||
|
||
public function collectForPersist(object $entity): void { | ||
$this->collectedForPersist[] = $entity; | ||
} | ||
|
||
public function collectForRemoval(object $entity): void { | ||
$this->collectedForRemoval[] = $entity; | ||
} | ||
|
||
private function persistRemoveAndFlush(): void { | ||
foreach($this->collectedForPersist as $entity) { | ||
$this->em->persist($entity); | ||
} | ||
|
||
foreach($this->collectedForRemoval as $entity) { | ||
$this->em->remove($entity); | ||
} | ||
|
||
$this->em->flush(); | ||
} | ||
|
||
public function onKernelTerminate(TerminateEvent $event): void { | ||
$this->persistRemoveAndFlush(); | ||
} | ||
|
||
public function onConsoleTerminate(ConsoleTerminateEvent $event): void { | ||
$this->persistRemoveAndFlush(); | ||
} | ||
|
||
public static function getSubscribedEvents(): array { | ||
return [ | ||
TerminateEvent::class => ['onKernelTerminate', 10], | ||
ConsoleTerminateEvent::class => ['onConsoleTerminate', 10] | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters