Print status at startup and show a log message.

Fixes https://github.com/the-draupnir-project/Draupnir/issues/349.
This commit is contained in:
gnuxie authored and Gnuxie committed 2024-04-16 18:31:12 +01:00
1 parent 06e966f227
commit 1a3666815c
3 files changed
+52 -28

No files matched your search

+18
View File
@@ -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<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);
+1 -1
View File
@@ -35,7 +35,7 @@ import {
StandardClientsInRoomMap,
DefaultEventDecoder,
setGlobalLoggerProvider,
RoomStateBackingStore
RoomStateBackingStore,
} from "matrix-protection-suite";
import {
BotSDKLogServiceLogger,
+33 -27
View File
@@ -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<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()));
@@ -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
);
}
});