Skip to content

Commit

Permalink
Handle join events, improve Matrix support overall
Browse files Browse the repository at this point in the history
  • Loading branch information
martindale committed Apr 18, 2018
1 parent 3ecc2e6 commit 93dd371
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ package-lock.json

# Optional REPL history
.node_repl_history

# sensitive configuration
config
config.json
16 changes: 14 additions & 2 deletions lib/doorman.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ Doorman.prototype.enable = function enable (name) {
});
});

service.on('join', async function (join) {
self.emit('join', {
user: [name, 'users', join.user].join('/'),
channel: [name, 'channels', join.channel].join('/')
});
});

service.on('message', async function (msg) {
let now = Date.now();
let id = [now, msg.actor, msg.target, msg.object].join('/');
Expand All @@ -115,8 +122,8 @@ Doorman.prototype.enable = function enable (name) {
self.emit('message', {
id: full,
created: now,
actor: msg.actor,
target: [name, msg.target].join('/'),
actor: [name, 'users', msg.actor].join('/'),
target: [name, 'channels', msg.target].join('/'),
object: msg.object,
'@data': msg
});
Expand Down Expand Up @@ -168,6 +175,11 @@ Doorman.prototype.use = function assemble (plugin) {

let handler = new Handler(self.config[name]);

handler.on('message', function (message) {
let parts = message.target.split('/');
self.services[parts[0]].send(parts[2], message.object);
});

self.plugins[name] = handler;
self.plugins[name].trust(self).start();

Expand Down
23 changes: 22 additions & 1 deletion services/matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ Matrix.prototype._getChannels = async function getChannels () {
room.id = room.roomId;
return room;
});

for (let i in result) {
await this._registerChannel(result[i]);
}

return result;
};

Expand All @@ -118,6 +123,11 @@ Matrix.prototype._getUsers = async function getUsers () {
user.id = user.userId;
return user;
});

for (let i in result) {
await this._registerUser(result[i]);
}

return result;
};

Expand All @@ -128,7 +138,10 @@ Matrix.prototype._getPresences = async function getPresences () {
};

Matrix.prototype._getMembers = async function getMembers(id) {
let room = this.connection.getRoom(id);
let room = await this.connection.getRoom(id);
for (let i in room.currentState.members) {
await this._registerUser(room.currentState.members[i]);
}
return Object.keys(room.currentState.members);
};

Expand Down Expand Up @@ -167,6 +180,14 @@ Matrix.prototype._presence_change = function handlePresence (message) {
});
};

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
});
};

Matrix.prototype.error = function errorHandler (error) {
console.error('[MATRIX]', error);
};
Expand Down

0 comments on commit 93dd371

Please sign in to comment.