Skip to content

Commit

Permalink
Get common conversations among multiple users
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinashe Musonza authored and Tinashe Musonza committed Oct 23, 2017
1 parent 74742d9 commit 90a52cc
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- [Delete a message](#delete-a-message)
- [Clear a conversation](#clear-a-conversation)
- [Get a conversation between two users](#get-a-conversation-between-two-users)
- [Get common conversations among users](#get-common-conversations-among-users)
- [Remove users from a conversation](#remove-users-from-a-conversation)
- [Add users to a conversation](#add-users-to-a-conversation)
- [Get messages in a conversation](#get-messages-in-a-conversation)
Expand Down Expand Up @@ -139,6 +140,15 @@ Chat::conversations($conversation)->for($user)->clear();
Chat::getConversationBetween($user1, $user2);
```

#### Get common conversations among users

```php
$conversations = Chat::commonConversations($users);
```
$users can be an array of user ids ex. [1,4,6]

OR a collection (\Illuminate\Database\Eloquent\Collection) of users

#### Remove users from a conversation

```php
Expand Down
11 changes: 11 additions & 0 deletions src/Chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,17 @@ public function readAll()
$this->conversation->readAll($this->user);
}

/**
* Get conversations that users have in common
* @param array | collection $users
*
* @return Conversations
*/
public function commonConversations($users)
{
return $this->conversation->common($users);
}

/**
* Get Private Conversation between two users.
*
Expand Down
21 changes: 21 additions & 0 deletions src/Conversations/Conversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,27 @@ public function userConversations($user)
->pluck('mc_conversations.id');
}

/**
* Gets conversations that are common for a list of users
* @param \Illuminate\Database\Eloquent\Collection | array $users ids
*
* @return \Illuminate\Database\Eloquent\Collection Conversation
*/
public function common($users)
{
if ($users instanceof \Illuminate\Database\Eloquent\Collection) {
$users = $users->map(function($user) {
return $user->id;
});
}

return $this->withCount(['users' => function ($query) use($users){
$query->whereIn('id', $users);
}])->get()->filter(function ($conversation, $key) use($users) {
return $conversation->users_count == count($users);
});
}

/**
* Gets the notifications.
*
Expand Down
26 changes: 26 additions & 0 deletions tests/ChatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,32 @@ public function it_can_return_recent_user_messsages()
$this->assertCount(3, $recent_messages);
}

/** @test */
public function it_can_return_a_common_conversation_among_users()
{
$users = $this->createUsers(4);

$conversation = Chat::createConversation([$users[0]->id, $users[1]->id]);
Chat::message('Hello 1')->from($users[1])->to($conversation)->send();
Chat::message('Hello 2')->from($users[0])->to($conversation)->send();

$conversation2 = Chat::createConversation([$users[0]->id, $users[2]->id]);
Chat::message('Hello Man 4')->from($users[0])->to($conversation2)->send();

$conversation3 = Chat::createConversation([$users[0]->id, $users[1]->id, $users[3]->id]);
Chat::message('Hello Man 5')->from($users[3])->to($conversation3)->send();
Chat::message('Hello Man 6')->from($users[0])->to($conversation3)->send();
Chat::message('Hello Man 3')->from($users[2])->to($conversation2)->send();

$users = \App\User::whereIn('id', [1,2,4])->get();

$conversations = Chat::commonConversations($users);

$this->assertCount(1, $conversations);

$this->assertEquals(3, $conversations->first()->id);
}

public function createUsers($count = 1)
{
return factory('App\User', $count)->create();
Expand Down

0 comments on commit 90a52cc

Please sign in to comment.