Find a conversation between user in Skygear Chat


#1

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


#2

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


#3

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


#4

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.


#5
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


#6

@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


#7

time to try it !!! is there the parameter to put?


#8

There are 3 parameters: getConversations(page: number, pageSize: number, includeLastMessage: boolean)

Reference here:

https://docs.skygear.io/js/chat/reference/latest/class/lib/container.js~SkygearChatContainer.html#instance-method-getConversations


#9

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


#10

Hi @arsene, I hope I understand your need correctly (let me know if i am wrong :stuck_out_tongue_winking_eye:):

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);
});

#11

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


#12

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.


#13

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.


#14

thank you for the assistance, I solve the problem. thank you