diff --git a/README.md b/README.md index 0ce93b4..c323837 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,10 @@ - [Update conversation details](#update-conversation-details) - [Send a text message](#send-a-text-message) - [Send a message of custom type](#send-a-message-of-custom-type) + - [Get a message by id](#get-a-message-by-id) - [Mark a message as read](#mark-a-message-as-read) - [Mark whole conversation as read](#mark-whole-conversation-as-read) + - [Unread messages count](#unread-messages-count) - [Delete a message](#delete-a-message) - [Clear a conversation](#clear-a-conversation) - [Get a conversation between two users](#get-a-conversation-between-two-users) @@ -142,6 +144,12 @@ $message = Chat::message('http://example.com/img') ->send(); ``` +### Get a message by id + +```php +$message = Chat::messageById($id); +``` + #### Mark a message as read @@ -155,7 +163,7 @@ Chat::messages($message)->for($user)->markRead(); Chat::conversations($conversation)->for($user)->readAll(); ``` -### Unread messages count +#### Unread messages count ```php $unreadCount = Chat::for($user)->unreadCount(); diff --git a/src/Chat.php b/src/Chat.php index f594b78..288f3aa 100644 --- a/src/Chat.php +++ b/src/Chat.php @@ -294,6 +294,18 @@ public function getMessages($perPage = null, $page = null) return $this->conversation->getMessages($this->user, $perPage, $page); } + /** + * Get messages by id. + * + * @param int $id + * + * @return Message + */ + public function messageById($id) + { + return $this->message->findOrFail($id); + } + /** * Deletes message. * diff --git a/src/Conversations/Conversation.php b/src/Conversations/Conversation.php index 75f1a62..df5f4ea 100644 --- a/src/Conversations/Conversation.php +++ b/src/Conversations/Conversation.php @@ -299,8 +299,8 @@ private function notifications($user, $readAll) if ($readAll) { return $notifications->markAsRead(); - } - + } + return $notifications; } @@ -309,8 +309,8 @@ private function notifications($user, $readAll) if ($readAll) { return $notifications->update(['is_seen' => 1]); - } - + } + return $notifications->get(); } diff --git a/src/config/musonza_chat.php b/src/config/musonza_chat.php index 7eac107..646bef8 100755 --- a/src/config/musonza_chat.php +++ b/src/config/musonza_chat.php @@ -3,11 +3,11 @@ return [ 'user_model' => 'App\User', - /* + /** * This will allow you to broadcast an event when a message is sent * Example: - * Channel: private-mc-chat-conversation.2, - * Event: Musonza\Chat\Messages\MessageWasSent + * Channel: private-mc-chat-conversation.2, + * Event: Musonza\Chat\Messages\MessageWasSent */ 'broadcasts' => false, diff --git a/tests/Unit/ChatTest.php b/tests/Unit/ChatTest.php index daf691a..4706229 100644 --- a/tests/Unit/ChatTest.php +++ b/tests/Unit/ChatTest.php @@ -416,4 +416,20 @@ public function it_return_unread_messages_count_for_user() $this->assertEquals(1, Chat::for($users[1])->unreadCount()); } + + /** @test */ + public function it_gets_a_message_by_id() + { + $users = $this->createUsers(2); + + $conversation = Chat::createConversation([$users[0]->id, $users[1]->id]); + + Chat::message('Hello 1')->from($users[1])->to($conversation)->send(); + + $message = Chat::messageById(1); + + $this->assertInstanceOf('Musonza\Chat\Messages\Message', $message); + + $this->assertEquals(1, $message->id); + } }