Skip to content
This repository has been archived by the owner on Apr 8, 2020. It is now read-only.

Commit

Permalink
Chat 295 - Chat Meta Feature Flag (#342)
Browse files Browse the repository at this point in the history
* conditionally update and get chat meta

* turn meta sync into feature flag

* global chat type custom

* remove console logs

* don't call non-existant handshake

* attempt to get unit tests passing

* linting

* see if locking versions gets us passing

* short circuit async waterfall

* lint code

* dont need to change global group in this pull
  • Loading branch information
ianjennings authored Mar 31, 2018
1 parent ffb94da commit 5e31d62
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 18 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
2 changes: 1 addition & 1 deletion src/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down
33 changes: 22 additions & 11 deletions src/components/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) => {
Expand Down
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down
41 changes: 41 additions & 0 deletions test/integration/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions test/unit/components/chat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ describe('#chat', () => {
});

it('connect chat', (done) => {
chatInstance.on('$.connected', () => {

chatInstance.once('$.connected', () => {
done();
});

Expand Down Expand Up @@ -57,7 +58,7 @@ describe('#chat', () => {

it('user join to chat', (done) => {

chatInstance.on('$.online.join', () => {
chatInstance.once('$.online.join', () => {
done();
});

Expand Down

0 comments on commit 5e31d62

Please sign in to comment.