Skip to content

Commit

Permalink
Merge pull request #247 from santiripper/feature/morph-classes
Browse files Browse the repository at this point in the history
Support custom polymorphic types
  • Loading branch information
musonza authored Apr 8, 2020
2 parents a6435d4 + cf39a15 commit c72618e
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 26 deletions.
12 changes: 6 additions & 6 deletions src/Models/Conversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function participantFromSender(Model $sender)
return $this->participants()->where([
'conversation_id' => $this->getKey(),
'messageable_id' => $sender->getKey(),
'messageable_type' => get_class($sender),
'messageable_type' => $sender->getMorphClass(),
])->first();
}

Expand Down Expand Up @@ -258,7 +258,7 @@ public function unReadNotifications(Model $participant): Collection
{
$notifications = MessageNotification::where([
['messageable_id', '=', $participant->getKey()],
['messageable_type', '=', get_class($participant)],
['messageable_type', '=', $participant->getMorphClass()],
['conversation_id', '=', $this->id],
['is_seen', '=', 0],
])->get();
Expand Down Expand Up @@ -320,7 +320,7 @@ private function getConversationMessages(Model $participant, $paginationParams,
{
$messages = $this->messages()
->join($this->tablePrefix.'message_notifications', $this->tablePrefix.'message_notifications.message_id', '=', $this->tablePrefix.'messages.id')
->where($this->tablePrefix.'message_notifications.messageable_type', get_class($participant))
->where($this->tablePrefix.'message_notifications.messageable_type', $participant->getMorphClass())
->where($this->tablePrefix.'message_notifications.messageable_id', $participant->getKey());
$messages = $deleted ? $messages->whereNotNull($this->tablePrefix.'message_notifications.deleted_at') : $messages->whereNull($this->tablePrefix.'message_notifications.deleted_at');
$messages = $messages->orderBy($this->tablePrefix.'messages.id', $paginationParams['sorting'])
Expand Down Expand Up @@ -358,7 +358,7 @@ private function getConversationsList(Model $participant, $options)
$query->join($this->tablePrefix.'message_notifications', $this->tablePrefix.'message_notifications.message_id', '=', $this->tablePrefix.'messages.id')
->select($this->tablePrefix.'message_notifications.*', $this->tablePrefix.'messages.*')
->where($this->tablePrefix.'message_notifications.messageable_id', $participant->getKey())
->where($this->tablePrefix.'message_notifications.messageable_type', get_class($participant))
->where($this->tablePrefix.'message_notifications.messageable_type', $participant->getMorphClass())
->whereNull($this->tablePrefix.'message_notifications.deleted_at');
},
'conversation.participants.messageable',
Expand Down Expand Up @@ -388,7 +388,7 @@ public function unDeletedCount()
private function notifications(Model $participant, $readAll)
{
$notifications = MessageNotification::where('messageable_id', $participant->getKey())
->where($this->tablePrefix.'message_notifications.messageable_type', get_class($participant))
->where($this->tablePrefix.'message_notifications.messageable_type', $participant->getMorphClass())
->where('conversation_id', $this->id);

if ($readAll) {
Expand All @@ -401,7 +401,7 @@ private function notifications(Model $participant, $readAll)
private function clearConversation($participant): void
{
MessageNotification::where('messageable_id', $participant->getKey())
->where($this->tablePrefix.'message_notifications.messageable_type', get_class($participant))
->where($this->tablePrefix.'message_notifications.messageable_type', $participant->getMorphClass())
->where('conversation_id', $this->getKey())
->delete();
}
Expand Down
10 changes: 5 additions & 5 deletions src/Models/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function unreadCount(Model $participant)
{
return MessageNotification::where('messageable_id', $participant->getKey())
->where('is_seen', 0)
->where('messageable_type', get_class($participant))
->where('messageable_type', $participant->getMorphClass())
->count();
}

Expand Down Expand Up @@ -120,7 +120,7 @@ protected function createNotifications($message)
public function trash(Model $participant): void
{
MessageNotification::where('messageable_id', $participant->getKey())
->where('messageable_type', get_class($participant))
->where('messageable_type', $participant->getMorphClass())
->where('message_id', $this->getKey())
->delete();

Expand All @@ -145,7 +145,7 @@ public function unDeletedCount()
public function getNotification(Model $participant): MessageNotification
{
return MessageNotification::where('messageable_id', $participant->getKey())
->where('messageable_type', get_class($participant))
->where('messageable_type', $participant->getMorphClass())
->where('message_id', $this->id)
->select([
'*',
Expand All @@ -168,7 +168,7 @@ public function flagged(Model $participant): bool
{
return (bool) MessageNotification::where('messageable_id', $participant->getKey())
->where('message_id', $this->id)
->where('messageable_type', get_class($participant))
->where('messageable_type', $participant->getMorphClass())
->where('flagged', 1)
->first();
}
Expand All @@ -177,7 +177,7 @@ public function toggleFlag(Model $participant): self
{
MessageNotification::where('messageable_id', $participant->getKey())
->where('message_id', $this->id)
->where('messageable_type', get_class($participant))
->where('messageable_type', $participant->getMorphClass())
->update(['flagged' => $this->flagged($participant) ? false : true]);

return $this;
Expand Down
2 changes: 1 addition & 1 deletion src/Models/MessageNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function unReadNotifications(Model $participant)
{
return self::where([
['messageable_id', '=', $participant->getKey()],
['messageable_type', '=', get_class($participant)],
['messageable_type', '=', $participant->getMorphClass()],
['is_seen', '=', 0],
])->get();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Traits/Messageable.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function joinConversation(Conversation $conversation)

$participation = new Participation([
'messageable_id' => $this->getKey(),
'messageable_type' => get_class($this),
'messageable_type' => $this->getMorphClass(),
'conversation_id' => $conversation->getKey(),
]);

Expand All @@ -41,7 +41,7 @@ public function leaveConversation($conversationId)
{
$this->participation()->where([
'messageable_id' => $this->getKey(),
'messageable_type' => get_class($this),
'messageable_type' => $this->getMorphClass(),
'conversation_id' => $conversationId,
])->delete();
}
Expand Down
10 changes: 5 additions & 5 deletions tests/Feature/ConversationControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public function testStore()
$botModel = factory(Bot::class)->create();

$participants = [
['id' => $userModel->getKey(), 'type' => get_class($userModel)],
['id' => $clientModel->getKey(), 'type' => get_class($clientModel)],
['id' => $botModel->getKey(), 'type' => get_class($botModel)],
['id' => $userModel->getKey(), 'type' => $userModel->getMorphClass()],
['id' => $clientModel->getKey(), 'type' => $clientModel->getMorphClass()],
['id' => $botModel->getKey(), 'type' => $botModel->getMorphClass()],
];

$payload = [
Expand All @@ -41,12 +41,12 @@ public function testStore()

$this->assertDatabaseHas(ConfigurationManager::PARTICIPATION_TABLE, [
'messageable_id' => $userModel->getKey(),
'messageable_type' => get_class($userModel),
'messageable_type' => $userModel->getMorphClass(),
]);

$this->assertDatabaseHas(ConfigurationManager::PARTICIPATION_TABLE, [
'messageable_id' => $botModel->getKey(),
'messageable_type' => get_class($botModel),
'messageable_type' => $botModel->getMorphClass(),
]);
}

Expand Down
8 changes: 4 additions & 4 deletions tests/Feature/ConversationMessageControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testStore()

$payload = [
'participant_id' => $userModel->getKey(),
'participant_type' => get_class($userModel),
'participant_type' => $userModel->getMorphClass(),
'message' => [
'body' => 'Hello',
],
Expand Down Expand Up @@ -50,7 +50,7 @@ public function testIndex()
$parameters = [
$conversation->getKey(),
'participant_id' => $userModel->getKey(),
'participant_type' => get_class($userModel),
'participant_type' => $userModel->getMorphClass(),
'page' => 1,
'perPage' => 2,
'sorting' => 'desc',
Expand Down Expand Up @@ -85,7 +85,7 @@ public function testClearConversation()
$parameters = [
$conversation->getKey(),
'participant_id' => $userModel->getKey(),
'participant_type' => get_class($userModel),
'participant_type' => $userModel->getMorphClass(),
];

Chat::conversation($conversation)->addParticipants([$userModel, $clientModel]);
Expand Down Expand Up @@ -113,7 +113,7 @@ public function testDestroy()
$conversation->getKey(),
$message->getKey(),
'participant_id' => $userModel->getKey(),
'participant_type' => get_class($userModel),
'participant_type' => $userModel->getMorphClass(),
];

$this->deleteJson(route('conversations.messages.destroy', $parameters))
Expand Down
6 changes: 3 additions & 3 deletions tests/Feature/ConversationParticipationControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public function testStore()
$clientModel = factory(Client::class)->create();
$payload = [
'participants' => [
['id' => $userModel->getKey(), 'type' => get_class($userModel)],
['id' => $clientModel->getKey(), 'type' => get_class($clientModel)],
['id' => $userModel->getKey(), 'type' => $userModel->getMorphClass()],
['id' => $clientModel->getKey(), 'type' => $clientModel->getMorphClass()],
],
];

Expand Down Expand Up @@ -54,7 +54,7 @@ public function testShow()
$this->getJson(route('conversations.participation.show', [$conversation->getKey(), $participant->getKey()]))
->assertStatus(200)
->assertJson([
'messageable_type' => get_class($userModel),
'messageable_type' => $userModel->getMorphClass(),
]);
}

Expand Down

0 comments on commit c72618e

Please sign in to comment.