Skip to content

Commit

Permalink
Revert "Implemented unread threads as a relationship on Messagable Tr…
Browse files Browse the repository at this point in the history
…ait"
  • Loading branch information
cmgmyr committed Jun 15, 2016
1 parent d6e8940 commit 6962ecb
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions src/Cmgmyr/Messenger/Traits/Messagable.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,35 +44,47 @@ public function threads()
);
}

/**
* Unread threads as a relationship
*
* @return \Illuminate\Database\Eloquent\Relations\belongsToMany
*/
public function unreadThreads()
{
return $this->threads()
->whereRaw(Models::table('threads') . '.updated_at > '
. Models::table('participants') . '.last_read');
}

/**
* Returns the new messages count for user.
*
* @return int
*/
public function newThreadsCount()
{
return $this->unreadThreads()->count();
return count($this->threadsWithNewMessages());
}

/**
* Returns the id of all threads with new messages.
* Returns all threads with new messages.
*
* @return array
*/
public function threadsWithNewMessages()
{
return $this->unreadThreads()->lists(Models::table('threads') . '.id');
$threadsWithNewMessages = [];

$participants = Models::participant()->where('user_id', $this->id)->lists('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;
}
}

0 comments on commit 6962ecb

Please sign in to comment.