From cdba40dbb9eb8cd9c018e91fdeea72f8fa876107 Mon Sep 17 00:00:00 2001 From: Santi Date: Tue, 7 Apr 2020 12:38:45 +0200 Subject: [PATCH 1/2] Support custom polymorphic types --- src/Models/Conversation.php | 12 ++++++------ src/Models/Message.php | 10 +++++----- src/Models/MessageNotification.php | 2 +- src/Traits/Messageable.php | 4 ++-- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Models/Conversation.php b/src/Models/Conversation.php index 921a23c..16c39bc 100644 --- a/src/Models/Conversation.php +++ b/src/Models/Conversation.php @@ -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(); } @@ -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(); @@ -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']) @@ -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', @@ -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) { @@ -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(); } diff --git a/src/Models/Message.php b/src/Models/Message.php index 894c90e..0c418a1 100644 --- a/src/Models/Message.php +++ b/src/Models/Message.php @@ -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(); } @@ -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(); @@ -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([ '*', @@ -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(); } @@ -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; diff --git a/src/Models/MessageNotification.php b/src/Models/MessageNotification.php index f70636a..c2026a4 100644 --- a/src/Models/MessageNotification.php +++ b/src/Models/MessageNotification.php @@ -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(); } diff --git a/src/Traits/Messageable.php b/src/Traits/Messageable.php index c9a8410..3190ca5 100644 --- a/src/Traits/Messageable.php +++ b/src/Traits/Messageable.php @@ -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(), ]); @@ -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(); } From cf39a15d48c04bdc8e63e81867c98fa56adb98d1 Mon Sep 17 00:00:00 2001 From: Santi Date: Wed, 8 Apr 2020 10:38:13 +0200 Subject: [PATCH 2/2] Update tests with polymorphic types --- tests/Feature/ConversationControllerTest.php | 10 +++++----- tests/Feature/ConversationMessageControllerTest.php | 8 ++++---- .../ConversationParticipationControllerTest.php | 6 +++--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/Feature/ConversationControllerTest.php b/tests/Feature/ConversationControllerTest.php index a8f0cd3..546da72 100644 --- a/tests/Feature/ConversationControllerTest.php +++ b/tests/Feature/ConversationControllerTest.php @@ -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 = [ @@ -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(), ]); } diff --git a/tests/Feature/ConversationMessageControllerTest.php b/tests/Feature/ConversationMessageControllerTest.php index bf4f2c0..2052539 100644 --- a/tests/Feature/ConversationMessageControllerTest.php +++ b/tests/Feature/ConversationMessageControllerTest.php @@ -21,7 +21,7 @@ public function testStore() $payload = [ 'participant_id' => $userModel->getKey(), - 'participant_type' => get_class($userModel), + 'participant_type' => $userModel->getMorphClass(), 'message' => [ 'body' => 'Hello', ], @@ -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', @@ -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]); @@ -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)) diff --git a/tests/Feature/ConversationParticipationControllerTest.php b/tests/Feature/ConversationParticipationControllerTest.php index a4be35c..b317793 100644 --- a/tests/Feature/ConversationParticipationControllerTest.php +++ b/tests/Feature/ConversationParticipationControllerTest.php @@ -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()], ], ]; @@ -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(), ]); }