Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added 'data()' function to store details about message type in 'data' column of 'message' table #290

Merged
merged 5 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 an array.

```php
$message = Chat::message('Attachment 1')
->type('attachment')
->data(['file_name' => 'post_image.jpg', 'file_url' => 'http://example.com/post_img.jpg'])
->from($model)
->to($conversation)
->send();
```

### Get a message by id

Expand Down
1 change: 1 addition & 0 deletions database/migrations/create_chat_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
1 change: 1 addition & 0 deletions src/Eventing/MessageWasSent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
Expand Down
4 changes: 3 additions & 1 deletion src/Messages/SendMessageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class SendMessageCommand
public $body;
public $conversation;
public $type;
public $data;
public $participant;

/**
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/Messages/SendMessageCommandHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
5 changes: 4 additions & 1 deletion src/Models/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Message extends BaseModel
'body',
'participation_id',
'type',
'data'
];

protected $table = ConfigurationManager::MESSAGES_TABLE;
Expand All @@ -35,6 +36,7 @@ class Message extends BaseModel
*/
protected $casts = [
'flagged' => 'boolean',
'data' => 'array'
];

protected $appends = ['sender'];
Expand Down Expand Up @@ -84,12 +86,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', array $data = []): Model
{
$message = $conversation->messages()->create([
'body' => $body,
'participation_id' => $participant->getKey(),
'type' => $type,
'data' => $data
]);

if (Chat::broadcasts()) {
Expand Down
10 changes: 9 additions & 1 deletion src/Services/MessageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class MessageService
use SetsParticipants;

protected $type = 'text';
protected $data = [];
protected $body;
/**
* @var CommandBus
Expand Down Expand Up @@ -54,6 +55,13 @@ public function type(string $type)
return $this;
}

public function data(array $data)
{
$this->data = $data;

return $this;
}

public function getById($id)
{
return $this->message->findOrFail($id);
Expand Down Expand Up @@ -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);
}
Expand Down