From 2cfba90823a1d59faeb4ad2b392401924a598c4c Mon Sep 17 00:00:00 2001 From: Tinashe Musonza Date: Mon, 28 May 2018 14:53:42 -0700 Subject: [PATCH] return unread notifications (#87) --- src/Chat.php | 30 ++++++++++++++++++++--- src/Notifications/MessageNotification.php | 8 ++++++ tests/Unit/ChatTest.php | 15 +++++++++++- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/Chat.php b/src/Chat.php index 288f3aa..6f0b86e 100644 --- a/src/Chat.php +++ b/src/Chat.php @@ -6,6 +6,7 @@ use Musonza\Chat\Conversations\Conversation; use Musonza\Chat\Messages\Message; use Musonza\Chat\Messages\SendMessageCommand; +use Musonza\Chat\Notifications\MessageNotification; class Chat { @@ -42,15 +43,21 @@ class Chat protected $page = 1; /** - * @param \Musonza\Chat\Conversations\Conversation $conversation The conversation - * @param \Musonza\Chat\Messages\Message $message The message - * @param \Musonza\Chat\Commanding\CommandBus $commandBus The command bus + * @param Conversation $conversation The conversation + * @param Message $message The message + * @param CommandBus $commandBus The command bus + * @param MessageNotification $messageNotification Notifications */ - public function __construct(Conversation $conversation, Message $message, CommandBus $commandBus) + public function __construct( + Conversation $conversation, + Message $message, + CommandBus $commandBus, + MessageNotification $messageNotification) { $this->conversation = $conversation; $this->message = $message; $this->commandBus = $commandBus; + $this->messageNotification = $messageNotification; } /** @@ -381,11 +388,26 @@ private function getConversationsInCommon($conversation1, $conversation2) return array_values(array_intersect($conversation1, $conversation2)); } + /** + * Get count for unread messages. + * + * @return void + */ public function unreadCount() { return $this->message->unreadCount($this->user); } + /** + * Get unread notifications + * + * @return MessageNotification + */ + public function unReadNotifications() + { + return $this->messageNotification->unReadNotifications($this->user); + } + /** * Returns the User Model class. * diff --git a/src/Notifications/MessageNotification.php b/src/Notifications/MessageNotification.php index 885848b..0dea1be 100644 --- a/src/Notifications/MessageNotification.php +++ b/src/Notifications/MessageNotification.php @@ -31,6 +31,14 @@ public static function make(Message $message, Conversation $conversation) } } + public function unReadNotifications($user) + { + return MessageNotification::where([ + ['user_id', '=', $user->id], + ['is_seen', '=', 0] + ])->get(); + } + public static function createCustomNotifications($message, $conversation) { $notification = []; diff --git a/tests/Unit/ChatTest.php b/tests/Unit/ChatTest.php index 7fa0bc3..b54cbd9 100644 --- a/tests/Unit/ChatTest.php +++ b/tests/Unit/ChatTest.php @@ -372,7 +372,20 @@ public function it_return_unread_messages_count_for_user() } /** @test */ - public function it_gets_unread_messages_per_conversation() + public function it_gets_unread_notifications() + { + $conversation1 = Chat::createConversation([$this->users[0]->id, $this->users[1]->id]); + Chat::message('Hello 1')->from($this->users[1])->to($conversation1)->send(); + Chat::message('Hello 2')->from($this->users[1])->to($conversation1)->send(); + $conversation2 = Chat::createConversation([$this->users[2]->id, $this->users[0]->id]); + Chat::message('Hello 3')->from($this->users[2])->to($conversation2)->send(); + + $notifications = Chat::for($this->users[0])->unReadNotifications(); + $this->assertEquals(3, $notifications->count()); + } + + /** @test */ + public function it_gets_unread_notifications_per_conversation() { $conversation1 = Chat::createConversation([$this->users[0]->id, $this->users[1]->id]); Chat::message('Hello 1')->from($this->users[1])->to($conversation1)->send();