Only current room works with postMessage

This commit is contained in:
Luke Barnard 2016-09-05 15:13:48 +01:00
parent 54f21c9acc
commit 1c29c95990

View file

@ -123,6 +123,7 @@ Example:
const SdkConfig = require('./SdkConfig'); const SdkConfig = require('./SdkConfig');
const MatrixClientPeg = require("./MatrixClientPeg"); const MatrixClientPeg = require("./MatrixClientPeg");
var dis = require("../../dispatcher");
function sendResponse(event, res) { function sendResponse(event, res) {
const data = JSON.parse(JSON.stringify(event.data)); 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); returnStateEvent(event, roomId, "m.room.bot.options", "_" + userId);
} }
function returnStateEvent(event, roomId, eventType, stateKey) { function returnStateEvent(event, roomId, eventType, stateKey) {
const client = MatrixClientPeg.get(); const client = MatrixClientPeg.get();
if (!client) { if (!client) {
@ -223,6 +223,18 @@ function returnStateEvent(event, roomId, eventType, stateKey) {
sendResponse(event, stateEvent.getContent()); 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) { const onMessage = function(event) {
if (!event.origin) { // stupid chrome if (!event.origin) { // stupid chrome
event.origin = event.originalEvent.origin; event.origin = event.originalEvent.origin;
@ -248,6 +260,15 @@ const onMessage = function(event) {
sendError(event, "Missing room_id in request"); sendError(event, "Missing room_id in request");
return; 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) { switch (event.data.action) {
case "membership_state": case "membership_state":
getMembershipState(event, roomId, userId); getMembershipState(event, roomId, userId);