-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip wip: added limit to the notificaton read options wip
- Loading branch information
Showing
11 changed files
with
251 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import CommandClear from './CommandClear'; | ||
import CommandRead from './CommandRead'; | ||
import CommandPolykey from '../../CommandPolykey'; | ||
|
||
class CommandInbox extends CommandPolykey { | ||
constructor(...args: ConstructorParameters<typeof CommandPolykey>) { | ||
super(...args); | ||
this.name('inbox'); | ||
this.description('Notifications Inbox Operations'); | ||
this.addCommand(new CommandClear(...args)); | ||
this.addCommand(new CommandRead(...args)); | ||
} | ||
} | ||
|
||
export default CommandInbox; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './CommandInbox'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import type PolykeyClient from 'polykey/dist/PolykeyClient'; | ||
import CommandPolykey from '../../CommandPolykey'; | ||
import * as binUtils from '../../utils'; | ||
import * as binOptions from '../../utils/options'; | ||
import * as binProcessors from '../../utils/processors'; | ||
|
||
class CommandClear extends CommandPolykey { | ||
constructor(...args: ConstructorParameters<typeof CommandPolykey>) { | ||
super(...args); | ||
this.name('clear'); | ||
this.description('Clear Outbox Notifications'); | ||
this.addOption(binOptions.nodeId); | ||
this.addOption(binOptions.clientHost); | ||
this.addOption(binOptions.clientPort); | ||
this.action(async (options) => { | ||
const { default: PolykeyClient } = await import( | ||
'polykey/dist/PolykeyClient' | ||
); | ||
const clientOptions = await binProcessors.processClientOptions( | ||
options.nodePath, | ||
options.nodeId, | ||
options.clientHost, | ||
options.clientPort, | ||
this.fs, | ||
this.logger.getChild(binProcessors.processClientOptions.name), | ||
); | ||
const auth = await binProcessors.processAuthentication( | ||
options.passwordFile, | ||
this.fs, | ||
); | ||
|
||
let pkClient: PolykeyClient; | ||
this.exitHandlers.handlers.push(async () => { | ||
if (pkClient != null) await pkClient.stop(); | ||
}); | ||
try { | ||
pkClient = await PolykeyClient.createPolykeyClient({ | ||
nodeId: clientOptions.nodeId, | ||
host: clientOptions.clientHost, | ||
port: clientOptions.clientPort, | ||
options: { | ||
nodePath: options.nodePath, | ||
}, | ||
logger: this.logger.getChild(PolykeyClient.name), | ||
}); | ||
await binUtils.retryAuthentication( | ||
(auth) => | ||
pkClient.rpcClient.methods.notificationsOutboxClear({ | ||
metadata: auth, | ||
}), | ||
auth, | ||
); | ||
} finally { | ||
if (pkClient! != null) await pkClient.stop(); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
export default CommandClear; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import CommandClear from './CommandClear'; | ||
import CommandRead from './CommandRead'; | ||
import CommandPolykey from '../../CommandPolykey'; | ||
|
||
class CommandOutbox extends CommandPolykey { | ||
constructor(...args: ConstructorParameters<typeof CommandPolykey>) { | ||
super(...args); | ||
this.name('outbox'); | ||
this.description('Notifications Outbox Operations'); | ||
this.addCommand(new CommandClear(...args)); | ||
this.addCommand(new CommandRead(...args)); | ||
} | ||
} | ||
|
||
export default CommandOutbox; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import type { Notification } from 'polykey/dist/notifications/types'; | ||
import type PolykeyClient from 'polykey/dist/PolykeyClient'; | ||
import type { NotificationOutboxMessage } from 'polykey/dist/client/types'; | ||
import CommandPolykey from '../../CommandPolykey'; | ||
import * as binUtils from '../../utils'; | ||
import * as binOptions from '../../utils/options'; | ||
import * as binProcessors from '../../utils/processors'; | ||
|
||
class CommandRead extends CommandPolykey { | ||
constructor(...args: ConstructorParameters<typeof CommandPolykey>) { | ||
super(...args); | ||
this.name('read'); | ||
this.description('Display Outbox Notifications'); | ||
this.option( | ||
'-l, --limit [number]', | ||
'(optional) Number of notifications to read', | ||
); | ||
this.option( | ||
'-o, --order [order]', | ||
'(optional) Order to read notifications', | ||
'newest', | ||
); | ||
this.addOption(binOptions.nodeId); | ||
this.addOption(binOptions.clientHost); | ||
this.addOption(binOptions.clientPort); | ||
this.action(async (options) => { | ||
const { default: PolykeyClient } = await import( | ||
'polykey/dist/PolykeyClient' | ||
); | ||
const notificationsUtils = await import( | ||
'polykey/dist/notifications/utils' | ||
); | ||
const clientOptions = await binProcessors.processClientOptions( | ||
options.nodePath, | ||
options.nodeId, | ||
options.clientHost, | ||
options.clientPort, | ||
this.fs, | ||
this.logger.getChild(binProcessors.processClientOptions.name), | ||
); | ||
const meta = await binProcessors.processAuthentication( | ||
options.passwordFile, | ||
this.fs, | ||
); | ||
|
||
let pkClient: PolykeyClient; | ||
this.exitHandlers.handlers.push(async () => { | ||
if (pkClient != null) await pkClient.stop(); | ||
}); | ||
try { | ||
pkClient = await PolykeyClient.createPolykeyClient({ | ||
nodeId: clientOptions.nodeId, | ||
host: clientOptions.clientHost, | ||
port: clientOptions.clientPort, | ||
options: { | ||
nodePath: options.nodePath, | ||
}, | ||
logger: this.logger.getChild(PolykeyClient.name), | ||
}); | ||
const notificationReadMessages = await binUtils.retryAuthentication( | ||
async (auth) => { | ||
const response = | ||
await pkClient.rpcClient.methods.notificationsOutboxRead({ | ||
metadata: auth, | ||
limit: parseInt(options.limit), | ||
order: options.order === 'newest' ? 'desc' : 'asc', | ||
}); | ||
const notificationReadMessages: Array<{ | ||
notification: Notification; | ||
taskMetadata: NotificationOutboxMessage['taskMetadata']; | ||
}> = []; | ||
for await (const notificationMessage of response) { | ||
const notification = notificationsUtils.parseNotification( | ||
notificationMessage.notification, | ||
); | ||
notificationReadMessages.push({ | ||
notification, | ||
taskMetadata: notificationMessage.taskMetadata, | ||
}); | ||
} | ||
return notificationReadMessages; | ||
}, | ||
meta, | ||
); | ||
if (notificationReadMessages.length === 0) { | ||
process.stderr.write('No notifications pending\n'); | ||
} | ||
if (options.format === 'json') { | ||
process.stdout.write( | ||
binUtils.outputFormatter({ | ||
type: 'json', | ||
data: notificationReadMessages, | ||
}), | ||
); | ||
} else { | ||
for (const notificationReadMessage of notificationReadMessages) { | ||
process.stdout.write( | ||
binUtils.outputFormatter({ | ||
type: 'dict', | ||
data: { | ||
notificiation: notificationReadMessage.notification, | ||
taskMetadata: notificationReadMessage.taskMetadata, | ||
}, | ||
}), | ||
); | ||
} | ||
} | ||
} finally { | ||
if (pkClient! != null) await pkClient.stop(); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
export default CommandRead; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './CommandOutbox'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters