Skip to content
Extraits de code Groupes Projets
Non vérifiée Valider 31c7d070 rédigé par Sorunome's avatar Sorunome
Parcourir les fichiers

Merge branch 'develop' of https://github.com/Pneumaticat/matrix-appservice-discord into develop

parents e2ca5093 dc254190
Branches
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -24,6 +24,7 @@ import { Client as MatrixClient } from "matrix-js-sdk";
const MIN_NAME_LENGTH = 2;
const MAX_NAME_LENGTH = 32;
const MATRIX_TO_LINK = "https://matrix.to/#/";
const DEFAULT_ROOM_NOTIFY_POWER_LEVEL = 50;
export interface IMatrixMessageProcessorParams {
displayname?: string;
......@@ -75,25 +76,35 @@ export class MatrixMessageProcessor {
return reply;
}
private async canNotifyRoom() {
if (!this.params || !this.params.mxClient || !this.params.roomId || !this.params.userId) {
return false;
}
const res: IMatrixEvent = await this.params.mxClient.getStateEvent(
this.params.roomId, "m.room.power_levels");
// Some rooms may not have notifications.room set if the value hasn't
// been changed from the default. If so, use our hardcoded power level.
const requiredPowerLevel = res && res.notifications && res.notifications.room
? res.notifications.room
: DEFAULT_ROOM_NOTIFY_POWER_LEVEL;
return res && res.users
&& res.users[this.params.userId] !== undefined
&& res.users[this.params.userId] >= requiredPowerLevel;
}
private async escapeDiscord(msg: string): Promise<string> {
// \u200B is the zero-width space --> they still look the same but don't mention
msg = msg.replace(/@everyone/g, "@\u200Beveryone");
msg = msg.replace(/@here/g, "@\u200Bhere");
if (msg.includes("@room") && this.params && this.params.mxClient && this.params.roomId && this.params.userId) {
// let's check for more complex logic if @room should be replaced
const res: IMatrixEvent = await this.params.mxClient.getStateEvent(
this.params.roomId, "m.room.power_levels");
if (
res && res.users
&& res.users[this.params.userId] !== undefined
&& res.notifications
&& res.notifications.room !== undefined
&& res.users[this.params.userId] >= res.notifications.room
) {
// Check the Matrix permissions to see if this user has the required
// power level to notify with @room; if so, replace it with @here.
if (msg.includes("@room") && await this.canNotifyRoom()) {
msg = msg.replace(/@room/g, "@here");
}
}
const escapeChars = ["\\", "*", "_", "~", "`", "|"];
msg = msg.split(" ").map((s) => {
if (s.match(/^https?:\/\//)) {
......
......@@ -29,23 +29,6 @@ import { MatrixMessageProcessor } from "../src/matrixmessageprocessor";
const expect = Chai.expect;
const mxClient = {
getStateEvent: async (roomId, stateType, _) => {
if (stateType === "m.room.power_levels") {
return {
notifications: {
room: 50,
},
users: {
"@nopower:localhost": 0,
"@power:localhost": 100,
},
};
}
return null;
},
};
const bot = {
GetEmojiByMxc: async (mxc: string): Promise<DbEmoji> => {
if (mxc === "mxc://real_emote:localhost") {
......@@ -377,6 +360,47 @@ code
});
});
describe("FormatMessage / formatted_body / matrix", () => {
/**
* Returns a mocked matrix client that mocks the m.room.power_levels
* event to test @room notifications.
*
* @param roomNotificationLevel the power level required to @room
* (if undefined, does not include notifications.room in
* m.room.power_levels)
*/
function getMxClient(roomNotificationLevel?: number) {
return {
getStateEvent: async (roomId, stateType, _) => {
if (stateType === "m.room.power_levels") {
return {
// Only include notifications.room when
// roomNotificationLevel is undefined
...roomNotificationLevel !== undefined && {
notifications: {
room: roomNotificationLevel,
},
},
users: {
"@nopower:localhost": 0,
"@power:localhost": 100,
},
};
}
return null;
},
};
}
/**
* Explicit power level required to notify @room.
*
* Essentially, we want to test two code paths - one where the explicit
* power level is set and one where it isn't, to see if the bridge can
* fall back to a default level (of 50). This is the explicit value we
* will set.
*/
const ROOM_NOTIFICATION_LEVEL = 50;
it("escapes @everyone", async () => {
const mp = new MatrixMessageProcessor(bot);
const guild = new MockGuild("1234");
......@@ -395,24 +419,46 @@ code
const mp = new MatrixMessageProcessor(bot);
const guild = new MockGuild("1234");
const msg = getPlainMessage("hey @room");
const params = {
mxClient,
let params = {
mxClient: getMxClient(ROOM_NOTIFICATION_LEVEL),
roomId: "!123456:localhost",
userId: "@power:localhost",
};
const result = await mp.FormatMessage(msg, guild as any, params as any);
let result = await mp.FormatMessage(msg, guild as any, params as any);
expect(result).is.equal("hey @here");
// Test again using an unset notifications.room value in
// m.room.power_levels, to ensure it falls back to a default
// requirement.
params = {
mxClient: getMxClient(),
roomId: "!123456:localhost",
userId: "@power:localhost",
};
result = await mp.FormatMessage(msg, guild as any, params as any);
expect(result).is.equal("hey @here");
});
it("ignores @room to @here conversion, if insufficient power", async () => {
const mp = new MatrixMessageProcessor(bot);
const guild = new MockGuild("1234");
const msg = getPlainMessage("hey @room");
const params = {
mxClient,
let params = {
mxClient: getMxClient(ROOM_NOTIFICATION_LEVEL),
roomId: "!123456:localhost",
userId: "@nopower:localhost",
};
const result = await mp.FormatMessage(msg, guild as any, params as any);
let result = await mp.FormatMessage(msg, guild as any, params as any);
expect(result).is.equal("hey @room");
// Test again using an unset notifications.room value in
// m.room.power_levels, to ensure it falls back to a default
// requirement.
params = {
mxClient: getMxClient(),
roomId: "!123456:localhost",
userId: "@nopower:localhost",
};
result = await mp.FormatMessage(msg, guild as any, params as any);
expect(result).is.equal("hey @room");
});
it("handles /me for normal names", async () => {
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter