From a306a5e694a31278a89526820d1d03331c427a64 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 9 Sep 2016 16:06:19 +0100 Subject: [PATCH 1/2] Fix bug whereby refreshing Vector would not allow querying of membership state This was caused by Vector only sending a room alias with the `view_room` action. We now resolve this to a room ID if we don't have a room ID. --- src/ScalarMessaging.js | 85 +++++++++++++++++++++++++----------------- 1 file changed, 50 insertions(+), 35 deletions(-) diff --git a/src/ScalarMessaging.js b/src/ScalarMessaging.js index 952648010c..c3b5b59e25 100644 --- a/src/ScalarMessaging.js +++ b/src/ScalarMessaging.js @@ -256,6 +256,7 @@ function returnStateEvent(event, roomId, eventType, stateKey) { } var currentRoomId = null; +var currentRoomAlias = null; // Listen for when a room is viewed dis.register(onAction); @@ -264,6 +265,7 @@ function onAction(payload) { return; } currentRoomId = payload.room_id; + currentRoomAlias = payload.room_alias; } const onMessage = function(event) { @@ -287,45 +289,58 @@ const onMessage = function(event) { sendError(event, "Missing room_id in request"); return; } + let promise = Promise.resolve(currentRoomId); if (!currentRoomId) { - sendError(event, "Must be viewing a room"); - return; - } - if (roomId !== currentRoomId) { - sendError(event, "Room " + roomId + " not visible"); - return; + if (!currentRoomAlias) { + sendError(event, "Must be viewing a room"); + } + // no room ID but there is an alias, look it up. + console.log("Looking up alias " + currentRoomAlias); + promise = MatrixClientPeg.get().getRoomIdForAlias(currentRoomAlias).then((res) => { + return res.room_id; + }); } - // Getting join rules does not require userId - if (event.data.action === "join_rules_state") { - getJoinRules(event, roomId); - return; - } + promise.then((viewingRoomId) => { + if (roomId !== viewingRoomId) { + sendError(event, "Room " + roomId + " not visible"); + return; + } - if (!userId) { - sendError(event, "Missing user_id in request"); - return; - } - switch (event.data.action) { - case "membership_state": - getMembershipState(event, roomId, userId); - break; - case "invite": - inviteUser(event, roomId, userId); - break; - case "bot_options": - botOptions(event, roomId, userId); - break; - case "set_bot_options": - setBotOptions(event, roomId, userId); - break; - case "set_bot_power": - setBotPower(event, roomId, userId, event.data.level); - break; - default: - console.warn("Unhandled postMessage event with action '" + event.data.action +"'"); - break; - } + // Getting join rules does not require userId + if (event.data.action === "join_rules_state") { + getJoinRules(event, roomId); + return; + } + + if (!userId) { + sendError(event, "Missing user_id in request"); + return; + } + switch (event.data.action) { + case "membership_state": + getMembershipState(event, roomId, userId); + break; + case "invite": + inviteUser(event, roomId, userId); + break; + case "bot_options": + botOptions(event, roomId, userId); + break; + case "set_bot_options": + setBotOptions(event, roomId, userId); + break; + case "set_bot_power": + setBotPower(event, roomId, userId, event.data.level); + break; + default: + console.warn("Unhandled postMessage event with action '" + event.data.action +"'"); + break; + } + }, (err) => { + console.error(err); + sendError(event, "Failed to lookup current room."); + }) }; module.exports = { From 9c290c4b8d7a85f3c9d7a74816f3afd708de0cb4 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 9 Sep 2016 16:14:41 +0100 Subject: [PATCH 2/2] Return after sending an error --- src/ScalarMessaging.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ScalarMessaging.js b/src/ScalarMessaging.js index c3b5b59e25..bb6b4d02cb 100644 --- a/src/ScalarMessaging.js +++ b/src/ScalarMessaging.js @@ -293,6 +293,7 @@ const onMessage = function(event) { if (!currentRoomId) { if (!currentRoomAlias) { sendError(event, "Must be viewing a room"); + return; } // no room ID but there is an alias, look it up. console.log("Looking up alias " + currentRoomAlias);