Conversation APIs
#
NamespaceMyckhel\ChatSystem\Models\Conversation
#
Columnsname | type | description |
---|---|---|
user_id | int | user id |
name | string|null | conversation name |
type | enum(private, group, issue) | type of the conversation |
#
Query BuildersMethods 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 MethodscreateMessateWithToken()
#
Creates a message with token.
#
@Return- type
Myckhel\ChatSystem\Models\Message
#
@Paramstoken
| 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
#
@Paramsuser
| 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
#
@Paramsuser
| 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
#
@Paramsmessage
| message props |array
$message = $conversation->createActivityMessage(message: [ 'user_id' => $user->id, 'message' => 'Hello']);
makeDelete()
#
create a chatEvent of type
delete
for theconversation
through the givenuser
#
@Return- type ChatEvent Model
#
@Emits- type Message Events
#
@Paramsuser
| 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 tofalse
?all
| specify whether to apply event for all. this should set the chat event column totrue|false
|bool
$conversation->makeDelete(user: $user, row: false, all: false);
makeRead()
#
create a chatEvent of type
read
for theconversation
through the givenuser
#
@Return- type ChatEvent Model
#
@Emits- type Message Events
#
@Paramsuser
| 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 totrue
?all
| specify whether to apply event for all. this should set the chat event column totrue|false
|bool
$conversation->makeRead(user: $user, row: true, all: false);
makeDeliver()
#
create a chatEvent of type
deliver
for theconversation
through the givenuser
#
@Return- type ChatEvent Model
#
@Emits- type Message Events
#
@Paramsuser
| 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 totrue
?all
| specify whether to apply event for all. this should set the chat event column totrue|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 tofalse
?all
| specify whether to apply event for all. this should set the chat event column totrue|false
|bool
$conversation->makeChatEvent(user: $user, type: 'delete', row: false, all: false);
#
RelationshipsThese 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.
#
@Paramsuser
| where participant = user |User
$conversation->participant($user)->first();
otherParticipant()
#
Conversation has one other latest conversation user.
#
@Paramsuser
| where participant != user |User
$conversation->otherParticipant($user)->first();
otherParticipants()
#
Conversation has many other latest conversation user.
#
@Paramsuser
| 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.
#
@Paramsuser
| 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.
#
@Paramsuser
| 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.
#
@Paramsuser
| 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 methodsThese are methods that could be called on collection of messages.
makeDeliver()
#
Method to mark conversations as delivered,
#
@Paramsuser
| user to assign chat events to. |IChatEventMaker
$messages = $user->messages()->get();
$messages->makeDeliver(user: $user);