Skip to content

Commit

Permalink
pull out functions
Browse files Browse the repository at this point in the history
  • Loading branch information
H-Shay committed Oct 9, 2024
1 parent 0d5f6d3 commit fd23a4f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/Mjolnir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ export class Mjolnir {
this.currentState = STATE_RUNNING;
await this.managementRoomOutput.logMessage(LogLevel.INFO, "Mjolnir@startup", "Startup complete. Now monitoring rooms.");
// update protected rooms set
this.protectedRoomsTracker.isAdmin = await this.isSynapseAdmin()
this.protectedRoomsTracker.isAdmin = await this.isSynapseAdmin();
} catch (err) {
try {
LogService.error("Mjolnir", "Error during startup:");
Expand Down
2 changes: 1 addition & 1 deletion src/ProtectedRoomsSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class ProtectedRoomsSet {
/**
* whether the mjolnir instance is server admin
*/
public isAdmin = false
public isAdmin = false;

constructor(
private readonly client: MatrixSendClient,
Expand Down
61 changes: 31 additions & 30 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,64 +70,65 @@ export function isTrueJoinEvent(event: any): boolean {
return membership === 'join' && prevMembership !== "join";
}

/**
* Redact a user's messages in a set of rooms.
* See `getMessagesByUserIn`.
*
* @param client Client to redact the messages with.
* @param managementRoom Management room to log messages back to.
* @param userIdOrGlob A mxid or a glob which is applied to the whole sender field of events in the room, which will be redacted if they match.
* See `MatrixGlob` in matrix-bot-sdk.
* @param targetRoomIds Rooms to redact the messages from.
* @param isAdmin whether the bot is server admin
* @param limit The number of messages to redact from most recent first. If the limit is reached then no further messages will be redacted.
* @param noop Whether to operate in noop mode.
*/
export async function redactUserMessagesIn(client: MatrixSendClient, managementRoom: ManagementRoomOutput, userIdOrGlob: string, targetRoomIds: string[], isAdmin: boolean, limit = 1000, noop = false) {
const usingGlob = userIdOrGlob.includes("*");

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

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

try {
await getMessagesByUserIn(c, uIdOrGlob, targetRoomId, lim, async (eventsToRedact) => {
await getMessagesByUserIn(client, userIdOrGlob, targetRoomId, limit, async (eventsToRedact) => {
for (const targetEvent of eventsToRedact) {
await mr.logMessage(LogLevel.DEBUG, "utils#redactUserMessagesIn", `Redacting ${targetEvent['event_id']} in ${targetRoomId}`, targetRoomId);
await managementRoom.logMessage(LogLevel.DEBUG, "utils#redactUserMessagesIn", `Redacting ${targetEvent['event_id']} in ${targetRoomId}`, targetRoomId);
if (!noop) {
await c.redactEvent(targetRoomId, targetEvent['event_id']);
await client.redactEvent(targetRoomId, targetEvent['event_id']);
} else {
await mr.logMessage(LogLevel.WARN, "utils#redactUserMessagesIn", `Tried to redact ${targetEvent['event_id']} in ${targetRoomId} but Mjolnir is running in no-op mode`, targetRoomId);
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 mr.logMessage(LogLevel.ERROR, "utils#redactUserMessagesIn", `Caught an error while trying to redact messages for ${userIdOrGlob} in ${targetRoomId}: ${error}`, targetRoomId);
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.
* See `getMessagesByUserIn`.
*
* @param client Client to redact the messages with.
* @param managementRoom Management room to log messages back to.
* @param userIdOrGlob A mxid or a glob which is applied to the whole sender field of events in the room, which will be redacted if they match.
* See `MatrixGlob` in matrix-bot-sdk.
* @param targetRoomIds Rooms to redact the messages from.
* @param isAdmin whether the bot is server admin
* @param limit The number of messages to redact from most recent first. If the limit is reached then no further messages will be redacted.
* @param noop Whether to operate in noop mode.
*/
export async function redactUserMessagesIn(client: MatrixSendClient, managementRoom: ManagementRoomOutput, userIdOrGlob: string, targetRoomIds: string[], isAdmin: boolean, 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 adminRedaction(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.`);
await redaction(client, managementRoom, userIdOrGlob, targetRoomIds, limit);
await botRedactUserMessagesIn(client, managementRoom, userIdOrGlob, targetRoomIds, limit, noop);
}
} else {
await redaction(client, managementRoom, userIdOrGlob, targetRoomIds, limit);
await botRedactUserMessagesIn(client, managementRoom, userIdOrGlob, targetRoomIds, limit, noop);
}
}

Expand Down

0 comments on commit fd23a4f

Please sign in to comment.