Spaces not tabs

This commit is contained in:
Kegan Dougal 2016-08-23 13:31:55 +01:00
parent 9162a0ff01
commit f6b008350d

View file

@ -17,36 +17,36 @@ limitations under the License.
/* /*
Listens for incoming postMessage requests from the integrations UI URL. The following API is exposed: Listens for incoming postMessage requests from the integrations UI URL. The following API is exposed:
{ {
action: "invite" | "membership_state", action: "invite" | "membership_state",
room_id: $ROOM_ID, room_id: $ROOM_ID,
user_id: $USER_ID user_id: $USER_ID
} }
The complete request object is returned to the caller with an additional "response" key like so: The complete request object is returned to the caller with an additional "response" key like so:
{ {
action: "invite" | "membership_state", action: "invite" | "membership_state",
room_id: $ROOM_ID, room_id: $ROOM_ID,
user_id: $USER_ID, user_id: $USER_ID,
response: { ... } response: { ... }
} }
"response" objects can consist of a sole "error" key to indicate an error. These look like: "response" objects can consist of a sole "error" key to indicate an error. These look like:
{ {
error: { error: {
message: "Unable to invite user into room.", message: "Unable to invite user into room.",
_error: <Original Error Object> _error: <Original Error Object>
} }
} }
The "message" key should be a human-friendly string. The "message" key should be a human-friendly string.
The response object for "membership_state" looks like: The response object for "membership_state" looks like:
{ {
membership_state: "join" | "leave" | "invite" | "ban" membership_state: "join" | "leave" | "invite" | "ban"
} }
The response object for "invite" looks like: The response object for "invite" looks like:
{ {
invite: true invite: true
} }
*/ */
@ -55,117 +55,117 @@ const SdkConfig = require('./SdkConfig');
const MatrixClientPeg = require("./MatrixClientPeg"); const MatrixClientPeg = require("./MatrixClientPeg");
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;
} }
// check it is from the integrations UI URL (remove trailing spaces) // check it is from the integrations UI URL (remove trailing spaces)
let url = SdkConfig.get().integrations_ui_url; let url = SdkConfig.get().integrations_ui_url;
if (url.endsWith("/")) { if (url.endsWith("/")) {
url = url.substr(0, url.length - 1); url = url.substr(0, url.length - 1);
} }
if (url !== event.origin) { if (url !== event.origin) {
console.warn("Unauthorised postMessage received. Source URL: " + event.origin); console.warn("Unauthorised postMessage received. Source URL: " + event.origin);
return; return;
} }
switch (event.data.action) { switch (event.data.action) {
case "membership_state": { case "membership_state": {
const roomId = event.data.room_id; const roomId = event.data.room_id;
const userId = event.data.user_id; const userId = event.data.user_id;
if (!userId) { if (!userId) {
return sendError(event, "Missing user_id in request"); return sendError(event, "Missing user_id in request");
} }
if (!roomId) { if (!roomId) {
return sendError(event, "Missing room_id in request"); return sendError(event, "Missing room_id in request");
} }
console.log(`membership_state of ${userId} in room ${roomId} requested.`); console.log(`membership_state of ${userId} in room ${roomId} requested.`);
const client = MatrixClientPeg.get(); const client = MatrixClientPeg.get();
if (!client) { if (!client) {
return sendError(event, "You need to be logged in."); return sendError(event, "You need to be logged in.");
} }
const room = client.getRoom(roomId); const room = client.getRoom(roomId);
if (!room) { if (!room) {
return sendError(event, "This room is not recognised."); return sendError(event, "This room is not recognised.");
} }
let membershipState = "leave"; let membershipState = "leave";
const member = room.getMember(userId); const member = room.getMember(userId);
if (member) { if (member) {
membershipState = member.membership; membershipState = member.membership;
} }
sendResponse(event, { sendResponse(event, {
membership_state: membershipState membership_state: membershipState
}); });
} }
break; break;
case "invite": { case "invite": {
const roomId = event.data.room_id; const roomId = event.data.room_id;
const userId = event.data.user_id; const userId = event.data.user_id;
if (!userId) { if (!userId) {
return sendError(event, "Missing user_id in request"); return sendError(event, "Missing user_id in request");
} }
if (!roomId) { if (!roomId) {
return sendError(event, "Missing room_id in request"); return sendError(event, "Missing room_id in request");
} }
console.log(`Received request to invite ${userId} into room ${roomId}`); console.log(`Received request to invite ${userId} into room ${roomId}`);
const client = MatrixClientPeg.get(); const client = MatrixClientPeg.get();
if (!client) { if (!client) {
return sendError(event, "You need to be logged in."); return sendError(event, "You need to be logged in.");
} }
const room = client.getRoom(roomId); const room = client.getRoom(roomId);
if (room) { if (room) {
// if they are already invited we can resolve immediately. // if they are already invited we can resolve immediately.
const member = room.getMember(userId); const member = room.getMember(userId);
if (member && member.membership === "invite") { if (member && member.membership === "invite") {
sendResponse(event, { sendResponse(event, {
invite: true invite: true
}); });
return; return;
} }
} }
client.invite(roomId, userId).then(function() { client.invite(roomId, userId).then(function() {
sendResponse(event, { sendResponse(event, {
invite: true invite: true
}); });
}, function(err) { }, function(err) {
sendError(event, "You need to be able to invite users to do that.", err); sendError(event, "You need to be able to invite users to do that.", err);
}); });
} }
break; break;
default: default:
console.warn("Unhandled postMessage event with action '" + event.data.action +"'"); console.warn("Unhandled postMessage event with action '" + event.data.action +"'");
break; break;
} }
}; };
function sendResponse(event, res) { function sendResponse(event, res) {
console.log("Action:" + event.data.action + " succeeded with response: " + JSON.stringify(res)); console.log("Action:" + event.data.action + " succeeded with response: " + JSON.stringify(res));
const data = event.data; const data = event.data;
data.response = res; data.response = res;
event.source.postMessage(data, event.origin) event.source.postMessage(data, event.origin)
} }
function sendError(event, msg, nestedError) { function sendError(event, msg, nestedError) {
console.error("Action:" + event.data.action + " failed with message: " + msg); console.error("Action:" + event.data.action + " failed with message: " + msg);
const data = event.data; const data = event.data;
data.response = { data.response = {
error: { error: {
message: msg, message: msg,
} }
}; };
if (nestedError) { if (nestedError) {
data.response.error._error = nestedError; data.response.error._error = nestedError;
} }
event.source.postMessage(data, event.origin); event.source.postMessage(data, event.origin);
} }
module.exports = { module.exports = {
startListening: function() { startListening: function() {
window.addEventListener("message", onMessage, false); window.addEventListener("message", onMessage, false);
}, },
stopListening: function() { stopListening: function() {
window.removeEventListener("message", onMessage); window.removeEventListener("message", onMessage);
} }
}; };