Skip to content

Commit f08a7b4

Browse files
committed
fix read_at
1 parent 29adba0 commit f08a7b4

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

src/Message.php

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,19 @@ public function markAsReadBy(User|int $user, ?Carbon $date = null): static
139139
{
140140
$userId = $user instanceof User ? $user->getKey() : $user;
141141

142-
$read = new MessageRead;
143-
$read->user_id = $userId;
144-
145-
if ($date) {
146-
$read->updated_at = $date;
147-
$read->created_at = $date;
148-
}
142+
$read = $this->reads()->firstOrNew([
143+
'user_id' => $userId,
144+
'message_id' => $this->id,
145+
]);
149146

150-
$this->reads()->save($read);
147+
$read->read_at = $date ?? now();
148+
$read->save();
151149

152150
if ($this->relationLoaded('reads')) {
153-
$this->reads->push($read);
151+
$this->setRelation(
152+
'reads',
153+
$this->reads->except([$read])->push($read)
154+
);
154155
}
155156

156157
return $this;
@@ -160,16 +161,11 @@ public function markAsUnreadBy(User|int $user): static
160161
{
161162
$userId = $user instanceof User ? $user->getKey() : $user;
162163

163-
$read = $this->reads
164-
->firstWhere('user_id', $userId);
164+
$read = $this->reads->firstWhere('user_id', $userId);
165165

166166
if ($read) {
167-
$read->delete();
168-
169-
$this->setRelation(
170-
'reads',
171-
$this->reads->except([$read->id])
172-
);
167+
$read->read_at = null;
168+
$read->save();
173169
}
174170

175171
return $this;
@@ -187,12 +183,14 @@ public function isReadBy(User|int $user): bool
187183
return true;
188184
}
189185

190-
return (bool) $this->reads->firstWhere('user_id', $userId);
186+
return (bool) $this->reads->firstWhere(function ($read) use ($userId) {
187+
return $read->user_id === $userId && $read->read_at !== null;
188+
});
191189
}
192190

193191
public function isReadByAnyone(): bool
194192
{
195-
return $this->read_at || $this->reads->isNotEmpty();
193+
return $this->read_at || $this->reads->where('reat_at', '!=', null)->isNotEmpty();
196194
}
197195

198196
public function hasWidget(): bool
@@ -257,7 +255,12 @@ public function scopeUnread(Builder $query, User|int $user): void
257255
$query
258256
->where('read_at', null)
259257
->where('user_id', '!=', $userId)
260-
->whereDoesntHave('reads', fn ($query) => $query->where('user_id', $userId));
258+
->whereDoesntHave(
259+
'reads',
260+
fn ($query) => $query
261+
->where('user_id', $userId)
262+
->where('reat_at', '!=', null)
263+
);
261264
}
262265

263266
public function scopeRead(Builder $query, User|int $user): void
@@ -268,7 +271,12 @@ public function scopeRead(Builder $query, User|int $user): void
268271
$query
269272
->where('read_at', '!=', null)
270273
->orWhere('user_id', $userId)
271-
->orWhereHas('reads', fn ($query) => $query->where('user_id', $userId));
274+
->orWhereHas(
275+
'reads',
276+
fn ($query) => $query
277+
->where('user_id', $userId)
278+
->where('reat_at', '!=', null)
279+
);
272280
});
273281
}
274282
}

0 commit comments

Comments
 (0)