Skip to content

Commit 956f032

Browse files
committed
reimplement unread messages fix from #314
1 parent 70f5cfd commit 956f032

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/Models/Thread.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ protected function createSelectString($columns)
398398
*/
399399
public function userUnreadMessages($userId)
400400
{
401-
$messages = $this->messages()->get();
401+
$messages = $this->messages()->where('user_id', '!=', $userId)->get();
402402

403403
try {
404404
$participant = $this->getParticipantFromUser($userId);

tests/EloquentThreadTest.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ public function it_should_get_all_unread_messages_for_user()
413413
$message_1 = $this->faktory->build('message', [
414414
'created_at' => Carbon::now(),
415415
'body' => 'Message 1',
416+
'user_id' => $participant_1->user_id,
416417
]);
417418

418419
$thread->participants()->saveMany([$participant_1, $participant_2]);
@@ -426,28 +427,30 @@ public function it_should_get_all_unread_messages_for_user()
426427
$message_2 = $this->faktory->build('message', [
427428
'created_at' => Carbon::now(),
428429
'body' => 'Message 2',
430+
'user_id' => $participant_1->user_id,
429431
]);
430432

431433
$thread->messages()->saveMany([$message_2]);
432434

433-
$this->assertEquals('Message 1', $thread->userUnreadMessages(1)->first()->body);
434-
$this->assertCount(2, $thread->userUnreadMessages(1));
435+
$this->assertCount(0, $thread->userUnreadMessages($participant_1->user_id));
435436

436-
$this->assertEquals('Message 2', $thread->userUnreadMessages(2)->first()->body);
437-
$this->assertCount(1, $thread->userUnreadMessages(2));
437+
$secondParticipantUnreadMessages = $thread->userUnreadMessages($participant_2->user_id);
438+
$this->assertCount(1, $secondParticipantUnreadMessages);
439+
$this->assertEquals('Message 2', $secondParticipantUnreadMessages->first()->body);
438440
}
439441

440442
/** @test */
441-
public function it_should_get_count_of_all_unread_messages_for_user()
443+
public function it_should_get_all_unread_messages_for_user_when_dates_not_set()
442444
{
443445
$thread = $this->faktory->create('thread');
444446

445447
$participant_1 = $this->faktory->build('participant');
446448
$participant_2 = $this->faktory->build('participant', ['user_id' => 2]);
447449

448450
$message_1 = $this->faktory->build('message', [
449-
'created_at' => Carbon::now(),
451+
// 'created_at' => Carbon::now(),
450452
'body' => 'Message 1',
453+
'user_id' => $participant_1->user_id,
451454
]);
452455

453456
$thread->participants()->saveMany([$participant_1, $participant_2]);
@@ -459,15 +462,18 @@ public function it_should_get_count_of_all_unread_messages_for_user()
459462
sleep(1);
460463

461464
$message_2 = $this->faktory->build('message', [
462-
'created_at' => Carbon::now(),
465+
// 'created_at' => Carbon::now(),
463466
'body' => 'Message 2',
467+
'user_id' => $participant_1->user_id,
464468
]);
465469

466470
$thread->messages()->saveMany([$message_2]);
467471

468-
$this->assertEquals(2, $thread->userUnreadMessagesCount(1));
472+
$this->assertCount(0, $thread->userUnreadMessages($participant_1->user_id));
469473

470-
$this->assertEquals(1, $thread->userUnreadMessagesCount(2));
474+
$secondParticipantUnreadMessages = $thread->userUnreadMessages($participant_2->user_id);
475+
$this->assertCount(1, $secondParticipantUnreadMessages);
476+
$this->assertEquals('Message 2', $secondParticipantUnreadMessages->first()->body);
471477
}
472478

473479
/** @test */

0 commit comments

Comments
 (0)