Skip to content

Commit

Permalink
Status command can distinguish between protected and watched lists.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gnuxie committed Oct 19, 2022
1 parent bdb9b36 commit ddbeaca
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
9 changes: 9 additions & 0 deletions src/Mjolnir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,15 @@ export class Mjolnir {
this.reportPoller?.stop();
}

/**
* Rooms that mjolnir is configured to explicitly protect.
* Do not use to access all of the rooms that mjolnir protects.
* FIXME: In future ProtectedRoomsSet on this mjolnir should not be public and should also be accessed via a delegator method.
*/
public get explicitlyProtectedRooms(): string[] {
return this.protectedRoomsConfig.getExplicitlyProtectedRooms()
}

/**
* Explicitly protect this room, adding it to the account data.
* Should NOT be used to protect a room to implement e.g. `config.protectAllJoinedRooms`,
Expand Down
33 changes: 20 additions & 13 deletions src/commands/StatusCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { Mjolnir, STATE_CHECKING_PERMISSIONS, STATE_NOT_STARTED, STATE_RUNNING,
import { RichReply } from "matrix-bot-sdk";
import { htmlEscape, parseDuration } from "../utils";
import { HumanizeDurationLanguage, HumanizeDuration } from "humanize-duration-ts";
import PolicyList from "../models/PolicyList";

const HUMANIZE_LAG_SERVICE: HumanizeDurationLanguage = new HumanizeDurationLanguage();
const HUMANIZER: HumanizeDuration = new HumanizeDuration(HUMANIZE_LAG_SERVICE);
Expand Down Expand Up @@ -67,22 +68,28 @@ async function showMjolnirStatus(roomId: string, event: any, mjolnir: Mjolnir) {
break;
}

html += `<b>Protected rooms: </b> ${Object.keys(mjolnir.protectedRooms).length}<br/>`;
text += `Protected rooms: ${Object.keys(mjolnir.protectedRooms).length}\n`;
html += `<b>Protected rooms: </b> ${mjolnir.protectedRoomsTracker.getProtectedRooms().length}<br/>`;
text += `Protected rooms: ${mjolnir.protectedRoomsTracker.getProtectedRooms().length}\n`;

// Append list information
html += "<b>Subscribed ban lists:</b><br><ul>";
text += "Subscribed ban lists:\n";
for (const list of mjolnir.lists) {
const ruleInfo = `rules: ${list.serverRules.length} servers, ${list.userRules.length} users, ${list.roomRules.length} rooms`;
html += `<li>${htmlEscape(list.listShortcode)} @ <a href="${list.roomRef}">${list.roomId}</a> (${ruleInfo})</li>`;
text += `* ${list.listShortcode} @ ${list.roomRef} (${ruleInfo})\n`;
}
if (mjolnir.lists.length === 0) {
html += "<li><i>None</i></li>";
text += "* None\n";
const renderPolicyLists = (header: string, lists: PolicyList[]) => {
html += `<b>${header}:</b><br><ul>`;
text += `${header}:\n`;
for (const list of lists) {
const ruleInfo = `rules: ${list.serverRules.length} servers, ${list.userRules.length} users, ${list.roomRules.length} rooms`;
html += `<li>${htmlEscape(list.listShortcode)} @ <a href="${list.roomRef}">${list.roomId}</a> (${ruleInfo})</li>`;
text += `* ${list.listShortcode} @ ${list.roomRef} (${ruleInfo})\n`;
}
if (lists.length === 0) {
html += "<li><i>None</i></li>";
text += "* None\n";
}
html += "</ul>";
}
html += "</ul>";
const subscribedLists = mjolnir.lists.filter(list => !mjolnir.explicitlyProtectedRooms.includes(list.roomId));
renderPolicyLists("Subscribed policy lists", subscribedLists);
const subscribedAndProtectedLists = mjolnir.lists.filter(list => mjolnir.explicitlyProtectedRooms.includes(list.roomId));
renderPolicyLists("Subscribed and protected policy lists", subscribedAndProtectedLists);

const reply = RichReply.createFor(roomId, event, text, html);
reply["msgtype"] = "m.notice";
Expand Down

0 comments on commit ddbeaca

Please sign in to comment.