Skip to content

Commit

Permalink
Update matrix-bot-sdk and use request cleaning function
Browse files Browse the repository at this point in the history
  • Loading branch information
turt2live committed Jul 1, 2021
1 parent d7bacad commit ff4cbc0
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 39 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@
"config": "^3.3.6",
"escape-html": "^1.0.3",
"js-yaml": "^4.1.0",
"matrix-bot-sdk": "^0.5.18"
"matrix-bot-sdk": "^0.5.19"
}
}
31 changes: 20 additions & 11 deletions src/Mjolnir.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
Copyright 2019-2021 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,16 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { CreateEvent, LogLevel, LogService, MatrixClient, MatrixGlob, Permalinks, UserID } from "matrix-bot-sdk";
import {
CreateEvent,
extractRequestError,
LogLevel,
LogService,
MatrixClient,
MatrixGlob,
Permalinks,
UserID
} from "matrix-bot-sdk";
import BanList, { ALL_RULE_TYPES } from "./models/BanList";
import { applyServerAcls } from "./actions/ApplyAcl";
import { RoomUpdateError } from "./models/RoomUpdateError";
Expand Down Expand Up @@ -152,7 +161,7 @@ export class Mjolnir {
}
}
} catch (e) {
LogService.warn("Mjolnir", e);
LogService.warn("Mjolnir", extractRequestError(e));
}
await this.buildWatchedBanLists();
this.applyUnprotectedRooms();
Expand All @@ -175,7 +184,7 @@ export class Mjolnir {
}).catch(async err => {
try {
LogService.error("Mjolnir", "Error during startup:");
LogService.error("Mjolnir", err);
LogService.error("Mjolnir", extractRequestError(err));
await logMessage(LogLevel.ERROR, "Mjolnir@startup", "Startup failed due to error - see console");
} catch (e) {
// If we failed to handle the error, just crash
Expand All @@ -196,7 +205,7 @@ export class Mjolnir {
try {
additionalProtectedRooms = await this.client.getAccountData(PROTECTED_ROOMS_EVENT_TYPE);
} catch (e) {
LogService.warn("Mjolnir", e);
LogService.warn("Mjolnir", extractRequestError(e));
}
if (!additionalProtectedRooms || !additionalProtectedRooms['rooms']) additionalProtectedRooms = {rooms: []};
additionalProtectedRooms.rooms.push(roomId);
Expand All @@ -214,7 +223,7 @@ export class Mjolnir {
try {
additionalProtectedRooms = await this.client.getAccountData(PROTECTED_ROOMS_EVENT_TYPE);
} catch (e) {
LogService.warn("Mjolnir", e);
LogService.warn("Mjolnir", extractRequestError(e));
}
if (!additionalProtectedRooms || !additionalProtectedRooms['rooms']) additionalProtectedRooms = {rooms: []};
additionalProtectedRooms.rooms = additionalProtectedRooms.rooms.filter(r => r !== roomId);
Expand Down Expand Up @@ -250,7 +259,7 @@ export class Mjolnir {
}
}
} catch (e) {
LogService.warn("Mjolnir", e);
LogService.warn("Mjolnir", extractRequestError(e));
}

return enabled;
Expand All @@ -261,7 +270,7 @@ export class Mjolnir {
try {
this.enableProtection(protection, false);
} catch (e) {
LogService.warn("Mjolnir", e);
LogService.warn("Mjolnir", extractRequestError(e));
}
}
}
Expand Down Expand Up @@ -464,7 +473,7 @@ export class Mjolnir {

// Otherwise OK
} catch (e) {
LogService.error("Mjolnir", e);
LogService.error("Mjolnir", extractRequestError(e));
errors.push({
roomId,
errorMessage: e.message || (e.body ? e.body.error : '<no message>'),
Expand Down Expand Up @@ -559,7 +568,7 @@ export class Mjolnir {
const eventPermalink = Permalinks.forEvent(roomId, event['event_id']);
LogService.error("Mjolnir", "Error handling protection: " + protection.name);
LogService.error("Mjolnir", "Failed event: " + eventPermalink);
LogService.error("Mjolnir", e);
LogService.error("Mjolnir", extractRequestError(e));
await this.client.sendNotice(config.managementRoom, "There was an error processing an event through a protection - see log for details. Event: " + eventPermalink);
}
}
Expand Down Expand Up @@ -631,7 +640,7 @@ export class Mjolnir {
return response['admin'];
} catch (e) {
LogService.error("Mjolnir", "Error determining if Mjolnir is a server admin:");
LogService.error("Mjolnir", e);
LogService.error("Mjolnir", extractRequestError(e));
return false; // assume not
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/commands/AddRemoveProtectedRoomsCommand.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2020 The Matrix.org Foundation C.I.C.
Copyright 2020-2021 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -15,7 +15,7 @@ limitations under the License.
*/

import { Mjolnir } from "../Mjolnir";
import { LogLevel, LogService } from "matrix-bot-sdk";
import { extractRequestError, LogLevel, LogService } from "matrix-bot-sdk";
import { logMessage } from "../LogProxy";

// !mjolnir rooms add <room alias/ID>
Expand All @@ -32,7 +32,7 @@ export async function execRemoveProtectedRoom(roomId: string, event: any, mjolni
try {
await mjolnir.client.leaveRoom(protectedRoomId);
} catch (e) {
LogService.warn("AddRemoveProtectedRoomsCommand", e);
LogService.warn("AddRemoveProtectedRoomsCommand", extractRequestError(e));
await logMessage(LogLevel.WARN, "AddRemoveProtectedRoomsCommand", `Failed to leave ${protectedRoomId} - the room is no longer being protected, but the bot could not leave`, protectedRoomId);
}
await mjolnir.client.unstableApis.addReactionToEvent(roomId, event['event_id'], '✅');
Expand Down
6 changes: 3 additions & 3 deletions src/commands/CommandHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
Copyright 2019-2021 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -18,7 +18,7 @@ import { Mjolnir } from "../Mjolnir";
import { execStatusCommand } from "./StatusCommand";
import { execBanCommand, execUnbanCommand } from "./UnbanBanCommand";
import { execDumpRulesCommand } from "./DumpRulesCommand";
import { LogService, RichReply } from "matrix-bot-sdk";
import { extractRequestError, LogService, RichReply } from "matrix-bot-sdk";
import * as htmlEscape from "escape-html";
import { execSyncCommand } from "./SyncCommand";
import { execPermissionCheckCommand } from "./PermissionCheckCommand";
Expand Down Expand Up @@ -141,7 +141,7 @@ export async function handleCommand(roomId: string, event: any, mjolnir: Mjolnir
return await mjolnir.client.sendMessage(roomId, reply);
}
} catch (e) {
LogService.error("CommandHandler", e);
LogService.error("CommandHandler", extractRequestError(e));
const text = "There was an error processing your command - see console/log for details";
const reply = RichReply.createFor(roomId, event, text, text);
reply["msgtype"] = "m.notice";
Expand Down
6 changes: 3 additions & 3 deletions src/commands/ProtectionsCommands.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019 The Matrix.org Foundation C.I.C.
Copyright 2019-2021 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -15,7 +15,7 @@ limitations under the License.
*/

import { Mjolnir } from "../Mjolnir";
import { LogService, RichReply } from "matrix-bot-sdk";
import { extractRequestError, LogService, RichReply } from "matrix-bot-sdk";
import { PROTECTIONS } from "../protections/protections";

// !mjolnir enable <protection>
Expand All @@ -24,7 +24,7 @@ export async function execEnableProtection(roomId: string, event: any, mjolnir:
await mjolnir.enableProtection(parts[2]);
await mjolnir.client.unstableApis.addReactionToEvent(roomId, event['event_id'], '✅');
} catch (e) {
LogService.error("ProtectionsCommands", e);
LogService.error("ProtectionsCommands", extractRequestError(e));

const message = `Error enabling protection '${parts[0]}' - check the name and try again.`;
const reply = RichReply.createFor(roomId, event, message, message);
Expand Down
6 changes: 3 additions & 3 deletions src/commands/SetPowerLevelCommand.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2020 The Matrix.org Foundation C.I.C.
Copyright 2020-2021 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -15,7 +15,7 @@ limitations under the License.
*/

import { Mjolnir } from "../Mjolnir";
import { LogLevel, LogService } from "matrix-bot-sdk";
import { extractRequestError, LogLevel, LogService } from "matrix-bot-sdk";
import { logMessage } from "../LogProxy";

// !mjolnir powerlevel <user ID> <level> [room]
Expand All @@ -32,7 +32,7 @@ export async function execSetPowerLevelCommand(roomId: string, event: any, mjoln
} catch (e) {
const message = e.message || (e.body ? e.body.error : '<no message>');
await logMessage(LogLevel.ERROR, "SetPowerLevelCommand", `Failed to set power level of ${victim} to ${level} in ${targetRoomId}: ${message}`, targetRoomId);
LogService.error("SetPowerLevelCommand", e);
LogService.error("SetPowerLevelCommand", extractRequestError(e));
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/commands/UnbanBanCommand.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
Copyright 2019-2021 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -16,7 +16,7 @@ limitations under the License.

import { Mjolnir } from "../Mjolnir";
import BanList, { RULE_ROOM, RULE_SERVER, RULE_USER, USER_RULE_TYPES } from "../models/BanList";
import { LogLevel, LogService, MatrixGlob, RichReply } from "matrix-bot-sdk";
import { extractRequestError, LogLevel, LogService, MatrixGlob, RichReply } from "matrix-bot-sdk";
import { RECOMMENDATION_BAN, recommendationToStable } from "../models/ListRule";
import config from "../config";
import { logMessage } from "../LogProxy";
Expand All @@ -37,7 +37,7 @@ export async function parseArguments(roomId: string, event: any, mjolnir: Mjolni
defaultShortcode = data['shortcode'];
} catch (e) {
LogService.warn("UnbanBanCommand", "Non-fatal error getting default ban list");
LogService.warn("UnbanBanCommand", e);
LogService.warn("UnbanBanCommand", extractRequestError(e));

// Assume no default.
}
Expand Down
6 changes: 3 additions & 3 deletions src/models/BanList.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019 The Matrix.org Foundation C.I.C.
Copyright 2019-2021 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { LogService, MatrixClient } from "matrix-bot-sdk";
import { extractRequestError, LogService, MatrixClient } from "matrix-bot-sdk";
import { ListRule } from "./ListRule";

export const RULE_USER = "m.room.rule.user";
Expand Down Expand Up @@ -50,7 +50,7 @@ export default class BanList {
const currentShortcode = this.shortcode;
this.shortcode = newShortcode;
this.client.sendStateEvent(this.roomId, SHORTCODE_EVENT_TYPE, '', {shortcode: this.shortcode}).catch(err => {
LogService.error("BanList", err);
LogService.error("BanList", extractRequestError(err));
if (this.shortcode === newShortcode) this.shortcode = currentShortcode;
});
}
Expand Down
6 changes: 3 additions & 3 deletions src/queues/AutomaticRedactionQueue.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019 The Matrix.org Foundation C.I.C.
Copyright 2019-2021 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { LogLevel, LogService, MatrixClient, Permalinks } from "matrix-bot-sdk";
import { extractRequestError, LogLevel, LogService, MatrixClient, Permalinks } from "matrix-bot-sdk";
import { logMessage } from "../LogProxy";
import config from "../config";

Expand Down Expand Up @@ -44,7 +44,7 @@ export class AutomaticRedactionQueue {
}
} catch (e) {
logMessage(LogLevel.WARN, "AutomaticRedactionQueue", `Unable to redact message: ${permalink}`);
LogService.warn("AutomaticRedactionQueue", e);
LogService.warn("AutomaticRedactionQueue", extractRequestError(e));
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
Copyright 2019-2021 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -15,6 +15,7 @@ limitations under the License.
*/

import {
extractRequestError,
LogLevel,
LogService,
MatrixClient,
Expand Down Expand Up @@ -194,7 +195,7 @@ export async function replaceRoomIdsWithPills(client: MatrixClient, text: string
} 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);
LogService.warn("utils", extractRequestError(e));
}
const regexRoomId = new RegExp(escapeRegex(roomId), "g");
content.body = content.body.replace(regexRoomId, alias);
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1261,10 +1261,10 @@ make-error@^1.1.1:
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.5.tgz#efe4e81f6db28cadd605c70f29c831b58ef776c8"
integrity sha512-c3sIjNUow0+8swNwVpqoH4YCShKNFkMaw6oH1mNS2haDZQqkeZFlHS3dhoeEbKKmJB4vXpJucU6oH75aDYeE9g==

matrix-bot-sdk@^0.5.18:
version "0.5.18"
resolved "https://registry.yarnpkg.com/matrix-bot-sdk/-/matrix-bot-sdk-0.5.18.tgz#c4e4e657fd632604380fce7453370d659a6f1d88"
integrity sha512-NntJoGmZdGlBsJ+HuEi1zB0lcRd8BCiPH6hExeuRNKtMOVHhJIKUgd5IY2cTy5YxHGk+kbBbkpzooFvRxVkOrg==
matrix-bot-sdk@^0.5.19:
version "0.5.19"
resolved "https://registry.yarnpkg.com/matrix-bot-sdk/-/matrix-bot-sdk-0.5.19.tgz#6ce13359ab53ea0af9dc3ebcbe288c5f6d9c02c6"
integrity sha512-RIPyvQPkOVp2yTKeDgp5rcn6z/DiKdHb6E8c69K+utai8ypRGtfDRj0PGqP+1XzqC9Wb1OFrESCUB5t0ffdC9g==
dependencies:
"@types/express" "^4.17.7"
chalk "^4.1.0"
Expand Down

0 comments on commit ff4cbc0

Please sign in to comment.