Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tmusonza authored and tmusonza committed Mar 18, 2016
0 parents commit c5237f7
Show file tree
Hide file tree
Showing 34 changed files with 1,863 additions and 0 deletions.
9 changes: 9 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

The MIT License (MIT)
Copyright (c) <Tinashe Musonza>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
86 changes: 86 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
## Chat

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

## Installation

Require the package via composer:

`composer require musonza/chat`

Publish the assets:

`php artisan vendor:publish`

This will publish database migrations.

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

`Musonza\Chat\ChatServiceProvider`

You can use the facade for shorter code. Add this 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');`

## Usage

The package assumes you have a User model in the App namespace

#### Creating a conversation
`$conversation = Chat::createConversation([$userId, $userId2,...]); //takes an array of user ids`

#### Send a message

`Chat::send($conversation->id, 'Hello', $userId); //$userId sending a message to created conversation`

#### Mark message as read

`Chat::messageRead($messageId, $userId); //$userId marks the mesage as read`

#### Mark whole conversation as read

`Chat::conversationRead($conversation->id, $userId);`

#### Delete a message

`Chat::trash($messageId, $userId);`

#### Clear a conversation

`Chat::clear($conversation->id, $userId);`

#### Get conversation for two users

`Chat::getConversationBetweenUsers($userId, $userId2);`

#### Remove user(s) from conversation

`Chat::removeParticipants($conversation->id, $usersId); //removing one user`

`Chat::removeParticipants($conversation->id, [$usersId, $userId2]); //removing multiple users`

#### Add user(s) to a conversation

`Chat::addParticipants($conversation->id, $userId3); //add one user`

`Chat::addParticipants($conversation->id, [$userId3, $userId4]); //add multiple users`

#### Get messages in a conversation

`Chat::messages($userId, $conversation->id, $perPage, $page);`

#### Get recent messages

`$mesages = Chat::conversations($userId);`

#### Get users in a conversation

`$users = $conversation->users;`




13 changes: 13 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "musonza/chat",
"description": "Laravel chat package",
"license": "MIT",
"authors": [
{
"name": "Tinashe Musonza",
"email": "[email protected]"
}
],
"minimum-stability": "dev",
"require": {}
}
18 changes: 18 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/integration</directory>
</testsuite>
</testsuites>
</phpunit>
8 changes: 8 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" stopOnError="false" stopOnFailure="false" stopOnIncomplete="false" stopOnSkipped="false" bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/integration</directory>
</testsuite>
</testsuites>
</phpunit>
194 changes: 194 additions & 0 deletions src/Chat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
<?php

namespace Musonza\Chat;

use Musonza\Chat\Commanding\CommandBus;
use Musonza\Chat\Conversations\Conversation;
use Musonza\Chat\Conversations\ConversationUser;
use Musonza\Chat\Messages\Message;
use Musonza\Chat\Messages\SendMessageCommand;
use Musonza\Chat\Notifications\MessageNotification;

class Chat
{
public function __construct(
Conversation $conversation,
Message $message,
CommandBus $commandBus
) {
$this->conversation = $conversation;
$this->message = $message;
$this->commandBus = $commandBus;
}

/**
* Creates a new conversation
*
* @param array $participants
*
* @return Conversation
*/
public function createConversation(array $participants = null)
{
return $this->conversation->start($participants);
}

/**
* Returns a new conversation
*
* @param int $conversationId
*
* @return Conversation
*/
public function conversation($conversationId)
{
return $this->conversation->findOrFail($conversationId);
}

/**
* Add user(s) to a conversation
*
* @param int $conversationId
* @param mixed $userId / array of user ids or an integer
*
* @return Conversation
*/
public function addParticipants($conversationId, $userId)
{
return $this->conversation($conversationId)->addParticipants($userId);
}

/**
* Sends a message
*
* @param int $conversationId
* @param string $body
* @param int $senderId
*
* @return
*/
public function send($conversationId, $body, $senderId)
{
$conversation = $this->conversation->findOrFail($conversationId);

$command = new SendMessageCommand($conversation, $body, $senderId);

$this->commandBus->execute($command);
}

/**
* Remove user(s) from a conversation
*
* @param int $conversationId
* @param mixed $userId / array of user ids or an integer
*
* @return Coonversation
*/
public function removeParticipants($conversationId, $userId)
{
return $this->conversation($conversationId)->removeUsers($userId);
}

/**
* Get recent user messages for each conversation
*
* @param int $userId
*
* @return Message
*/
public function conversations($userId)
{
$c = ConversationUser::join('messages', 'messages.conversation_id', '=', 'conversation_user.conversation_id')
->where('conversation_user.user_id', $userId)
->groupBy('messages.conversation_id')
->orderBy('messages.id', 'DESC')
->get(['messages.*', 'messages.id as message_id', 'conversation_user.*']);

$messages = [];

foreach ($c as $user) {

$recent_message = $user->conversation->messages()->orderBy('id', 'desc')->first()->toArray();

$notification = MessageNotification::where('user_id', $userId)
->where('message_id', $user->id)
->get(['message_notification.id',
'message_notification.is_seen',
'message_notification.is_sender']
);

$messages[] = array_merge(
$recent_message, ['notification' => $notification]
);

}

return $messages;
}

/**
* Get messages in a conversation
*
* @param int $userId
* @param int $conversationId
* @param int $perPage
* @param int $page
*
* @return Message
*/
public function messages($userId, $conversationId, $perPage = null, $page = null)
{
return $this->conversation($conversationId)->getMessages($userId, $perPage, $page);
}

/**
* Deletes message
*
* @param int $messageId
* @param int $userId user id
*
* @return void
*/
public function trash($messageId, $userId)
{
return $this->message->trash($messageId, $userId);
}

/**
* clears conversation
*
* @param int $conversationId
* @param int $userId
*/
public function clear($conversationId, $userId)
{
return $this->conversation->clear($conversationId, $userId);
}

public function messageRead($messageId, $userId)
{
return $this->message->messageRead($messageId, $userId);
}

public function conversationRead($conversationId, $userId)
{
$this->conversation->conversationRead($conversationId, $userId);
}

public function getConversationBetweenUsers($userOne, $userTwo)
{
$conversation1 = $this->conversation->userConversations($userOne)->toArray();

$conversation2 = $this->conversation->userConversations($userTwo)->toArray();

$common_conversations = $this->getConversationsInCommon($conversation1, $conversation2);

return $this->conversation->findOrFail($common_conversations[0]);
}

private function getConversationsInCommon($conversation1, $conversation2)
{
return array_values(array_intersect($conversation1, $conversation2));
}

}
41 changes: 41 additions & 0 deletions src/ChatServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Musonza\Chat;

use Illuminate\Support\ServiceProvider;

class ChatServiceProvider extends ServiceProvider
{

/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;

public function boot()
{
$this->registerAssets();
}

public function register()
{
$this->registerChat();
}

private function registerChat()
{
$this->app->bind('chat', function () {
return $this->app->make('Musonza\Chat\Chat');
});
}

public function registerAssets()
{
$this->publishes([
__DIR__ . '/migrations' => database_path('/migrations'),
]);

}
}
Loading

0 comments on commit c5237f7

Please sign in to comment.