Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use spoilers when mentioning targets of a rule #559

Merged
merged 4 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ManagementRoomOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const levelToFn = {
*/
export default class ManagementRoomOutput {
constructor(
private readonly managementRoomId: string,
public readonly managementRoomId: string,
private readonly client: MatrixSendClient,
private readonly config: IConfig,
) {}
Expand Down
27 changes: 13 additions & 14 deletions src/ProtectedRoomsSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,12 @@ export class ProtectedRoomsSet {
// ignore - assume no ACL
}

// We specifically use sendNotice to avoid having to escape HTML
await this.managementRoomOutput.logMessage(
LogLevel.DEBUG,
"ApplyAcl",
`Applying ACL in ${roomId}`,
roomId,
);
await this.client.sendMessage(this.managementRoomId, {
msgtype: "m.text",
body: `Applying ACL in ${roomId}.`,
format: "org.matrix.custom.html",
formatted_body: `Applying ACL in <span data-mx-spoiler>${roomId}</span>.`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we'll need to escape HTML in all these spoilers. There should already be a function for it, somewhere.

});

if (!this.config.noop) {
await this.client.sendStateEvent(roomId, "m.room.server_acl", "", finalAcl);
Expand Down Expand Up @@ -439,13 +438,13 @@ export class ProtectedRoomsSet {
const memberAccess = this.accessControlUnit.getAccessForUser(member.userId, "IGNORE_SERVER");
if (memberAccess.outcome === Access.Banned) {
const reason = memberAccess.rule ? memberAccess.rule.reason : "<no reason supplied>";
// We specifically use sendNotice to avoid having to escape HTML
await this.managementRoomOutput.logMessage(
LogLevel.INFO,
"ApplyBan",
`Banning ${member.userId} in ${roomId} for: ${reason}`,
roomId,
);

await this.client.sendMessage(this.managementRoomId, {
msgtype: "m.text",
body: `Banning ${member.userId} in ${roomId} for: ${reason}.`,
format: "org.matrix.custom.html",
formatted_body: `Banning <span data-mx-spoiler>${member.userId}</span> in ${roomId} for: ${reason}.`,
});

if (!this.config.noop) {
if (this.moderators.checkMembership(member.userId)) {
Expand Down
23 changes: 12 additions & 11 deletions src/commands/KickCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,25 @@ export async function execKickCommand(roomId: string, event: any, mjolnir: Mjoln
const target = member.membershipFor;

if (kickRule.test(target)) {
await mjolnir.managementRoomOutput.logMessage(
LogLevel.DEBUG,
"KickCommand",
`Removing ${target} in ${protectedRoomId}`,
protectedRoomId,
);
await mjolnir.client.sendMessage(mjolnir.managementRoomId, {
msgtype: "m.text",
body: `Removing ${target} in ${protectedRoomId}.`,
format: "org.matrix.custom.html",
formatted_body: `Removing <span data-mx-spoiler>${target}</span> in ${protectedRoomId}.`,
});

if (!mjolnir.config.noop) {
try {
await mjolnir.taskQueue.push(async () => {
return mjolnir.client.kickUser(target, protectedRoomId, reason);
});
} catch (e) {
await mjolnir.managementRoomOutput.logMessage(
LogLevel.WARN,
"KickCommand",
`An error happened while trying to kick ${target}: ${e}`,
);
await mjolnir.client.sendMessage(mjolnir.managementRoomId, {
msgtype: "m.text",
body: `An error happened while trying to kick ${target}: ${e}`,
format: "org.matrix.custom.html",
formatted_body: `An error happened while trying to kick <span data-mx-spoiler>${target}</span>: ${e}.`,
});
}
} else {
await mjolnir.managementRoomOutput.logMessage(
Expand Down
3 changes: 2 additions & 1 deletion src/commands/SuspendCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export async function execSuspendCommand(roomId: string, event: any, mjolnir: Mj

await mjolnir.suspendSynapseUser(target);
const msg = `User ${target} has been suspended.`;
const confirmation = RichReply.createFor(roomId, event, msg, msg);
const htmlMsg = `User <span data-mx-spoiler>${target}</span> has been suspended.`;
const confirmation = RichReply.createFor(roomId, event, msg, htmlMsg);
confirmation["msgtype"] = "m.notice";
await mjolnir.client.sendMessage(roomId, confirmation);
await mjolnir.client.unstableApis.addReactionToEvent(roomId, event["event_id"], "✅");
Expand Down
12 changes: 6 additions & 6 deletions src/protections/BasicFlooding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ export class BasicFlooding extends Protection {
}

if (messageCount >= this.settings.maxPerMinute.value) {
await mjolnir.managementRoomOutput.logMessage(
LogLevel.WARN,
"BasicFlooding",
`Banning ${event["sender"]} in ${roomId} for flooding (${messageCount} messages in the last minute)`,
roomId,
);
await mjolnir.client.sendMessage(mjolnir.managementRoomId, {
msgtype: "m.text",
body: `Banning ${event["sender"]} in ${roomId} for flooding (${messageCount} messages in the last minute)`,
format: "org.matrix.custom.html",
formatted_body: `Banning <span data-mx-spoiler>${event["sender"]}</span> in ${roomId} for flooding (${messageCount} messages in the last minute).`,
});
if (!mjolnir.config.noop) {
if (mjolnir.moderators.checkMembership(event["sender"])) {
mjolnir.managementRoomOutput.logMessage(
Expand Down
11 changes: 6 additions & 5 deletions src/protections/FirstMessageIsImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ export class FirstMessageIsImage extends Protection {
const isMedia =
msgtype === "m.image" || msgtype === "m.video" || formattedBody.toLowerCase().includes("<img");
if (isMedia && this.justJoined[roomId].includes(event["sender"])) {
await mjolnir.managementRoomOutput.logMessage(
LogLevel.WARN,
"FirstMessageIsImage",
`Banning ${event["sender"]} for posting an image as the first thing after joining in ${roomId}.`,
);
await mjolnir.client.sendMessage(mjolnir.managementRoomId, {
msgtype: "m.text",
body: `Banning ${event["sender"]} for posting an image as the first thing after joining in ${roomId}.`,
format: "org.matrix.custom.html",
formatted_body: `Banning <span data-mx-spoiler>${event["sender"]}</span> for posting an image as the first thing after joining in ${roomId}.`,
});
if (!mjolnir.config.noop) {
if (mjolnir.moderators.checkMembership(event["sender"])) {
await mjolnir.managementRoomOutput.logMessage(
Expand Down
25 changes: 13 additions & 12 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ async function botRedactUserMessagesIn(
}
});
} catch (error) {
await managementRoom.logMessage(
LogLevel.ERROR,
"utils#redactUserMessagesIn",
`Caught an error while trying to redact messages for ${userIdOrGlob} in ${targetRoomId}: ${error}`,
targetRoomId,
);
await client.sendMessage(managementRoom.managementRoomId, {
msgtype: "m.text",
body: `Caught an error while trying to redact messages for ${userIdOrGlob} in ${targetRoomId}: ${error}`,
format: "org.matrix.custom.html",
formatted_body: `Caught an error while trying to redact messages for <span data-mx-spoiler>${userIdOrGlob}</span> in ${targetRoomId}: ${error}`,
});
}
}
}
Expand Down Expand Up @@ -215,12 +215,13 @@ export async function redactUserMessagesIn(
"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 client.sendMessage(managementRoom.managementRoomId, {
msgtype: "m.text",
body: `Error using admin API to redact messages for user ${userIdOrGlob}, please check logs for more info - falling back to non-admin redaction process.`,
format: "org.matrix.custom.html",
formatted_body: `Error using admin API to redact messages for user <span data-mx-spoiler>${userIdOrGlob}</span>, please check logs for more info - falling
back to non-admin redaction process.`,
});
await botRedactUserMessagesIn(client, managementRoom, userIdOrGlob, filteredRooms, limit, noop);
}
} else {
Expand Down
Loading