Skip to content

Commit 01d3f2f

Browse files
committed
implement /part, /j, and error rather than pass-through unrecognised commands
1 parent ee4da24 commit 01d3f2f

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

‎src/SlashCommands.js‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ var commands = {
9191
var matches = args.match(/^(\S+)$/);
9292
if (matches) {
9393
var room_alias = matches[1];
94+
if (room_alias[0] !== '#') {
95+
return reject("Usage: /join #alias:domain");
96+
}
97+
if (!room_alias.match(/:/)) {
98+
var domain = MatrixClientPeg.get().credentials.userId.replace(/^.*:/, '');
99+
room_alias += ':' + domain;
100+
}
101+
94102
// Try to find a room with this alias
95103
var rooms = MatrixClientPeg.get().getRooms();
96104
var roomId;
@@ -134,6 +142,52 @@ var commands = {
134142
return reject("Usage: /join <room_alias>");
135143
},
136144

145+
part: function(room_id, args) {
146+
var targetRoomId;
147+
if (args) {
148+
var matches = args.match(/^(\S+)$/);
149+
if (matches) {
150+
var room_alias = matches[1];
151+
if (room_alias[0] !== '#') {
152+
return reject("Usage: /part [#alias:domain]");
153+
}
154+
if (!room_alias.match(/:/)) {
155+
var domain = MatrixClientPeg.get().credentials.userId.replace(/^.*:/, '');
156+
room_alias += ':' + domain;
157+
}
158+
159+
// Try to find a room with this alias
160+
var rooms = MatrixClientPeg.get().getRooms();
161+
for (var i = 0; i < rooms.length; i++) {
162+
var aliasEvents = rooms[i].currentState.getStateEvents(
163+
"m.room.aliases"
164+
);
165+
for (var j = 0; j < aliasEvents.length; j++) {
166+
var aliases = aliasEvents[j].getContent().aliases || [];
167+
for (var k = 0; k < aliases.length; k++) {
168+
if (aliases[k] === room_alias) {
169+
targetRoomId = rooms[i].roomId;
170+
break;
171+
}
172+
}
173+
if (targetRoomId) { break; }
174+
}
175+
if (targetRoomId) { break; }
176+
}
177+
}
178+
if (!targetRoomId) {
179+
return reject("Unrecognised room alias: " + room_alias);
180+
}
181+
}
182+
if (!targetRoomId) targetRoomId = room_id;
183+
return success(
184+
MatrixClientPeg.get().leave(targetRoomId).then(
185+
function() {
186+
dis.dispatch({action: 'view_next_room'});
187+
})
188+
);
189+
},
190+
137191
// Kick a user from the room with an optional reason
138192
kick: function(room_id, args) {
139193
if (args) {
@@ -227,6 +281,9 @@ var commands = {
227281
}
228282
};
229283

284+
// helpful aliases
285+
commands.j = commands.join;
286+
230287
module.exports = {
231288
/**
232289
* Process the given text for /commands and perform them.
@@ -244,9 +301,13 @@ module.exports = {
244301
var bits = input.match(/^(\S+?)( +(.*))?$/);
245302
var cmd = bits[1].substring(1).toLowerCase();
246303
var args = bits[3];
304+
if (cmd === "me") return null;
247305
if (commands[cmd]) {
248306
return commands[cmd](roomId, args);
249307
}
308+
else {
309+
return reject("Unrecognised command: " + input);
310+
}
250311
}
251312
return null; // not a command
252313
}

0 commit comments

Comments
 (0)