diff --git a/package.json b/package.json index 73ed088f..4384f053 100644 --- a/package.json +++ b/package.json @@ -62,9 +62,9 @@ "webpack-stream": "^4.0.0" }, "dependencies": { - "async": "^2.1.2", - "axios": "^0.16.2", - "eventemitter2": "^2.2.1", - "pubnub": "^4.20.1" + "async": "2.1.2", + "axios": "0.16.2", + "eventemitter2": "2.2.1", + "pubnub": "4.20.2" } } diff --git a/src/bootstrap.js b/src/bootstrap.js index 71eabcd9..6f0f10f9 100644 --- a/src/bootstrap.js +++ b/src/bootstrap.js @@ -95,7 +95,7 @@ module.exports = (ceConfig = {}, pnConfig = {}) => { let countObject = {}; - ChatEngine.onAny((event, payload) => { + ChatEngine.onAny((event) => { countObject['event: ' + event] = countObject[event] || 0; countObject['event: ' + event] += 1; }); diff --git a/src/components/chat.js b/src/components/chat.js index 08dbc8d7..7760cb0c 100644 --- a/src/components/chat.js +++ b/src/components/chat.js @@ -678,14 +678,7 @@ class Chat extends Emitter { connect() { // establish good will with the server - this.handshake((response) => { - - // asign metadata locally - if (response.data.found) { - this.meta = response.data.chat.meta; - } else { - this.update(this.meta); - } + this.handshake(() => { // now that we've got connection, do everything else via connectionReady this.connectionReady(); @@ -733,9 +726,27 @@ class Chat extends Emitter { }, (next) => { - this.chatEngine.request('get', 'chat', {}, { channel: this.channel }) - .then(callback) - .catch(next); + + if (this.chatEngine.ceConfig.enableMeta) { + + this.chatEngine.request('get', 'chat', {}, { channel: this.channel }) + .then((response) => { + + // asign metadata locally + if (response.data.found) { + this.meta = response.data.chat.meta; + } else { + this.update(this.meta); + } + + callback(); + + }) + .catch(next); + + } else { + callback(); + } } ], (error) => { diff --git a/src/index.js b/src/index.js index 48417dbc..d3da71a0 100644 --- a/src/index.js +++ b/src/index.js @@ -8,6 +8,7 @@ Global object used to create an instance of {@link ChatEngine}. @param ceConfig {Object} A list of ChatEngine specific configuration options. @param [ceConfig.globalChannel=chat-engine] {String} The root channel. See {@link ChatEngine.global} @param [ceConfig.enableSync] {Boolean} Synchronizes chats between instances with the same {@link Me#uuid}. See {@link Me#sync}. +@param [ceConfig.enableMeta] {Boolean} Persists {@link Chat#meta} on the server. See {@link Chat#update}. @param [ceConfig.throwErrors=true] {Boolean} Throws errors in JS console. @param [ceConfig.endpoint='https://pubsub.pubnub.com/v1/blocks/sub-key/YOUR_SUB_KEY/chat-engine-server'] {String} The root URL of the server used to manage permissions for private channels. Set by default to match the PubNub functions deployed to your account. See {@tutorial privacy} for more. @param [ceConfig.debug] {Boolean} Logs all ChatEngine events to the console This should not be enabled in production. @@ -36,6 +37,10 @@ const create = (pnConfig, ceConfig = {}) => { ceConfig.enableSync = false; } + if (typeof ceConfig.enableMeta === 'undefined') { + ceConfig.enableMeta = false; + } + ceConfig.endpoint = ceConfig.endpoint || 'https://pubsub.pubnub.com/v1/blocks/sub-key/' + pnConfig.subscribeKey + '/chat-engine-server'; pnConfig.heartbeatInterval = pnConfig.heartbeatInterval || 120; diff --git a/test/integration/main.test.js b/test/integration/main.test.js index 45f5a9c1..c24e1dd5 100644 --- a/test/integration/main.test.js +++ b/test/integration/main.test.js @@ -154,6 +154,25 @@ function createChatEngineConnect(done) { } +function createChatEngineMeta(done) { + + this.timeout(60000); + + ChatEngine = require('../../src/index.js').create({ + publishKey: pubkey, + subscribeKey: subkey + }, { + globalChannel, + throwErrors: true, + enableMeta: true + }); + ChatEngine.connect(username, { works: true }, username); + ChatEngine.on('$.ready', () => { + done(); + }); + +} + let examplePlugin = () => { class extension { @@ -538,6 +557,28 @@ describe('history', () => { }); }); +describe('meta', () => { + + beforeEach(reset); + beforeEach(createChatEngineMeta); + + it('should update meta', function getMeta(done) { + + this.timeout(60000); + + let meta = { works: true }; + + let chat = new ChatEngine.Chat('chat-tester' + new Date().getTime(), false, true, meta); + + chat.on('$.connected', () => { + assert.equal(chat.meta, meta); + done(); + }); + + }); + +}); + describe('remote chat list', () => { beforeEach(reset); diff --git a/test/unit/components/chat.test.js b/test/unit/components/chat.test.js index 0070e500..14752c91 100644 --- a/test/unit/components/chat.test.js +++ b/test/unit/components/chat.test.js @@ -29,7 +29,8 @@ describe('#chat', () => { }); it('connect chat', (done) => { - chatInstance.on('$.connected', () => { + + chatInstance.once('$.connected', () => { done(); }); @@ -57,7 +58,7 @@ describe('#chat', () => { it('user join to chat', (done) => { - chatInstance.on('$.online.join', () => { + chatInstance.once('$.online.join', () => { done(); });