Skip to content

Commit

Permalink
Improve Matrix support, standardize some service methods
Browse files Browse the repository at this point in the history
  • Loading branch information
martindale committed Apr 18, 2018
1 parent a30c4ab commit 3ecc2e6
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 8 deletions.
5 changes: 4 additions & 1 deletion lib/doorman.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Doorman.prototype.start = function configure () {

self.register({
name: 'help',
value: `Available ${self.config.trigger}triggers: ${Object.keys(self.triggers).map(x => '`' + x + '`').join(', ')}`
value: `Available triggers: ${Object.keys(self.triggers).map(x => '`' + self.config.trigger + x + '`').join(', ')}`
});

if (self.config.debug) {
Expand Down Expand Up @@ -91,6 +91,8 @@ Doorman.prototype.enable = function enable (name) {
self.emit('user', {
id: [name, 'users', user.id].join('/'),
name: user.name,
online: user.online || false,
subscriptions: [],
'@data': user
});
});
Expand All @@ -99,6 +101,7 @@ Doorman.prototype.enable = function enable (name) {
self.emit('channel', {
id: [name, 'channels', channel.id].join('/'),
name: channel.name,
members: [],
'@data': channel
});
});
Expand Down
33 changes: 33 additions & 0 deletions lib/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,37 @@ Service.prototype.send = function send (channel, message, extra) {
return this;
};

Service.prototype._registerUser = function registerUser (user) {
if (!user.id) return console.error('User must have an id.');
let id = `/users/${user.id}`;
this.map[id] = Object.assign({
subscriptions: []
}, this.map[id], user);
this.emit('user', this.map[id]);
};

Service.prototype._registerChannel = function registerChannel (channel) {
if (!channel.id) return console.error('Channel must have an id.');
let id = `/channels/${channel.id}`;
this.map[id] = Object.assign({
members: []
}, this.map[id], channel);
this.emit('channel', this.map[id]);
};

Service.prototype._getSubscriptions = async function getSubscriptions (id) {
let member = this.map[`/users/${id}`] || {};
return member.subscriptions || null;
};

Service.prototype._getMembers = async function getMembers (id) {
let channel = this.map[`/channels/${id}`] || {};
return channel.members || null;
};

Service.prototype._getPresence = async function getPresence (id) {
let member = this.map[`/users/${id}`] || {};
return member.presence || null;
};

module.exports = Service;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
"homepage": "https://github.com/FabricLabs/doorman#readme",
"dependencies": {
"@slack/client": "^4.1.0",
"discord.js": "^11.1.0",
"erm": "0.0.1",
"erm": "^0.0.1",
"marked": "^0.3.19",
"matrix-js-sdk": "^0.10.1"
},
"devDependencies": {
Expand Down
25 changes: 20 additions & 5 deletions services/matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const util = require('util');
const matrix = require('matrix-js-sdk');
const markdown = require('marked');
const Service = require('../lib/service');

function Matrix (config) {
Expand Down Expand Up @@ -38,7 +39,7 @@ Matrix.prototype.ready = async function () {

this.connection.on('event', this.handler.bind(this));
this.connection.on('Room.timeline', function (event, room) {
console.log('shenanigans!', event, room);
//- console.log('shenanigans!', event, room);
});
this.connection.on('RoomMember.membership', function (event, member) {
if (member.membership === 'invite' && member.userId === self.config.user) {
Expand Down Expand Up @@ -99,10 +100,9 @@ Matrix.prototype.handler = function route (message) {
};

Matrix.prototype.send = function send (channel, message) {
this.connection.sendMessage(channel, {
msgtype: 'text',
body: message
});
let html = markdown(message);
// TODO: complain to `matrix-js-sdk` about duplicate params
this.connection.sendHtmlMessage(channel, html, html);
};

Matrix.prototype._getChannels = async function getChannels () {
Expand All @@ -127,13 +127,28 @@ Matrix.prototype._getPresences = async function getPresences () {
return result;
};

Matrix.prototype._getMembers = async function getMembers(id) {
let room = this.connection.getRoom(id);
return Object.keys(room.currentState.members);
};

Matrix.prototype._registerChannel = function registerChannel (channel) {
if (!channel.id) return console.error('Channel must have an id.');
let id = `/channels/${channel.id}`;
this.map[id] = Object.assign({}, this.map[id], channel);
this.emit('channel', this.map[id]);
};

Matrix.prototype._registerUser = function registerUser (user) {
if (!user.id) return console.error('User must have an id.');
let id = `/users/${user.id}`;
this.map[id] = Object.assign({
online: user.currentlyActive || false,
name: user.displayName || user.id
}, this.map[id], user);
this.emit('user', this.map[id]);
};

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

Expand Down

0 comments on commit 3ecc2e6

Please sign in to comment.