Skip to content

Commit 602f080

Browse files
committed
Merge pull request #48 from RaoulDijksman/ThreadsBetweenUsers
Add scope to Thread to retrieve threads shared between specified users.
2 parents 360ec24 + 7ce7c93 commit 602f080

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/Cmgmyr/Messenger/Models/Thread.php

+16
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,22 @@ public function scopeForUserWithNewMessages($query, $userId)
131131
->select('threads.*');
132132
}
133133

134+
/**
135+
* Returns threads between given user ids
136+
*
137+
* @param $query
138+
* @param $participants
139+
* @return mixed
140+
*/
141+
public function scopeBetween($query, array $participants)
142+
{
143+
$query->whereHas('participants', function ($query) use ($participants) {
144+
$query->whereIn('user_id', $participants)
145+
->groupBy('thread_id')
146+
->havingRaw('COUNT(thread_id)='.count($participants));
147+
});
148+
}
149+
134150
/**
135151
* Adds users to this thread
136152
*

tests/EloquentThreadTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,23 @@ public function it_should_get_all_threads_for_a_user_with_new_messages()
121121
$this->assertCount(1, $threads);
122122
}
123123

124+
/** @test */
125+
public function it_should_get_all_threads_shared_by_specified_users()
126+
{
127+
$userId = 1;
128+
$userId2 = 2;
129+
130+
$thread = $this->faktory->create('thread');
131+
$thread2 = $this->faktory->create('thread');
132+
133+
$this->faktory->create('participant', ['user_id' => $userId, 'thread_id' => $thread->id]);
134+
$this->faktory->create('participant', ['user_id' => $userId2, 'thread_id' => $thread->id]);
135+
$this->faktory->create('participant', ['user_id' => $userId, 'thread_id' => $thread2->id]);
136+
137+
$threads = Thread::between([$userId, $userId2])->get();
138+
$this->assertCount(1, $threads);
139+
}
140+
124141
/** @test */
125142
public function it_should_add_participants_to_a_thread()
126143
{

0 commit comments

Comments
 (0)