From ebd16401ea3c3e45f94716febb01b027055665dd Mon Sep 17 00:00:00 2001 From: alibabaazimi Date: Fri, 17 Dec 2021 11:48:57 +0430 Subject: [PATCH 1/5] Added 'data' column to message model --- database/migrations/create_chat_tables.php | 1 + 1 file changed, 1 insertion(+) diff --git a/database/migrations/create_chat_tables.php b/database/migrations/create_chat_tables.php index 8b4a0f9..e10e954 100644 --- a/database/migrations/create_chat_tables.php +++ b/database/migrations/create_chat_tables.php @@ -44,6 +44,7 @@ public function up() $table->bigInteger('conversation_id')->unsigned(); $table->bigInteger('participation_id')->unsigned()->nullable(); $table->string('type')->default('text'); + $table->text('data')->nullable(); $table->timestamps(); $table->foreign('participation_id') From 98a3464b53c74dd5719204837dedb8b6f7616d29 Mon Sep 17 00:00:00 2001 From: alibabaazimi Date: Fri, 17 Dec 2021 11:52:44 +0430 Subject: [PATCH 2/5] Added 'data()' function to store details of message types --- src/Eventing/MessageWasSent.php | 1 + src/Messages/SendMessageCommand.php | 4 +++- src/Messages/SendMessageCommandHandler.php | 2 +- src/Models/Message.php | 4 +++- src/Services/MessageService.php | 10 +++++++++- 5 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/Eventing/MessageWasSent.php b/src/Eventing/MessageWasSent.php index f4ab5ba..6de2cfb 100644 --- a/src/Eventing/MessageWasSent.php +++ b/src/Eventing/MessageWasSent.php @@ -40,6 +40,7 @@ public function broadcastWith() 'body' => $this->message->body, 'conversation_id' => $this->message->conversation_id, 'type' => $this->message->type, + 'data' => $this->message->data, 'created_at' => $this->message->created_at, 'sender' => $this->message->sender, ], diff --git a/src/Messages/SendMessageCommand.php b/src/Messages/SendMessageCommand.php index 85bbe0c..012cad3 100644 --- a/src/Messages/SendMessageCommand.php +++ b/src/Messages/SendMessageCommand.php @@ -10,6 +10,7 @@ class SendMessageCommand public $body; public $conversation; public $type; + public $data; public $participant; /** @@ -18,11 +19,12 @@ class SendMessageCommand * @param Model $sender The sender identifier * @param string $type The message type */ - public function __construct(Conversation $conversation, $body, Model $sender, $type = 'text') + public function __construct(Conversation $conversation, $body, Model $sender, $type = 'text', $data = '') { $this->conversation = $conversation; $this->body = $body; $this->type = $type; + $this->data = $data; $this->participant = $this->conversation->participantFromSender($sender); } } diff --git a/src/Messages/SendMessageCommandHandler.php b/src/Messages/SendMessageCommandHandler.php index f97f247..07c6d68 100644 --- a/src/Messages/SendMessageCommandHandler.php +++ b/src/Messages/SendMessageCommandHandler.php @@ -30,7 +30,7 @@ public function __construct(EventDispatcher $dispatcher, Message $message) */ public function handle(SendMessageCommand $command) { - $message = $this->message->send($command->conversation, $command->body, $command->participant, $command->type); + $message = $this->message->send($command->conversation, $command->body, $command->participant, $command->type, $command->data); $this->dispatcher->dispatch($this->message->releaseEvents()); diff --git a/src/Models/Message.php b/src/Models/Message.php index 31bb043..4407aa6 100644 --- a/src/Models/Message.php +++ b/src/Models/Message.php @@ -18,6 +18,7 @@ class Message extends BaseModel 'body', 'participation_id', 'type', + 'data' ]; protected $table = ConfigurationManager::MESSAGES_TABLE; @@ -84,12 +85,13 @@ public function conversation() * * @return Model */ - public function send(Conversation $conversation, string $body, Participation $participant, string $type = 'text'): Model + public function send(Conversation $conversation, string $body, Participation $participant, string $type = 'text', string $data = ''): Model { $message = $conversation->messages()->create([ 'body' => $body, 'participation_id' => $participant->getKey(), 'type' => $type, + 'data' => $data ]); if (Chat::broadcasts()) { diff --git a/src/Services/MessageService.php b/src/Services/MessageService.php index aca9d4d..5c09522 100644 --- a/src/Services/MessageService.php +++ b/src/Services/MessageService.php @@ -13,6 +13,7 @@ class MessageService use SetsParticipants; protected $type = 'text'; + protected $data = ''; protected $body; /** * @var CommandBus @@ -54,6 +55,13 @@ public function type(string $type) return $this; } + public function data(string $data) + { + $this->data = $data; + + return $this; + } + public function getById($id) { return $this->message->findOrFail($id); @@ -120,7 +128,7 @@ public function send() throw new Exception('Message receiver has not been set'); } - $command = new SendMessageCommand($this->recipient, $this->body, $this->sender, $this->type); + $command = new SendMessageCommand($this->recipient, $this->body, $this->sender, $this->type, $this->data); return $this->commandBus->execute($command); } From d33c511301f586dd95700d03fb1ec094922e227e Mon Sep 17 00:00:00 2001 From: alibabaazimi Date: Fri, 17 Dec 2021 11:54:02 +0430 Subject: [PATCH 3/5] Modified message instruction for 'data()' function --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 7b24709..b051550 100644 --- a/README.md +++ b/README.md @@ -185,6 +185,18 @@ $message = Chat::message('http://example.com/img') ->to($conversation) ->send(); ``` +#### To add more details about a message + +Sometimes you might want to add details about a message. For example, when the message type is an attachment, and you want to add details such as attachment's filename, and attachment's file url, you can call the `data()` function and pass your data as json. + +```php +$message = Chat::message('Attachment 1') + ->type('attachment') + ->data('{ file_name: "post_image.jpg", file_url: "http://example.com/img.jpg"}') + ->from($model) + ->to($conversation) + ->send(); +``` ### Get a message by id From 9da3125202b5b77799c3b8ecec27cf87803c821f Mon Sep 17 00:00:00 2001 From: alibabaazimi Date: Wed, 22 Dec 2021 13:47:16 +0430 Subject: [PATCH 4/5] Changed 'data' function to accept array instead string --- README.md | 4 ++-- src/Messages/SendMessageCommand.php | 2 +- src/Models/Message.php | 3 ++- src/Services/MessageService.php | 4 ++-- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b051550..220c15b 100644 --- a/README.md +++ b/README.md @@ -187,12 +187,12 @@ $message = Chat::message('http://example.com/img') ``` #### To add more details about a message -Sometimes you might want to add details about a message. For example, when the message type is an attachment, and you want to add details such as attachment's filename, and attachment's file url, you can call the `data()` function and pass your data as json. +Sometimes you might want to add details about a message. For example, when the message type is an attachment, and you want to add details such as attachment's filename, and attachment's file url, you can call the `data()` function and pass your data as an array. ```php $message = Chat::message('Attachment 1') ->type('attachment') - ->data('{ file_name: "post_image.jpg", file_url: "http://example.com/img.jpg"}') + ->data(['file_name' => 'post_image.jpg', 'file_url' => 'http://example.com/post_img.jpg']) ->from($model) ->to($conversation) ->send(); diff --git a/src/Messages/SendMessageCommand.php b/src/Messages/SendMessageCommand.php index 012cad3..b62ed21 100644 --- a/src/Messages/SendMessageCommand.php +++ b/src/Messages/SendMessageCommand.php @@ -19,7 +19,7 @@ class SendMessageCommand * @param Model $sender The sender identifier * @param string $type The message type */ - public function __construct(Conversation $conversation, $body, Model $sender, $type = 'text', $data = '') + public function __construct(Conversation $conversation, $body, Model $sender, $type = 'text', $data) { $this->conversation = $conversation; $this->body = $body; diff --git a/src/Models/Message.php b/src/Models/Message.php index 4407aa6..4f10e94 100644 --- a/src/Models/Message.php +++ b/src/Models/Message.php @@ -36,6 +36,7 @@ class Message extends BaseModel */ protected $casts = [ 'flagged' => 'boolean', + 'data' => 'array' ]; protected $appends = ['sender']; @@ -85,7 +86,7 @@ public function conversation() * * @return Model */ - public function send(Conversation $conversation, string $body, Participation $participant, string $type = 'text', string $data = ''): Model + public function send(Conversation $conversation, string $body, Participation $participant, string $type = 'text', array $data): Model { $message = $conversation->messages()->create([ 'body' => $body, diff --git a/src/Services/MessageService.php b/src/Services/MessageService.php index 5c09522..066e03b 100644 --- a/src/Services/MessageService.php +++ b/src/Services/MessageService.php @@ -13,7 +13,7 @@ class MessageService use SetsParticipants; protected $type = 'text'; - protected $data = ''; + protected $data = []; protected $body; /** * @var CommandBus @@ -55,7 +55,7 @@ public function type(string $type) return $this; } - public function data(string $data) + public function data(array $data) { $this->data = $data; From ab6d11d801a637a8f5c9097943ddb6ece78dd9b3 Mon Sep 17 00:00:00 2001 From: alibabaazimi Date: Wed, 22 Dec 2021 21:11:26 +0430 Subject: [PATCH 5/5] Added default value for 'data' paramater --- src/Models/Message.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Models/Message.php b/src/Models/Message.php index 4f10e94..f7967a9 100644 --- a/src/Models/Message.php +++ b/src/Models/Message.php @@ -86,7 +86,7 @@ public function conversation() * * @return Model */ - public function send(Conversation $conversation, string $body, Participation $participant, string $type = 'text', array $data): Model + public function send(Conversation $conversation, string $body, Participation $participant, string $type = 'text', array $data = []): Model { $message = $conversation->messages()->create([ 'body' => $body,