dominservice/conversations

This package will allow you to add a full user messaging system into your Laravel application.

2.0.26 2023-10-24 20:47 UTC

README

Latest Version Total Downloads Software License

Conversations

This package will allow you to add a full user messaging system into your Laravel application.

Notice

This package is for Laravel

Version Compatibility
1.* 5.6 --> 9.*
2.* 8.* --> 10.*

IMPORTANT

This package is a continuation of the dominservice/laravel_chat package, due to significant structural changes I decided to create a separate repository. If you have a previous version, you can uninstall it while retaining the database contents, the new package includes a migration moving the data to the new structure. Remember to make a backup before performing this operation!

Installation

composer require dominservice/conversations

Or place manually in composer.json:

"require": {
    "dominservice/conversations": "^1.0"
}

Run:

composer update

Add the service provider to config/app.php

'providers' => [
    Dominservice\Conversations\ConversationsServiceProvider::class,
],

(...)

'aliases' => [
    'Conversations' => Dominservice\Conversations\Facade\Conversations::class,
]

Publish config:

php artisan vendor:publish --provider="Dominservice\Conversations\ConversationsServiceProvider"

Migrate

php artisan migrate

REMEMBER

Configure the package in the config/conversations.php file

Usage

Create New Conversation:

$convs = (new Dominservice\Conversations\Conversations)->create($users, $relationType = null, $relationId = null, $content = null, $getObject = false);

Or short with helper

$convs = conversation_create($users, $relationType = null, $relationId = null, $content = null, $getObject = false);

if $getObject === true you get conversation object "Dominservice\Conversations\Entities\Conversation" with all relations and users, else method return onlu conversation ID

Add message to Conversation if exists or create:

$messageId = (new Dominservice\Conversations\Conversations)->addMessageOrCreateConversation($users, $content, $relationType = null, $relationId = null);

Or short with helper

$messageId = conversation_add_or_create($users, $content, $relationType = null, $relationId = null);

Add message to Conversation:

$messageId = (new Dominservice\Conversations\Conversations)->addMessage($convUuid, $content, $addUser = false);

Or short with helper

$messageId = conversation_add_message($convUuid, $content, $addUser = false);

Get Conversation ID between users:

$conversationId = (new Dominservice\Conversations\Conversations)->getIdBetweenUsers(array $users, $relationType = null, $relationId = null);

Or short with helper

$conversationId = conversation_id_between($users, $relationType = null, $relationId = null);

Check exists user in Conversation:

$existsUser = (new Dominservice\Conversations\Conversations)->existsUser($convUuid, $userId);

Or short with helper

$existsUser = conversation_user_exists($convUuid, $userId = null);

On helper if userId is null, userId = \Auth::user()->id

Get count all unreaded messages:

$count = (new Dominservice\Conversations\Conversations)->getUnreadCount($userId);

Or short with helper

$count = conversation_unread_count($userId = null);

On helper if userId is null, userId = \Auth::user()->id

Get count unreaded messages in specific conversation:

$count = (new Dominservice\Conversations\Conversations)->getConversationUnreadCount($convUuid, $userId);

Or short with helper

$count = conversation_unread_count_per_id($convUuid, $userId = null);

On helper if userId is null, userId = \Auth::user()->id

Delete Conversation:

This method tes status to DELETED for all messages in conversation for selected user. If all messages for all users has status DELETED remove permanently all values for conversation.

(new Dominservice\Conversations\Conversations)->delete($convUuid, $userId);

Or short with helper

conversation_delete($convUuid, $userId = null);

On helper if userId is null, userId = \Auth::user()->id

Get all Conversations for specyfic user:

$conversations = (new Dominservice\Conversations\Conversations)->getConversations($userId, $relationType = null, $relationId = null);

This will return you a "Illuminate\Support\Collection" of "Dominservice\Conversations\Entities\Conversation" objects. And foreach Conversation there, you will have the last message of the conversation, and the users of the conversation. Example:

foreach ( $conversations as $conv ) {
    $getNumOfUsers = $conv->getNumOfUsers();
    $users = $conv->users; /* Collection */
            
    /* $lastMessage Dominservice\Conversations\Entities\Message */
    $lastMessage = $conv->getLastMessage();
            
    $senderId = $lastMessage->sender;
    $content = $lastMessage->content;
    $status = $lastMessage->status;
}

Or short with helper

$conversations = conversations($userId = null, $relationType = null, $relationId = null, $withUsersList = true);

On helper if userId is null, userId = \Auth::user()->id, and helper set array with users adn conversations.

Get messages of conversation:

$messages = (new Dominservice\Conversations\Conversations)->getMessages($convUuid, $userId, $newToOld = true, $limit = null, $start = null);

Or short with helper

$messages = conversation_messages($convUuid, $userId = null, $newToOld = true, $limit = null, $start = null);

On helper if userId is null, userId = \Auth::user()->id

Get unread messages of conversation:

$messages = (new Dominservice\Conversations\Conversations)->getUnreadMessages($convUuid, $userId, $newToOld = true, $limit = null, $start = null);

Or short with helper

$messages = conversation_messages_unread($convUuid, $userId = null, $newToOld = true, $limit = null, $start = null);

On helper if userId is null, userId = \Auth::user()->id

Set status for message:

Mark messages. If userId is null then set current user id.

conversation_mark_as_archived($convUuid, $msgId, $userId = null);
conversation_mark_as_deleted($convUuid, $msgId, $userId = null);
conversation_mark_as_unread($convUuid, $msgId, $userId = null);
conversation_mark_as_read($convUuid, $msgId, $userId = null);

conversation_mark_as_read_all($convUuid, $userId = null);
conversation_mark_as_unread_all($convUuid, $userId = null);

Example

    public function conversations() {
        $currentUser = Auth::user();
        //get the conversations
        $conversations = conversations($currentUser->id);
        //array for storing our users data, as that Conversations only provides user id's
        $users = collect();
        
        //gathering users
        foreach ( $conversations as $conv ) {
            $users->push($conv->users);
        }
        //making sure each user appears once
        $users = $users->unique();
        
        return View::make('conversations_page')
            ->with('users', $users)
            ->with('user', $currentUser)
            ->with('conversations', $conversations);
    }

Credits

tzookb/tbmsg