-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] Admin Signup Notifications (#1242)
- Loading branch information
1 parent
71c9c0f
commit 6bae56c
Showing
15 changed files
with
212 additions
and
5 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,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DoctrineMigrations; | ||
|
||
use Doctrine\DBAL\Schema\Schema; | ||
use Doctrine\Migrations\AbstractMigration; | ||
|
||
final class Version20241124155724 extends AbstractMigration | ||
{ | ||
public function getDescription(): string | ||
{ | ||
return 'Add new_user_id to notification table and notify_on_user_signup to "user" table for the `NewSignupNotification`'; | ||
} | ||
|
||
public function up(Schema $schema): void | ||
{ | ||
$this->addSql('ALTER TABLE notification ADD new_user_id INT DEFAULT NULL'); | ||
$this->addSql('ALTER TABLE notification ADD CONSTRAINT FK_BF5476CA7C2D807B FOREIGN KEY (new_user_id) REFERENCES "user" (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE'); | ||
$this->addSql('CREATE INDEX IDX_BF5476CA7C2D807B ON notification (new_user_id)'); | ||
$this->addSql('ALTER TABLE "user" ADD notify_on_user_signup BOOLEAN DEFAULT TRUE'); | ||
} | ||
|
||
public function down(Schema $schema): void | ||
{ | ||
$this->addSql('ALTER TABLE notification DROP CONSTRAINT FK_BF5476CA7C2D807B'); | ||
$this->addSql('DROP INDEX IDX_BF5476CA7C2D807B'); | ||
$this->addSql('ALTER TABLE notification DROP new_user_id'); | ||
$this->addSql('ALTER TABLE "user" DROP notify_on_user_signup'); | ||
} | ||
} |
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,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Entity; | ||
|
||
use App\Payloads\PushNotification; | ||
use Doctrine\ORM\Mapping\Entity; | ||
use Doctrine\ORM\Mapping\JoinColumn; | ||
use Doctrine\ORM\Mapping\ManyToOne; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
|
||
#[Entity] | ||
class NewSignupNotification extends Notification | ||
{ | ||
#[ManyToOne(targetEntity: User::class, cascade: ['remove'])] | ||
#[JoinColumn(nullable: true, onDelete: 'CASCADE')] | ||
public ?User $newUser; | ||
|
||
public function getType(): string | ||
{ | ||
return 'new_signup'; | ||
} | ||
|
||
public function getSubject(): ?User | ||
{ | ||
return $this->newUser; | ||
} | ||
|
||
public function getMessage(TranslatorInterface $trans, string $locale, UrlGeneratorInterface $urlGenerator): PushNotification | ||
{ | ||
$message = str_replace('%u%', $this->newUser->username, $trans->trans('notification_body_new_signup', locale: $locale)); | ||
$title = $trans->trans('notification_title_new_signup', locale: $locale); | ||
$url = $urlGenerator->generate('user_overview', ['username' => $this->newUser->username]); | ||
$slash = $this->newUser->avatar && !str_starts_with('/', $this->newUser->avatar->filePath) ? '/' : ''; | ||
$avatarUrl = $this->newUser->avatar ? '/media/cache/resolve/avatar_thumb'.$slash.$this->newUser->avatar->filePath : null; | ||
|
||
return new PushNotification($message, $title, actionUrl: $url, avatarUrl: $avatarUrl); | ||
} | ||
} |
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
14 changes: 14 additions & 0 deletions
14
src/Message/Notification/SentNewSignupNotificationMessage.php
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,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Message\Notification; | ||
|
||
use App\Message\Contracts\AsyncMessageInterface; | ||
|
||
class SentNewSignupNotificationMessage implements AsyncMessageInterface | ||
{ | ||
public function __construct(public int $userId) | ||
{ | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/MessageHandler/Notification/SentNewSignupNotificationHandler.php
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,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\MessageHandler\Notification; | ||
|
||
use App\Message\Contracts\MessageInterface; | ||
use App\Message\Notification\SentNewSignupNotificationMessage; | ||
use App\MessageHandler\MbinMessageHandler; | ||
use App\Repository\UserRepository; | ||
use App\Service\Notification\SignupNotificationManager; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Symfony\Component\Messenger\Attribute\AsMessageHandler; | ||
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException; | ||
|
||
#[AsMessageHandler] | ||
class SentNewSignupNotificationHandler extends MbinMessageHandler | ||
{ | ||
public function __construct( | ||
readonly EntityManagerInterface $entityManager, | ||
private readonly UserRepository $userRepository, | ||
private readonly SignupNotificationManager $signupNotificationManager, | ||
) { | ||
parent::__construct($entityManager); | ||
} | ||
|
||
public function __invoke(SentNewSignupNotificationMessage $message) | ||
{ | ||
$this->workWrapper($message); | ||
} | ||
|
||
public function doWork(MessageInterface $message): void | ||
{ | ||
if (!($message instanceof SentNewSignupNotificationMessage)) { | ||
throw new \LogicException(); | ||
} | ||
$user = $this->userRepository->findOneBy(['id' => $message->userId]); | ||
if (!$user) { | ||
throw new UnrecoverableMessageHandlingException('user not found'); | ||
} | ||
$this->signupNotificationManager->sendNewSignupNotification($user); | ||
} | ||
} |
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,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Service\Notification; | ||
|
||
use App\Entity\NewSignupNotification; | ||
use App\Entity\User; | ||
use App\Event\NotificationCreatedEvent; | ||
use App\Repository\UserRepository; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
|
||
readonly class SignupNotificationManager | ||
{ | ||
public function __construct( | ||
private EntityManagerInterface $entityManager, | ||
private UserRepository $userRepository, | ||
private EventDispatcherInterface $dispatcher, | ||
) { | ||
} | ||
|
||
public function sendNewSignupNotification(User $newUser): void | ||
{ | ||
$receivers = $this->userRepository->findAllAdmins(); | ||
foreach ($receivers as $receiver) { | ||
if (!$receiver->notifyOnUserSignup) { | ||
continue; | ||
} | ||
$notification = new NewSignupNotification($receiver); | ||
$notification->newUser = $newUser; | ||
$this->entityManager->persist($notification); | ||
$this->dispatcher->dispatch(new NotificationCreatedEvent($notification)); | ||
} | ||
$this->entityManager->flush(); | ||
} | ||
} |
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
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