|
| 1 | +import type PolykeyClient from 'polykey/dist/PolykeyClient'; |
| 2 | +import * as notificationsUtils from 'polykey/dist/notifications/utils'; |
| 3 | +import CommandPolykey from '../../CommandPolykey'; |
| 4 | +import * as binUtils from '../../utils'; |
| 5 | +import * as binOptions from '../../utils/options'; |
| 6 | +import * as binProcessors from '../../utils/processors'; |
| 7 | +import * as binParsers from '../../utils/parsers'; |
| 8 | + |
| 9 | +class CommandRemove extends CommandPolykey { |
| 10 | + constructor(...args: ConstructorParameters<typeof CommandPolykey>) { |
| 11 | + super(...args); |
| 12 | + this.name('remove'); |
| 13 | + this.description('Remove a Notification in the Inbox'); |
| 14 | + this.argument( |
| 15 | + '<notificationId>', |
| 16 | + 'Id of the notification to remove', |
| 17 | + binParsers.parseNotificationId, |
| 18 | + ); |
| 19 | + this.addOption(binOptions.nodeId); |
| 20 | + this.addOption(binOptions.clientHost); |
| 21 | + this.addOption(binOptions.clientPort); |
| 22 | + this.action(async (notificationId, options) => { |
| 23 | + const { default: PolykeyClient } = await import( |
| 24 | + 'polykey/dist/PolykeyClient' |
| 25 | + ); |
| 26 | + const clientOptions = await binProcessors.processClientOptions( |
| 27 | + options.nodePath, |
| 28 | + options.nodeId, |
| 29 | + options.clientHost, |
| 30 | + options.clientPort, |
| 31 | + this.fs, |
| 32 | + this.logger.getChild(binProcessors.processClientOptions.name), |
| 33 | + ); |
| 34 | + const auth = await binProcessors.processAuthentication( |
| 35 | + options.passwordFile, |
| 36 | + this.fs, |
| 37 | + ); |
| 38 | + |
| 39 | + let pkClient: PolykeyClient; |
| 40 | + this.exitHandlers.handlers.push(async () => { |
| 41 | + if (pkClient != null) await pkClient.stop(); |
| 42 | + }); |
| 43 | + try { |
| 44 | + pkClient = await PolykeyClient.createPolykeyClient({ |
| 45 | + nodeId: clientOptions.nodeId, |
| 46 | + host: clientOptions.clientHost, |
| 47 | + port: clientOptions.clientPort, |
| 48 | + options: { |
| 49 | + nodePath: options.nodePath, |
| 50 | + }, |
| 51 | + logger: this.logger.getChild(PolykeyClient.name), |
| 52 | + }); |
| 53 | + await binUtils.retryAuthentication( |
| 54 | + (auth) => |
| 55 | + pkClient.rpcClient.methods.notificationsInboxRemove({ |
| 56 | + notificationIdEncoded: |
| 57 | + notificationsUtils.encodeNotificationId(notificationId), |
| 58 | + metadata: auth, |
| 59 | + }), |
| 60 | + auth, |
| 61 | + ); |
| 62 | + } finally { |
| 63 | + if (pkClient! != null) await pkClient.stop(); |
| 64 | + } |
| 65 | + }); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +export default CommandRemove; |
0 commit comments