Skip to content

Commit 3a8e19f

Browse files
committed
added hasParticipant() method to Thread model
1 parent f8ed89d commit 3a8e19f

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/Cmgmyr/Messenger/Models/Thread.php

+16
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,22 @@ public function participantsString($userId=null, $columns=['name'])
233233
return implode(', ', $userNames);
234234
}
235235

236+
/**
237+
* Checks to see if a user is a current participant of the thread
238+
*
239+
* @param $userId
240+
* @return bool
241+
*/
242+
public function hasParticipant($userId)
243+
{
244+
$participants = $this->participants()->where('user_id', '=', $userId);
245+
if ($participants->count() > 0) {
246+
return true;
247+
}
248+
249+
return false;
250+
}
251+
236252
/**
237253
* Generates a select string used in participantsString()
238254
*

tests/EloquentThreadTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,19 @@ public function it_should_get_participants_string()
247247
$string = $thread->participantsString(1, ['email']);
248248
$this->assertEquals("[email protected], [email protected]", $string);
249249
}
250+
251+
/** @test */
252+
public function it_should_check_users_and_participants()
253+
{
254+
$thread = $this->faktory->create('thread');
255+
256+
$participant_1 = $this->faktory->build('participant');
257+
$participant_2 = $this->faktory->build('participant', ['user_id' => 2]);
258+
259+
$thread->participants()->saveMany([$participant_1, $participant_2]);
260+
261+
$this->assertTrue($thread->hasParticipant(1));
262+
$this->assertTrue($thread->hasParticipant(2));
263+
$this->assertFalse($thread->hasParticipant(3));
264+
}
250265
}

0 commit comments

Comments
 (0)