Skip to content

Commit

Permalink
Moved sending emails to the syncer. This separates sending each indiv…
Browse files Browse the repository at this point in the history
…idual mail,

thus hardening the app. There are still many improvements possible in this code,
eg chaining these commands, making emails just another notification type and listening
to the Notify event instead. We can postpone this to a later stable release.
  • Loading branch information
luceos authored and franzliedke committed Feb 14, 2020
1 parent 4d2c078 commit e4e9282
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 17 deletions.
40 changes: 40 additions & 0 deletions src/Notification/Job/SendEmailNotificationJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Flarum\Notification\Job;

use Flarum\Notification\Blueprint\BlueprintInterface;
use Flarum\Notification\MailableInterface;
use Flarum\Notification\NotificationMailer;
use Flarum\Queue\AbstractJob;
use Flarum\User\User;

class SendEmailNotificationJob extends AbstractJob
{
/**
* @var MailableInterface
*/
private $blueprint;
/**
* @var User
*/
private $recipient;

public function __construct(MailableInterface $blueprint, User $recipient)
{
$this->blueprint = $blueprint;
$this->recipient = $recipient;
}
public function handle(NotificationMailer $mailer)
{
$mailer->send($this->blueprint, $this->recipient);
}
}
24 changes: 7 additions & 17 deletions src/Notification/Job/SendNotificationsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Flarum\Notification\Event\Sending;
use Flarum\Notification\MailableInterface;
use Flarum\Notification\Notification;
use Flarum\Notification\NotificationMailer;
use Flarum\Queue\AbstractJob;
use Flarum\User\User;

Expand All @@ -28,20 +27,20 @@ class SendNotificationsJob extends AbstractJob
*/
private $blueprint;
/**
* @var array
* @var User[]
*/
private $recipientIds;
private $recipients;

public function __construct(BlueprintInterface $blueprint, array $recipientIds = [])
public function __construct(BlueprintInterface $blueprint, array $recipients = [])
{
$this->blueprint = $blueprint;
$this->recipientIds = $recipientIds;
$this->recipients = $recipients;
}

public function handle(NotificationMailer $mailer)
public function handle()
{
$now = Carbon::now('utc')->toDateTimeString();
$recipients = $this->recipientIds;
$recipients = $this->recipients;

event(new Sending($this->blueprint, $recipients));

Expand All @@ -59,16 +58,7 @@ public function handle(NotificationMailer $mailer)
event(new Notifying($this->blueprint, $recipients));

if ($this->blueprint instanceof MailableInterface) {
$this->email($mailer, $this->blueprint, $recipients);
}
}

protected function email(NotificationMailer $mailer, MailableInterface $blueprint, array $recipients)
{
foreach ($recipients as $user) {
if ($user->shouldEmail($blueprint::getType())) {
$mailer->send($blueprint, $user);
}
$this->chain([new SendEmailNotificationJob($this->blueprint, $recipients)]);
}
}
}
10 changes: 10 additions & 0 deletions src/Notification/NotificationSyncer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Flarum\Notification;

use Flarum\Notification\Blueprint\BlueprintInterface;
use Flarum\Notification\Job\SendEmailNotificationJob;
use Flarum\Notification\Job\SendNotificationsJob;
use Flarum\User\User;
use Illuminate\Contracts\Queue\Queue;
Expand Down Expand Up @@ -112,6 +113,15 @@ public function sync(Blueprint\BlueprintInterface $blueprint, array $users)
if (count($newRecipients)) {
$this->queue->push(new SendNotificationsJob($blueprint, $newRecipients));
}

if ($blueprint instanceof MailableInterface) {
/** @var User $recipient */
foreach($newRecipients as $recipient) {
if ($recipient->shouldEmail($blueprint::getType())) {
$this->queue->push(new SendEmailNotificationJob($blueprint, $recipient));
}
}
}
}

/**
Expand Down

0 comments on commit e4e9282

Please sign in to comment.