Skip to content

Commit

Permalink
Merge pull request #4 from FabricLabs/simple-start
Browse files Browse the repository at this point in the history
update
  • Loading branch information
naterchrdsn authored Apr 25, 2018
2 parents 2a0b572 + 71b2ff9 commit 88bc8fb
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 56 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ List of external plugins for you to include with your installation (if you wish)
- [mathfact](https://github.com/FabricLabs/doorman-mathfact) => spits out a random math fact
- [yearfact](https://github.com/FabricLabs/doorman-yearfact) => spits out a random year fact
- [datefact](https://github.com/FabricLabs/doorman-datefact) => spits out a random date fact
- [remaeusfact](https://github.com/FabricLabs/doorman-remaeusfact) => spits out a random fact about [Remaeus](https://github.com/martindale)
- [remaeusfact](https://github.com/FabricLabs/doorman-remaeusfact) => spits out a random fact about [Remaeus](https://www.roleplaygateway.com/member/Rem%C3%A6us/)

### Simple Plugins
#### Ping-Pong Example Plugin, `./plugins/ping.json`
Expand Down
46 changes: 26 additions & 20 deletions lib/doorman.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ util.inherits(Doorman, require('events').EventEmitter);
Doorman.prototype.start = function configure () {
let self = this;

self.enable('local');

if (self.config.services && Array.isArray(self.config.services)) {
self.config.services.forEach(service => self.enable(service));
}

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

if (self.config.triggers) {
Object.keys(self.config.triggers).forEach(name => {
let route = {
Expand All @@ -47,16 +57,6 @@ Doorman.prototype.start = function configure () {
});
}

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

self.enable('local');

if (self.config.services && Array.isArray(self.config.services)) {
self.config.services.forEach(service => self.enable(service));
}

self.register({
name: 'help',
value: `Available triggers: ${Object.keys(self.triggers).map(x => '`' + self.config.trigger + x + '`').join(', ')}`
Expand Down Expand Up @@ -117,30 +117,34 @@ Doorman.prototype.enable = function enable (name) {
let now = Date.now();
let id = [now, msg.actor, msg.target, msg.object].join('/');
let hash = crypto.createHash('sha256').update(id).digest('hex');
let full = [name, 'messages', (msg.id || hash)].join('/');

self.emit('message', {
id: full,
created: now,
let message = {
id: [name, 'messages', (msg.id || hash)].join('/'),
actor: [name, 'users', msg.actor].join('/'),
target: [name, 'channels', msg.target].join('/'),
object: msg.object,
created: now,
'@data': msg
});
};

let response = await self.parse(msg.object);
self.emit('message', message);

let response = await self.parse(message);
if (response) {
self.emit('response', {
parent: full,
parent: message,
response: response
});

service.send(msg.target, response, {
parent: full
parent: message
});
}
});

service.on('ready', function () {
self.emit('service', { name });
});

this.services[name] = service;
this.services[name].connect();

Expand Down Expand Up @@ -185,7 +189,9 @@ Doorman.prototype.use = function assemble (plugin) {

if (self.plugins[name].triggers) {
Object.keys(self.plugins[name].triggers).forEach(trigger => {
self.register(self.plugins[name].triggers[trigger]);
self.register(Object.assign({
plugin: name
}, self.plugins[name].triggers[trigger]));
});
}
} else {
Expand Down
7 changes: 4 additions & 3 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ function Router (config) {
* @return {Array} List of outputs generated from the input string.
*/
Router.prototype.route = async function handle (msg) {
if (typeof msg !== 'string') return;
if (!msg.actor || !msg.object || !msg.target) return null;
if (typeof msg.object !== 'string') return null;
let output = [];
let parts = msg
let parts = msg.object
.split(/\s+/g)
.filter(x => x.charAt(0) === this.config.trigger)
.map(x => x.substr(1));
Expand All @@ -35,7 +36,7 @@ Router.prototype.route = async function handle (msg) {
result = handler.value;
break;
default:
result = await handler.value.apply(this.fabric.plugins[command], [msg]);
result = await handler.value.apply(this.fabric.plugins[handler.plugin], [msg]);
break;
}

Expand Down
2 changes: 2 additions & 0 deletions lib/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Service.prototype.connect = function initialize () {
callback(null, chunk);
}
});

this.ready();
};

Service.prototype.ready = function ready () {
Expand Down
57 changes: 26 additions & 31 deletions services/matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,6 @@ Matrix.prototype.connect = function initialize () {

Matrix.prototype.ready = async function () {
let self = this;

console.log('[MATRIX]', 'ready!');

this.connection.on('event', this.handler.bind(this));
this.connection.on('Room.timeline', function (event, room) {
//- console.log('shenanigans!', event, room);
});
this.connection.on('RoomMember.membership', function (event, member) {
if (member.membership === 'invite' && member.userId === self.config.user) {
self.connection.joinRoom(member.roomId);
}
});

let users = await self._getUsers();
let channels = await self._getChannels();
let presences = await self._getPresences();
Expand All @@ -59,12 +46,19 @@ Matrix.prototype.ready = async function () {
self._registerChannel(channels[id]);
}

console.log('[MATRIX]', 'ready!');

this.connection.on('event', this.handler.bind(this));
this.connection.on('RoomMember.membership', function (event, member, old) {
if (member.membership === 'invite' && member.userId === self.config.user) {
self.connection.joinRoom(member.roomId);
}
});

self.emit('ready');
};

Matrix.prototype.sync = function handleSync (state, previous, data) {
console.log('[MATRIX]', 'sync received:', state, previous, data);

switch (state) {
case 'PREPARED':
this.ready();
Expand Down Expand Up @@ -102,7 +96,7 @@ Matrix.prototype.handler = function route (message) {
Matrix.prototype.send = function send (channel, message) {
let html = markdown(message);
// TODO: complain to `matrix-js-sdk` about duplicate params
this.connection.sendHtmlMessage(channel, html, html);
this.connection.sendHtmlMessage(channel, message, html);
};

Matrix.prototype._getChannels = async function getChannels () {
Expand Down Expand Up @@ -131,16 +125,28 @@ Matrix.prototype._getUsers = async function getUsers () {
return result;
};

Matrix.prototype._getUser = async function getUser (id) {
let result = await this.connection.getUser(id);
let user = Object.assign({
id: result.userId,
name: result.displayName
}, result);
return user;
};

Matrix.prototype._getPresences = async function getPresences () {
let result = await this.connection.getPresenceList();
console.log('getPresences() got:', result);
return result;
};

Matrix.prototype._getMembers = async function getMembers(id) {
Matrix.prototype._getMembers = async function getMembers (id) {
let room = await this.connection.getRoom(id);
for (let i in room.currentState.members) {
await this._registerUser(room.currentState.members[i]);
let member = Object.assign({
id: i
}, room.currentState.members[i]);
await this._registerUser(member);
}
return Object.keys(room.currentState.members);
};
Expand All @@ -163,28 +169,17 @@ Matrix.prototype._registerUser = function registerUser (user) {
};

Matrix.prototype._presence_change = function handlePresence (message) {
console.log('presence change:', message);

let id = `/users/${message.event.sender}`;

if (!this.map[id]) this._registerUser({ id: message.event.sender });

this.map[id].online = (message.event.content.presence === 'online');
this.map[id].presence = message.event.content.presence;

// TODO: generate this from Fabric
this.emit('patch', {
op: 'replace',
path: [id, 'online'].join('/'),
value: this.map[id].online
});
};

Matrix.prototype._member_joined_channel = function handleJoin (message) {
if (message.event.content.membership !== 'join') return;
this.emit('join', {
user: message.sender.userId,
channel: message.target.roomId
user: message.event.sender,
channel: message.event.room_id
});
};

Expand Down
2 changes: 1 addition & 1 deletion test/doorman.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('Doorman', function () {
doorman.use(sample);

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

0 comments on commit 88bc8fb

Please sign in to comment.