Skip to content

Commit

Permalink
Merge e5ee564 into 2a658fc
Browse files Browse the repository at this point in the history
  • Loading branch information
martindale committed Aug 17, 2018
2 parents 2a658fc + e5ee564 commit 973f918
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 13 deletions.
32 changes: 28 additions & 4 deletions lib/doorman.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,30 @@ function Doorman (config) {

util.inherits(Doorman, require('events').EventEmitter);

Doorman.prototype.stop = function stop () {
let self = this;

self.scribe.log('Stopping...');

if (self.config.services && Array.isArray(self.config.services)) {
for (let field in self.services) {
console.log('yo:', field);
}
}

if (self.config.plugins && Array.isArray(self.config.plugins)) {
self.config.plugins.forEach(module => self.close(module));
}

self.scribe.log('Stopped!');

return self;
};

/**
* Activates a Doorman instance.
* @return {Doorman} Chainable method.
*/
Doorman.prototype.start = function configure () {
let self = this;

Expand Down Expand Up @@ -89,7 +113,7 @@ Doorman.prototype.enable = function enable (name) {

service.on('user', function (user) {
self.emit('user', {
id: [name, 'users', user.id].join('/'),
id: [name, 'users', encodeURIComponent(user.id)].join('/'),
name: user.name,
online: user.online || false,
subscriptions: []
Expand All @@ -98,15 +122,15 @@ Doorman.prototype.enable = function enable (name) {

service.on('channel', function (channel) {
self.emit('channel', {
id: [name, 'channels', channel.id].join('/'),
id: [name, 'channels', encodeURIComponent(channel.id)].join('/'),
name: channel.name,
members: []
});
});

service.on('join', async function (join) {
self.emit('join', {
user: [name, 'users', join.user].join('/'),
user: [name, 'users', encodeURIComponent(join.user)].join('/'),
channel: [name, 'channels', join.channel].join('/')
});
});
Expand All @@ -116,7 +140,7 @@ Doorman.prototype.enable = function enable (name) {
let id = [now, msg.actor, msg.target, msg.object].join('/');
let hash = crypto.createHash('sha256').update(id).digest('hex');
let message = {
id: [name, 'messages', (msg.id || hash)].join('/'),
id: [name, 'messages', (encodeURIComponent(msg.id) || hash)].join('/'),
actor: [name, 'users', msg.actor].join('/'),
target: [name, 'channels', msg.target].join('/'),
object: msg.object,
Expand Down
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
"description": "Multi-platform chatbot framework.",
"main": "lib/doorman.js",
"scripts": {
"docs": "npm run make:docs && http-server docs/",
"start": "node doorman.js",
"test": "NODE_ENV=test mocha --recursive",
"coverage": "NODE_ENV=test istanbul cover _mocha -- --recursive",
"make:docs": "jsdoc lib README.md -d docs"
"make:docs": "jsdoc lib README.md -d docs",
"review:coverage": "npm run coverage && http-server coverage/"
},
"repository": {
"type": "git",
Expand All @@ -27,8 +29,8 @@
],
"author": "Fabric Labs <labs@fabric.pub> (https://labs.fabric.pub)",
"contributors": [
"Nate Richardson <hello@naterichardson.com> (https://naterichardson.com)",
"Eric Martindale <eric@ericmartindale.com> (https://www.ericmartindale.com)"
"Eric Martindale <eric@ericmartindale.com> (https://www.ericmartindale.com)",
"Nate Richardson <hello@naterichardson.com> (https://naterichardson.com)"
],
"license": "MIT",
"bugs": {
Expand All @@ -45,6 +47,7 @@
"devDependencies": {
"chai": "^4.1.2",
"coveralls": "^3.0.0",
"http-server": "^0.11.1",
"istanbul": "^1.1.0-alpha.1",
"jsdoc": "^3.5.5",
"mocha": "^4.0.0",
Expand Down
2 changes: 1 addition & 1 deletion services/matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function Matrix (config) {
this.config = config || {};
this.connection = null;
this.map = {};
this.self = { id: config.user };
this.self = { id: this.config.user };
}

util.inherits(Matrix, Service);
Expand Down
8 changes: 3 additions & 5 deletions test/doorman.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const assert = require('assert');
const expect = require('chai').expect;

const Doorman = require('../lib/doorman');
const Service = require('../lib/service');

const EventEmitter = require('events').EventEmitter;

Expand All @@ -13,14 +12,13 @@ describe('Doorman', function () {

it('can handle a message', function (done) {
let doorman = new Doorman();
let source = new EventEmitter();
let sample = { 'test': 'Successfully handled!' };
let plugin = { 'test': 'Successfully handled!' };

doorman.use(sample);
doorman.use(plugin);

doorman.on('response', function (message) {
assert.equal(message.parent.id, 'local/messages/test');
assert.equal(message.response, sample.test);
assert.equal(message.response, plugin.test);
done();
});

Expand Down
87 changes: 87 additions & 0 deletions test/service.unit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const assert = require('assert');
const expect = require('chai').expect;

const Doorman = require('../lib/doorman');
const Service = require('../lib/service');

const EventEmitter = require('events').EventEmitter;

describe('Service', function () {
it('should expose a constructor', function () {
assert(Service instanceof Function);
});

it('can handle a message', function (done) {
let doorman = new Doorman();
let plugin = { 'test': 'Successfully handled!' };
let local = new Service();
let msg = new Buffer([0x01, 0x02]);

doorman.on('response', async function (message) {
assert.equal(message.parent.id, 'local/messages/test');
assert.equal(message.response, plugin.test);
await doorman.stop();
done();
});

doorman.use(plugin).start();

doorman.services.local.emit('message', {
id: 'test',
actor: 'Alice',
target: 'test',
object: 'Hello, world! This is a !test of the message handling flow.'
});
});

describe('_registerUser', function () {
it('emits a "user" event with a routable "id" attribute.', function (done) {
let doorman = new Doorman();

doorman.on('user', async function (message) {
assert.equal(message.id, 'local/users/alice');
await doorman.stop();
done();
}).start();

doorman.services.local._registerUser({
id: 'alice'
});
});
});

describe('_registerChannel', function (done) {
it ('emits a "channel" event with a routable "id" attribute.', function (done) {
let doorman = new Doorman();

doorman.on('channel', async function (message) {
assert.equal(message.id, 'local/channels/test');
await doorman.stop();
done();
}).start();

doorman.services.local._registerChannel({
id: 'test'
});
});
});

describe('_getSubscriptions', function (done) {
xit ('returns an array', function (done) {
let doorman = new Doorman();

doorman.on('channel', async function (message) {
let result = await doorman.services.local._getSubscriptions('test');
console.log('result:', result);
assert(result instanceof Array);
await doorman.stop();
done();
}).start();

doorman.services.local._registerChannel({
id: 'test'
});
});
});

});

0 comments on commit 973f918

Please sign in to comment.