Skip to content
This repository was archived by the owner on Jun 17, 2022. It is now read-only.

Commit

Permalink
Add 25.3: Chatting with Admins
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Feb 13, 2022
1 parent e9d6940 commit 1e99d01
Show file tree
Hide file tree
Showing 14 changed files with 159 additions and 9 deletions.
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ DATABASE_URL="postgresql://127.0.0.1:5432/db?serverVersion=13&charset=utf8"
# BLACKFIRE_SERVER_ID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
# BLACKFIRE_SERVER_TOKEN=yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
###< blackfireio/blackfire-symfony-meta ###

###> symfony/slack-notifier ###
# SLACK_DSN=slack://TOKEN@default?channel=CHANNEL
###< symfony/slack-notifier ###
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"symfony/runtime": "6.0.*",
"symfony/security-bundle": "6.0.*",
"symfony/serializer": "6.0.*",
"symfony/slack-notifier": "6.0.*",
"symfony/string": "6.0.*",
"symfony/translation": "6.0.*",
"symfony/twig-bundle": "6.0.*",
Expand Down
72 changes: 71 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions config/packages/notifier.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
framework:
notifier:
#chatter_transports:
# slack: '%env(SLACK_DSN)%'
chatter_transports:
slack: '%env(SLACK_DSN)%'
# telegram: '%env(TELEGRAM_DSN)%'
#texter_transports:
# twilio: '%env(TWILIO_DSN)%'
Expand Down
3 changes: 3 additions & 0 deletions config/secrets/dev/dev.SLACK_DSN.b2b579.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php // dev.SLACK_DSN.b2b579 on Sun, 13 Feb 2022 09:42:00 +0100

return "b\x23A\xA4\x2A\xEF\x5D\x3B\x91\xF8\x40\xFD\x85\x9F\x1B\x8A\x89I\x8F\x7F\x11\x04\xFE\x80\xA7e\xF7\xB6\xF3\x8A\x7F\x3AO\xDA\x5CP\x2A\xE8\x1FU\xC1\x83-\x26m\x9B\xC7\x3E\x27~u\xD4\x40\xC4\xD0\xA7_\x8Am\x7B\xF1\xF3\x7B\xC2\xE4\x9D\x94\x7B\x9F\x3F\x06S\xFD\xDB\xA7\x8B\x04\xAD\x1D\x94\xADG\x99\xD9\xCE\xCE\x88\xD7M\xC1\xD0\x7F";
1 change: 1 addition & 0 deletions config/secrets/dev/dev.list.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

return [
'AKISMET_KEY' => null,
'SLACK_DSN' => null,
];
3 changes: 3 additions & 0 deletions config/secrets/prod/prod.SLACK_DSN.b2b579.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php // prod.SLACK_DSN.b2b579 on Sun, 13 Feb 2022 09:42:04 +0100

return "\x86\xC5\x9C\xBEd\x84\x8D\xEC\xC4\x85\x8B\xF7\xF6\xDBv\xFB\x5D\xC9\xFB\x92\x2A\x10\xA3Ik\x9CL\xF4\x40\x08\x98\x5E\xC6\xFE\xE9\xBA\xEF\x9B\x5B\xA9\x25\xE8\xBC\xA2\x1F0\x7D\x1A\xF9\xB7\xC3\x3B\xC1c\x89\xE3\x9F\xF9\xDDt\x99\x09\x25\x90\x2BK\xF0\x90\xBD\xD4\x2AG\xB4J6aG\xD8U\x29S\xBF\x28\x19\xFE\x88\x8C\xBF\xD8\x7Du\x03";
1 change: 1 addition & 0 deletions config/secrets/prod/prod.list.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

return [
'AKISMET_KEY' => null,
'SLACK_DSN' => null,
];
4 changes: 3 additions & 1 deletion src/Controller/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Workflow\Registry;
use Twig\Environment;

Expand Down Expand Up @@ -47,7 +48,8 @@ public function reviewComment(Request $request, Comment $comment, Registry $regi
$this->entityManager->flush();

if ($accepted) {
$this->bus->dispatch(new CommentMessage($comment->getId()));
$reviewUrl = $this->generateUrl('review_comment', ['id' => $comment->getId()], UrlGeneratorInterface::ABSOLUTE_URL);
$this->bus->dispatch(new CommentMessage($comment->getId(), $reviewUrl));
}

return new Response($this->twig->render('admin/review.html.twig', [
Expand Down
4 changes: 3 additions & 1 deletion src/Controller/ConferenceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\NotifierInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Twig\Environment;

class ConferenceController extends AbstractController
Expand Down Expand Up @@ -82,7 +83,8 @@ public function show(Request $request, Conference $conference, CommentRepository
'permalink' => $request->getUri(),
];

$this->bus->dispatch(new CommentMessage($comment->getId(), $context));
$reviewUrl = $this->generateUrl('review_comment', ['id' => $comment->getId()], UrlGeneratorInterface::ABSOLUTE_URL);
$this->bus->dispatch(new CommentMessage($comment->getId(), $reviewUrl, $context));

$notifier->send(new Notification('Thank you for the feedback; your comment will be posted after moderation.', ['browser']));

Expand Down
9 changes: 8 additions & 1 deletion src/Message/CommentMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@
class CommentMessage
{
private $id;
private $reviewUrl;
private $context;

public function __construct(int $id, array $context = [])
public function __construct(int $id, string $reviewUrl, array $context = [])
{
$this->id = $id;
$this->reviewUrl = $reviewUrl;
$this->context = $context;
}

public function getReviewUrl(): string
{
return $this->reviewUrl;
}

public function getId(): int
{
return $this->id;
Expand Down
3 changes: 2 additions & 1 deletion src/MessageHandler/CommentMessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public function __invoke(CommentMessage $message)

$this->bus->dispatch($message);
} elseif ($this->workflow->can($comment, 'publish') || $this->workflow->can($comment, 'publish_ham')) {
$this->notifier->send(new CommentReviewNotification($comment), ...$this->notifier->getAdminRecipients());
$notification = new CommentReviewNotification($comment, $message->getReviewUrl());
$this->notifier->send($notification, ...$this->notifier->getAdminRecipients());
} elseif ($this->workflow->can($comment, 'optimize')) {
if ($comment->getPhotoFilename()) {
$this->imageOptimizer->resize($this->photoDir.'/'.$comment->getPhotoFilename());
Expand Down
50 changes: 48 additions & 2 deletions src/Notification/CommentReviewNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,27 @@
namespace App\Notification;

use App\Entity\Comment;
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackActionsBlock;
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackDividerBlock;
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackSectionBlock;
use Symfony\Component\Notifier\Bridge\Slack\SlackOptions;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\EmailMessage;
use Symfony\Component\Notifier\Notification\ChatNotificationInterface;
use Symfony\Component\Notifier\Notification\EmailNotificationInterface;
use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\Recipient\EmailRecipientInterface;
use Symfony\Component\Notifier\Recipient\RecipientInterface;

class CommentReviewNotification extends Notification implements EmailNotificationInterface
class CommentReviewNotification extends Notification implements EmailNotificationInterface, ChatNotificationInterface
{
private $comment;
private $reviewUrl;

public function __construct(Comment $comment)
public function __construct(Comment $comment, string $reviewUrl)
{
$this->comment = $comment;
$this->reviewUrl = $reviewUrl;

parent::__construct('New comment posted');
}
Expand All @@ -29,4 +38,41 @@ public function asEmailMessage(EmailRecipientInterface $recipient, string $trans

return $message;
}

public function asChatMessage(RecipientInterface $recipient, string $transport = null): ?ChatMessage
{
if ('slack' !== $transport) {
return null;
}

$message = ChatMessage::fromNotification($this, $recipient, $transport);
$message->subject($this->getSubject());
$message->options((new SlackOptions())
->iconEmoji('tada')
->iconUrl('https://guestbook.example.com')
->username('Guestbook')
->block((new SlackSectionBlock())->text($this->getSubject()))
->block(new SlackDividerBlock())
->block((new SlackSectionBlock())
->text(sprintf('%s (%s) says: %s', $this->comment->getAuthor(), $this->comment->getEmail(), $this->comment->getText()))
)
->block((new SlackActionsBlock())
->button('Accept', $this->reviewUrl, 'primary')
->button('Reject', $this->reviewUrl.'?reject=1', 'danger')
)
);

return $message;
}

public function getChannels(RecipientInterface $recipient): array
{
if (preg_match('{\b(great|awesome)\b}i', $this->comment->getText())) {
return ['email', 'chat/slack'];
}

$this->importance(Notification::IMPORTANCE_LOW);

return ['email'];
}
}
9 changes: 9 additions & 0 deletions symfony.lock
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,15 @@
"symfony/service-contracts": {
"version": "v3.0.0"
},
"symfony/slack-notifier": {
"version": "6.0",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "master",
"version": "5.2",
"ref": "192bc241a3095c6861353e5a7a652ad35659ca92"
}
},
"symfony/stopwatch": {
"version": "v6.0.3"
},
Expand Down

0 comments on commit 1e99d01

Please sign in to comment.