Add additional safety around room alias resolution for pills

Just fall back to not caring if we have to.
This commit is contained in:
Travis Ralston 2020-05-11 21:30:22 -06:00
parent 7fa025f678
commit 9e18a6dfff
2 changed files with 21 additions and 4 deletions

View File

@ -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);
}

View File

@ -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, `<a href="${Permalinks.forRoom(alias, viaServers)}">${alias}</a>`);