musonza/chat

Laravel的聊天包

维护者

详细信息

github.com/musonza/chat

源代码

问题

资助包维护!
www.paypal.me/tinashemusonza

安装: 179,062

依赖项: 1

建议者: 0

安全: 0

星星: 1,119

关注者: 36

分支: 308

公开问题: 58

v6.3.0 2024-03-10 00:37 UTC

README

chat

Build Status Downloads Packagist

聊天

为多个模型创建聊天应用

想了解如何制作这样的包? https://leanpub.com/laravel-package-development

目录

点击展开

查看简单的示例应用

简介

此包允许您将聊天系统添加到您的Laravel ^5.4应用程序

安装

在命令行中运行

composer require musonza/chat

发布资产

php artisan vendor:publish

这将发布数据库迁移和配置文件 musonza_chat.php 在Laravel配置文件夹中。

配置

参见 musonza_chat.php 进行配置

运行迁移

php artisan migrate

用法

您可以混合模型作为参与者。例如,您可以有 ParentsStudentsProfessors 模型进行通信

给模型添加参与能力

Musonza\Chat\Traits\Messageable 特性添加到您想要参与对话的任何模型中。例如,假设我们想要我们的 Bot 模型与其他模型聊天

use Illuminate\Database\Eloquent\Model;
use Musonza\Chat\Traits\Messageable;

class Bot extends Model
{
    use Messageable;
}

获取参与者详情

由于我们允许结构不同的模型聊天,我们可能需要一个统一的方式来以统一的方式表示参与者的详情。

您可以通过以下方式获取详情

$participantModel->getParticipantDetails();

假设您有一个名为 name 的列,这返回默认数组 ['name' => 'column_value']。但是,您可以添加Eloquent访问器来根据需要自定义此数组,如下所示

    public function getParticipantDetailsAttribute()
    {
        return [
            'name' => $this->someValue,
            'foo' => 'bar',
        ];
    }

创建对话

您可以通过传递模型数组作为参与者来启动对话

$participants = [$model1, $model2,..., $modelN];

$conversation = Chat::createConversation($participants);

创建私有/公开类型的对话

您可能想要将对话分类为私有或公开

$participants = [$model1, $model2,..., $modelN];

// Create a private conversation
$conversation = Chat::createConversation($participants)->makePrivate();

// Create a public conversation
$conversation = Chat::createConversation($participants)->makePrivate(false);

// Create a direct message

// Make direct conversation after creation
$conversation = Chat::createConversation($participants)->makeDirect();

// Specify intent for direct conversation before creation
$conversation = Chat::makeDirect()->createConversation($participants);

注意: 您将无法向直接对话添加更多参与者。另外,您不能从直接对话中删除参与者。

通过id获取对话

$conversation = Chat::conversations()->getById($id);

更新对话详情

$data = ['title' => 'PHP Channel', 'description' => 'PHP Channel Description'];
$conversation->update(['data' => $data]);

发送文本消息

$message = Chat::message('Hello')
            ->from($model)
            ->to($conversation)
            ->send();

发送自定义类型的消息

默认消息类型是 text。如果您想指定自定义类型,可以调用以下 type() 函数

$message = Chat::message('http://example.com/img')
		->type('image')
		->from($model)
		->to($conversation)
		->send();

添加更多消息详情

有时您可能想添加关于消息的详细信息。例如,当消息类型是附件时,您想添加有关附件的文件名和文件URL的详细信息,您可以通过调用 data() 函数并将数据作为数组传递来这样做。

$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();

通过ID获取消息

$message = Chat::messages()->getById($id);

获取消息发送者

$sendModel = $message->sender;

标记消息为已读

Chat::message($message)->setParticipant($participantModel)->markRead();

标记/标记消息

Chat::message($message)->setParticipant($participantModel)->toggleFlag();

Chat::message($message)->setParticipant($participantModel)->flagged(); // true

标记整个对话为已读

Chat::conversation($conversation)->setParticipant($participantModel)->readAll();

未读消息数

$unreadCount = Chat::messages()->setParticipant($participantModel)->unreadCount();

每场对话的未读消息数量

Chat::conversation($conversation)->setParticipant($participantModel)->unreadCount();

删除消息

Chat::message($message)->setParticipant($participantModel)->delete();

清理已删除的消息

当所有参与者都删除了 $message$conversation 时,应该清理什么?

监听 \Musonza\Chat\Eventing\AllParticipantsDeletedMessage\Musonza\Chat\Eventing\AllParticipantsClearedConversation

清除对话

Chat::conversation($conversation)->setParticipant($participantModel)->clear();

获取参与者的对话

Chat::conversations()->setPaginationParams(['sorting' => 'desc'])
	->setParticipant($participantModel)
	->limit(1)
	->page(1)
	->get();

获取两个参与者之间的对话

$conversation = Chat::conversations()->between($participantModel1, $participantModel2);

获取参与者之间的公共对话

$conversations = Chat::conversations()->common($participants);

$participants 是参与者模型数组

从对话中移除参与者

/* removing one user */
Chat::conversation($conversation)->removeParticipants([$participantModel]);
/* removing multiple participants */
Chat::conversation($conversation)->removeParticipants([$participantModel, $participantModel2,...,$participantModelN]);

向对话中添加参与者

/* add one user */
Chat::conversation($conversation)->addParticipants([$participantModel]);
/* add multiple participants */
Chat::conversation($conversation)->addParticipants([$participantModel, $participantModel2]);

获取对话中的消息

Chat::conversation($conversation)->setParticipant($participantModel)->getMessages()

按类型获取用户对话

// private conversations
$conversations = Chat::conversations()->setParticipant($participantModel)->isPrivate()->get();

// public conversations
$conversations = Chat::conversations()->setParticipant($participantModel)->isPrivate(false)->get();

// direct conversations / messages
$conversations = Chat::conversations()->setParticipant($participantModel)->isDirect()->get();

// all conversations
$conversations = Chat::conversations()->setParticipant($participantModel)->get();

获取最新消息

$messages = Chat::conversations()->setParticipant($participantModel)->limit(25)->page(1)->get();

分页

您可以通过几种方式实现分页。您可以使用相应的函数或如下方式指定 limitpage

   $paginated = Chat::conversations()->setParticipant($participant)
            ->setPaginationParams([
                'page' => 3,
                'perPage' => 10,
                'sorting' => "desc",
                'columns' => [
                    '*'
                ],
                'pageName' => 'test'
            ])
            ->get();

您不必指定所有参数。如果您省略参数,将使用默认值。上面的 $paginated 将返回 Illuminate\Pagination\LengthAwarePaginator。要获取 conversations,只需调用 $paginated->items()

获取对话中的参与者

$participants = $conversation->getParticipants();

获取模型在对话中的参与条目

Chat::conversation($conversation)->getParticipation($model);

更新参与设置

为参与者设置对话设置(例如:mute_mentions, mute_conversation)

$settings = ['mute_mentions' => true];

Chat::conversation($conversation)
    ->getParticipation($this->alpha)
    ->update(['settings' => $settings]);

数据转换器

需要更多地控制从包路由返回的数据?您可以指定自己的模型转换器并利用 Fractal

您只需在配置文件 musonza_chat.php 中指定转换器的位置如下

/**
 * Model Transformers
 */
'transformers' => [
    'conversation' => \MyApp\Transformers\ConversationTransformer::class,
    'message' => \MyApp\Transformers\MessageTransformer::class,
    'participant' => \MyApp\Transformers\ParticipantTransformer::class,
]

注意: 这仅适用于包路由的响应。

许可证

聊天是开源软件,许可证为 MIT 许可证