diff options
author | Joas Schilling <coding@schilljs.com> | 2020-06-04 16:19:19 +0300 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2020-07-09 12:37:49 +0300 |
commit | 429305bb8aaedac3c9176d81b58368a34e492ae1 (patch) | |
tree | 520c1b26d51e0bd5cd4c4e5f3b31ec037347384c /lib | |
parent | 78836260e467bba23bd827c6d4184a75b4208c63 (diff) |
Defer sending the notifications when we interact multiple times
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Chat/ChatManager.php | 7 | ||||
-rw-r--r-- | lib/Chat/Notifier.php | 14 | ||||
-rw-r--r-- | lib/Controller/PageController.php | 5 | ||||
-rw-r--r-- | lib/Notification/Listener.php | 19 |
4 files changed, 42 insertions, 3 deletions
diff --git a/lib/Chat/ChatManager.php b/lib/Chat/ChatManager.php index 56568af6f..1da1f0fb2 100644 --- a/lib/Chat/ChatManager.php +++ b/lib/Chat/ChatManager.php @@ -34,6 +34,7 @@ use OCP\Comments\ICommentsManager; use OCP\Comments\NotFoundException; use OCP\EventDispatcher\IEventDispatcher; use OCP\IUser; +use OCP\Notification\IManager as INotificationManager; /** * Basic polling chat manager. @@ -64,10 +65,12 @@ class ChatManager { public function __construct(CommentsManager $commentsManager, IEventDispatcher $dispatcher, + INotificationManager $notificationManager, Notifier $notifier, ITimeFactory $timeFactory) { $this->commentsManager = $commentsManager; $this->dispatcher = $dispatcher; + $this->notificationManager = $notificationManager; $this->notifier = $notifier; $this->timeFactory = $timeFactory; } @@ -170,6 +173,7 @@ class ChatManager { $event = new ChatParticipantEvent($chat, $comment, $participant); $this->dispatcher->dispatch(self::EVENT_BEFORE_MESSAGE_SEND, $event); + $shouldFlush = $this->notificationManager->defer(); try { $this->commentsManager->save($comment); @@ -192,6 +196,9 @@ class ChatManager { $this->dispatcher->dispatch(self::EVENT_AFTER_MESSAGE_SEND, $event); } catch (NotFoundException $e) { } + if ($shouldFlush) { + $this->notificationManager->flush(); + } return $comment; } diff --git a/lib/Chat/Notifier.php b/lib/Chat/Notifier.php index f381a55bb..d4130ef17 100644 --- a/lib/Chat/Notifier.php +++ b/lib/Chat/Notifier.php @@ -95,6 +95,7 @@ class Notifier { } $notification = $this->createNotification($chat, $comment, 'mention'); + $shouldFlush = $this->notificationManager->defer(); foreach ($mentionedUserIds as $mentionedUserId) { if (in_array($mentionedUserId, $alreadyNotifiedUsers, true)) { continue; @@ -107,6 +108,10 @@ class Notifier { } } + if ($shouldFlush) { + $this->notificationManager->flush(); + } + return $alreadyNotifiedUsers; } @@ -187,6 +192,7 @@ class Notifier { */ public function removePendingNotificationsForRoom(Room $chat): void { $notification = $this->notificationManager->createNotification(); + $shouldFlush = $this->notificationManager->defer(); // @todo this should be in the Notifications\Hooks $notification->setApp('spreed'); @@ -199,6 +205,10 @@ class Notifier { $notification->setObject('call', $chat->getToken()); $this->notificationManager->markProcessed($notification); + + if ($shouldFlush) { + $this->notificationManager->flush(); + } } /** @@ -212,6 +222,7 @@ class Notifier { return; } + $shouldFlush = $this->notificationManager->defer(); $notification = $this->notificationManager->createNotification(); $notification @@ -220,6 +231,9 @@ class Notifier { ->setUser($userId); $this->notificationManager->markProcessed($notification); + if ($shouldFlush) { + $this->notificationManager->flush(); + } } /** diff --git a/lib/Controller/PageController.php b/lib/Controller/PageController.php index d3246e1d0..58acd72b0 100644 --- a/lib/Controller/PageController.php +++ b/lib/Controller/PageController.php @@ -187,6 +187,7 @@ class PageController extends Controller { try { $room = $this->manager->getRoomByToken($token); $notification = $this->notificationManager->createNotification(); + $shouldFlush = $this->notificationManager->defer(); try { $notification->setApp('spreed') ->setUser($this->userId) @@ -198,6 +199,10 @@ class PageController extends Controller { $this->logger->logException($e, ['app' => 'spreed']); } + if ($shouldFlush) { + $this->notificationManager->flush(); + } + // If the room is not a public room, check if the user is in the participants if ($room->getType() !== Room::PUBLIC_CALL) { $this->manager->getRoomForParticipant($room->getId(), $this->userId); diff --git a/lib/Notification/Listener.php b/lib/Notification/Listener.php index ec832633d..67ab8aa68 100644 --- a/lib/Notification/Listener.php +++ b/lib/Notification/Listener.php @@ -121,6 +121,7 @@ class Listener { $actorId = $actor->getUID(); $notification = $this->notificationManager->createNotification(); + $shouldFlush = $this->notificationManager->defer(); $dateTime = $this->timeFactory->getDateTime(); try { $notification->setApp('spreed') @@ -131,6 +132,9 @@ class Listener { ]); } catch (\InvalidArgumentException $e) { $this->logger->logException($e, ['app' => 'spreed']); + if ($shouldFlush) { + $this->notificationManager->flush(); + } return; } @@ -147,6 +151,10 @@ class Listener { $this->logger->logException($e, ['app' => 'spreed']); } } + + if ($shouldFlush) { + $this->notificationManager->flush(); + } } /** @@ -206,8 +214,8 @@ class Listener { $actor = $this->userSession->getUser(); $actorId = $actor instanceof IUser ? $actor->getUID() :''; - $this->dispatcher->dispatch(IApp::class . '::defer', new Event()); $notification = $this->notificationManager->createNotification(); + $shouldFlush = $this->notificationManager->defer(); $dateTime = $this->timeFactory->getDateTime(); try { // Remove all old notifications for this room @@ -224,7 +232,9 @@ class Listener { ->setDateTime($dateTime); } catch (\InvalidArgumentException $e) { $this->logger->logException($e, ['app' => 'spreed']); - $this->dispatcher->dispatch(IApp::class . '::flush', new Event()); + if ($shouldFlush) { + $this->notificationManager->flush(); + } return; } @@ -241,7 +251,10 @@ class Listener { $this->logger->logException($e, ['app' => 'spreed']); } } - $this->dispatcher->dispatch(IApp::class . '::flush', new Event()); + + if ($shouldFlush) { + $this->notificationManager->flush(); + } } /** |