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 63f8a5b commit 5c70803
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 45 deletions.
26 changes: 16 additions & 10 deletions src/Mjolnir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class Mjolnir {
/**
* Members of the moderator room and others who should not be banned, ACL'd etc.
*/
public moderators: string[]
public moderators: string[];

/**
* Adds a listener to the client that will automatically accept invitations.
Expand Down Expand Up @@ -186,16 +186,21 @@ export class Mjolnir {
);
Mjolnir.addJoinOnInviteListener(mjolnir, client, config);

const memberEvents = await mjolnir.client.getRoomMembers(managementRoomId, undefined, ["join"], ["ban", "leave"])
let moderators: string[] = []
memberEvents.forEach(event => {
moderators.push(event.stateKey)
const server = event.stateKey.split(":")[1]
const memberEvents = await mjolnir.client.getRoomMembers(
managementRoomId,
undefined,
["join"],
["ban", "leave"],
);
let moderators: string[] = [];
memberEvents.forEach((event) => {
moderators.push(event.stateKey);
const server = event.stateKey.split(":")[1];
if (!moderators.includes(server)) {
moderators.push(server)
moderators.push(server);
}
})
mjolnir.moderators = moderators
});
mjolnir.moderators = moderators;

return mjolnir;
}
Expand Down Expand Up @@ -296,7 +301,8 @@ export class Mjolnir {
this.managementRoomOutput,
this.protectionManager,
config,
this.moderators);
this.moderators,
);
}

public get state(): string {
Expand Down
10 changes: 7 additions & 3 deletions src/ProtectedRoomsSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class ProtectedRoomsSet {
private readonly managementRoomOutput: ManagementRoomOutput,
private readonly protectionManager: ProtectionManager,
private readonly config: IConfig,
private readonly moderators: string[]
private readonly moderators: string[],
) {
for (const reason of this.config.automaticallyRedactForReasons) {
this.automaticRedactionReasons.push(new MatrixGlob(reason.toLowerCase()));
Expand Down Expand Up @@ -443,8 +443,12 @@ export class ProtectedRoomsSet {

if (!this.config.noop) {
if (this.moderators.includes(member.userId)) {
await this.managementRoomOutput.logMessage(LogLevel.WARN, "ApplyBan", `Attempted
to ban ${member.userId} but this is a member of the management room, skipping.`);
await this.managementRoomOutput.logMessage(
LogLevel.WARN,
"ApplyBan",
`Attempted
to ban ${member.userId} but this is a member of the management room, skipping.`,
);
continue;
}
await this.client.banUser(member.userId, roomId, memberAccess.rule!.reason);
Expand Down
20 changes: 10 additions & 10 deletions src/commands/CommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ import { execMakeRoomAdminCommand } from "./MakeRoomAdminCommand";
import { parse as tokenize } from "shell-quote";
import { execSinceCommand } from "./SinceCommand";
import { execSetupProtectedRoom } from "./SetupDecentralizedReportingCommand";
import {execSuspendCommand} from "./SuspendCommand";
import {execUnsuspendCommand} from "./UnsuspendCommand";
import {execIgnoreCommand, execListIgnoredCommand} from "./IgnoreCommand";
import { execSuspendCommand } from "./SuspendCommand";
import { execUnsuspendCommand } from "./UnsuspendCommand";
import { execIgnoreCommand, execListIgnoredCommand } from "./IgnoreCommand";

export const COMMAND_PREFIX = "!mjolnir";

Expand Down Expand Up @@ -140,12 +140,12 @@ export async function handleCommand(roomId: string, event: { content: { body: st
return await execMakeRoomAdminCommand(roomId, event, mjolnir, parts);
} else if (parts[1] === "suspend" && parts.length > 2) {
return await execSuspendCommand(roomId, event, mjolnir, parts);
} else if (parts[1] === 'unsuspend' && parts.length > 2) {
return await execUnsuspendCommand(roomId, event, mjolnir, parts)
} else if (parts[1] === 'ignore') {
return await execIgnoreCommand(roomId, event, mjolnir, parts)
} else if (parts[1] === 'ignored') {
return await execListIgnoredCommand(roomId, event, mjolnir, parts)
} else if (parts[1] === "unsuspend" && parts.length > 2) {
return await execUnsuspendCommand(roomId, event, mjolnir, parts);
} else if (parts[1] === "ignore") {
return await execIgnoreCommand(roomId, event, mjolnir, parts);
} else if (parts[1] === "ignored") {
return await execListIgnoredCommand(roomId, event, mjolnir, parts);
} else {
// Help menu
const menu =
Expand Down Expand Up @@ -193,7 +193,7 @@ export async function handleCommand(roomId: string, event: { content: { body: st
"!mjolnir unsuspend <user ID> - Unsuspend the specified user\n" +
"!mjolnir ignore <user ID/server name> - Add user to list of users/servers that cannot be banned/ACL'd. Note that this does not survive restart.\n" +
"mjolnir ignored - List currently ignored entities.\n" +
"!mjolnir help - This menu\n"
"!mjolnir help - This menu\n";
const html = `<b>Mjolnir help:</b><br><pre><code>${htmlEscape(menu)}</code></pre>`;
const text = `Mjolnir help:\n${menu}`;
const reply = RichReply.createFor(roomId, event, text, html);
Expand Down
19 changes: 11 additions & 8 deletions src/commands/IgnoreCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,24 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import {Mjolnir} from "../Mjolnir";
import {LogLevel, RichReply} from "@vector-im/matrix-bot-sdk";
import { Mjolnir } from "../Mjolnir";
import { LogLevel, RichReply } from "@vector-im/matrix-bot-sdk";

// !mjolnir ignore <user|server>
export async function execIgnoreCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) {
const target = parts[2];

await mjolnir.managementRoomOutput.logMessage(LogLevel.INFO, "IgnoreCommand", `Adding ${target} to internal moderator list.`);
mjolnir.moderators.push(target)
await mjolnir.client.unstableApis.addReactionToEvent(roomId, event['event_id'], '✅');
await mjolnir.managementRoomOutput.logMessage(
LogLevel.INFO,
"IgnoreCommand",
`Adding ${target} to internal moderator list.`,
);
mjolnir.moderators.push(target);
await mjolnir.client.unstableApis.addReactionToEvent(roomId, event["event_id"], "✅");
}

// !mjolnir ignored
export async function execListIgnoredCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) {

let html = "Ignored users:<ul>";
let text = "Ignored users:\n";

Expand All @@ -42,5 +45,5 @@ export async function execListIgnoredCommand(roomId: string, event: any, mjolnir
const reply = RichReply.createFor(roomId, event, text, html);
reply["msgtype"] = "m.notice";
await mjolnir.client.sendMessage(roomId, reply);
await mjolnir.client.unstableApis.addReactionToEvent(roomId, event['event_id'], '✅');
}
await mjolnir.client.unstableApis.addReactionToEvent(roomId, event["event_id"], "✅");
}
6 changes: 5 additions & 1 deletion src/commands/SinceCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,11 @@ async function execSinceCommandAux(
for (let join of recentJoins) {
try {
if (mjolnir.moderators.includes(join.userId)) {
await mjolnir.managementRoomOutput.logMessage(LogLevel.WARN, "SinceCommand", `Attempting to ban user ${join.userId} but this is a member of the management room, skipping.`);
await mjolnir.managementRoomOutput.logMessage(
LogLevel.WARN,
"SinceCommand",
`Attempting to ban user ${join.userId} but this is a member of the management room, skipping.`,
);
continue;
}
await mjolnir.client.banUser(join.userId, targetRoomId, reason);
Expand Down
10 changes: 7 additions & 3 deletions src/commands/UnbanBanCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,16 @@ export async function execBanCommand(roomId: string, event: any, mjolnir: Mjolni
if (!bits) return; // error already handled

const matcher = new MatrixGlob(bits.entity);
mjolnir.moderators.forEach(async(name) => {
mjolnir.moderators.forEach(async (name) => {
if (matcher.test(name)) {
await mjolnir.managementRoomOutput.logMessage(LogLevel.ERROR, "UnbanBanCommand", `The ban command ${bits.entity} matches user in moderation room ${name}, aborting command.`);
await mjolnir.managementRoomOutput.logMessage(
LogLevel.ERROR,
"UnbanBanCommand",
`The ban command ${bits.entity} matches user in moderation room ${name}, aborting command.`,
);
return;
}
})
});

await bits.list!.banEntity(bits.ruleType!, bits.entity, bits.reason);
await mjolnir.client.unstableApis.addReactionToEvent(roomId, event["event_id"], "✅");
Expand Down
8 changes: 6 additions & 2 deletions src/protections/BasicFlooding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,14 @@ export class BasicFlooding extends Protection {
);
if (!mjolnir.config.noop) {
if (mjolnir.moderators.includes(event["sender"])) {
mjolnir.managementRoomOutput.logMessage(LogLevel.WARN, "BasicFlooding", `Attempting to ban ${event["sender"]} but this is a member of the management room, aborting.`);
mjolnir.managementRoomOutput.logMessage(
LogLevel.WARN,
"BasicFlooding",
`Attempting to ban ${event["sender"]} but this is a member of the management room, aborting.`,
);
return;
}
await mjolnir.client.banUser(event['sender'], roomId, "spam");
await mjolnir.client.banUser(event["sender"], roomId, "spam");
} else {
await mjolnir.managementRoomOutput.logMessage(
LogLevel.WARN,
Expand Down
8 changes: 6 additions & 2 deletions src/protections/FirstMessageIsImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,14 @@ export class FirstMessageIsImage extends Protection {
);
if (!mjolnir.config.noop) {
if (mjolnir.moderators.includes(event["sender"])) {
await mjolnir.managementRoomOutput.logMessage(LogLevel.WARN, "FirstMessageIsImage", `Attempting to ban ${event["sender"]} but they are member of moderation room, aborting.`);
await mjolnir.managementRoomOutput.logMessage(
LogLevel.WARN,
"FirstMessageIsImage",
`Attempting to ban ${event["sender"]} but they are member of moderation room, aborting.`,
);
return;
}
await mjolnir.client.banUser(event['sender'], roomId, "spam");
await mjolnir.client.banUser(event["sender"], roomId, "spam");
} else {
await mjolnir.managementRoomOutput.logMessage(
LogLevel.WARN,
Expand Down
6 changes: 5 additions & 1 deletion src/protections/ProtectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,11 @@ export class ProtectionManager {
/* take no additional action, just print the below message to management room */
} else if (consequence.name === "ban") {
if (this.mjolnir.moderators.includes(sender)) {
await this.mjolnir.managementRoomOutput.logMessage(LogLevel.WARN, "ProtectionManager", `Attempting to ban ${sender} but this is a member of management room, skipping.`);
await this.mjolnir.managementRoomOutput.logMessage(
LogLevel.WARN,
"ProtectionManager",
`Attempting to ban ${sender} but this is a member of management room, skipping.`,
);
continue;
}
await this.mjolnir.client.banUser(sender, roomId, "abuse detected");
Expand Down
10 changes: 7 additions & 3 deletions src/protections/TrustedReporters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
import { Protection } from "./IProtection";
import { MXIDListProtectionSetting, NumberProtectionSetting } from "./ProtectionSettings";
import { Mjolnir } from "../Mjolnir";
import {LogLevel} from "@vector-im/matrix-bot-sdk";
import { LogLevel } from "@vector-im/matrix-bot-sdk";

const MAX_REPORTED_EVENT_BACKLOG = 20;

Expand Down Expand Up @@ -84,8 +84,12 @@ export class TrustedReporters extends Protection {
}
if (reporters.size === this.settings.banThreshold.value) {
if (mjolnir.moderators.includes(event.userId)) {
await mjolnir.managementRoomOutput.logMessage(LogLevel.WARN, "TrustedReporters", `Attempting to ban
${event.userId} but this is a member of the management room, aborting.`);
await mjolnir.managementRoomOutput.logMessage(
LogLevel.WARN,
"TrustedReporters",
`Attempting to ban
${event.userId} but this is a member of the management room, aborting.`,
);
return;
}
met.push("ban");
Expand Down
8 changes: 6 additions & 2 deletions src/report/ReportManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

import { PowerLevelAction } from "@vector-im/matrix-bot-sdk/lib/models/PowerLevelAction";
import {LogLevel, LogService, UserID} from "@vector-im/matrix-bot-sdk";
import { LogLevel, LogService, UserID } from "@vector-im/matrix-bot-sdk";
import { htmlToText } from "html-to-text";
import { htmlEscape } from "../utils";
import { JSDOM } from "jsdom";
Expand Down Expand Up @@ -771,7 +771,11 @@ class BanAccused implements IUIAction {
}
public async execute(manager: ReportManager, report: IReport): Promise<string | undefined> {
if (manager.mjolnir.moderators.includes(report.accused_id)) {
await manager.mjolnir.managementRoomOutput.logMessage(LogLevel.WARN, "ReportManager", `Attempting to ban ${report.accused_id} but this is a member of management room, aborting.`);
await manager.mjolnir.managementRoomOutput.logMessage(
LogLevel.WARN,
"ReportManager",
`Attempting to ban ${report.accused_id} but this is a member of management room, aborting.`,
);
return;
}
await manager.mjolnir.client.banUser(report.accused_id, report.room_id);
Expand Down

0 comments on commit 5c70803

Please sign in to comment.