Implement /join

This commit is contained in:
Kegan Dougal 2015-07-17 11:48:40 +01:00
parent 001372ec39
commit 2bb2295499
2 changed files with 45 additions and 11 deletions

View File

@ -61,15 +61,48 @@ var commands = {
var matches = args.match(/^(\S+)$/);
if (matches) {
var room_alias = matches[1];
// TODO:
// Map alias to room ID
// Find the room (if joined, then just view the room)
// else join the room alias
/*
dis.dispatch({
action: 'view_room',
room_id: roomId
}); */
// Try to find a room with this alias
var rooms = MatrixClientPeg.get().getRooms();
var roomId;
for (var i = 0; i < rooms.length; i++) {
var aliasEvents = rooms[i].currentState.getStateEvents(
"m.room.aliases"
);
for (var j = 0; j < aliasEvents.length; j++) {
var aliases = aliasEvents[j].getContent().aliases || [];
for (var k = 0; k < aliases.length; k++) {
if (aliases[k] === room_alias) {
roomId = rooms[i].roomId;
break;
}
}
if (roomId) { break; }
}
if (roomId) { break; }
}
if (roomId) { // we've already joined this room, view it.
dis.dispatch({
action: 'view_room',
room_id: roomId
});
return success();
}
else {
// attempt to join this alias.
return success(
MatrixClientPeg.get().joinRoom(room_alias).done(
function(room) {
dis.dispatch({
action: 'view_room',
room_id: room.roomId
});
}, function(err) {
console.error(
"Failed to join room: %s", JSON.stringify(err)
);
})
);
}
}
}
return reject("Usage: /join <room_alias> [NOT IMPLEMENTED]");

View File

@ -176,18 +176,19 @@ module.exports = {
var cmd = SlashCommands.processInput(this.props.room.roomId, contentText);
if (cmd) {
ev.preventDefault();
if (!cmd.error) {
this.refs.textarea.getDOMNode().value = '';
}
if (cmd.promise) {
cmd.promise.done(function() {
console.log("Command success.");
}, function(err) {
console.error("Command failure: %s", err);
});
this.refs.textarea.getDOMNode().value = '';
}
else if (cmd.error) {
console.error(cmd.error);
}
return;
}