Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
H-Shay committed Oct 9, 2024
1 parent 0ed9b73 commit d652727
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 104 deletions.
2 changes: 1 addition & 1 deletion src/Mjolnir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ export class Mjolnir {
this.currentState = STATE_RUNNING;

await this.managementRoomOutput.logMessage(
LogLevel.INFO,
LogLevel.INFO,
"Mjolnir@startup",
"Startup complete. Now monitoring rooms.",
);
Expand Down
2 changes: 1 addition & 1 deletion src/ProtectedRoomsSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class ProtectedRoomsSet {
/**
* whether the mjolnir instance is server admin
*/
public isAdmin = false;
public isAdmin = false;

constructor(
private readonly client: MatrixSendClient,
Expand Down
2 changes: 1 addition & 1 deletion src/commands/RedactCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { Permalinks } from "@vector-im/matrix-bot-sdk";
export async function execRedactCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) {
const userId = parts[2];

let targetRoom: string|null = null;
let targetRoom: string | null = null;
let limit = Number.parseInt(parts.length > 3 ? parts[3] : "", 10); // default to NaN for later
if (parts.length > 3 && isNaN(limit)) {
targetRoom = await mjolnir.client.resolveRoom(parts[3]);
Expand Down
5 changes: 3 additions & 2 deletions src/queues/EventRedactionQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ export class RedactUserInRoom implements QueuedRedaction {

public async redact(client: MatrixClient, managementRoom: ManagementRoomOutput) {
await managementRoom.logMessage(
LogLevel.DEBUG,
LogLevel.DEBUG,
"Mjolnir",
`Redacting events from ${this.userId} in room ${this.roomId}.`);
`Redacting events from ${this.userId} in room ${this.roomId}.`,
);
await redactUserMessagesIn(client, managementRoom, this.userId, [this.roomId], this.isAdmin);
}

Expand Down
119 changes: 82 additions & 37 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import {
LogService,
MatrixGlob,
getRequestFn,
setRequestFn, extractRequestError,
setRequestFn,
extractRequestError,
} from "@vector-im/matrix-bot-sdk";
import { ClientRequest, IncomingMessage } from "http";
import { default as parseDuration } from "parse-duration";
Expand Down Expand Up @@ -73,36 +74,72 @@ export function isTrueJoinEvent(event: any): boolean {
return membership === "join" && prevMembership !== "join";
}

async function adminRedactUserMessagesIn(client: MatrixSendClient, managementRoom: ManagementRoomOutput, userId: string, targetRooms: string[], limit = 1000) {
let redactID: string;
const body = {"limit": limit, "rooms": targetRooms};
const redactEndpoint = `/_synapse/admin/v1/user/${userId}/redact`;
const response = await client.doRequest("GET", redactEndpoint, null, body);
redactID = response["redact_id"];
await managementRoom.logMessage(LogLevel.INFO, "utils#redactUserMessagesIn", `Successfully requested redaction, ID for task is ${redactID}`);
}
async function adminRedactUserMessagesIn(
client: MatrixSendClient,
managementRoom: ManagementRoomOutput,
userId: string,
targetRooms: string[],
limit = 1000,
) {
let redactID: string;
const body = { limit: limit, rooms: targetRooms };
const redactEndpoint = `/_synapse/admin/v1/user/${userId}/redact`;
const response = await client.doRequest("GET", redactEndpoint, null, body);
redactID = response["redact_id"];
await managementRoom.logMessage(
LogLevel.INFO,
"utils#redactUserMessagesIn",
`Successfully requested redaction, ID for task is ${redactID}`,
);
}

async function botRedactUserMessagesIn(client: MatrixSendClient, managementRoom: ManagementRoomOutput, userIdOrGlob: string, targetRooms: string [], limit = 1000, noop = false) {
for (const targetRoomId of targetRooms) {
await managementRoom.logMessage(LogLevel.DEBUG, "utils#redactUserMessagesIn", `Fetching sent messages for ${userIdOrGlob} in ${targetRoomId} to redact...`, targetRoomId);

try {
await getMessagesByUserIn(client, userIdOrGlob, targetRoomId, limit, async (eventsToRedact) => {
for (const targetEvent of eventsToRedact) {
await managementRoom.logMessage(LogLevel.DEBUG, "utils#redactUserMessagesIn", `Redacting ${targetEvent['event_id']} in ${targetRoomId}`, targetRoomId);
if (!noop) {
await client.redactEvent(targetRoomId, targetEvent['event_id']);
} else {
await managementRoom.logMessage(LogLevel.WARN, "utils#redactUserMessagesIn", `Tried to redact ${targetEvent['event_id']} in ${targetRoomId} but Mjolnir is running in no-op mode`, targetRoomId);
}
async function botRedactUserMessagesIn(
client: MatrixSendClient,
managementRoom: ManagementRoomOutput,
userIdOrGlob: string,
targetRooms: string[],
limit = 1000,
noop = false,
) {
for (const targetRoomId of targetRooms) {
await managementRoom.logMessage(
LogLevel.DEBUG,
"utils#redactUserMessagesIn",
`Fetching sent messages for ${userIdOrGlob} in ${targetRoomId} to redact...`,
targetRoomId,
);

try {
await getMessagesByUserIn(client, userIdOrGlob, targetRoomId, limit, async (eventsToRedact) => {
for (const targetEvent of eventsToRedact) {
await managementRoom.logMessage(
LogLevel.DEBUG,
"utils#redactUserMessagesIn",
`Redacting ${targetEvent["event_id"]} in ${targetRoomId}`,
targetRoomId,
);
if (!noop) {
await client.redactEvent(targetRoomId, targetEvent["event_id"]);
} else {
await managementRoom.logMessage(
LogLevel.WARN,
"utils#redactUserMessagesIn",
`Tried to redact ${targetEvent["event_id"]} in ${targetRoomId} but Mjolnir is running in no-op mode`,
targetRoomId,
);
}
});
} catch (error) {
await managementRoom.logMessage(LogLevel.ERROR, "utils#redactUserMessagesIn", `Caught an error while trying to redact messages for ${userIdOrGlob} in ${targetRoomId}: ${error}`, targetRoomId);
}
}
});
} catch (error) {
await managementRoom.logMessage(
LogLevel.ERROR,
"utils#redactUserMessagesIn",
`Caught an error while trying to redact messages for ${userIdOrGlob} in ${targetRoomId}: ${error}`,
targetRoomId,
);
}
}

}

/**
* Redact a user's messages in a set of rooms.
Expand All @@ -119,22 +156,30 @@ async function botRedactUserMessagesIn(client: MatrixSendClient, managementRoom:
*/

export async function redactUserMessagesIn(
client: MatrixSendClient,
managementRoom: ManagementRoomOutput,
userIdOrGlob: string,
targetRoomIds: string[],
client: MatrixSendClient,
managementRoom: ManagementRoomOutput,
userIdOrGlob: string,
targetRoomIds: string[],
isAdmin: boolean,
limit = 1000,
noop = false) {
limit = 1000,
noop = false,
) {
const usingGlob = userIdOrGlob.includes("*");
// if admin use the Admin API, but admin endpoint does not support globs
if (isAdmin && !usingGlob) {
try {
await adminRedactUserMessagesIn(client, managementRoom, userIdOrGlob, targetRoomIds, limit)
await adminRedactUserMessagesIn(client, managementRoom, userIdOrGlob, targetRoomIds, limit);
} catch (e) {
LogService.error("utils#redactUserMessagesIn", `Error using admin API to redact messages: ${extractRequestError(e)}`);
await managementRoom.logMessage(LogLevel.ERROR, "utils#redactUserMessagesIn", `Error using admin API to redact messages for user ${userIdOrGlob}, please check logs for more info - falling
back to non-admin redaction process.`);
LogService.error(
"utils#redactUserMessagesIn",
`Error using admin API to redact messages: ${extractRequestError(e)}`,
);
await managementRoom.logMessage(
LogLevel.ERROR,
"utils#redactUserMessagesIn",
`Error using admin API to redact messages for user ${userIdOrGlob}, please check logs for more info - falling
back to non-admin redaction process.`,
);
await botRedactUserMessagesIn(client, managementRoom, userIdOrGlob, targetRoomIds, limit, noop);
}
} else {
Expand Down
Loading

0 comments on commit d652727

Please sign in to comment.