Skip to content

Commit

Permalink
Feature/remove laravel notifications (#89)
Browse files Browse the repository at this point in the history
* notifications soft delete
  • Loading branch information
musonza authored Jun 2, 2018
1 parent 5d83827 commit 5e04b33
Show file tree
Hide file tree
Showing 36 changed files with 1,623 additions and 1,541 deletions.
126 changes: 90 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Build Status](https://travis-ci.org/musonza/chat.svg?branch=master)](https://travis-ci.org/musonza/chat)
[![Downloads](https://img.shields.io/packagist/dt/musonza/chat.svg)](https://packagist.org/packages/musonza/chat)
[![Packagist](https://img.shields.io/packagist/v/musonza/chat.svg)](https://packagist.org/packages/musonza/chat)
## Chat
## Chat

- [Introduction](#introduction)
- [Installation](#installation)
Expand Down Expand Up @@ -32,8 +32,6 @@

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

> **Note:** If you are using a Laravel version less than 5.4 [install the release on this branch instead](https://github.com/musonza/chat/tree/1.0).
## Installation

From the command line, run:
Expand Down Expand Up @@ -68,9 +66,6 @@ php artisan vendor:publish

This will publish database migrations and a configuration file `musonza_chat.php` in the Laravel config folder.

> **Note:** This package takes advantage of Laravel Notifications.
If you have already setup Laravel notifications you can delete the `2017_07_12_034227_create_notifications_table.php` migration file.

## Configuration

```php
Expand All @@ -80,18 +75,10 @@ If you have already setup Laravel notifications you can delete the `2017_07_12_0
/**
* This will allow you to broadcast an event when a message is sent
* Example:
* Channel: private-mc-chat-conversation.2,
* Event: Musonza\Chat\Messages\MessageWasSent
* Channel: private-mc-chat-conversation.2,
* Event: Musonza\Chat\Messages\MessageWasSent
*/
'broadcasts' => false,

/**
* If set to true, this will use Laravel notifications table to store each
* user message notification.
* Otherwise it will use mc_message_notification table.
* If your database doesn't support JSON columns you will need to set this to false.
*/
'laravel_notifications' => true,
];
```

Expand All @@ -103,20 +90,20 @@ php artisan migrate

## Usage

By default the package assumes you have a User model in the App namespace.
By default the package assumes you have a User model in the App namespace.

However, you can update the user model in `musonza_chat.php` published in the `config` folder.

#### Creating a conversation
```php
$participants = [$userId, $userId2,...];

$conversation = Chat::createConversation($participants);
$conversation = Chat::createConversation($participants);
```

#### Get a conversation by id
```php
$conversation = Chat::conversation($conversation_id);
$conversation = Chat::conversations()->getById($id);
```

#### Update conversation details
Expand All @@ -132,7 +119,7 @@ $conversation->update(['data' => $data]);
$message = Chat::message('Hello')
->from($user)
->to($conversation)
->send();
->send();
```
#### Send a message of custom type

Expand All @@ -143,81 +130,95 @@ $message = Chat::message('http://example.com/img')
->type('image')
->from($user)
->to($conversation)
->send();
->send();
```

### Get a message by id

```php
$message = Chat::messageById($id);
$message = Chat::messages()->getById($id);
```


#### Mark a message as read

```php
Chat::messages($message)->for($user)->markRead();
Chat::message($message)->for($user)->markRead();
```

#### Flag / mark a message

```php
Chat::message($message)->for($user)->toggleFlag();

Chat::message($message)->for($user)->flagged(); // true
```

#### Mark whole conversation as read

```php
Chat::conversations($conversation)->for($user)->readAll();
```
Chat::conversation($conversation)->for($user)->readAll();
```

#### Unread messages count

```php
$unreadCount = Chat::for($user)->unreadCount();
$unreadCount = Chat::messages()->for($user)->unreadCount();
```

#### Unread messages count per Conversation

```php
Chat::conversation($conversation)->for($user)->unreadCount();
```

#### Delete a message

```php
Chat::messages($message)->for($user)->delete();
Chat::message($message)->for($user)->delete();
```

#### Clear a conversation

```php
Chat::conversations($conversation)->for($user)->clear();
Chat::conversation($conversation)->for($user)->clear();
```

#### Get a conversation between two users

```php
Chat::getConversationBetween($user1, $user2);
$conversation = Chat::conversations()->between($user1, $user2);
```

#### Get common conversations among users

```php
$conversations = Chat::commonConversations($users);
$conversations = Chat::conversations()->common($users);
```
`$users` can be an array of user ids ex. `[1,4,6]` or a collection `(\Illuminate\Database\Eloquent\Collection)` of users

#### Remove users from a conversation

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

```php
/* removing multiple users */
Chat::removeParticipants($conversation, [$user1, $user2, $user3,...,$userN]);
Chat::conversation($conversation)->removeParticipants([$user1, $user2, $user3,...,$userN]);
```

#### Add users to a conversation

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

```php
/* add multiple users */
Chat::addParticipants($conversation, [$user3, $user4]);
Chat::conversation($conversation)->addParticipants([$user3, $user4]);
```

<b>Note:</b> A third user will classify the conversation as not private if it was.
Expand All @@ -226,15 +227,68 @@ Chat::addParticipants($conversation, [$user3, $user4]);
#### Get messages in a conversation

```php
Chat::conversations($conversation)->for($user)->getMessages($perPage, $page)
Chat::conversation($conversation)->for($user)->getMessages()
```

#### Get recent messages
#### Get recent messages

```php
$messages = Chat::conversations()->for($user)->limit(25)->page(1)->get();
```

Example

```
[
"id" => 1
"private" => "1"
"data" => []
"created_at" => "2018-06-02 21:35:52"
"updated_at" => "2018-06-02 21:35:52"
"last_message" => array:13 [
"id" => 2
"message_id" => "2"
"conversation_id" => "1"
"user_id" => "1"
"is_seen" => "1"
"is_sender" => "1"
"flagged" => false
"created_at" => "2018-06-02 21:35:52"
"updated_at" => "2018-06-02 21:35:52"
"deleted_at" => null
"body" => "Hello 2"
"type" => "text"
"sender" => array:7 [
"id" => 1
"name" => "Jalyn Ernser"
"email" => "colt.howell@example.com"
]
]
]
```

#### Pagination

There are a few ways you can achieve pagination
You can specify the `limit` and `page` as above using the respective functions or as below:
```
$paginated = Chat::conversations()->for($user)
->setPaginationParams([
'page' => 3,
'perPage' => 10,
'sorting' => "desc",
'columns' => [
'*'
],
'pageName' => 'test'
])
->get();
```
You don't have to specify all the parameters. If you leave the parameters out, default values will be used.
`$paginated` above will return `Illuminate\Pagination\LengthAwarePaginator`
To get the `conversations` simply call `$paginated->items()`


#### Get users in a conversation

```php
Expand Down
20 changes: 12 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "musonza/chat",
"description": "Laravel Chat Package",
"description": "Chat Package for Laravel 5.4 and up",
"type": "library",
"keywords": [
"laravel",
Expand All @@ -9,20 +9,21 @@
"conversation"
],
"require": {
"php": ">=5.6.0",
"illuminate/support": "^5.3",
"illuminate/config": "^5.3"
"php": ">=7.0",
"illuminate/support": "~5.4.0|~5.5.0|~5.6.0",
"illuminate/config": "~5.4.0|~5.5.0|~5.6.0"
},
"require-dev": {
"phpunit/phpunit": "~6.0",
"orchestra/testbench": "3.5.*",
"orchestra/database": "3.5.*"
"phpunit/phpunit": "^5.7|6.2|^7.0",
"orchestra/testbench": "~3.3.0|~3.4.2|^3.5.0",
"orchestra/database": "~3.3.0|~3.4.2|^3.5.0"
},
"license": "MIT",
"authors": [
{
"name": "Tinashe Musonza",
"email": "tinashemusonza@gmail.com"
"email": "tashtin263@gmail.com",
"role": "Developer"
}
],
"minimum-stability": "dev",
Expand All @@ -36,6 +37,9 @@
"Musonza\\Chat\\Tests\\": "tests"
}
},
"scripts": {
"test": "phpunit"
},
"extra": {
"laravel": {
"providers": [
Expand Down
Loading

0 comments on commit 5e04b33

Please sign in to comment.