Skip to main content
Version: Next

Conversation APIs

Namespace#

Myckhel\ChatSystem\Models\Conversation

Columns#

nametypedescription
user_idintuser id
namestring|nullconversation name
typeenum(private, group, issue)type of the conversation

Query Builders#

Methods that build queries.

whereHasLastMessage()#

adds query where conversation has latest message where message is not a system message.

@Params#

  • ?user | pass user arg for query to exclude messages deleted by the user. | int|IChatEventMaker|null
Conversation::whereHasLastMessage($user)->get();

whereNotParticipant()#

Adds query where conversation doesn't have the given user as a participant.

@Params#

  • ?user | pass user arg to query conversation where doesn't have participant equals the user. | int|IChatEventMaker
Conversation::whereNotParticipant($user)->first();

Util Methods#

createMessateWithToken()#

Creates a message with token.

@Return#

  • type Myckhel\ChatSystem\Models\Message

@Params#

  • token | unique token | string|int
  • message | message props | array
$message = $conversation->createMessageWithToken(  $token,  ['message' => 'hello', 'user_id' => $user->id]);

addParticipant()#

Adds a user as participant of the conversaton.

@Return#

  • type Myckhel\ChatSystem\Models\ConversationUser

@Params#

  • user | participant to add | Myckhel\ChatSystem\Contracts\IChatEventMaker
  • message | message text for the activity message that may be created | string
$participant = $conversation->addParticipant($user, message: 'Someone joined the conversation');

removeParticipant()#

Removes a user as participant of the conversaton.

@Return#

  • type null|bool

@Params#

  • user | participant to remove | Myckhel\ChatSystem\Contracts\IChatEventMaker
  • message | message text for the activity message that may be created | string
$participant = $conversation->addParticipant($user, message: 'Someone joined the conversation');

createActivityMessage()#

Creates an activity message.

@Return#

  • type Myckhel\ChatSystem\Models\Message

@Params#

  • message | message props | array
$message = $conversation->createActivityMessage(message: [  'user_id' => $user->id,  'message' => 'Hello']);

makeDelete()#

create a chatEvent of type delete for the conversation through the given user

@Return#

@Emits#

@Params#

  • user | user to assign the event to | user
  • ?row | specify whether to always create a new chat_events db row ortherwise update or create chat_events db row. | bool default to false
  • ?all | specify whether to apply event for all. this should set the chat event column to true|false | bool
$conversation->makeDelete(user: $user, row: false, all: false);

makeRead()#

create a chatEvent of type read for the conversation through the given user

@Return#

@Emits#

@Params#

  • user | user to assign the event to | user
  • ?row | specify whether to always create a new chat_events db row ortherwise update or create chat_events db row. | bool default to true
  • ?all | specify whether to apply event for all. this should set the chat event column to true|false | bool
$conversation->makeRead(user: $user, row: true, all: false);

makeDeliver()#

create a chatEvent of type deliver for the conversation through the given user

@Return#

@Emits#

@Params#

  • user | user to assign the event to | user
  • ?row | specify whether to always create a new chat_events db row ortherwise update or create chat_events db row. | bool default to true
  • ?all | specify whether to apply event for all. this should set the chat event column to true|false | bool
$conversation->makeDeliver(user: $user, row: true, all: false);

makeChatEvent()#

Method to make events for conversation.

  • user | user to assign the event to | user
  • ?row | specify whether to always create a new chat_events db row ortherwise update or create chat_events db row. | bool default to false
  • ?all | specify whether to apply event for all. this should set the chat event column to true|false | bool
$conversation->makeChatEvent(user: $user, type: 'delete', row: false, all: false);

Relationships#

These are methods that defines the relationship between models.

last_message()#

Conversation has one latest message.

$conversation->last_message()->first();

participants()#

Conversation has many conversation user.

$conversation->participants()->get();

participant()#

Conversation has one latest conversation user.

@Params#

  • user | where participant = user | User
$conversation->participant($user)->first();

otherParticipant()#

Conversation has one other latest conversation user.

@Params#

  • user | where participant != user | User
$conversation->otherParticipant($user)->first();

otherParticipants()#

Conversation has many other latest conversation user.

@Params#

  • user | where participants doesn't include user | User
$conversation->otherParticipants($user)->get();

messages()#

Conversation has many messages.

$conversation->messages()->get();

unread()#

Conversation has many unread messages where given user is not the message sender.

@Params#

  • user | user to query unread messages for. | int|IChatEventMaker
$conversation->unread($user)->get();

undelivered()#

Conversation has many undelivered messages where given user is not the message sender.

@Params#

  • user | user to query unread messages for. | int|IChatEventMaker
$conversation->undelivered($user)->get();

doesntHaveChatEvents()#

Conversation has many messages where given user is not the event emitter.

@Params#

  • user | user to query unread messages for. | int|IChatEventMaker
  • type | message event type message should not have. | enum(read|deliver|delete)
$conversation->doesntHaveChatEvents($user, 'read')->get();

author()#

Conversation belongs to a user.

$conversation->author;

Collection methods#

These are methods that could be called on collection of messages.

makeDeliver()#

Method to mark conversations as delivered,

@Params#

  • user | user to assign chat events to. | IChatEventMaker
$messages = $user->messages()->get();
$messages->makeDeliver(user: $user);