The following will search all channels a user is a member of for all messages with a text
field that does not equal " "
.
You may also want to add the $or
operator to the query for attachments, giphys, etc
const searchUserMessages = async (userId) => {
const messages = await chatClient.search(
{
members: { $in: [userId] },
},
{ text: { $ne: " " } }
);
return messages;
};
To search for all messages from a user, just add this to an $and
operator:
const searchUser = async (userId) => {
const messages = await chatClient.search(
{
$and: [ { members: { $in: [userId] } }, { id:userId } ]
},
{ text: { $ne: " " } }
);
return messages;
};
Note that not all Channel Type permissions require membership to send messages - such as Livestream
How to query all the messages, where user is mentioned?
const result = await chatClient.search( { type: 'messaging' }, { 'mentioned_users.id': { $contains: 'vishal' || '', }
}, { limit: 20, offset: 0, }, );
Search in all channels, where user is member
const result = await chatClient.search( { members: { $in: ['vishal' || null], }, }, { 'mentioned_users.id': { $contains: 'vishal' || '',
} }, { limit: 20, offset: 0, }, );
Search a channel, for attachments
const result = await chatClient?.search( { cid: { $in: [channel.cid] }, }, { 'attachments.type': { $in: ['image', 'file'] } }, { limit: 10, offset: offset.current, }, );
- Docs for message search
Comments
0 comments
Please sign in to leave a comment.