I have a user list, when I click on a user I have the detail of it. and I want every time I click on send message I find our old post if possible
Find a conversation between user in Skygear Chat
Hi, you can use getConversations to get a list of all conversations the current user involved with. And than you can loop through to find the conversations between users
yes, I have already done. but my problem is that every time I click on send message this create a new conversation instead of finding the conversation between the two
This sounds like a coding logic problem in your code base? Maybe you can post the relevant code here so we can help.
Logically, you shouldnāt have any code about ācreate a new conversationā in send message.
const conversationOptions = {
distinctByParticipants: true
};
const conversationMeta = {
sender :skygear.auth.currentUser.name,
received : this.nom_docteur
};
const query = new skygear.Query(skygear.UserRecord);
query.equalTo('email', this.email);
skygear.publicDB.query(query).then((records) => {
const user = records[0];
skygearChat.createConversation([user,skygear.auth.currentUser.email], this.nom_docteur,conversationMeta, conversationOptions).then((conversation)=>{
// a new direct conversation is created
this.navCtrl.push("ChatDetailPage", conversation);
console.log('Conversation created!', conversation);
}).catch((err)=> {
console.log('Failed to create conversation');
});
}, (error) => {
console.error(error);
});
my concern is that a times I click on send message this finds the subsequent messages between the two users
@arsene from the code you sent, it didnāt try to use getConversations()
to find existing conversations, so it will create new conversation every time the code is run.
Take a look at Fetching existing conversations
There are 3 parameters: getConversations(page: number, pageSize: number, includeLastMessage: boolean)
Reference here:
i use this code yet
getConversation(){
let loading = this.loadingCtrl.create({
content:āChargementā¦ā,
showBackdrop:false
});
loading.present();
/* GET USER CONVERSATION */
skygearChat.getConversations().then((data)=> {
this.conversation = data;
this.storage.set(āconversationā, this.conversation);
console.log(āAll conversationsā, data);
loading.dismiss();
}, function (err) {
console.log(āFailed to retrieve conversationsā);
});
}
my problem is to create when i click on send message in user profil
Hi @arsene, I hope I understand your need correctly (let me know if i am wrong ):
You want to make a send button so that when you click on it, you will see all the previous messages you sent to/received from the user.
If this is right, you need to do two things in the send button:
1. Create a conversation or get an existing conversation
In this step, you either use createConversation
(with distinctByParticipant=true
) or getConversation
. The createConversation
function is more convenient because if the conversation already exists, Skygear Chat will return an existing conversation to you, instead of creating a new one
2. Fetch messages in a conversation
Now that you have a conversation, you need to put that conversation to getMessages to fetch previous messages.
Example:
Here is an example, I hope it makes sense.
skygearChat.createConversation(
[user,skygear.auth.currentUser.email],
this.nom_docteur,
conversationMeta,
conversationOptions
).then((conversation)=>{
console.log('Conversation created!', conversation);
return skygearChat.getMessages(conversation, 10, new Date());
}).catch((err)=> {
console.log('Failed to create conversation');
}).then((messages) => {
console.log('previous messages', messages);
});
thank you for the answer @cheungpat , but it is the same thing as my previous code. but my concern is that if there is a conversation that exists we are referred to that
Sorry I misunderstood your question. If you want a new conversation (ignoring existing conversation), it is simple: donāt use distinctByParticipants=true
Your code will look something like:
const conversationOptions = {
distinctByParticipants: false // or omit this altogether
};
// ...
skygearChat.createConversation([user,skygear.auth.currentUser.email], this.nom_docteur,conversationMeta, conversationOptions)
You should use createConversation
without distinctByParticipant=true
. In this case a new conversation is always created, no matter the user you are talking to have a conversation with you already. In other words, ignore existing conversation and always create anew.
In this case, the user will not see messages from a previous conversation, because the conversation is a new one.
without lying to you, I do not find myself. Iām waiting if there will be a follow up to this request https://github.com/SkygearIO/features/issues/118.