|
| 1 | +import type { Notification } from 'polykey/dist/notifications/types'; |
| 2 | +import type PolykeyClient from 'polykey/dist/PolykeyClient'; |
| 3 | +import type { NotificationOutboxMessage } from 'polykey/dist/client/types'; |
| 4 | +import CommandPolykey from '../../CommandPolykey'; |
| 5 | +import * as binUtils from '../../utils'; |
| 6 | +import * as binOptions from '../../utils/options'; |
| 7 | +import * as binProcessors from '../../utils/processors'; |
| 8 | + |
| 9 | +class CommandRead extends CommandPolykey { |
| 10 | + constructor(...args: ConstructorParameters<typeof CommandPolykey>) { |
| 11 | + super(...args); |
| 12 | + this.name('read'); |
| 13 | + this.description('Display Outbox Notifications'); |
| 14 | + this.option( |
| 15 | + '-l, --limit [number]', |
| 16 | + '(optional) Number of notifications to read', |
| 17 | + ); |
| 18 | + this.option( |
| 19 | + '-o, --order [order]', |
| 20 | + '(optional) Order to read notifications', |
| 21 | + 'newest', |
| 22 | + ); |
| 23 | + this.addOption(binOptions.nodeId); |
| 24 | + this.addOption(binOptions.clientHost); |
| 25 | + this.addOption(binOptions.clientPort); |
| 26 | + this.action(async (options) => { |
| 27 | + const { default: PolykeyClient } = await import( |
| 28 | + 'polykey/dist/PolykeyClient' |
| 29 | + ); |
| 30 | + const notificationsUtils = await import( |
| 31 | + 'polykey/dist/notifications/utils' |
| 32 | + ); |
| 33 | + const clientOptions = await binProcessors.processClientOptions( |
| 34 | + options.nodePath, |
| 35 | + options.nodeId, |
| 36 | + options.clientHost, |
| 37 | + options.clientPort, |
| 38 | + this.fs, |
| 39 | + this.logger.getChild(binProcessors.processClientOptions.name), |
| 40 | + ); |
| 41 | + const meta = await binProcessors.processAuthentication( |
| 42 | + options.passwordFile, |
| 43 | + this.fs, |
| 44 | + ); |
| 45 | + |
| 46 | + let pkClient: PolykeyClient; |
| 47 | + this.exitHandlers.handlers.push(async () => { |
| 48 | + if (pkClient != null) await pkClient.stop(); |
| 49 | + }); |
| 50 | + try { |
| 51 | + pkClient = await PolykeyClient.createPolykeyClient({ |
| 52 | + nodeId: clientOptions.nodeId, |
| 53 | + host: clientOptions.clientHost, |
| 54 | + port: clientOptions.clientPort, |
| 55 | + options: { |
| 56 | + nodePath: options.nodePath, |
| 57 | + }, |
| 58 | + logger: this.logger.getChild(PolykeyClient.name), |
| 59 | + }); |
| 60 | + const notificationReadMessages = await binUtils.retryAuthentication( |
| 61 | + async (auth) => { |
| 62 | + const response = |
| 63 | + await pkClient.rpcClient.methods.notificationsOutboxRead({ |
| 64 | + metadata: auth, |
| 65 | + limit: parseInt(options.limit), |
| 66 | + order: options.order === 'newest' ? 'desc' : 'asc', |
| 67 | + }); |
| 68 | + const notificationReadMessages: Array<{ |
| 69 | + notification: Notification; |
| 70 | + taskMetadata: NotificationOutboxMessage['taskMetadata']; |
| 71 | + }> = []; |
| 72 | + for await (const notificationMessage of response) { |
| 73 | + const notification = notificationsUtils.parseNotification( |
| 74 | + notificationMessage.notification, |
| 75 | + ); |
| 76 | + notificationReadMessages.push({ |
| 77 | + notification, |
| 78 | + taskMetadata: notificationMessage.taskMetadata, |
| 79 | + }); |
| 80 | + } |
| 81 | + return notificationReadMessages; |
| 82 | + }, |
| 83 | + meta, |
| 84 | + ); |
| 85 | + if (notificationReadMessages.length === 0) { |
| 86 | + process.stderr.write('No notifications pending\n'); |
| 87 | + } |
| 88 | + if (options.format === 'json') { |
| 89 | + process.stdout.write( |
| 90 | + binUtils.outputFormatter({ |
| 91 | + type: 'json', |
| 92 | + data: notificationReadMessages, |
| 93 | + }), |
| 94 | + ); |
| 95 | + } else { |
| 96 | + for (const notificationReadMessage of notificationReadMessages) { |
| 97 | + process.stdout.write( |
| 98 | + binUtils.outputFormatter({ |
| 99 | + type: 'dict', |
| 100 | + data: { |
| 101 | + notificiation: notificationReadMessage.notification, |
| 102 | + taskMetadata: notificationReadMessage.taskMetadata, |
| 103 | + }, |
| 104 | + }), |
| 105 | + ); |
| 106 | + } |
| 107 | + } |
| 108 | + } finally { |
| 109 | + if (pkClient! != null) await pkClient.stop(); |
| 110 | + } |
| 111 | + }); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +export default CommandRead; |
0 commit comments