Merge pull request #443 from matrix-org/luke/post-messages
Scalar Messaging: Expose join_rules and restrict to currently viewed roompull/21833/head
commit
6ddc31fb66
|
@ -123,6 +123,8 @@ Example:
|
||||||
|
|
||||||
const SdkConfig = require('./SdkConfig');
|
const SdkConfig = require('./SdkConfig');
|
||||||
const MatrixClientPeg = require("./MatrixClientPeg");
|
const MatrixClientPeg = require("./MatrixClientPeg");
|
||||||
|
const MatrixEvent = require("matrix-js-sdk").MatrixEvent;
|
||||||
|
const 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));
|
||||||
|
@ -188,17 +190,52 @@ function setBotOptions(event, roomId, userId) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function setBotPower(event, roomId, userId, level) {
|
||||||
|
if (!(Number.isInteger(level) && level >= 0)) {
|
||||||
|
sendError(event, "Power level must be positive integer.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`Received request to set power level to ${level} for bot ${userId} in room ${roomId}.`);
|
||||||
|
const client = MatrixClientPeg.get();
|
||||||
|
if (!client) {
|
||||||
|
sendError(event, "You need to be logged in.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
client.getStateEvent(roomId, "m.room.power_levels", "").then((powerLevels) => {
|
||||||
|
let powerEvent = new MatrixEvent(
|
||||||
|
{
|
||||||
|
type: "m.room.power_levels",
|
||||||
|
content: powerLevels,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
client.setPowerLevel(roomId, userId, level, powerEvent).done(() => {
|
||||||
|
sendResponse(event, {
|
||||||
|
success: true,
|
||||||
|
});
|
||||||
|
}, (err) => {
|
||||||
|
sendError(event, err.message ? err.message : "Failed to send request.", err);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function getMembershipState(event, roomId, userId) {
|
function getMembershipState(event, roomId, userId) {
|
||||||
console.log(`membership_state of ${userId} in room ${roomId} requested.`);
|
console.log(`membership_state of ${userId} in room ${roomId} requested.`);
|
||||||
returnStateEvent(event, roomId, "m.room.member", 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", "");
|
||||||
|
}
|
||||||
|
|
||||||
function botOptions(event, roomId, userId) {
|
function botOptions(event, roomId, userId) {
|
||||||
console.log(`bot_options of ${userId} in room ${roomId} requested.`);
|
console.log(`bot_options of ${userId} in room ${roomId} requested.`);
|
||||||
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) {
|
||||||
|
@ -218,6 +255,17 @@ 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) {
|
||||||
|
if (payload.action !== "view_room") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
currentRoomId = payload.room_id;
|
||||||
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
@ -235,14 +283,29 @@ const onMessage = function(event) {
|
||||||
|
|
||||||
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) {
|
|
||||||
sendError(event, "Missing user_id in request");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!roomId) {
|
if (!roomId) {
|
||||||
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 " + roomId + " not visible");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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) {
|
switch (event.data.action) {
|
||||||
case "membership_state":
|
case "membership_state":
|
||||||
getMembershipState(event, roomId, userId);
|
getMembershipState(event, roomId, userId);
|
||||||
|
@ -256,6 +319,9 @@ const onMessage = function(event) {
|
||||||
case "set_bot_options":
|
case "set_bot_options":
|
||||||
setBotOptions(event, roomId, userId);
|
setBotOptions(event, roomId, userId);
|
||||||
break;
|
break;
|
||||||
|
case "set_bot_power":
|
||||||
|
setBotPower(event, roomId, userId, event.data.level);
|
||||||
|
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;
|
||||||
|
|
Loading…
Reference in New Issue