Skip to content

Commit

Permalink
Merge branch 'main' into feat-upload-files
Browse files Browse the repository at this point in the history
  • Loading branch information
199ocero committed Jul 15, 2024
2 parents bf523b0 + ae4d350 commit 1673137
Show file tree
Hide file tree
Showing 4 changed files with 344 additions and 179 deletions.
19 changes: 1 addition & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,7 @@ Run the following command to install FilaChat, which will take care of all migra
php artisan filachat:install
```

This is the contents of the published config file:

```php
<?php

return [
'enable_roles' => true,
'user_model' => \App\Models\User::class,
'agent_model' => \App\Models\User::class,
'sender_name_column' => 'name',
'receiver_name_column' => 'name',
'slug' => 'filachat',
'navigation_icon' => 'heroicon-o-chat-bubble-bottom-center',
'max_content_width' => \Filament\Support\Enums\MaxWidth::Full,
'timezone' => 'UTC',
];

```
You can view the full content of the config file here: [config/filachat.php](https://github.com/199ocero/filachat/blob/main/config/filachat.php)

> [!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.
Expand Down
56 changes: 54 additions & 2 deletions config/filachat.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,31 @@
*/
'user_model' => \App\Models\User::class,

/*
|--------------------------------------------------------------------------
| User Searchable Columns
|--------------------------------------------------------------------------
|
| This option specifies the searchable columns for the user model. This is used
| to search for users in the chat.
|
*/
'user_searchable_columns' => [
'name',
'email',
],

/*
|--------------------------------------------------------------------------
| User Chat List Display Column
|--------------------------------------------------------------------------
|
| This option specifies the column to be displayed when selecting the user
| in the chat list.
|
*/
'user_chat_list_display_column' => 'name',

/*
|--------------------------------------------------------------------------
| Agent Model
Expand All @@ -36,13 +61,39 @@
*/
'agent_model' => \App\Models\User::class,

/*
|--------------------------------------------------------------------------
| Agent Searchable Columns
|--------------------------------------------------------------------------
|
| This option specifies the searchable columns for the agent model. This is used
| to search for agents in the chat.
|
*/
'agent_searchable_columns' => [
'name',
'email',
],

/*
|--------------------------------------------------------------------------
| Agent Chat List Display Column
|--------------------------------------------------------------------------
|
| This option specifies the column to be displayed when selecting the agent
| in the chat list.
|
*/
'agent_chat_list_display_column' => 'name',

/*
|--------------------------------------------------------------------------
| Sender Name Column
|--------------------------------------------------------------------------
|
| This option specifies the column name for the sender's name. You can
| customize this if your user model uses a different column name.
| customize this if your user model uses a different column name. This also
| use to search for users in the chat.
|
*/
'sender_name_column' => 'name',
Expand All @@ -53,7 +104,8 @@
|--------------------------------------------------------------------------
|
| This option specifies the column name for the receiver's name. You can
| customize this if your user model uses a different column name.
| customize this if your user model uses a different column name. This also
| use to search for users in the chat.
|
*/
'receiver_name_column' => 'name',
Expand Down
188 changes: 29 additions & 159 deletions src/Livewire/ChatList.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@
use Filament\Forms;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Notifications\Notification;
use Filament\Support\Enums\MaxWidth;
use JaOcero\FilaChat\Events\FilaChatMessageEvent;
use JaOcero\FilaChat\Models\FilaChatConversation;
use JaOcero\FilaChat\Models\FilaChatMessage;
use JaOcero\FilaChat\Pages\FilaChat;
use JaOcero\FilaChat\Services\ChatListService;
use Livewire\Attributes\On;
use Livewire\Component;

Expand Down Expand Up @@ -74,88 +70,40 @@ public function createConversationAction(string $name, bool $isLabelHidden = fal
}

return 'To Agent';
} else {
return 'To';
}
})
->getSearchResultsUsing(function (string $search) use ($isRoleEnabled, $isAgent) {
$authId = auth()->id();
$searchTerm = '%' . trim($search) . '%';

$senderNameColumn = config('filachat.sender_name_column');
$receiverNameColumn = config('filachat.receiver_name_column');

$name = $senderNameColumn ?? $receiverNameColumn;

$userModelClass = config('filachat.user_model');
$agentModelClass = config('filachat.agent_model');

if ($isRoleEnabled) {

$agentIds = config('filachat.agent_model')::getAllAgentIds();
return 'To';
})
->placeholder(function () use ($isRoleEnabled, $isAgent) {
if ($isRoleEnabled && ! $isAgent) {
return 'Select Agent by Name or Email';
}

if ($isAgent) {
return $userModelClass::query()
->whereNotIn('id', $agentIds)
->where(function ($query) use ($searchTerm, $senderNameColumn, $receiverNameColumn) {
$query->where($senderNameColumn, 'like', '%' . $searchTerm . '%')
->orWhere($receiverNameColumn, 'like', '%' . $searchTerm . '%');
})
->get()
->mapWithKeys(function ($item) use ($name) {
return ['user_' . $item->id => $item->{$name}];
});
}
return 'Select User by Name or Email';
})
->searchPrompt(function () use ($isRoleEnabled, $isAgent) {
if ($isRoleEnabled && ! $isAgent) {
return 'Search Agent by Name or Email';
}

return $agentModelClass::query()
->whereIn('id', $agentIds)
->where(function ($query) use ($searchTerm, $senderNameColumn, $receiverNameColumn) {
$query->where($senderNameColumn, 'like', '%' . $searchTerm . '%')
->orWhere($receiverNameColumn, 'like', '%' . $searchTerm . '%');
})
->get()
->mapWithKeys(function ($item) use ($name) {
return ['agent_' . $item->id => $item->{$name}];
});
} else {
if ($userModelClass === $agentModelClass) {
return $userModelClass::query()
->whereNot('id', $authId)
->where(function ($query) use ($searchTerm, $senderNameColumn, $receiverNameColumn) {
$query->where($senderNameColumn, 'like', '%' . $searchTerm . '%')
->orWhere($receiverNameColumn, 'like', '%' . $searchTerm . '%');
})
->get()
->mapWithKeys(function ($item) use ($name) {
return ['user_' . $item->id => $item->{$name}];
});
}
return 'Search User by Name or Email';
})
->loadingMessage(function () use ($isRoleEnabled, $isAgent) {
if ($isRoleEnabled && ! $isAgent) {
return 'Loading Agents...';
}

$userModel = $userModelClass::query()
->whereNot('id', $authId)
->where(function ($query) use ($searchTerm, $senderNameColumn, $receiverNameColumn) {
$query->where($senderNameColumn, 'like', '%' . $searchTerm . '%')
->orWhere($receiverNameColumn, 'like', '%' . $searchTerm . '%');
})
->get()
->mapWithKeys(function ($item) use ($name) {
return ['user_' . $item->id => $item->{$name}];
});

$agentModel = $agentModelClass::query()
->whereNot('id', $authId)
->where(function ($query) use ($searchTerm, $senderNameColumn, $receiverNameColumn) {
$query->where($senderNameColumn, 'like', '%' . $searchTerm . '%')
->orWhere($receiverNameColumn, 'like', '%' . $searchTerm . '%');
})
->get()
->mapWithKeys(function ($item) use ($name) {
return ['agent_' . $item->id => $item->{$name}];
});

return $userModel->merge($agentModel);
return 'Loading Users...';
})
->noSearchResultsMessage(function () use ($isRoleEnabled, $isAgent) {
if ($isRoleEnabled && ! $isAgent) {
return 'No Agents Found.';
}

return 'No Users Found.';
})
->getSearchResultsUsing(fn (string $search): array => ChatListService::make()->getSearchResults($search)->toArray())
->getOptionLabelUsing(fn ($value): ?string => ChatListService::make()->getOptionLabel($value))
->searchable()
->required(),
Forms\Components\Textarea::make('message')
Expand All @@ -165,85 +113,7 @@ public function createConversationAction(string $name, bool $isLabelHidden = fal
->autosize(),
])
->modalWidth(MaxWidth::Large)
->action(function (array $data) {

try {
$receiverableId = $data['receiverable_id'];

if (preg_match('/^user_(\d+)$/', $receiverableId, $matches)) {
$receiverableType = config('filachat.user_model');
$receiverableId = (int) $matches[1];
}

if (preg_match('/^agent_(\d+)$/', $receiverableId, $matches)) {
$receiverableType = config('filachat.agent_model');
$receiverableId = (int) $matches[1];
}

$foundConversation = FilaChatConversation::query()
->where(function ($query) use ($receiverableId, $receiverableType) {
$query->where(function ($query) {
$query->where('senderable_id', auth()->id())
->where('senderable_type', auth()->user()::class);
})
->orWhere(function ($query) use ($receiverableId, $receiverableType) {
$query->where('senderable_id', $receiverableId)
->where('senderable_type', $receiverableType);
});
})
->where(function ($query) use ($receiverableId, $receiverableType) {
$query->where(function ($query) use ($receiverableId, $receiverableType) {
$query->where('receiverable_id', $receiverableId)
->where('receiverable_type', $receiverableType);
})
->orWhere(function ($query) {
$query->where('receiverable_id', auth()->id())
->where('receiverable_type', auth()->user()::class);
});
})
->first();

if (! $foundConversation) {
$conversation = FilaChatConversation::query()->create([
'senderable_id' => auth()->id(),
'senderable_type' => auth()->user()::class,
'receiverable_id' => $receiverableId,
'receiverable_type' => $receiverableType,
]);
} else {
$conversation = $foundConversation;
}

$message = FilaChatMessage::query()->create([
'filachat_conversation_id' => $conversation->id,
'senderable_id' => auth()->id(),
'senderable_type' => auth()->user()::class,
'receiverable_id' => $receiverableId,
'receiverable_type' => $receiverableType,
'message' => $data['message'],
]);

$conversation->updated_at = now();

$conversation->save();

broadcast(new FilaChatMessageEvent(
$conversation->id,
$message->id,
$receiverableId,
auth()->id(),
));

return $this->redirect(FilaChat::getUrl(tenant: filament()->getTenant()) . '/' . $conversation->id);
} catch (\Exception $exception) {
Notification::make()
->title('Something went wrong')
->body($exception->getMessage())
->danger()
->persistent()
->send();
}
});
->action(fn (array $data) => ChatListService::make()->createConversation($data));
}

public function render()
Expand Down
Loading

0 comments on commit 1673137

Please sign in to comment.