diff --git a/README.md b/README.md index bd55ad5..e343f5a 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 diff --git a/src/Chat.php b/src/Chat.php index 70ae453..561b515 100644 --- a/src/Chat.php +++ b/src/Chat.php @@ -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. * diff --git a/src/Conversations/Conversation.php b/src/Conversations/Conversation.php index e065f2a..6ac16f3 100644 --- a/src/Conversations/Conversation.php +++ b/src/Conversations/Conversation.php @@ -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. * diff --git a/tests/ChatTest.php b/tests/ChatTest.php index 55988ea..4e002da 100644 --- a/tests/ChatTest.php +++ b/tests/ChatTest.php @@ -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();