diff --git a/src/ScalarMessaging.js b/src/ScalarMessaging.js index e58a5ab21b..ee894ac785 100644 --- a/src/ScalarMessaging.js +++ b/src/ScalarMessaging.js @@ -123,6 +123,7 @@ Example: const SdkConfig = require('./SdkConfig'); const MatrixClientPeg = require("./MatrixClientPeg"); +var dis = require("../../dispatcher"); function sendResponse(event, res) { const data = JSON.parse(JSON.stringify(event.data)); @@ -203,7 +204,6 @@ function botOptions(event, roomId, userId) { returnStateEvent(event, roomId, "m.room.bot.options", "_" + userId); } - function returnStateEvent(event, roomId, eventType, stateKey) { const client = MatrixClientPeg.get(); if (!client) { @@ -223,6 +223,18 @@ function returnStateEvent(event, roomId, eventType, stateKey) { sendResponse(event, stateEvent.getContent()); } +var currentRoomId = null; + +// Listen for when a room is viewed +dis.register(onAction); +function onAction(payload) { + switch (payload.action) { + case 'view_room': + currentRoomId = payload.room_id; + break; + } +} + const onMessage = function(event) { if (!event.origin) { // stupid chrome event.origin = event.originalEvent.origin; @@ -248,6 +260,15 @@ const onMessage = function(event) { sendError(event, "Missing room_id in request"); return; } + if (!currentRoomId) { + sendError(event, "Must be viewing a room"); + return; + } + if (roomId !== currentRoomId) { + sendError(event, "Room not in view"); + return; + } + switch (event.data.action) { case "membership_state": getMembershipState(event, roomId, userId);