1
0
mirror of https://github.com/flarum/core.git synced 2025-08-07 00:47:00 +02:00
This commit is contained in:
Daniël Klabbers
2022-06-07 21:20:56 +02:00
parent b7332895db
commit 6be8d3dd17
3 changed files with 76 additions and 1 deletions

View File

@@ -0,0 +1,16 @@
<?php
namespace Flarum\Notification\Driver;
interface GroupableNotificationDriverInterface extends NotificationDriverInterface
{
/**
* Implies the delay to wait before sending out notifications to allow blueprintGrouping to happen.
*
* @todo Enable for v2.0+
*/
public function blueprintGroupingDelay(): ?\DateInterval;
public function sendGrouped();
}

View File

@@ -0,0 +1,35 @@
<?php
namespace Flarum\Notification\Job;
use Flarum\Notification\Blueprint\BlueprintInterface;
use Flarum\Notification\Driver\NotificationDriverInterface;
use Flarum\Queue\AbstractJob;
use Flarum\User\User;
use Illuminate\Queue\Middleware\WithoutOverlapping;
class DelayedBlueprintGroupingJob extends AbstractJob
{
private BlueprintInterface $blueprint;
private NotificationDriverInterface $driver;
private User $user;
public function __construct(BlueprintInterface $blueprint, User $user, NotificationDriverInterface $driver)
{
$this->blueprint = $blueprint;
$this->driver = $driver;
$this->user = $user;
}
public function handle()
{
}
public function middleware()
{
return [
(new WithoutOverlapping('delayed-blueprint-grouping:' . $this->user->id))->dontRelease()
];
}
}

View File

@@ -10,8 +10,11 @@
namespace Flarum\Notification;
use Flarum\Notification\Blueprint\BlueprintInterface;
use Flarum\Notification\Driver\GroupableNotificationDriverInterface;
use Flarum\Notification\Driver\NotificationDriverInterface;
use Flarum\Notification\Job\DelayedBlueprintGroupingJob;
use Flarum\User\User;
use Illuminate\Contracts\Queue\Queue;
/**
* The Notification Syncer commits notification blueprints to the database, and
@@ -47,6 +50,13 @@ class NotificationSyncer
*/
protected static $beforeSendingCallbacks = [];
private Queue $queue;
public function __construct(Queue $queue)
{
$this->queue = $queue;
}
/**
* Sync a notification so that it is visible to the specified users, and not
* visible to anyone else. If it is being made visible for the first time,
@@ -107,7 +117,21 @@ class NotificationSyncer
// didn't have a record in the database). As both operations can be
// intensive on resources (database and mail server), we queue them.
foreach (static::getNotificationDrivers() as $driverName => $driver) {
$driver->send($blueprint, $newRecipients);
if ($driver instanceof GroupableNotificationDriverInterface) {
$delay = $driver->blueprintGroupingDelay();
foreach($newRecipients as $recipient) {
$job = new DelayedBlueprintGroupingJob(
$blueprint,
$recipient,
$driver
);
$this->queue->later($delay, $job);
}
} else {
$driver->send($blueprint, $newRecipients);
}
}
}