Skip to content

Commit

Permalink
Merge pull request #48 from RaoulDijksman/ThreadsBetweenUsers
Browse files Browse the repository at this point in the history
Add scope to Thread to retrieve threads shared between specified users.
  • Loading branch information
cmgmyr committed Jul 10, 2015
2 parents 360ec24 + 7ce7c93 commit 602f080
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Cmgmyr/Messenger/Models/Thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,22 @@ public function scopeForUserWithNewMessages($query, $userId)
->select('threads.*');
}

/**
* Returns threads between given user ids
*
* @param $query
* @param $participants
* @return mixed
*/
public function scopeBetween($query, array $participants)
{
$query->whereHas('participants', function ($query) use ($participants) {
$query->whereIn('user_id', $participants)
->groupBy('thread_id')
->havingRaw('COUNT(thread_id)='.count($participants));
});
}

/**
* Adds users to this thread
*
Expand Down
17 changes: 17 additions & 0 deletions tests/EloquentThreadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,23 @@ public function it_should_get_all_threads_for_a_user_with_new_messages()
$this->assertCount(1, $threads);
}

/** @test */
public function it_should_get_all_threads_shared_by_specified_users()
{
$userId = 1;
$userId2 = 2;

$thread = $this->faktory->create('thread');
$thread2 = $this->faktory->create('thread');

$this->faktory->create('participant', ['user_id' => $userId, 'thread_id' => $thread->id]);
$this->faktory->create('participant', ['user_id' => $userId2, 'thread_id' => $thread->id]);
$this->faktory->create('participant', ['user_id' => $userId, 'thread_id' => $thread2->id]);

$threads = Thread::between([$userId, $userId2])->get();
$this->assertCount(1, $threads);
}

/** @test */
public function it_should_add_participants_to_a_thread()
{
Expand Down

0 comments on commit 602f080

Please sign in to comment.