Example on creating conversation in skygear-chat


#1

I use this code to create conversation.

const skygear = require('skygear');
const skygearChat = require('skygear-chat');

skygear.pubsub.authPubsub = false;
skygear.config({
  'endPoint': 'https://rickmak.skygeario.com/',
  'apiKey': 'xxxx',
});

skygear.auth.loginWithUsername('rickmak', '123456').then((user) => {
console.log('log in ok');

const conversationOptions = {
  distinctByParticipants: true
};
const conversationMeta = {};
const query = new skygear.Query(skygear.UserRecord);
query.equalTo('username', 'plugin');

skygear.publicDB.query(query).then((records) => {
const user = records[0];
console.log('Conversation creating!');
skygearChat.createConversation(
  [user, skygear.auth.currentUser],
 "Testing creating conversation",
  conversationMeta,
  conversationOptions
).then((conversation)=>{
  console.log('Conversation created!', conversation);
}).catch((err)=> {
  console.log('Failed to create conversation', err);
});
}, (error) => {
  console.error(error);
});

});

If you want to test it. Copy the above code as index.js and run the following

$ npm install skygear
$ npm install skygeat-chat
$ node index.js

You should get output like following

log in ok
Conversation creating!
Conversation created! RecordCls {
  _recordType: 'conversation',
  _id: '0864fe5a-06d4-4e0e-90b1-5292d7af4d56',
  _access: 
   ACL {
     public: null,
     roles: 
      { 'conversation-admin-0864fe5a-06d4-4e0e-90b1-5292d7af4d56': 'write',
        'conversation-participant-0864fe5a-06d4-4e0e-90b1-5292d7af4d56': 'read' },
     users: {} },
  ownerID: '0b6a551e-dac7-476c-adc2-3f99e26beb0e',
  admin_ids: [ '0b6a551e-dac7-476c-adc2-3f99e26beb0e' ],
  distinct_by_participants: true,
  metadata: {},
  participant_ids: 
   [ 'efc8ff04-2207-4595-9c89-5029925c1679',
     '0b6a551e-dac7-476c-adc2-3f99e26beb0e' ],
  title: null,
  _transient: {} }

If you run it again, it should raise an error, since we set the distinctByParticipants: true.

log in ok
Conversation creating!
Failed to create conversation { status: 500,
  error: 
   { name: 'UnexpectedError',
     code: 10000,
     message: 'Conversation with the participants already exists' } }

#2

doesn’t work. when i try my code it work but it create new conversation

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

#3

@arsene Actually it should create new conversation, but just at the 2nd time due to the distinctByPartcipants option, it will return an error.

I notice that you have at least one error in the following line:

skygearChat.createConversation([user,skygear.auth.currentUser.email], this.nom_docteur,conversationMeta, conversationOptions).then((conversation)=>{

You passed the currentUser’s email instead of User object to the participant list, it should be:

skygearChat.createConversation([user,skygear.auth.currentUser], this.nom_docteur,conversationMeta, conversationOptions).then((conversation)=>{

#4

without the email user i get the error 500


#5

What is the error message? If it said something like below:

Failed to create conversation { status: 500,
  error: 
   { name: 'UnexpectedError',
     code: 10000,
     message: 'Conversation with the participants already exists' } }

It is the correct behaviour with distinctByParticipants: true. You should than query the conversations instead.


#16

@arsene I suggest you might want to write clearly your question (in a new topic) instead of this thread, so we can properly answer your question.