From b6c7e8673b66c1cc3ee4f92682a13df48d6ca4a4 Mon Sep 17 00:00:00 2001 From: 199ocero <199ocero@gmail.com> Date: Sun, 21 Jul 2024 09:32:32 +0800 Subject: [PATCH] feat:added file upload and minor improvements --- README.md | 27 +++ config/filachat.php | 62 +++++++ resources/css/filachat.css | 3 + resources/css/index.css | 1 - .../filachat/components/chat-box.blade.php | 152 +++++++++++++--- .../filachat/components/chat-list.blade.php | 6 +- .../FilaChatMessageReceiverIsAwayEvent.php | 37 ++++ src/FilaChatServiceProvider.php | 2 +- src/Livewire/ChatBox.php | 164 +++++++++++++----- src/Models/FilaChatConversation.php | 20 ++- src/Traits/CanGetOriginalFileName.php | 11 ++ src/Traits/CanValidateAudio.php | 27 +++ src/Traits/CanValidateDocument.php | 29 ++++ src/Traits/CanValidateImage.php | 24 +++ src/Traits/CanValidateVideo.php | 28 +++ 15 files changed, 521 insertions(+), 72 deletions(-) create mode 100644 resources/css/filachat.css delete mode 100644 resources/css/index.css create mode 100644 src/Events/FilaChatMessageReceiverIsAwayEvent.php create mode 100644 src/Traits/CanGetOriginalFileName.php create mode 100644 src/Traits/CanValidateAudio.php create mode 100644 src/Traits/CanValidateDocument.php create mode 100644 src/Traits/CanValidateImage.php create mode 100644 src/Traits/CanValidateVideo.php diff --git a/README.md b/README.md index 0668c7b..eca50a1 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,12 @@ php artisan filachat:install You can view the full content of the config file here: [config/filachat.php](https://github.com/199ocero/filachat/blob/main/config/filachat.php) +Next, execute the following command to generate assets in your public folder. + +```bash +php artisan filament:assets +``` + > [!NOTE] > This step is optional if you want to enable role restrictions. You only need to create an agent if you want to set up role-based chat support. @@ -112,6 +118,27 @@ Then everytime you start your application in your local environment, you will ne php artisan reverb:start ``` +When using file uploads, Livewire has a default file size limit of 12 MB. To change this limit, you need to publish the Livewire configuration file using the command `php artisan livewire:publish --config` and then adjust the `rule`. + +```php + [ + 'rules' => 'max:20000', // Example: ['file', 'mimes:png,jpg'] | Default: ['required', 'file', 'max:12288'] (12MB) + ], + //... +]; +``` + +You also need to adjust the `post_max_size` and `upload_max_filesize` settings in your `php.ini` file. + +```ini +post_max_size = 20MB +upload_max_filesize = 20MB +``` + ## Testing ```bash diff --git a/config/filachat.php b/config/filachat.php index c906f12..e5cd326 100644 --- a/config/filachat.php +++ b/config/filachat.php @@ -110,6 +110,68 @@ */ 'receiver_name_column' => 'name', + /* + |-------------------------------------------------------------------------- + | Upload Files + |-------------------------------------------------------------------------- + | + | This option specifies the mime types and the type of disk to be used. + | + */ + 'disk' => 'public', + // this configuration is only use if the disk is S3 + 's3' => [ + 'directory' => 'attachments', + 'visibility' => 'public', + ], + // these are the mime types that are allowed and you can remove if you want + 'mime_types' => [ + // audio + 'audio/m4a', + 'audio/wav', + 'audio/mpeg', + 'audio/ogg', + 'audio/aac', + 'audio/flac', + 'audio/midi', + + // images + 'image/png', + 'image/jpeg', + 'image/jpg', + 'image/gif', + + // videos + 'video/mp4', + 'video/avi', + 'video/quicktime', + 'video/webm', + 'video/x-matroska', + 'video/x-flv', + 'video/mpeg', + + // documents + 'application/pdf', + 'application/msword', + 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', + 'text/csv', + 'text/plain', + 'application/vnd.ms-excel', + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', + 'application/vnd.ms-powerpoint', + 'application/vnd.openxmlformats-officedocument.presentationml.presentation', + ], + /* + | If you want to change the maximum file size above 12mb you need to publish + | livewire config file and change the value for rules. Example below is from livewire config file. + | 'rules' => null, // Example: ['file', 'mimes:png,jpg'] | Default: ['required', 'file', 'max:12288'] (12MB) + */ + 'max_file_size' => 12288, // default livewire 12MB converted to kilobytes + 'min_file_size' => 1, + // this option here is for number of files to be uploaded + 'max_files' => 10, + 'min_files' => 0, + /* |-------------------------------------------------------------------------- | Route Slug diff --git a/resources/css/filachat.css b/resources/css/filachat.css new file mode 100644 index 0000000..c9043c7 --- /dev/null +++ b/resources/css/filachat.css @@ -0,0 +1,3 @@ +.filachat-filepond .filepond--root { + max-height: 20rem; +} \ No newline at end of file diff --git a/resources/css/index.css b/resources/css/index.css deleted file mode 100644 index b3949d5..0000000 --- a/resources/css/index.css +++ /dev/null @@ -1 +0,0 @@ -@import '../../vendor/filament/filament/resources/css/theme.css'; diff --git a/resources/views/filachat/components/chat-box.blade.php b/resources/views/filachat/components/chat-box.blade.php index 9263a57..2f85290 100644 --- a/resources/views/filachat/components/chat-box.blade.php +++ b/resources/views/filachat/components/chat-box.blade.php @@ -1,6 +1,8 @@ @props(['selectedConversation']) -
+
@if ($selectedConversation)
@@ -27,18 +29,27 @@
-
- @foreach ($messages as $index => $message) + @foreach ($conversationMessages as $index => $message)
@php - $nextMessage = $messages[$index + 1] ?? null; + $nextMessage = $conversationMessages[$index + 1] ?? null; $nextMessageDate = $nextMessage ? \Carbon\Carbon::parse($nextMessage->created_at)->setTimezone(config('filachat.timezone', 'app.timezone'))->format('Y-m-d') : null; $currentMessageDate = \Carbon\Carbon::parse($message->created_at)->setTimezone(config('filachat.timezone', 'app.timezone'))->format('Y-m-d'); @@ -47,7 +58,7 @@ class="flex flex-col-reverse flex-1 p-5 overflow-y-auto"> @endphp @if ($showDateBadge) -
+
{{ \Carbon\Carbon::parse($message->created_at)->setTimezone(config('filachat.timezone', 'app.timezone'))->format('F j, Y') }} @@ -55,11 +66,11 @@ class="flex flex-col-reverse flex-1 p-5 overflow-y-auto"> @endif @if ($message->senderable_id !== auth()->user()->id) @php - $previousMessageDate = isset($messages[$index - 1]) ? \Carbon\Carbon::parse($messages[$index - 1]->created_at)->setTimezone(config('filachat.timezone', 'app.timezone'))->format('Y-m-d') : null; + $previousMessageDate = isset($conversationMessages[$index - 1]) ? \Carbon\Carbon::parse($conversationMessages[$index - 1]->created_at)->setTimezone(config('filachat.timezone', 'app.timezone'))->format('Y-m-d') : null; $currentMessageDate = \Carbon\Carbon::parse($message->created_at)->setTimezone(config('filachat.timezone', 'app.timezone'))->format('Y-m-d'); - $previousSenderId = $messages[$index - 1]->senderable_id ?? null; + $previousSenderId = $conversationMessages[$index - 1]->senderable_id ?? null; // Show avatar if the current message is the first in a consecutive sequence or a new day $showAvatar = $message->senderable_id !== auth()->user()->id && ($message->senderable_id !== $previousSenderId || $currentMessageDate !== $previousMessageDate); @@ -73,20 +84,112 @@ class="flex flex-col-reverse flex-1 p-5 overflow-y-auto"> @else
@endif -
-

{{ $message->message }}

+
+ @if ($message->message) +

{{ $message->message }}

+ @endif + @if ($message->attachments && count($message->attachments) > 0) + @foreach ($message->attachments as $attachment) + @php + $originalFileName = $this->getOriginalFileName($attachment, $message->original_attachment_file_names); + @endphp +
+
+ @php + $icon = 'heroicon-m-x-mark'; + + if($this->validateImage($attachment)) { + $icon = 'heroicon-m-photo'; + } + + if ($this->validateDocument($attachment)) { + $icon = 'heroicon-m-paper-clip'; + } + + if ($this->validateVideo($attachment)) { + $icon = 'heroicon-m-video-camera'; + } + + if ($this->validateAudio($attachment)) { + $icon = 'heroicon-m-speaker-wave'; + } + + @endphp + +
+

+ {{ $originalFileName }} +

+
+ @endforeach + @endif

- {{ \Carbon\Carbon::parse($message->created_at)->setTimezone(config('filachat.timezone', 'app.timezone'))->format('F j, Y') }} + @php + $createdAt = \Carbon\Carbon::parse($message->created_at)->setTimezone(config('filachat.timezone', 'app.timezone')); + + if ($createdAt->isToday()) { + $date = $createdAt->format('g:i A'); + } else { + $date = $createdAt->format('M d, Y g:i A'); + } + @endphp + {{ $date }}

@else
-
-

{{ $message->message }}

+
+ @if ($message->message) +

{{ $message->message }}

+ @endif + @if ($message->attachments && count($message->attachments) > 0) + @foreach ($message->attachments as $attachment) + @php + $originalFileName = $this->getOriginalFileName($attachment, $message->original_attachment_file_names); + @endphp +
+
+ @php + $icon = 'heroicon-m-x-circle'; + + if($this->validateImage($attachment)) { + $icon = 'heroicon-m-photo'; + } + + if ($this->validateDocument($attachment)) { + $icon = 'heroicon-m-paper-clip'; + } + + if ($this->validateVideo($attachment)) { + $icon = 'heroicon-m-video-camera'; + } + + if ($this->validateAudio($attachment)) { + $icon = 'heroicon-m-speaker-wave'; + } + + @endphp + +
+

+ {{ $originalFileName }} +

+
+ @endforeach + @endif

- {{ \Carbon\Carbon::parse($message->created_at)->setTimezone(config('filachat.timezone', 'app.timezone'))->format('F j, Y') }} + @php + $createdAt = \Carbon\Carbon::parse($message->created_at)->setTimezone(config('filachat.timezone', 'app.timezone')); + + if ($createdAt->isToday()) { + $date = $createdAt->format('g:i A'); + } else { + $date = $createdAt->format('M d, Y g:i A'); + } + @endphp + {{ $date }}