Skip to content

Commit 2bb2295

Browse files
committed
Implement /join
1 parent 001372e commit 2bb2295

2 files changed

Lines changed: 45 additions & 11 deletions

File tree

‎src/SlashCommands.js‎

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,48 @@ var commands = {
6161
var matches = args.match(/^(\S+)$/);
6262
if (matches) {
6363
var room_alias = matches[1];
64-
// TODO:
65-
// Map alias to room ID
66-
// Find the room (if joined, then just view the room)
67-
// else join the room alias
68-
/*
69-
dis.dispatch({
70-
action: 'view_room',
71-
room_id: roomId
72-
}); */
64+
// Try to find a room with this alias
65+
var rooms = MatrixClientPeg.get().getRooms();
66+
var roomId;
67+
for (var i = 0; i < rooms.length; i++) {
68+
var aliasEvents = rooms[i].currentState.getStateEvents(
69+
"m.room.aliases"
70+
);
71+
for (var j = 0; j < aliasEvents.length; j++) {
72+
var aliases = aliasEvents[j].getContent().aliases || [];
73+
for (var k = 0; k < aliases.length; k++) {
74+
if (aliases[k] === room_alias) {
75+
roomId = rooms[i].roomId;
76+
break;
77+
}
78+
}
79+
if (roomId) { break; }
80+
}
81+
if (roomId) { break; }
82+
}
83+
if (roomId) { // we've already joined this room, view it.
84+
dis.dispatch({
85+
action: 'view_room',
86+
room_id: roomId
87+
});
88+
return success();
89+
}
90+
else {
91+
// attempt to join this alias.
92+
return success(
93+
MatrixClientPeg.get().joinRoom(room_alias).done(
94+
function(room) {
95+
dis.dispatch({
96+
action: 'view_room',
97+
room_id: room.roomId
98+
});
99+
}, function(err) {
100+
console.error(
101+
"Failed to join room: %s", JSON.stringify(err)
102+
);
103+
})
104+
);
105+
}
73106
}
74107
}
75108
return reject("Usage: /join <room_alias> [NOT IMPLEMENTED]");

‎src/controllers/molecules/MessageComposer.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,18 +176,19 @@ module.exports = {
176176
var cmd = SlashCommands.processInput(this.props.room.roomId, contentText);
177177
if (cmd) {
178178
ev.preventDefault();
179+
if (!cmd.error) {
180+
this.refs.textarea.getDOMNode().value = '';
181+
}
179182
if (cmd.promise) {
180183
cmd.promise.done(function() {
181184
console.log("Command success.");
182185
}, function(err) {
183186
console.error("Command failure: %s", err);
184187
});
185-
this.refs.textarea.getDOMNode().value = '';
186188
}
187189
else if (cmd.error) {
188190
console.error(cmd.error);
189191
}
190-
191192
return;
192193
}
193194

0 commit comments

Comments
 (0)