Skip to content

Commit

Permalink
refactor lists/pluck references so Laravel 5.[0,1,2] installs will wo…
Browse files Browse the repository at this point in the history
…rk correctly

now also returning the full Eloquent relationship from threadsWithNewMessages() instead of an array of ids
  • Loading branch information
cmgmyr committed Aug 23, 2016
1 parent eac4dd1 commit 227dc72
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 37 deletions.
8 changes: 4 additions & 4 deletions src/Cmgmyr/Messenger/Models/Thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ public static function getBySubject($subjectQuery)
*/
public function participantsUserIds($userId = null)
{
$users = $this->participants()->withTrashed()->pluck('user_id');
$users = $this->participants()->withTrashed()->select('user_id')->get()->map(function ($participant) {
return $participant->user_id;
})->toArray();

if ($userId) {
$users[] = $userId;
Expand Down Expand Up @@ -294,9 +296,7 @@ public function participantsString($userId = null, $columns = ['name'])
$participantNames->where($usersTable . '.id', '!=', $userId);
}

$userNames = $participantNames->pluck($usersTable . '.name');

return implode(', ', $userNames);
return $participantNames->implode('name', ', ');
}

/**
Expand Down
34 changes: 7 additions & 27 deletions src/Cmgmyr/Messenger/Traits/Messagable.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,40 +51,20 @@ public function threads()
*/
public function newThreadsCount()
{
return count($this->threadsWithNewMessages());
return $this->threadsWithNewMessages()->count();
}

/**
* Returns all threads with new messages.
*
* @return array
* @return \Illuminate\Database\Eloquent\Relations\belongsToMany
*/
public function threadsWithNewMessages()
{
$threadsWithNewMessages = [];

$participants = Models::participant()->where('user_id', $this->id)->pluck('last_read', 'thread_id');

/**
* @todo: see if we can fix this more in the future.
* Illuminate\Foundation is not available through composer, only in laravel/framework which
* I don't want to include as a dependency for this package...it's overkill. So let's
* exclude this check in the testing environment.
*/
if (getenv('APP_ENV') == 'testing' || !str_contains(\Illuminate\Foundation\Application::VERSION, '5.0')) {
$participants = $participants->all();
}

if ($participants) {
$threads = Models::thread()->whereIn('id', array_keys($participants))->get();

foreach ($threads as $thread) {
if ($thread->updated_at > $participants[$thread->id]) {
$threadsWithNewMessages[] = $thread->id;
}
}
}

return $threadsWithNewMessages;
return $this->threads()
->where(function ($q) {
$q->whereNull(Models::table('participants') . '.last_read');
$q->orWhere(Models::table('threads') . '.updated_at', '>', $this->getConnection()->raw(Models::table('participants') . '.last_read'));
})->get();
}
}
11 changes: 6 additions & 5 deletions tests/EloquentThreadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,20 @@ public function it_should_return_all_threads()
public function it_should_get_all_thread_participants()
{
$thread = $this->faktory->create('thread');
$participants = $thread->participantsUserIds();
$this->assertCount(0, $participants);
$participantIds = $thread->participantsUserIds();
$this->assertCount(0, $participantIds);

$user_1 = $this->faktory->build('participant');
$user_2 = $this->faktory->build('participant', ['user_id' => 2]);
$user_3 = $this->faktory->build('participant', ['user_id' => 3]);

$thread->participants()->saveMany([$user_1, $user_2, $user_3]);

$participants = $thread->participantsUserIds();
$this->assertCount(3, $participants);
$participantIds = $thread->participantsUserIds();
$this->assertCount(3, $participantIds);
$this->assertEquals(2, $participantIds[1]);

$this->assertInternalType('object', $participants);
$this->assertInternalType('array', $participantIds);
}

/** @test */
Expand Down
2 changes: 1 addition & 1 deletion tests/MessagableTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function it_should_get_all_threads_with_new_messages()
$thread2->messages()->saveMany([$message_1b]);

$threads = $user->threadsWithNewMessages();
$this->assertEquals(1, $threads[0]);
$this->assertEquals(1, $threads->first()->id);

$this->assertEquals(1, $user->newThreadsCount());
}
Expand Down

0 comments on commit 227dc72

Please sign in to comment.