Skip to content

Commit

Permalink
Refactoring & Cleaning the Discussion Model (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanedev-maroc authored Aug 13, 2016
1 parent 84cd1fc commit 258c28f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 51 deletions.
11 changes: 1 addition & 10 deletions src/Contracts/Discussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public static function getBySubject($subject, $strict = false);
*
* @param int|null $userId
*
* @return array
* @return \Illuminate\Support\Collection
*/
public function participantsUserIds($userId = null);

Expand Down Expand Up @@ -246,13 +246,4 @@ public function hasParticipant($userId);
* @return \Illuminate\Support\Collection
*/
public function userUnreadMessages($userId);

/**
* Returns count of unread messages in thread for given user.
*
* @param int $userId
*
* @return int
*/
public function userUnreadMessagesCount($userId);
}
47 changes: 11 additions & 36 deletions src/Models/Discussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,19 +260,14 @@ public static function getBySubject($subject, $strict = false)
*
* @param int|null $userId
*
* @return array
* @return \Illuminate\Support\Collection
*/
public function participantsUserIds($userId = null)
{
$usersIds = $this->participants()
->withTrashed()
->lists('user_id')
->toArray();

if ( ! is_null($userId) && ! in_array($userId, $usersIds))
$usersIds[] = $userId;
/** @var \Illuminate\Support\Collection $usersIds */
$usersIds = $this->participants()->withTrashed()->lists('user_id');

return $usersIds;
return $usersIds->push($userId)->filter()->unique();
}

/**
Expand Down Expand Up @@ -319,14 +314,7 @@ public function addParticipants(array $userIds)
*/
public function removeParticipant($userId, $reload = true)
{
$deleted = $this->participants()
->where('discussion_id', $this->id)
->where('user_id', $userId)
->delete();

if ($reload) $this->load(['participants']);

return $deleted;
return $this->removeParticipants([$userId], $reload);
}

/**
Expand Down Expand Up @@ -477,25 +465,12 @@ public function userUnreadMessages($userId)
{
$participant = $this->getParticipantByUserId($userId);

if (is_null($participant)) return collect();
if (is_null($participant->last_read)) return collect($this->messages);
if (is_null($participant)) return collect();

$messages = $this->messages->filter(function (MessageContract $message) use ($participant) {
return $message->updated_at->gt($participant->last_read);
});

return collect($messages);
}

/**
* Returns count of unread messages in thread for given user.
*
* @param int $userId
*
* @return int
*/
public function userUnreadMessagesCount($userId)
{
return $this->userUnreadMessages($userId)->count();
return is_null($participant->last_read)
? $this->messages->toBase()
: $this->messages->filter(function (MessageContract $message) use ($participant) {
return $message->updated_at->gt($participant->last_read);
})->toBase();
}
}
13 changes: 8 additions & 5 deletions tests/Models/DiscussionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,20 +453,23 @@ public function it_can_get_users_ids()

$usersIds = $discussion->participantsUserIds();

$this->assertInstanceOf(\Illuminate\Support\Collection::class, $usersIds);
$this->assertCount(count($ids), $usersIds);
$this->assertEquals($ids, $usersIds);
$this->assertEquals($ids, $usersIds->toArray());

// Ignore the user id if exists
$usersIds = $discussion->participantsUserIds(3);

$this->assertInstanceOf(\Illuminate\Support\Collection::class, $usersIds);
$this->assertCount(count($ids), $usersIds);
$this->assertEquals($ids, $usersIds);
$this->assertEquals($ids, $usersIds->toArray());

$ids[] = 4;
$usersIds = $discussion->participantsUserIds(4);

$this->assertInstanceOf(\Illuminate\Support\Collection::class, $usersIds);
$this->assertCount(count($ids), $usersIds);
$this->assertEquals($ids, $usersIds);
$this->assertEquals($ids, $usersIds->toArray());
}

/** @test */
Expand Down Expand Up @@ -594,7 +597,7 @@ public function it_can_get_count_of_all_unread_messages_for_user()
$this->factory->make(Message::class, ['body' => 'Message 2', 'created_at' => Carbon::now()])
);

$this->assertEquals(2, $discussion->userUnreadMessagesCount($participantOne->user_id));
$this->assertEquals(1, $discussion->userUnreadMessagesCount($participantTwo->user_id));
$this->assertCount(2, $discussion->userUnreadMessages($participantOne->user_id));
$this->assertCount(1, $discussion->userUnreadMessages($participantTwo->user_id));
}
}

0 comments on commit 258c28f

Please sign in to comment.