From 1a3666815c4e3bf16dd61d950fd398f96b4b3672 Mon Sep 17 00:00:00 2001 From: gnuxie Date: Tue, 16 Apr 2024 18:26:58 +0100 Subject: [PATCH] Print status at startup and show a log message. Fixes https://github.com/the-draupnir-project/Draupnir/issues/349. --- src/Draupnir.ts | 18 ++++++++++ src/DraupnirBotMode.ts | 2 +- src/commands/StatusCommand.tsx | 60 +++++++++++++++++++--------------- 3 files changed, 52 insertions(+), 28 deletions(-) diff --git a/src/Draupnir.ts b/src/Draupnir.ts index 8855b1d3..6010c56d 100644 --- a/src/Draupnir.ts +++ b/src/Draupnir.ts @@ -42,6 +42,8 @@ import { ARGUMENT_PROMPT_LISTENER, DEFAUILT_ARGUMENT_PROMPT_LISTENER, makeListen import { RendererMessageCollector } from "./capabilities/RendererMessageCollector"; import { DraupnirRendererMessageCollector } from "./capabilities/DraupnirRendererMessageCollector"; import { renderProtectionFailedToStart } from "./protections/ProtectedRoomsSetRenderers"; +import { draupnirStatusInfo, renderStatusInfo } from "./commands/StatusCommand"; +import { renderMatrixAndSend } from "./commands/interface-manager/DeadDocumentMatrix"; const log = new Logger('Draupnir'); @@ -177,9 +179,25 @@ export class Draupnir implements Client { if (isError(managementRoomProtectResult)) { return managementRoomProtectResult; } + void Task(draupnir.startupComplete()); return Ok(draupnir); } + private async startupComplete(): Promise { + const statusInfo = await draupnirStatusInfo(this); + try { + await this.managementRoomOutput.logMessage(LogLevel.INFO, "Mjolnir@startup", "Startup complete. Now monitoring rooms.") + await renderMatrixAndSend( + renderStatusInfo(statusInfo), + this.managementRoomID, + undefined, + this.client + ) + } catch (ex) { + log.error(`Caught an error when trying to show status at startup`, ex); + } + } + public handleTimelineEvent(roomID: StringRoomID, event: RoomEvent): void { Task(this.joinOnInviteListener(roomID, event)); this.managementRoomMessageListener(roomID, event); diff --git a/src/DraupnirBotMode.ts b/src/DraupnirBotMode.ts index 2fde654b..fb048acd 100644 --- a/src/DraupnirBotMode.ts +++ b/src/DraupnirBotMode.ts @@ -35,7 +35,7 @@ import { StandardClientsInRoomMap, DefaultEventDecoder, setGlobalLoggerProvider, - RoomStateBackingStore + RoomStateBackingStore, } from "matrix-protection-suite"; import { BotSDKLogServiceLogger, diff --git a/src/commands/StatusCommand.tsx b/src/commands/StatusCommand.tsx index fd8910e5..aea7a489 100644 --- a/src/commands/StatusCommand.tsx +++ b/src/commands/StatusCommand.tsx @@ -35,6 +35,7 @@ import { tickCrossRenderer } from "./interface-manager/MatrixHelpRenderer"; import { renderMatrixAndSend } from "./interface-manager/DeadDocumentMatrix"; import { ActionResult, Ok, PolicyRoomRevision, PolicyRoomWatchProfile, PolicyRuleType, isError } from "matrix-protection-suite"; import { Draupnir } from "../Draupnir"; +import { DocumentNode } from "./interface-manager/DeadDocument"; defineInterfaceCommand({ designator: ["status"], @@ -81,7 +82,7 @@ export async function listInfo(draupnir: Draupnir): Promise { } // FIXME: need a shoutout to dependencies in here and NOTICE info. -async function draupnirStatusInfo(draupnir: Draupnir): Promise { +export async function draupnirStatusInfo(draupnir: Draupnir): Promise { const watchedListInfo = await listInfo(draupnir); const protectedWatchedLists = watchedListInfo.filter((info) => draupnir.protectedRoomsSet.isProtectedRoom(info.revision.room.toRoomIDOrAlias())); const unprotectedListProfiles = watchedListInfo.filter((info) => !draupnir.protectedRoomsSet.isProtectedRoom(info.revision.room.toRoomIDOrAlias())); @@ -94,39 +95,44 @@ async function draupnirStatusInfo(draupnir: Draupnir): Promise { } } +export function renderStatusInfo(info: StatusInfo): DocumentNode { + const renderPolicyLists = (header: string, lists: ListInfo[]) => { + const renderedLists = lists.map(list => { + return
  • + {list.revision.room.toRoomIDOrAlias()} + ({list.revision.shortcode ?? ''}) propagation: {list.watchedListProfile.propagation} + (rules: {list.revision.allRulesOfType(PolicyRuleType.Server).length} servers, {list.revision.allRulesOfType(PolicyRuleType.User).length} users, {list.revision.allRulesOfType(PolicyRuleType.Room).length} rooms) +
  • + }); + return + {header}
    +
      + {renderedLists.length === 0 ?
    • None
    • : renderedLists} +
    +
    + }; + return + Protected Rooms: {info.numberOfProtectedRooms}
    + {renderPolicyLists('Subscribed policy rooms', info.subscribedLists)} + {renderPolicyLists('Subscribed and protected policy rooms', info.subscribedAndProtectedLists)} + Version: {info.version}
    + Repository: {info.repository}
    +
    +} + defineMatrixInterfaceAdaptor({ interfaceCommand: findTableCommand("mjolnir", "status"), renderer: async function (this, client, commandRoomID, event, result: ActionResult): Promise { - const renderPolicyLists = (header: string, lists: ListInfo[]) => { - const renderedLists = lists.map(list => { - return
  • - {list.revision.room.toRoomIDOrAlias()} - ({list.revision.shortcode ?? ''}) propagation: {list.watchedListProfile.propagation} - (rules: {list.revision.allRulesOfType(PolicyRuleType.Server).length} servers, {list.revision.allRulesOfType(PolicyRuleType.User).length} users, {list.revision.allRulesOfType(PolicyRuleType.Room).length} rooms) -
  • - }); - return - {header}
    -
      - {renderedLists.length === 0 ?
    • None
    • : renderedLists} -
    -
    - }; if (isError(result)) { await tickCrossRenderer.call(this, client, commandRoomID, event, result); return; } const info = result.ok; - - await renderMatrixAndSend( - Protected Rooms: {info.numberOfProtectedRooms}
    - {renderPolicyLists('Subscribed policy rooms', info.subscribedLists)} - {renderPolicyLists('Subscribed and protected policy rooms', info.subscribedAndProtectedLists)} - Version: {info.version}
    - Repository: {info.repository}
    -
    , - commandRoomID, - event, - client); + await renderMatrixAndSend( + renderStatusInfo(info), + commandRoomID, + event, + client + ); } });