yeknava/simple-chat 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

yeknava/simple-chat

Composer 安装命令:

composer require yeknava/simple-chat

包简介

simple chat package for laravel

README 文档

README

Laravel Simple Chat Package

Installation

Use the package manager composer to install simple chat package.

composer require yeknava/simple-chat

Usage

Run this command in your terminal:

php artisan vendor:publish

Add CanChat trait to models that may initiate chats (normally User model).

<?php

use Yeknava\SimpleChat\CanChat;

class User extends Model {
    use CanChat;
}

You may also add ChatTopic to models that may be chat topics

<?php

use Yeknava\SimpleChat\ChatTopic;

class Post extends Model {
    use ChatTopic;
}

Then You can create chats with one of these ways


$chat = $user->newDirectChat(
    [
        'title' => 'post discussion',
        'description' => 'a chat about this specific post'
    ],
    $post // or null if this chat has no references
);

// or

$chat = $post->newDirectChat(
    [
        'title' => 'post discussion',
        'description' => 'a chat about this specific post'
    ],
    $user // or null if this chat has no creator (may created by system)
);

In case you don't like using traits or don't access models you may still create chats with SimpleChat class

use Yeknava\SimpleChat\SimpleChat;

$simpleChat = new SimpleChat();

$chat = $simpleChat->setUser($user)->newDirectChat(
    [
        'title' => 'post discussion',
        'description' => 'a chat about this specific post',
        'category' => 'recent'
    ],
    $post
);

There are 3 type of chats:

  • Direct: chat between 2 member
  • Group: chat between several members (you may set limits in config file)
  • Channel: channel is like groups which many members may join but only admins and owner can post messages in that
use Yeknava\SimpleChat\SimpleChat;
use Yeknava\SimpleChat\SimpleChat\DTOs\ChatPermissionDTO;

$simpleChat = new SimpleChat();

$directChat = $simpleChat->setUser($user)->newDirectChat(
    [
        'title' => 'post discussion',
        'description' => 'a chat about this specific post'
    ],
    $post
);

$groupChat = $simpleChat->setUser($user)->newGroupChat(
    [
        'title' => 'post discussion',
        'description' => 'a chat about this specific post',
        'permissions' => (new ChatPermissionDTO())
    ],
    $post
);

$channelChat = $simpleChat->setUser($user)->newChannelChat(
    [
        'title' => 'post discussion',
        'description' => 'a chat about this specific post'
    ],
    $post
);

Add new member to chats:

use Yeknava\SimpleChat\Models\ChatMember;

$member = $chat->addMember($user2, ChatMember::ROLE_MEMBER);

Post messages to chats:

use Yeknava\SimpleChat\Models\ChatMessages;
use Yeknava\SimpleChat\SimpleChat;

$member = (new SimpleChat())->setUser($user)->findChatMember($chat);

$message = $member->message(
    'hi', //body
    ChatMessage::TYPE_MESSAGE, //type
    false, //anonymous (bool)
    null, //reply to (ChatMessage)
    null, //media (MediaDTO)
);

// or 

$message = (new SimpleChat($chat))->setUser($user)->newMessage(
    'hi', //body
    ChatMessage::TYPE_MESSAGE, //type
    false, //anonymous (bool)
    null, //reply to (ChatMessage)
    null, //media (MediaDTO)
);

React to a message:

use Yeknava\SimpleChat\Models\ChatMessages;
use Yeknava\SimpleChat\SimpleChat;

$member = (new SimpleChat())->setUser($user)->findChatMember($chat);

$reaction = $message->react(
    $member,
    1 //reaction type (you can specify types in config)
);

// or 

$message = (new SimpleChat())->react(
    $member,
    1
);

Get message total reaction

use Yeknava\SimpleChat\SimpleChat;

(new SimpleChat())->totalReactions($message);

/** 
 *  get 'total_reactions' and 'total_replies' for list of messages, and
 *  'member_reactions' for a specific member. it is highly recommended to use
 *  this method when you return list of messages instead of getting reactions and
 *  replies for each message.
 */
(new SimpleChat())->messagesTotalReactions(
    [
        $message->id
    ],
    $member->id,
);

Check if user can view a chat or not (depends on chat's type, in groups and direct chats only members may view them and in channels it depends on allow_spectators field):

use Yeknava\SimpleChat\SimpleChat;

(new SimpleChat())->setUser($user)->cantView($chat);

Editing message by user or admin:

use Yeknava\SimpleChat\SimpleChat;

(new SimpleChat($chat))
    ->setUser($user)
    ->edit(
        $message1->id,
        null,
        null,
        ChatMessage::TYPE_MESSAGE,
        false,
        $mediaDTO
    );

Config

<?php

return [
    'use_reaction' => true, // you may disable reaction feature
    /*
     *  you may disable tagging feature.
     *  message tagging automatically works on messages
     *  which have # (hashtag) in their message bodies.
     */
    'use_tag' => true,

    'media_storage_disk' => 'local',
    'media_default_path' => storage_path('app/chat/media'),

    /**
     * there are 3 supported reaction's behaviors:
     *      - infinite: user can react as many as they want
     *          (a user may like a message many times)
     *      - per_type: user can only react once per type
     *          (a user may like a message and favorite same message as well)
     *      - once: user can only react once per message
     *          (a user may only like or favorite a message)
     */
    'reaction_behavior' => 'once', 
    'reaction_types' => [
        1 => 'clap',
        2 => 'like',
        3 => 'favorite',
    ],

    /**
     * if false members can not react to self's messages
     */
    'react_to_self_messages' => false
];

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT

yeknava/simple-chat 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-08-11