mirror of
https://github.com/matrix-org/mjolnir.git
synced 2024-09-29 20:56:23 +00:00
Add additional safety around room alias resolution for pills
Just fall back to not caring if we have to.
This commit is contained in:
parent
7fa025f678
commit
9e18a6dfff
@ -14,9 +14,10 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { LogLevel, LogService } from "matrix-bot-sdk";
|
import { LogLevel, LogService, TextualMessageEventContent } from "matrix-bot-sdk";
|
||||||
import config from "./config";
|
import config from "./config";
|
||||||
import { replaceRoomIdsWithPills } from "./utils";
|
import { replaceRoomIdsWithPills } from "./utils";
|
||||||
|
import * as htmlEscape from "escape-html";
|
||||||
|
|
||||||
const levelToFn = {
|
const levelToFn = {
|
||||||
[LogLevel.DEBUG.toString()]: LogService.debug,
|
[LogLevel.DEBUG.toString()]: LogService.debug,
|
||||||
@ -25,7 +26,7 @@ const levelToFn = {
|
|||||||
[LogLevel.ERROR.toString()]: LogService.error,
|
[LogLevel.ERROR.toString()]: LogService.error,
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function logMessage(level: LogLevel, module: string, message: string | any, additionalRoomIds: string[] | string = null) {
|
export async function logMessage(level: LogLevel, module: string, message: string | any, additionalRoomIds: string[] | string = null, isRecursive=false) {
|
||||||
if (!additionalRoomIds) additionalRoomIds = [];
|
if (!additionalRoomIds) additionalRoomIds = [];
|
||||||
if (!Array.isArray(additionalRoomIds)) additionalRoomIds = [additionalRoomIds];
|
if (!Array.isArray(additionalRoomIds)) additionalRoomIds = [additionalRoomIds];
|
||||||
|
|
||||||
@ -37,7 +38,16 @@ export async function logMessage(level: LogLevel, module: string, message: strin
|
|||||||
const roomIds = [config.managementRoom, ...additionalRoomIds];
|
const roomIds = [config.managementRoom, ...additionalRoomIds];
|
||||||
const client = config.RUNTIME.client;
|
const client = config.RUNTIME.client;
|
||||||
|
|
||||||
const evContent = await replaceRoomIdsWithPills(client, clientMessage, roomIds, "m.notice");
|
let evContent: TextualMessageEventContent = {
|
||||||
|
body: message,
|
||||||
|
formatted_body: htmlEscape(message),
|
||||||
|
msgtype: "m.notice",
|
||||||
|
format: "org.matrix.custom.html",
|
||||||
|
};
|
||||||
|
if (!isRecursive) {
|
||||||
|
evContent = await replaceRoomIdsWithPills(client, clientMessage, roomIds, "m.notice");
|
||||||
|
}
|
||||||
|
|
||||||
await client.sendMessage(config.managementRoom, evContent);
|
await client.sendMessage(config.managementRoom, evContent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -185,7 +185,14 @@ export async function replaceRoomIdsWithPills(client: MatrixClient, text: string
|
|||||||
|
|
||||||
const viaServers = [(new UserID(await client.getUserId())).domain];
|
const viaServers = [(new UserID(await client.getUserId())).domain];
|
||||||
for (const roomId of roomIds) {
|
for (const roomId of roomIds) {
|
||||||
const alias = (await getRoomAlias(client, roomId)) || roomId;
|
let alias = roomId;
|
||||||
|
try {
|
||||||
|
alias = (await getRoomAlias(client, roomId)) || roomId;
|
||||||
|
} catch (e) {
|
||||||
|
// This is a recursive call, so tell the function not to try and call us
|
||||||
|
await logMessage(LogLevel.WARN, "utils", `Failed to resolve room alias for ${roomId} - see console for details`, null, true);
|
||||||
|
LogService.warn("utils", e);
|
||||||
|
}
|
||||||
const regexRoomId = new RegExp(escapeRegex(roomId), "g");
|
const regexRoomId = new RegExp(escapeRegex(roomId), "g");
|
||||||
content.body = content.body.replace(regexRoomId, alias);
|
content.body = content.body.replace(regexRoomId, alias);
|
||||||
content.formatted_body = content.formatted_body.replace(regexRoomId, `<a href="${Permalinks.forRoom(alias, viaServers)}">${alias}</a>`);
|
content.formatted_body = content.formatted_body.replace(regexRoomId, `<a href="${Permalinks.forRoom(alias, viaServers)}">${alias}</a>`);
|
||||||
|
Loading…
Reference in New Issue
Block a user