Skip to content

Commit 67f88c2

Browse files
committed
implement getReadAt
1 parent 3ac39f8 commit 67f88c2

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

src/Concerns/HasConversationsTrait.php renamed to src/Concerns/ParticipateToConversations.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
1111
use Illuminate\Database\Eloquent\Relations\HasMany;
1212
use Illuminate\Database\Eloquent\Relations\HasOne;
13+
use Illuminate\Support\Facades\DB;
1314

1415
/**
1516
* @template TConversationUser of ConversationUser
@@ -21,7 +22,7 @@
2122
* @property ?TMessage $latestMessage
2223
* @property ?TConversationUser $conversationUser Conversation Pivot
2324
*/
24-
trait HasConversationsTrait
25+
trait ParticipateToConversations
2526
{
2627
protected static function bootHasConversationsTrait(): void
2728
{
@@ -71,6 +72,8 @@ public function conversations(): BelongsToMany
7172
}
7273

7374
/**
75+
* Return unread conversations using the `message_reads` table
76+
*
7477
* @return BelongsToMany<TConversation, $this>
7578
*/
7679
public function conversationsUnread(): BelongsToMany
@@ -79,13 +82,39 @@ public function conversationsUnread(): BelongsToMany
7982
}
8083

8184
/**
85+
* Return read conversations using the `message_reads` table
86+
*
8287
* @return BelongsToMany<TConversation, $this>
8388
*/
8489
public function conversationsRead(): BelongsToMany
8590
{
8691
return $this->conversations()->read($this->id);
8792
}
8893

94+
/**
95+
* Return unread conversations using the pivot column `conversation_user.last_read_message_id`
96+
*
97+
* @return BelongsToMany<TConversation, $this>
98+
*/
99+
public function denormalizedConversationsUnread(): BelongsToMany
100+
{
101+
return $this
102+
->conversations()
103+
->wherePivot('last_read_message_id', '<', DB::raw('conversations.latest_message_id'));
104+
}
105+
106+
/**
107+
* Return read conversations using the pivot column `conversation_user.last_read_message_id`
108+
*
109+
* @return BelongsToMany<TConversation, $this>
110+
*/
111+
public function denormalizedConversationsRead(): BelongsToMany
112+
{
113+
return $this
114+
->conversations()
115+
->wherePivot('last_read_message_id', '>=', DB::raw('conversations.latest_message_id'));
116+
}
117+
89118
/**
90119
* @return BelongsToMany<TConversation, $this>
91120
*/

src/Message.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
* @property ?TUser $user
4040
* @property Collection<int, MessageRead> $reads
4141
* @property ?ArrayObject<array-key, mixed> $metadata
42-
* @property ?Carbon $created_at
43-
* @property ?Carbon $read_at
42+
* @property Carbon $created_at
43+
* @property Carbon $read_at
4444
* @property ?Carbon $deleted_at
4545
*/
4646
class Message extends Model
@@ -179,21 +179,28 @@ public function markAsUnreadBy(User|int $user): static
179179
return $this;
180180
}
181181

182-
public function isReadBy(User|int $user): bool
182+
public function getReadAt(User|int $user): ?Carbon
183183
{
184184
$userId = $user instanceof User ? $user->getKey() : $user;
185185

186186
if ($this->user_id === $userId) {
187-
return true;
187+
return $this->created_at;
188188
}
189189

190190
if ($this->read_at) {
191-
return true;
191+
return $this->read_at;
192192
}
193193

194-
return (bool) $this->reads->firstWhere(function ($read) use ($userId) {
194+
$read = $this->reads->firstWhere(function ($read) use ($userId) {
195195
return $read->user_id === $userId && $read->read_at !== null;
196196
});
197+
198+
return $read?->read_at;
199+
}
200+
201+
public function isReadBy(User|int $user): bool
202+
{
203+
return (bool) $this->getReadAt($user);
197204
}
198205

199206
public function isReadByAnyone(): bool

0 commit comments

Comments
 (0)