Skip to content

Commit

Permalink
Print status at startup and show a log message.
Browse files Browse the repository at this point in the history
Fixes #349.
  • Loading branch information
Gnuxie committed Apr 16, 2024
1 parent 06e966f commit 1a36668
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 28 deletions.
18 changes: 18 additions & 0 deletions src/Draupnir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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<void> {
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);
Expand Down
2 changes: 1 addition & 1 deletion src/DraupnirBotMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
StandardClientsInRoomMap,
DefaultEventDecoder,
setGlobalLoggerProvider,
RoomStateBackingStore
RoomStateBackingStore,
} from "matrix-protection-suite";
import {
BotSDKLogServiceLogger,
Expand Down
60 changes: 33 additions & 27 deletions src/commands/StatusCommand.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down Expand Up @@ -81,7 +82,7 @@ export async function listInfo(draupnir: Draupnir): Promise<ListInfo[]> {
}

// FIXME: need a shoutout to dependencies in here and NOTICE info.
async function draupnirStatusInfo(draupnir: Draupnir): Promise<StatusInfo> {
export async function draupnirStatusInfo(draupnir: Draupnir): Promise<StatusInfo> {
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()));
Expand All @@ -94,39 +95,44 @@ async function draupnirStatusInfo(draupnir: Draupnir): Promise<StatusInfo> {
}
}

export function renderStatusInfo(info: StatusInfo): DocumentNode {
const renderPolicyLists = (header: string, lists: ListInfo[]) => {
const renderedLists = lists.map(list => {
return <li>
<a href={list.revision.room.toPermalink()}>{list.revision.room.toRoomIDOrAlias()}</a> &#32;
({list.revision.shortcode ?? '<no shortcode>'}) propagation: {list.watchedListProfile.propagation} &#32;
(rules: {list.revision.allRulesOfType(PolicyRuleType.Server).length} servers, {list.revision.allRulesOfType(PolicyRuleType.User).length} users, {list.revision.allRulesOfType(PolicyRuleType.Room).length} rooms)
</li>
});
return <fragment>
<b>{header}</b><br/>
<ul>
{renderedLists.length === 0 ? <li><i>None</i></li> : renderedLists}
</ul>
</fragment>
};
return <root>
<b>Protected Rooms: </b>{info.numberOfProtectedRooms}<br/>
{renderPolicyLists('Subscribed policy rooms', info.subscribedLists)}
{renderPolicyLists('Subscribed and protected policy rooms', info.subscribedAndProtectedLists)}
<b>Version: </b><code>{info.version}</code><br/>
<b>Repository: </b><code>{info.repository}</code><br/>
</root>
}

defineMatrixInterfaceAdaptor({
interfaceCommand: findTableCommand("mjolnir", "status"),
renderer: async function (this, client, commandRoomID, event, result: ActionResult<StatusInfo>): Promise<void> {
const renderPolicyLists = (header: string, lists: ListInfo[]) => {
const renderedLists = lists.map(list => {
return <li>
<a href={list.revision.room.toPermalink()}>{list.revision.room.toRoomIDOrAlias()}</a> &#32;
({list.revision.shortcode ?? '<no shortcode>'}) propagation: {list.watchedListProfile.propagation} &#32;
(rules: {list.revision.allRulesOfType(PolicyRuleType.Server).length} servers, {list.revision.allRulesOfType(PolicyRuleType.User).length} users, {list.revision.allRulesOfType(PolicyRuleType.Room).length} rooms)
</li>
});
return <fragment>
<b>{header}</b><br/>
<ul>
{renderedLists.length === 0 ? <li><i>None</i></li> : renderedLists}
</ul>
</fragment>
};
if (isError(result)) {
await tickCrossRenderer.call(this, client, commandRoomID, event, result);
return;
}
const info = result.ok;

await renderMatrixAndSend(<root>
<b>Protected Rooms: </b>{info.numberOfProtectedRooms}<br/>
{renderPolicyLists('Subscribed policy rooms', info.subscribedLists)}
{renderPolicyLists('Subscribed and protected policy rooms', info.subscribedAndProtectedLists)}
<b>Version: </b><code>{info.version}</code><br/>
<b>Repository: </b><code>{info.repository}</code><br/>
</root>,
commandRoomID,
event,
client);
await renderMatrixAndSend(
renderStatusInfo(info),
commandRoomID,
event,
client
);
}
});

0 comments on commit 1a36668

Please sign in to comment.