chat
chat copied to clipboard
Get Conversation messages
I am using v4 of the package. Two users. One user was a participant when the conversation was created. Another was added a day later.
However, when the initial user tries to fetch the messages.
$conversation = Chat::conversations()->getById(8);
$conversation_messages = Chat::conversation($conversation)
->setPaginationParams(['sorting' => 'desc'])
->setParticipant(User::find(2)) // User 2 is the author of the conversation
->limit(10)
->page(1)
->getMessages();
This above code does output all messages from this user as well as the other users messages.
But, if we change user to user Id 3. Which was added as participant after few messages were sent using.
$conversation = Chat::conversations()->getById(8);
$conversation_messages = Chat::conversation($conversation)
->setPaginationParams(['sorting' => 'desc'])
->setParticipant(User::find(3)) // User 2 is the author of the conversation
->limit(10)
->page(1)
->getMessages();
This code should return a list of all messages from user 2 as well as user 3. But the above code just returns one message from user 3 and no messages from user 2.
Let me have any more questions.
Regards
Same problem here
Hey all, i found a solution that may helps you. You need to change the file : \musonza\chat\src\Models\Conversation.php in function getConversationMessages.
$messages = $this->messages()
->join(
$this->tablePrefix . 'message_notifications',
$this->tablePrefix.'message_notifications.message_id',
'=', $this->tablePrefix.'messages.id')
->where($this->tablePrefix.'message_notifications.messageable_type', get_class($participant))
->where($this->tablePrefix.'message_notifications.is_sender', 1);
the last line was :
->where($this->tablePrefix.'message_notifications.messageable_id', $participant->getKey());
and that gives you the messages ( readed and unreaded ) of the same user and mark it as a sender.
The idea was one shouldn't be able to see messages that were sent before they were part of the conversation. Much like how whatsapp groups work. Instead of changing the query we should make it more configurable on how the end-user wants this to work
@musonza Totally understand. What if we add a flag. Which defaults to just fetching the current logic. But can be overridden to fetch all messages of the conversation. Basically just to be backward compatible. So that people already using don't create a different issue for it.
This problem was created a year ago, but I think a flag would help a lot. I developed a support tool using this package, and I've been having this problem when viewing messages prior to a user entering the conversation. I can try to add a flag that suits this case.
@gabrielcpolidoro Yes feel free to make a PR and I can take a look. Thanks