Skip to content

Commit 6962ecb

Browse files
committed
Revert "Implemented unread threads as a relationship on Messagable Trait"
1 parent d6e8940 commit 6962ecb

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

src/Cmgmyr/Messenger/Traits/Messagable.php

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,35 +44,47 @@ public function threads()
4444
);
4545
}
4646

47-
/**
48-
* Unread threads as a relationship
49-
*
50-
* @return \Illuminate\Database\Eloquent\Relations\belongsToMany
51-
*/
52-
public function unreadThreads()
53-
{
54-
return $this->threads()
55-
->whereRaw(Models::table('threads') . '.updated_at > '
56-
. Models::table('participants') . '.last_read');
57-
}
58-
5947
/**
6048
* Returns the new messages count for user.
6149
*
6250
* @return int
6351
*/
6452
public function newThreadsCount()
6553
{
66-
return $this->unreadThreads()->count();
54+
return count($this->threadsWithNewMessages());
6755
}
6856

6957
/**
70-
* Returns the id of all threads with new messages.
58+
* Returns all threads with new messages.
7159
*
7260
* @return array
7361
*/
7462
public function threadsWithNewMessages()
7563
{
76-
return $this->unreadThreads()->lists(Models::table('threads') . '.id');
64+
$threadsWithNewMessages = [];
65+
66+
$participants = Models::participant()->where('user_id', $this->id)->lists('last_read', 'thread_id');
67+
68+
/**
69+
* @todo: see if we can fix this more in the future.
70+
* Illuminate\Foundation is not available through composer, only in laravel/framework which
71+
* I don't want to include as a dependency for this package...it's overkill. So let's
72+
* exclude this check in the testing environment.
73+
*/
74+
if (getenv('APP_ENV') == 'testing' || !str_contains(\Illuminate\Foundation\Application::VERSION, '5.0')) {
75+
$participants = $participants->all();
76+
}
77+
78+
if ($participants) {
79+
$threads = Models::thread()->whereIn('id', array_keys($participants))->get();
80+
81+
foreach ($threads as $thread) {
82+
if ($thread->updated_at > $participants[$thread->id]) {
83+
$threadsWithNewMessages[] = $thread->id;
84+
}
85+
}
86+
}
87+
88+
return $threadsWithNewMessages;
7789
}
7890
}

0 commit comments

Comments
 (0)