diff --git a/src/LogProxy.ts b/src/LogProxy.ts index 28e4d0a..dbc9256 100644 --- a/src/LogProxy.ts +++ b/src/LogProxy.ts @@ -14,9 +14,10 @@ See the License for the specific language governing permissions and limitations under the License. */ -import { LogLevel, LogService } from "matrix-bot-sdk"; +import { LogLevel, LogService, TextualMessageEventContent } from "matrix-bot-sdk"; import config from "./config"; import { replaceRoomIdsWithPills } from "./utils"; +import * as htmlEscape from "escape-html"; const levelToFn = { [LogLevel.DEBUG.toString()]: LogService.debug, @@ -25,7 +26,7 @@ const levelToFn = { [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 (!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 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); } diff --git a/src/utils.ts b/src/utils.ts index 66d1d8c..edff319 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -185,7 +185,14 @@ export async function replaceRoomIdsWithPills(client: MatrixClient, text: string const viaServers = [(new UserID(await client.getUserId())).domain]; 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"); content.body = content.body.replace(regexRoomId, alias); content.formatted_body = content.formatted_body.replace(regexRoomId, `${alias}`);