Skip to content

Commit 26faa96

Browse files
authored
fix participant soft deletes (#380)
1 parent b7f205b commit 26faa96

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

src/Models/Thread.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,15 @@ public function participants()
102102
*/
103103
public function users()
104104
{
105-
return $this->belongsToMany(Models::classname('User'), Models::table('participants'), 'thread_id', 'user_id');
105+
return $this
106+
->belongsToMany(
107+
Models::classname('User'),
108+
Models::table('participants'),
109+
'thread_id',
110+
'user_id'
111+
)
112+
->whereNull(Models::table('participants') . '.deleted_at')
113+
->withTimestamps();
106114
}
107115

108116
/**

src/Traits/Messagable.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,15 @@ public function participants()
4646
*/
4747
public function threads()
4848
{
49-
return $this->belongsToMany(
50-
Models::classname(Thread::class),
51-
Models::table('participants'),
52-
'user_id',
53-
'thread_id'
54-
);
49+
return $this
50+
->belongsToMany(
51+
Models::classname(Thread::class),
52+
Models::table('participants'),
53+
'user_id',
54+
'thread_id'
55+
)
56+
->whereNull(Models::table('participants') . '.deleted_at')
57+
->withTimestamps();
5558
}
5659

5760
/**

tests/MessagableTraitTest.php

+27
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,31 @@ public function it_should_get_participant_threads(): void
7575
$firstThread = $user->threads->first();
7676
$this->assertInstanceOf(Thread::class, $firstThread);
7777
}
78+
79+
/** @test */
80+
public function it_should_not_include_deleted_threads(): void
81+
{
82+
$user = $this->userFactory();
83+
84+
$thread1 = $this->threadFactory();
85+
$user_thread1 = $this->participantFactory([
86+
'user_id' => $user->id,
87+
'thread_id' => $thread1->id,
88+
]);
89+
90+
91+
$thread2 = $this->threadFactory();
92+
$user_thread2 = $this->participantFactory([
93+
'user_id' => $user->id,
94+
'thread_id' => $thread2->id,
95+
]);
96+
97+
$this->assertSame(2, $user->threads()->count());
98+
$this->assertSame(1, $thread1->users()->count());
99+
100+
$thread1->removeParticipant($user->id);
101+
102+
$this->assertSame(1, $user->threads()->count());
103+
$this->assertSame(0, $thread1->users()->count());
104+
}
78105
}

0 commit comments

Comments
 (0)