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' } }