Skip to main content
Version: v1.0.0-beta.0

Message APIs

Namespace#

Myckhel\ChatSystem\Models\Message

Columns#

nametypedescription
conversation_idintconversation id message belongs to
user_idintuser id message belongs to
reply_idintreply id message belongs to
reply_typestringreply class message belongs to
messagestringmessage text
typeenum(user, system, activity)message text
metasjsonmessage key values

Query Builders#

Message Model Query Builder APIs

whereNotSender()#

adds query to to exclude the given user

@Params#

  • ?user | message sender to exclude. | int|IChatEventMaker|null
Message::whereNotSender($user)->get();

whereReply()#

adds query condition on the given reply_id and or reply_type

@Params#

  • reply | message sender to exclude. | array[reply_id => int, reply_type => string]
Message::whereReply([  'reply_id' => 1,  'reply_type' => Message::class])->get();

whereDoesntHaveChatEvents()#

adds query where message doesn't have chatEvents

@Params#

  • ?type | adds condition where = message chatEvents.type. | string(read|delete|deliver)
  • ?user | adds condition where user = message chatEvents maker | int|IChatEventMaker|null
  • ?conversationScope | callback to get the conversation query object. | null|Closure
Message::whereDoesntHaveChatEvents(  'read',  $user,  fn ($query) => $query->where('created_at', '<', NOW()))->get();

whereNotReadBy()#

adds query where message is not read by the given user

@Params#

  • ?user | adds condition where user = message chatEvents maker | int|IChatEventMaker|null
Message::whereNotReadBy(  $user,)->get();

whereNotDeliveredTo()#

adds query where message is not delivered to the given user

@Params#

  • ?user | adds condition where user = message chatEvents maker | int|IChatEventMaker|null
Message::whereNotDeliveredTo(  $user,)->get();

whereNotDeletedBy()#

adds query where message is not deleted by the given user

@Params#

  • ?user | adds condition where user = message chatEvents maker | int|IChatEventMaker|null
Message::whereNotDeletedBy(  $user,)->get();

whereRelatedTo()#

adds query where message has participant = user

@Params#

  • user | adds condition where user = participant | int|IChatEventMaker|null
Message::whereRelatedTo(  $user,)->get();

hasEvent()#

adds query where message has chatEvents

@Params#

  • eventScope? | callback to get the chatEvents query object. | callable
Message::hasEvents(  fn ($q) => $q->whereType('read'),)->get();

HasNoEvent()#

adds query where message has no chatEvents

@Params#

  • eventScope? | callback to get the chatEvents query object. | callable
Message::HasNoEvent(  fn ($q) => $q->whereType('deliver'),)->get();

whereConversationWasntDeleted()#

query where message's conversation has not been deleted

@Params#

  • by? | adds condition where conversation was not deleted by the given user. | user
Message::whereConversationWasntDeleted(  $user,)->get();

whereConversationWasntDeleted()#

query where message's conversation has not been deleted

@Params#

  • by? | adds condition where conversation was not deleted by the given user. | user
Message::whereConversationWasntDeleted(  $user,)->get();

Util Methods#

participantsHasDeleted()#

check if message has been deleted by all participants of the conversation message belongs to.

@Return#

  • type bool

@Params#

  • ?maker_id | chatEvent maker_id to exclude | int
$message = $user->messages()->first();$message->participantsHasDeleted($user->id); // true|false

makeDelete()#

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

@Return#

@Emits#

@Params#

  • user | user to assign the event to | user
  • ?all | specify whether to apply event for all. this should set the chat event column to true|false | bool
$message = $user->messages()->first();$message->makeDelete($user);

makeRead()#

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

@Return#

@Emits#

@Params#

  • user | user to assign the event to | user
$message = $user->messages()->first();$message->makeRead($user);

makeDeliver()#

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

@Return#

@Emits#

@Params#

  • user | user to assign the event to | user
$message = $user->messages()->first();$message->makeDeliver($user);

participants()#

Query participants of the conversation the message belongs to.

@Return#

  • type ConversationUser Query Builder

@Params#

  • ?user | adds condition where participant = user | int|user

find user from the message's participants

$message = $user->messages()->first();$message->participants($otherUser)->find(); // ConversationUser|null

Relationships#

These are methods that defines the relationship between models.

conversation()#

Conversation message belongs to.

$message = $user->messages()->first();$message->conversation->id;

chatEvents()#

Message has many chat events

$message = $user->messages()->first();$message->chatEvents;

sender()#

Message belongs to user

$message = $user->messages()->first();$message->user;

reply()#

Message belongs to message as reply

$message = $user->messages()->first();$message->reply;

Collection methods#

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

makeRead()#

Method to mark messages as read, pass a user arg to specify the user reading the messages.

$messages = $user->messages()->get();
$messages->makeRead($user);

makeDelete()#

Method to mark messages as deleted, pass a user arg to specify the user deleting the messages. pass a all arg to delete the messages for a participants of the message conversation.

$messages = $user->messages()->get();
$messages->makeDelete(user: $user, all: false);

makeDeliver()#

Method to mark messages as delivered, pass a user arg to specify the user which messages are being delivered to.

$messages = $user->messages()->get();
$messages->makeDeliver(user: $user, all: false);

makeChatEvent()#

Method to make events for messages, pass a user arg to specify the user making the event. pass a type arg to specify the type of the event. pass a all arg to specify the event is for all participant of the conversation message belongs to.

$messages = $user->messages()->get();
$messages->makeChatEvent(user: $user, type: 'delete', all: false);