rkcreative/laravel-cometchat

A CometChat API integration for Laravel

v0.0.5 2021-04-09 00:21 UTC

This package is auto-updated.

Last update: 2024-11-09 09:46:47 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

Laravel package for integrating the CometChat API into your app.

Dependencies

Requirements

You will need to create a free account with CometChat, create an app and create a rest api key. You will need to define your app id, api key and the api url in the config file. The api url can be obtained from the reference docs, ie. (https://api-us.cometchat.io/v2.0)

Install

You can install the package via Composer

$ composer require rkcreative/laravel-cometchat

In Laravel 5.5 or above the service provider will automatically get registered. In older versions of the framework just add the service provider in config/app.php file:

'providers' => [
    ...
    /*
    * Package Service Providers...
    */
    
    RKCreative\LaravelCometChat\LaravelCometChatServiceProvider::class,   
    ...
],

'aliases' => [
    ...
    'CometChat'      => RKCreative\LaravelCometChat\LaravelCometChatFacade::class,
    ...
],

You can publish the config file with:

$ php artisan vendor:publish --provider="RKCreative\LaravelCometChat\LaravelCometChatServiceProvider" --tag=config

When published, the config/cometchat.php config file contains:

<?php

return [
    /*
    |--------------------------------------------------------------------------
    | CometChat API Key
    |--------------------------------------------------------------------------
    |
    | You can generate a rest api key for your app from the CometChat dashboard
    | and enter it here
    |
    */
    'api_key' => env('COMETCHAT_API_KEY', ''),

    /*
    |--------------------------------------------------------------------------
    | CometChat API Url
    |--------------------------------------------------------------------------
    |
    | Add the CometChat API url here
    |
    */
    'api_url'   => env('COMETCHAT_API_URL', 'https://api-us.cometchat.io/v2.0'),

    /*
    |--------------------------------------------------------------------------
    | App ID
    |--------------------------------------------------------------------------
    |
    | The App ID that was generated when you created your app from the dashboard.
    |
    */
    'app_id'    => config('COMETCHAT_APP_ID', ''),
];

Usage

Each action below has required and optional parameters needed to return results. You can read more about CometChat API Options.

Facade

Create a User

CometChat::createUser($uid, $name, $parameters = [])

Accepts:

  • uid (string):required A unique identifier for the user.
  • name (string):required A label or name such as a username for the user.
  • parameters (array):optional An optional array of parameters provided by CometChat.

Returns on success:

  • data (object) A data object
    • uid (string); A unique user id.
    • name (string) A name or label for the user.
    • status (string) The online/offline status of the user.
    • createdAt (datetime) The date of creation of the user.
    • authToken (string) When withAuthToken is included in the request and set to true, an authToken for the user is returned.
    • Any additional parameters that were passed in optionally.

Example:

$user = \CometChat::createUser('12345', 'User1', ['withAuthToken' => true]);
$uid = $user->uid;
$name = $user->name;
$status = $user->status;
$createdAt = $user->createdAt;
$authToken = $user->authToken;

Update a User

CometChat::updateUser($uid, $parameters = [])

Accepts:

  • uid (string):required The uid when the user was added.
  • parameters (array):optional An optional array of parameters provided by CometChat.

Returns on success:

  • data (object) A data object
    • uid (string); A unique user id.
    • name (string) A name or label for the user.
    • status (string) The online/offline status of the user.
    • createdAt (datetime) The date of creation of the user.
    • updatedAt (datetime) The date of update of the user.
    • Any additional parameters that were passed in optionally.

Example:

$user = \CometChat::updateUser('12345', ['name' => 'NewUsername']);
$uid = $user->uid;
$name = $user->name;
$status = $user->status;
$createdAt = $user->createdAt;
$updatedAt = $user->updatedAt;

Delete a User

CometChat::deleteUser($uid)

Accepts:

  • uid (string):required The uid when the user was added.

Returns on success:

  • data (object) A data object
    • message (string); A success message noting the user who was deleted.

Example:

\CometChat::deleteUser('12345');

Deactivate a User

CometChat::deactivateUser($uid)

Accepts:

  • uid (string):required The uid when the user was added.

Returns on success:

  • data (object) A data object
    • message (string); A success message noting the user who was deactivated.

Example:

\CometChat::deactivateUser('12345');

Reactivate a User or Users

CometChat::reactivateUser($uids)

Accepts:

  • uids (array):required An array of user ids to reactivate.

Returns on success:

  • data (object) A data object containing user objects of requested reactivations. Example:
\CometChat::reactivateUser('12345');

Create a User Auth Token

CometChat::createToken($uid, $force === false)

Accepts:

  • uid (string):required The uid when the user was added.
  • force (boolean):optional Generates new auth token forcefully.

Returns on success:

  • data (object) A data object
    • uid (string); The UID of the user.
    • authToken (string) The authToken for the user.
    • createdAt (datetime) The date of creation of the authToken.

Example:

$result = \CometChat::createToken('12345');
$authToken = $result->authToken;

Delete a User Token

CometChat::deleteToken($uid, $authToken)

Accepts:

  • uid (string):required The UID when the user was added.
  • authToken (string):required The authToken for the user that is to be deleted.

Returns on success:

  • data (object) A data object
    • success (boolean); A success status if true/false.
    • message (string); A status message of the request.

Example:

\CometChat::deleteToken('12345', '3lk3kdk333ksliu');

Block Users

CometChat::blockUsers($uid, $blockedUsers = [])

Accepts:

  • uid (string):required The UID of the user who is adding the blocked users.
  • blockedUsers (array):required An array of UID's to be blocked by the user.

Returns on success:

  • A data object with UID objects that were blocked successfully.

Example:

\CometChat::blockUsers('12345', ['12346', '12347', '12348']);

UnBlock Users

CometChat::unblockUsers($uid, $blockedUsers = [])

Accepts:

  • uid (string):required The UID of the user who is removing the blocked users.
  • blockedUsers (array):required An array of UID's to be unblocked by the user.

Returns on success:

  • A data object with UID objects that were unblocked successfully.

Example:

\CometChat::unblockUsers('12345', ['12346', '12347', '12348']);

Add Friends

CometChat::addFriends($uid, $friends = [])

Accepts:

  • uid (string):required The UID of the user who is adding the friends.
  • friends (array):required An array of UID's to be added as friends by the user.

Returns on success:

  • A data object with UID objects that were added successfully.

Example:

\CometChat::addFriends('12345', ['12346', '12347', '12348']);

Remove Friends

CometChat::removeFriends($uid, $friends = [])

Accepts:

  • uid (string):required The UID of the user who is removing the friends.
  • friends (array):required An array of UID's to be removed by the user.

Returns on success:

  • A data object with UID objects that were unblocked successfully.

Example:

\CometChat::removeFriends('12345', ['12346', '12347', '12348']);

Reserved for future actions

Testing

$ composer test

Contributing

Adding code

The current set of actions that this library supports is incomplete. Additional actions will be added as time permits. If you wish to add to this library, please create a pull request.

Security Vulnerabilities

If you discover any security-related issues, please use the issue tracker. All security vulnerabilities will be promptly addressed.

License

The Laravel CometChat package is open-source software licensed under the MIT license.