gicminos / chat
musonza/chat 的分支,以维护未来 Laravel 版本中的 3 版本
Requires
- php: >=7.3
- laravel/framework: ^6.20.12|^7.30.4
Requires (Dev)
- mockery/mockery: ^1.0.0
- orchestra/database: ^4.0|^5.0
- orchestra/testbench: ^4.0|^5.0
- phpunit/phpunit: ^7.0|^8.0
- dev-master
- v3.8.2
- v3.8.1
- v3.8.0
- v3.7.0
- v3.6.0
- v3.5.0
- v3.4.2
- v3.4.1
- v3.4.0
- v3.3.2
- v3.3.1
- v3.3.0
- v3.2.7
- v3.2.6
- v3.2.5
- v3.2.4
- v3.2.3
- v3.2.2
- v3.2.1
- v3.2.0
- v3.1.2
- v3.1.1
- v3.1.0
- 3.0.x-dev
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.2
- v2.0.1
- v2.0.0
- v1.0.0
- v0.1
- dev-dev
- dev-revert-244-revert-242-patch-1
- dev-revert-242-patch-1
- dev-musonza-patch-2
- dev-musonza-patch-1
- dev-multiple-model-recipients
This package is auto-updated.
Last update: 2024-09-16 20:49:46 UTC
README
聊天
想将任何 Laravel 模型用作聊天参与者?请遵循此 PR musonza#163
简介
此包允许您将聊天系统添加到 Laravel ^5.4 应用程序
安装
从命令行运行
composer require musonza/chat
将服务提供者添加到 config\app.php
的 providers 数组
Musonza\Chat\ChatServiceProvider::class
将外观添加到别名
'Chat' => Musonza\Chat\Facades\ChatFacade::class to your `config\app.php`
该类绑定到 IoC 的 chat
$chat = App::make('chat');
发布资产
php artisan vendor:publish
这将发布数据库迁移和配置文件 musonza_chat.php
到 Laravel 配置文件夹。
配置
return [ 'user_model' => 'App\User', /** * If not set, the package will use getKeyName() on the user_model specified above */ 'user_model_primary_key' => null, /* * This will allow you to broadcast an event when a message is sent * Example: * Channel: mc-chat-conversation.2, * Event: Musonza\Chat\Eventing\MessageWasSent */ 'broadcasts' => false, /** * The event to fire when a message is sent * See Musonza\Chat\Eventing\MessageWasSent if you want to customize. */ 'sent_message_event' => 'Musonza\Chat\Eventing\MessageWasSent', /** * Automatically convert conversations with more than two users to public */ 'make_three_or_more_users_public' => true, ];
运行迁移
php artisan migrate
用法
默认情况下,该包假设您在 App 命名空间中有一个 User 模型。
但是,您可以更新 config
文件夹中发布的 musonza_chat.php
中的用户模型。
创建对话
$participants = [$userId, $userId2,...]; $conversation = Chat::createConversation($participants);
创建私有/公共类型的对话
$participants = [$userId, $userId2,...]; // Create a private conversation $conversation = Chat::createConversation($participants)->makePrivate(); // Create a public conversation $conversation = Chat::createConversation($participants)->makePrivate(false);
通过 id 获取对话
$conversation = Chat::conversations()->getById($id);
更新对话详情
$data = ['title' => 'PHP Channel', 'description' => 'PHP Channel Description']; $conversation->update(['data' => $data]);
发送文本消息
$message = Chat::message('Hello') ->from($user) ->to($conversation) ->send();
发送自定义类型消息
默认消息类型是 text
。如果您想指定自定义类型,可以调用以下 type()
函数
$message = Chat::message('http://example.com/img') ->type('image') ->from($user) ->to($conversation) ->send();
通过 id 获取消息
$message = Chat::messages()->getById($id);
将消息标记为已读
Chat::message($message)->setUser($user)->markRead();
标记/标记消息
Chat::message($message)->setUser($user)->toggleFlag(); Chat::message($message)->setUser($user)->flagged(); // true
将整个对话标记为已读
Chat::conversation($conversation)->setUser($user)->readAll();
未读消息计数
$unreadCount = Chat::messages()->setUser($user)->unreadCount();
每个对话的未读消息计数
Chat::conversation($conversation)->setUser($user)->unreadCount();
删除消息
Chat::message($message)->setUser($user)->delete();
清除对话
Chat::conversation($conversation)->setUser($user)->clear();
获取两个用户之间的对话
$conversation = Chat::conversations()->between($user1, $user2);
获取用户之间的公共对话
$conversations = Chat::conversations()->common($users);
$users
可以是用户 ID 的数组,例如 [1,4,6]
或用户的集合 (\Illuminate\Database\Eloquent\Collection)
从对话中移除用户
/* removing one user */ Chat::conversation($conversation)->removeParticipants($user);
/* removing multiple users */ Chat::conversation($conversation)->removeParticipants([$user1, $user2, $user3,...,$userN]);
向对话中添加用户
/* add one user */ Chat::conversation($conversation)->addParticipants($user);
/* add multiple users */ Chat::conversation($conversation)->addParticipants([$user3, $user4]);
注意:默认情况下,第三个用户将对话分类为非私密,如果它是私密的话。请参阅配置以更改此设置。
获取对话中的消息
Chat::conversation($conversation)->setUser($user)->getMessages()
按类型获取用户对话
// private conversations $conversations = Chat::conversations()->setUser($user)->isPrivate()->get(); // public conversations $conversations = Chat::conversations()->setUser($user)->isPrivate(false)->get(); // all conversations $conversations = Chat::conversations()->setUser($user)->get();
获取最近的消息
$messages = Chat::conversations()->setUser($user)->limit(25)->page(1)->get();
示例
[
"id" => 1
"private" => "1"
"data" => []
"created_at" => "2018-06-02 21:35:52"
"updated_at" => "2018-06-02 21:35:52"
"last_message" => array:13 [
"id" => 2
"message_id" => "2"
"conversation_id" => "1"
"user_id" => "1"
"is_seen" => "1"
"is_sender" => "1"
"flagged" => false
"created_at" => "2018-06-02 21:35:52"
"updated_at" => "2018-06-02 21:35:52"
"deleted_at" => null
"body" => "Hello 2"
"type" => "text"
"sender" => array:7 [
"id" => 1
"name" => "Jalyn Ernser"
"email" => "colt.howell@example.com"
]
]
]
分页
您可以通过几种方式实现分页。您可以使用相应的函数指定 limit
和 page
,如下所示,或者如下所示
$paginated = Chat::conversations()->setUser($user)
->setPaginationParams([
'page' => 3,
'perPage' => 10,
'sorting' => "desc",
'columns' => [
'*'
],
'pageName' => 'test'
])
->get();
您不必指定所有参数。如果您省略了参数,将使用默认值。上面的 $paginated
将返回 Illuminate\Pagination\LengthAwarePaginator
。要获取 conversations
,只需调用 $paginated->items()
获取对话中的用户
$users = $conversation->users;
许可证
Chat 是开源软件,许可协议为 MIT 许可证