vns/chatting

简化聊天功能的API

0.3.0 2022-10-18 14:15 UTC

This package is not auto-updated.

Last update: 2024-10-02 00:14:15 UTC


README

Latest Version on Packagist Total Downloads GitHub Actions

一个小型的包,用于执行各种聊天任务,如管理对话、消息、参与者等...

安装

您可以通过composer安装此包

composer require vns/chatting-api

使用方法

首先,您必须发布迁移

php artisan vendor:publish --provider="VnsChattingApi\ChattingApiServiceProvider" --tag="migrations"

发布配置文件

php artisan vendor:publish --provider="VnsChattingApi\ChattingApiServiceProvider" --tag="config"

为参与者模型添加Messageable特性

use VnsChattingApi\Traits\Messageable;

管理对话

// Create new Conversation
ChattingApiFacade::createConversation([$model1,$model2]); // create un-direct conversation
ChattingApiFacade::createDirectConversation([$model1,$model2]); // create direct conversation


// Get conversation by id
ChattingApiFacade::conversations()->getById($conversation_id);


// Get conversation messages
ChattingApiFacade::conversation($conversation)->setParticipant($participant)
            ->setPaginationParams([
                'page' => 1,
                'perPage' => 25,
                'sorting' => "desc",
                'columns' => [
                    '*'
                ],
                'pageName' => 'page'
            ])
            ->page($page)
            ->getMessages();

// OR

$participant->conversations;

// Get Count of unread messages for conversations for specific participant
ChattingApiFacade::conversation($conversation)->setParticipant($participant)->unreadCount();


// Get Conversation between two participant
ChattingApiFacade::conversations()->between( $participantOne,  $participantTwo);


// Clear all message notifications for one participant
ChattingApiFacade::conversations()->setParticipant($participant)->clear();


// Make all Messages as read by specific participant
ChattingApiFacade::conversations()->setParticipant($participant)->readAll();


// Add new participants for conversation
ChattingApiFacade::conversation($conversation)->addParticipants($participants);


// Remove participants from conversation
ChattingApiFacade::conversation($conversation)->removeParticipants($participants);

对话API

管理参与者

// added Messageable Trait for participant model
use VnsChattingApi\Traits\Messageable;


// Get all conversations for participant
$participant->conversations();


// Join Conversation
$participant->joinConversation($conversation);


// leave Conversation
$participant->leaveConversation($conversation);

管理消息

// Create new Message
ChattingApiFacade::message('message content')
        ->from($participant)
        ->to($conversation)
        ->send();

// Or

ChattingApiFacade::message('message content')
    ->from($participant)
    ->to($conversation)
    ->type('text')
    ->send();


// Delete Message For Specific Participant
ChattingApiFacade::messages()->getById($message_id)->trash($participant);