From 56e0c56eebea89ebc5751bb27b710c4918ce2daa Mon Sep 17 00:00:00 2001 From: Gnuxie <50846879+Gnuxie@users.noreply.github.com> Date: Wed, 11 Sep 2024 14:57:37 +0100 Subject: [PATCH] Use `@gnuxie/typescript-result`'s `expect` where applicable instead of throwing garbage. (#552) Part of https://github.com/the-draupnir-project/Draupnir/issues/547 * Update for MPS v1.3.0. * Use @gnuxie/typescript-result's `expect` method where applicable. * Update to @the-draupnir-project/interface-manager@2.3.0. --- package.json | 10 ++--- src/DraupnirBotMode.ts | 24 ++++------ src/appservice/AppService.ts | 43 ++++++++---------- src/appservice/AppServiceDraupnirManager.ts | 2 +- src/commands/StatusCommand.tsx | 4 +- yarn.lock | 50 +++++++++++---------- 6 files changed, 61 insertions(+), 72 deletions(-) diff --git a/package.json b/package.json index 15076839..558892d0 100644 --- a/package.json +++ b/package.json @@ -52,12 +52,12 @@ "typescript-formatter": "^7.2" }, "dependencies": { - "@gnuxie/typescript-result": "^0.2.0", + "@gnuxie/typescript-result": "^1.0.0", "@sentry/node": "^7.17.2", "@sentry/tracing": "^7.17.2", "@sinclair/typebox": "0.32.34", - "@the-draupnir-project/interface-manager": "2.2.0", - "@the-draupnir-project/matrix-basic-types": "^0.1.1", + "@the-draupnir-project/interface-manager": "^2.3.0", + "@the-draupnir-project/matrix-basic-types": "^0.2.0", "await-lock": "^2.2.2", "better-sqlite3": "^9.4.3", "body-parser": "^1.20.2", @@ -69,8 +69,8 @@ "js-yaml": "^4.1.0", "jsdom": "^24.0.0", "matrix-appservice-bridge": "^9.0.1", - "matrix-protection-suite": "npm:@gnuxie/matrix-protection-suite@1.2.0", - "matrix-protection-suite-for-matrix-bot-sdk": "npm:@gnuxie/matrix-protection-suite-for-matrix-bot-sdk@1.2.1", + "matrix-protection-suite": "npm:@gnuxie/matrix-protection-suite@1.3.0", + "matrix-protection-suite-for-matrix-bot-sdk": "npm:@gnuxie/matrix-protection-suite-for-matrix-bot-sdk@1.3.0", "parse-duration": "^1.0.2", "pg": "^8.8.0", "shell-quote": "^1.7.3", diff --git a/src/DraupnirBotMode.ts b/src/DraupnirBotMode.ts index 11016fb1..7d9b6b76 100644 --- a/src/DraupnirBotMode.ts +++ b/src/DraupnirBotMode.ts @@ -9,7 +9,6 @@ // import { - isError, StandardClientsInRoomMap, DefaultEventDecoder, setGlobalLoggerProvider, @@ -70,16 +69,13 @@ export async function makeDraupnirBotModeFromConfig( const configManagementRoomReference = MatrixRoomReference.fromRoomIDOrAlias( config.managementRoom ); - const managementRoom = await resolveRoomReferenceSafe( - client, - configManagementRoomReference - ); - if (isError(managementRoom)) { - throw managementRoom.error; - } + const managementRoom = ( + await resolveRoomReferenceSafe(client, configManagementRoomReference) + ).expect("Unable to resolve Draupnir's management room"); + await client.joinRoom( - managementRoom.ok.toRoomIDOrAlias(), - managementRoom.ok.getViaServers() + managementRoom.toRoomIDOrAlias(), + managementRoom.getViaServers() ); const clientsInRoomMap = new StandardClientsInRoomMap(); const clientProvider = async (userID: StringUserID) => { @@ -106,13 +102,9 @@ export async function makeDraupnirBotModeFromConfig( ); const draupnir = await draupnirFactory.makeDraupnir( clientUserId, - managementRoom.ok, + managementRoom, config ); - if (isError(draupnir)) { - const error = draupnir.error; - throw new Error(`Unable to create Draupnir: ${error.message}`); - } matrixEmitter.on("room.invite", (roomID, event) => { clientsInRoomMap.handleTimelineEvent(roomID, event); }); @@ -120,5 +112,5 @@ export async function makeDraupnirBotModeFromConfig( roomStateManagerFactory.handleTimelineEvent(roomID, event); clientsInRoomMap.handleTimelineEvent(roomID, event); }); - return draupnir.ok; + return draupnir.expect("Unable to create Draupnir"); } diff --git a/src/appservice/AppService.ts b/src/appservice/AppService.ts index f343027c..8568d8c2 100644 --- a/src/appservice/AppService.ts +++ b/src/appservice/AppService.ts @@ -134,13 +134,9 @@ export class MjolnirAppService { return parseResult.ok; } })(); - const accessControlRoom = await resolveRoomReferenceSafe( - bridge.getBot().getClient(), - adminRoom - ); - if (isError(accessControlRoom)) { - throw accessControlRoom.error; - } + const accessControlRoom = ( + await resolveRoomReferenceSafe(bridge.getBot().getClient(), adminRoom) + ).expect("Unable to resolve the admin room"); const clientsInRoomMap = new StandardClientsInRoomMap(); const clientProvider = async (clientUserID: StringUserID) => bridge.getIntent(clientUserID).matrixClient; @@ -154,26 +150,23 @@ export class MjolnirAppService { eventDecoder ); const botUserID = bridge.getBot().getUserId() as StringUserID; - const clientRooms = await clientsInRoomMap.makeClientRooms( - botUserID, - async () => joinedRoomsSafe(bridge.getBot().getClient()) - ); - if (isError(clientRooms)) { - throw clientRooms.error; - } + ( + await clientsInRoomMap.makeClientRooms(botUserID, async () => + joinedRoomsSafe(bridge.getBot().getClient()) + ) + ).expect("Unable to initialize client rooms for the appservice bot user"); const botRoomJoiner = clientCapabilityFactory .makeClientPlatform(botUserID, bridge.getBot().getClient()) .toRoomJoiner(); const appserviceBotPolicyRoomManager = await roomStateManagerFactory.getPolicyRoomManager(botUserID); - const accessControl = await AccessControl.setupAccessControlForRoom( - accessControlRoom.ok, - appserviceBotPolicyRoomManager, - botRoomJoiner - ); - if (isError(accessControl)) { - throw accessControl.error; - } + const accessControl = ( + await AccessControl.setupAccessControlForRoom( + accessControlRoom, + appserviceBotPolicyRoomManager, + botRoomJoiner + ) + ).expect("Unable to setup access control for the appservice"); // Activate /metrics endpoint for Prometheus // This should happen automatically but in testing this didn't happen in the docker image @@ -192,7 +185,7 @@ export class MjolnirAppService { serverName, dataStore, bridge, - accessControl.ok, + accessControl, roomStateManagerFactory, clientCapabilityFactory, clientProvider, @@ -202,14 +195,14 @@ export class MjolnirAppService { config, bridge, mjolnirManager, - accessControl.ok, + accessControl, dataStore, eventDecoder, roomStateManagerFactory, clientCapabilityFactory, clientsInRoomMap, prometheus, - accessControlRoom.ok.toRoomIDOrAlias(), + accessControlRoom.toRoomIDOrAlias(), botUserID ); bridge.opts.controller = { diff --git a/src/appservice/AppServiceDraupnirManager.ts b/src/appservice/AppServiceDraupnirManager.ts index de37c2fe..bcd3d0f0 100644 --- a/src/appservice/AppServiceDraupnirManager.ts +++ b/src/appservice/AppServiceDraupnirManager.ts @@ -396,7 +396,7 @@ async function createFirstList( { name: `${draupnirOwnerID}'s policy room` } ); if (isError(policyRoom)) { - throw policyRoom.error; + return policyRoom; } const addRoomResult = await draupnir.protectedRoomsSet.protectedRoomsManager.addRoom( diff --git a/src/commands/StatusCommand.tsx b/src/commands/StatusCommand.tsx index bf6918c0..5f3d7245 100644 --- a/src/commands/StatusCommand.tsx +++ b/src/commands/StatusCommand.tsx @@ -61,7 +61,9 @@ export async function listInfo( ); return issuerResults.map((result) => { if (isError(result)) { - throw result.error; + throw result.expect( + "We should be able to get the issuers for all of the watched lits." + ); } const revision = result.ok.currentRevision; const associatedProfile = watchedListProfiles.find( diff --git a/yarn.lock b/yarn.lock index b84984a3..47b779b5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -105,10 +105,10 @@ resolved "https://registry.yarnpkg.com/@gnuxie/super-cool-stream/-/super-cool-stream-0.2.1.tgz#3e7d964f3861f534a207709c88c4acea194ae41f" integrity sha512-Sk6MXHTAn3GVRk/EkF9dt514OB/tdmesAJnOwygU1s3YoHcxNkf5t9p0U6BfZt7dYSuqEnMnc/XH5PCVsbrjkA== -"@gnuxie/typescript-result@^0.2.0": - version "0.2.0" - resolved "https://registry.yarnpkg.com/@gnuxie/typescript-result/-/typescript-result-0.2.0.tgz#870e29d079341c32a7cae7e0ee77ffdd09aad60f" - integrity sha512-V5VZUpl21fRNTBHqdigWVkkXhoue/6MVfpWLBOMVgi5unR4ePoXjzNXc9diNmnmDe+mrQfXGPT8LiB18Cc+0Ig== +"@gnuxie/typescript-result@^1.0.0": + version "1.0.0" + resolved "https://registry.yarnpkg.com/@gnuxie/typescript-result/-/typescript-result-1.0.0.tgz#d96ae02123261e42c03b2046e78d0cd6c5a24fb4" + integrity sha512-evHf0XIR2wrpc8iDfQKaU+9jwrutjbDfnjmZOLij+8aFKjpOi4oSzaUfUBv89WyvEW0XtUZPUQpSe5KiE9t1ng== "@humanwhocodes/module-importer@^1.0.1": version "1.0.1" @@ -256,20 +256,20 @@ resolved "https://registry.yarnpkg.com/@sinclair/typebox/-/typebox-0.27.8.tgz#6667fac16c436b5434a387a34dedb013198f6e6e" integrity sha512-+Fj43pSMwJs4KRrH/938Uf+uAELIgVBmQzg/q1YG10djyfA3TnrU8N8XzqCh/okZdszqBQTZf96idMfE5lnwTA== -"@the-draupnir-project/interface-manager@2.2.0": - version "2.2.0" - resolved "https://registry.yarnpkg.com/@the-draupnir-project/interface-manager/-/interface-manager-2.2.0.tgz#2326aca4ccbf0b0ed743efb3a20222a5d78ab23e" - integrity sha512-ycNBKT78RsRhU+HfHe3gjgtQY2pCn7Umw8uAJBHGzLFpXwk2E9kmv6dGbtGubINjsFdkacr7lVxZwm69K4bSUg== +"@the-draupnir-project/interface-manager@^2.3.0": + version "2.3.0" + resolved "https://registry.yarnpkg.com/@the-draupnir-project/interface-manager/-/interface-manager-2.3.0.tgz#d09b69334d73b10856853962f25a1526a38a0b17" + integrity sha512-DwgyJXoJt53r4B7sMAznEsl5QRshFRBBLZ86vVzguJKDo0XrJVrguPY0IegI8YeaeY1VyPJDkSN7CkcQ2+iDig== dependencies: "@gnuxie/super-cool-stream" "^0.2.1" - "@gnuxie/typescript-result" "^0.2.0" + "@gnuxie/typescript-result" "^1.0.0" -"@the-draupnir-project/matrix-basic-types@^0.1.1": - version "0.1.1" - resolved "https://registry.yarnpkg.com/@the-draupnir-project/matrix-basic-types/-/matrix-basic-types-0.1.1.tgz#27a131e2f92d6d25b377f7339177d8ac204d95a2" - integrity sha512-g5CDAdQMe1dxbVlzPvrrPPluZCFKaBqJ2iAarspGHkr0V3YBBCdz9v3Yb6TY8e2d/eXz5gSWDKzeZElWD33oeA== +"@the-draupnir-project/matrix-basic-types@^0.2.0": + version "0.2.0" + resolved "https://registry.yarnpkg.com/@the-draupnir-project/matrix-basic-types/-/matrix-basic-types-0.2.0.tgz#21502e4b9c0ad7b1cce077dc7d5ddefd768d0b42" + integrity sha512-plYd/Oi9/JPo+C5ga2D/8L41St+QpdfMwDjE3LuFnjF7TdmxEIw4rx/AFMqZk3KTMbukLQDQDCQF1ZFYmKI6WA== dependencies: - "@gnuxie/typescript-result" "^0.2.0" + "@gnuxie/typescript-result" "^1.0.0" glob-to-regexp "^0.4.1" "@tsconfig/node10@^1.0.7": @@ -2526,17 +2526,19 @@ matrix-appservice@^2.0.0: request-promise "^4.2.6" sanitize-html "^2.8.0" -"matrix-protection-suite-for-matrix-bot-sdk@npm:@gnuxie/matrix-protection-suite-for-matrix-bot-sdk@1.2.1": - version "1.2.1" - resolved "https://registry.yarnpkg.com/@gnuxie/matrix-protection-suite-for-matrix-bot-sdk/-/matrix-protection-suite-for-matrix-bot-sdk-1.2.1.tgz#abbfb08abba5dcec6c5affd775a0577e35aa3c31" - integrity sha512-EurAFV2gvWV1RZnSIflVW2n0tSh9nJRyXL09zDTzy21Z6l6UvVyvCZ06uvizrqOfT7Ns9S5/N1wsW7wxlkIjfg== - -"matrix-protection-suite@npm:@gnuxie/matrix-protection-suite@1.2.0": - version "1.2.0" - resolved "https://registry.yarnpkg.com/@gnuxie/matrix-protection-suite/-/matrix-protection-suite-1.2.0.tgz#bbdf1df56ea18118e8cfefe21463e929ba5df484" - integrity sha512-5CWGu7Qu5XjatfvQblMbYsv6c3rqN1bw6mS/AGaB1mOYBWftbR30nUMMkMQ3jzWjptRIJdpX2I7VOnATr8B6bA== +"matrix-protection-suite-for-matrix-bot-sdk@npm:@gnuxie/matrix-protection-suite-for-matrix-bot-sdk@1.3.0": + version "1.3.0" + resolved "https://registry.yarnpkg.com/@gnuxie/matrix-protection-suite-for-matrix-bot-sdk/-/matrix-protection-suite-for-matrix-bot-sdk-1.3.0.tgz#2efc303e9e43787e1eee7624cec9b37bfe954f4b" + integrity sha512-6tRMml+1+c4dBLqXKCWwpaESVtnDFQO6dC/AqTQCvij610bp5dQQFIdraGyKS+XNj7bm2AEJK/VAk6xQusmz+Q== dependencies: - "@gnuxie/typescript-result" "^0.2.0" + "@gnuxie/typescript-result" "^1.0.0" + +"matrix-protection-suite@npm:@gnuxie/matrix-protection-suite@1.3.0": + version "1.3.0" + resolved "https://registry.yarnpkg.com/@gnuxie/matrix-protection-suite/-/matrix-protection-suite-1.3.0.tgz#b6df1352b6dd62876690444ba5a8257aa9f1cb52" + integrity sha512-Gn6OeHC92PNcUQH4azN1rK12aywqcOey5dXsJEK00a7PZDseZE2EkATmhjABfptmSNTb/WRqkOi+xmdFi9bLrA== + dependencies: + "@gnuxie/typescript-result" "^1.0.0" await-lock "^2.2.2" crypto-js "^4.2.0" glob-to-regexp "^0.4.1"