Skip to content

Commit

Permalink
return unread notifications (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
musonza authored May 28, 2018
1 parent b747895 commit 2cfba90
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
30 changes: 26 additions & 4 deletions src/Chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down
8 changes: 8 additions & 0 deletions src/Notifications/MessageNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
15 changes: 14 additions & 1 deletion tests/Unit/ChatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 2cfba90

Please sign in to comment.