定制 gicminos/chat 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

gicminos/chat

Composer 安装命令:

composer require gicminos/chat

包简介

Fork of musonza/chat to mantain version 3 in future laravel releases

README 文档

README

chat

Build Status Downloads Packagist

Chat

Demo Application

Want to use any Laravel model as Chat participant? Follow this PR musonza#163

Introduction

This package allows you to add a chat system to your Laravel ^5.4 application

Installation

From the command line, run:

composer require musonza/chat

Add the service provider to your config\app.php the providers array

Musonza\Chat\ChatServiceProvider::class

Add the Facade to your aliases:

'Chat' => Musonza\Chat\Facades\ChatFacade::class to your `config\app.php`

The class is bound to the ioC as chat

$chat = App::make('chat');

Publish the assets:

php artisan vendor:publish

This will publish database migrations and a configuration file musonza_chat.php in the Laravel config folder.

Configuration

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,
];

Run the migrations:

php artisan migrate

Usage

By default the package assumes you have a User model in the App namespace.

However, you can update the user model in musonza_chat.php published in the config folder.

Creating a conversation

$participants = [$userId, $userId2,...];

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

Creating a conversation of type private / public

$participants = [$userId, $userId2,...];

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

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

Get a conversation by id

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

Update conversation details

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

Send a text message

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

Send a message of custom type

The default message type is text. If you want to specify custom type you can call the type() function as below:

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

Get a message by id

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

Mark a message as read

Chat::message($message)->setUser($user)->markRead();

Flag / mark a message

Chat::message($message)->setUser($user)->toggleFlag();

Chat::message($message)->setUser($user)->flagged(); // true

Mark whole conversation as read

Chat::conversation($conversation)->setUser($user)->readAll();

Unread messages count

$unreadCount = Chat::messages()->setUser($user)->unreadCount();

Unread messages count per Conversation

Chat::conversation($conversation)->setUser($user)->unreadCount();

Delete a message

Chat::message($message)->setUser($user)->delete();

Clear a conversation

Chat::conversation($conversation)->setUser($user)->clear();

Get a conversation between two users

$conversation = Chat::conversations()->between($user1, $user2);

Get common conversations among users

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

$users can be an array of user ids ex. [1,4,6] or a collection (\Illuminate\Database\Eloquent\Collection) of users

Remove users from a conversation

/* removing one user */
Chat::conversation($conversation)->removeParticipants($user);
/* removing multiple users */
Chat::conversation($conversation)->removeParticipants([$user1, $user2, $user3,...,$userN]);

Add users to a conversation

/* add one user */
Chat::conversation($conversation)->addParticipants($user);
/* add multiple users */
Chat::conversation($conversation)->addParticipants([$user3, $user4]);

Note: By default, a third user will classify the conversation as not private if it was. See config on how to change this.

Get messages in a conversation

Chat::conversation($conversation)->setUser($user)->getMessages()

Get user conversations by type

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

Get recent messages

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

Example

[
      "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"
        ]
      ]
    ]

Pagination

There are a few ways you can achieve pagination You can specify the limit and page as above using the respective functions or as below:

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

You don't have to specify all the parameters. If you leave the parameters out, default values will be used. $paginated above will return Illuminate\Pagination\LengthAwarePaginator To get the conversations simply call $paginated->items()

Get users in a conversation

$users = $conversation->users;

License

Chat is open-sourced software licensed under the MIT license

gicminos/chat 适用场景与选型建议

gicminos/chat 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.68k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 06 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「messaging」 「chat」 「laravel」 「conversation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 gicminos/chat 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 gicminos/chat 我们能提供哪些服务?
定制开发 / 二次开发

基于 gicminos/chat 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.68k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 17
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 312
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-06-03