在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):musonza/chat开源软件地址(OpenSource Url):https://github.com/musonza/chat开源编程语言(OpenSource Language):PHP 100.0%开源软件介绍(OpenSource Introduction):ChatCreate a Chat application for your multiple Models What to learn how to make a package like this? https://leanpub.com/laravel-package-development Table of ContentsClick to expand
Checkout a simple Demo Application IntroductionThis package allows you to add a chat system to your Laravel ^5.4 application InstallationFrom the command line, run:
Publish the assets:
This will publish database migrations and a configuration file ConfigurationSee Run the migrations:
UsageYou can mix Models as participants. For instance you can have Adding the ability to participate to a ModelAdd the use Illuminate\Database\Eloquent\Model;
use Musonza\Chat\Traits\Messageable;
class Bot extends Model
{
use Messageable;
} Get participant detailsSince we allow Models with data that differ in structure to chat, we may want a uniform way to represent the participant details in a uniform way. You can get the details as follows: $participantModel->getParticipantDetails(); Assuming you have a column public function getParticipantDetailsAttribute()
{
return [
'name' => $this->someValue,
'foo' => 'bar',
];
} Creating a conversationYou can start a conversation by passing an array of Models as participants $participants = [$model1, $model2,..., $modelN];
$conversation = Chat::createConversation($participants); Creating a conversation of type private / publicYou may want to classify conversations as private or public $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);
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($model)
->to($conversation)
->send(); Send a message of custom typeThe default message type is $message = Chat::message('http://example.com/img')
->type('image')
->from($model)
->to($conversation)
->send(); To add more details about a messageSometimes you might want to add details about a message. For example, when the message type is an attachment, and you want to add details such as attachment's filename, and attachment's file url, you can call the $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(); Get a message by id$message = Chat::messages()->getById($id); Get message sender$sendModel = $message->sender; Mark a message as readChat::message($message)->setParticipant($participantModel)->markRead(); Flag / mark a messageChat::message($message)->setParticipant($participantModel)->toggleFlag();
Chat::message($message)->setParticipant($participantModel)->flagged(); // true Mark whole conversation as readChat::conversation($conversation)->setParticipant($participantModel)->readAll(); Unread messages count$unreadCount = Chat::messages()->setParticipant($participantModel)->unreadCount(); Unread messages count per ConversationChat::conversation($conversation)->setParticipant($participantModel)->unreadCount(); Delete a messageChat::message($message)->setParticipant($participantModel)->delete(); Cleanup Deleted MessagesWhat to cleanup when all participants have deleted a Listen for
Clear a conversationChat::conversation($conversation)->setParticipant($participantModel)->clear(); Get participant conversationsChat::conversations()->setPaginationParams(['sorting' => 'desc'])
->setParticipant($participantModel)
->limit(1)
->page(1)
->get(); Get a conversation between two participants$conversation = Chat::conversations()->between($participantModel1, $participantModel2); Get common conversations among participants$conversations = Chat::conversations()->common($participants);
Remove participants from a conversation/* removing one user */
Chat::conversation($conversation)->removeParticipants([$participantModel]); /* removing multiple participants */
Chat::conversation($conversation)->removeParticipants([$participantModel, $participantModel2,...,$participantModelN]); Add participants to a conversation/* add one user */
Chat::conversation($conversation)->addParticipants([$participantModel]); /* add multiple participants */
Chat::conversation($conversation)->addParticipants([$participantModel, $participantModel2]); Get messages in a conversationChat::conversation($conversation)->setParticipant($participantModel)->getMessages() Get user conversations by type// 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(); Get recent messages$messages = Chat::conversations()->setParticipant($participantModel)->limit(25)->page(1)->get(); PaginationThere are a few ways you can achieve pagination
You can specify the
You don't have to specify all the parameters. If you leave the parameters out, default values will be used.
Get participants in a conversation$participants = $conversation->getParticipants(); Get participation entry for a Model in a conversationChat::conversation($conversation)->getParticipation($model); Update participation settingsSet Conversation settings for participant (example: mute_mentions, mute_conversation) $settings = ['mute_mentions' => true];
Chat::conversation($conversation)
->getParticipation($this->alpha)
->update(['settings' => $settings]); Data TransformersNeed to have more control on the data returned from the package routes? You can specify your own Model transformers and take advantage of Fractal. All you need to do is specify the location of your transformers in the configuration
file /**
* Model Transformers
*/
'transformers' => [
'conversation' => \MyApp\Transformers\ConversationTransformer::class,
'message' => \MyApp\Transformers\MessageTransformer::class,
'participant' => \MyApp\Transformers\ParticipantTransformer::class,
]
LicenseChat is open-sourced software licensed under the MIT license |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论