Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Scalar Messaging: Expose join_rules and restrict to currently viewed room #443

Merged
merged 11 commits into from
Sep 9, 2016
31 changes: 30 additions & 1 deletion src/ScalarMessaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -193,12 +194,16 @@ function getMembershipState(event, roomId, userId) {
returnStateEvent(event, roomId, "m.room.member", userId);
}

function getJoinRules(event, roomId) {
console.log(`join_rules of ${roomId} requested.`);
returnStateEvent(event, roomId, "m.room.join_rules");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a state_key of "":

returnStateEvent(event, roomId, "m.room.join_rules", "");

}

function botOptions(event, roomId, userId) {
console.log(`bot_options of ${userId} in room ${roomId} requested.`);
returnStateEvent(event, roomId, "m.room.bot.options", "_" + userId);
}


function returnStateEvent(event, roomId, eventType, stateKey) {
const client = MatrixClientPeg.get();
if (!client) {
Expand All @@ -218,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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just do:

function onAction(payload) {
  if (payload.action !== "view_room") {
    return;
  }
  currentRoomId = payload.room_id;
}

We don't expect to add more listeners so the switch is a bit premature.

}
}

const onMessage = function(event) {
if (!event.origin) { // stupid chrome
event.origin = event.originalEvent.origin;
Expand All @@ -243,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 " + roomId + " not in view");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/not in view/not visible/

return;
}

switch (event.data.action) {
case "membership_state":
getMembershipState(event, roomId, userId);
Expand All @@ -256,6 +282,9 @@ const onMessage = function(event) {
case "set_bot_options":
setBotOptions(event, roomId, userId);
break;
case "join_rules_state":
getJoinRules(event, roomId);
break;
default:
console.warn("Unhandled postMessage event with action '" + event.data.action +"'");
break;
Expand Down