Skip to content

Commit

Permalink
Develop (#50)
Browse files Browse the repository at this point in the history
* Custom notifications, broadcast (#44)

* check for notifications config

* add broadcasting option

* return unread messages count

* get message by id (#49)
  • Loading branch information
musonza authored Feb 7, 2018
1 parent a65b029 commit b4067eb
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 8 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand All @@ -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();
Expand Down
12 changes: 12 additions & 0 deletions src/Chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
8 changes: 4 additions & 4 deletions src/Conversations/Conversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,8 @@ private function notifications($user, $readAll)

if ($readAll) {
return $notifications->markAsRead();
}

}
return $notifications;
}

Expand All @@ -309,8 +309,8 @@ private function notifications($user, $readAll)

if ($readAll) {
return $notifications->update(['is_seen' => 1]);
}

}
return $notifications->get();
}

Expand Down
6 changes: 3 additions & 3 deletions src/config/musonza_chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
16 changes: 16 additions & 0 deletions tests/Unit/ChatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit b4067eb

Please sign in to comment.