Skip to content

Commit 412d5fd

Browse files
authored
Merge pull request #180 from MatrixAI/feature-outbox
Notification Lifecycle Commands
2 parents e889d53 + 8335af8 commit 412d5fd

19 files changed

Lines changed: 740 additions & 52 deletions

npmDepsHash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sha256-pckkN6xZ+tj+srTB5iHCwuHb3EFv0KiTPvFIGTTy1nQ=
1+
sha256-z97XzEPI3Dwl+Ld26FdKVzkstLbTWRgLiKVT8Dh1uZY=

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149
"nexpect": "^0.6.0",
150150
"node-gyp-build": "^4.4.0",
151151
"nodemon": "^3.0.1",
152-
"polykey": "^1.2.3",
152+
"polykey": "^1.4.0",
153153
"prettier": "^3.0.0",
154154
"shelljs": "^0.8.5",
155155
"shx": "^0.3.4",

src/notifications/CommandNotifications.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import CommandClear from './CommandClear';
2-
import CommandRead from './CommandRead';
1+
import CommandInbox from './inbox';
2+
import CommandOutbox from './outbox';
33
import CommandSend from './CommandSend';
44
import CommandPolykey from '../CommandPolykey';
55

@@ -8,8 +8,8 @@ class CommandNotifications extends CommandPolykey {
88
super(...args);
99
this.name('notifications');
1010
this.description('Notifications Operations');
11-
this.addCommand(new CommandClear(...args));
12-
this.addCommand(new CommandRead(...args));
11+
this.addCommand(new CommandInbox(...args));
12+
this.addCommand(new CommandOutbox(...args));
1313
this.addCommand(new CommandSend(...args));
1414
}
1515
}

src/notifications/CommandSend.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ class CommandSend extends CommandPolykey {
1717
binParsers.parseNodeId,
1818
);
1919
this.argument('<message>', 'Message to send');
20+
this.option(
21+
'-r, --retries [number]',
22+
'(optional) Number of retries that should be attempted before giving up',
23+
);
2024
this.addOption(binOptions.nodeId);
2125
this.addOption(binOptions.clientHost);
2226
this.addOption(binOptions.clientPort);
@@ -52,12 +56,14 @@ class CommandSend extends CommandPolykey {
5256
},
5357
logger: this.logger.getChild(PolykeyClient.name),
5458
});
59+
const retries = parseInt(options.retries);
5560
await binUtils.retryAuthentication(
5661
(auth) =>
5762
pkClient.rpcClient.methods.notificationsSend({
5863
metadata: auth,
5964
nodeIdEncoded: nodesUtils.encodeNodeId(nodeId),
6065
message: message,
66+
retries: Number.isNaN(retries) ? undefined : retries,
6167
}),
6268
auth,
6369
);
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import type PolykeyClient from 'polykey/dist/PolykeyClient';
2-
import CommandPolykey from '../CommandPolykey';
3-
import * as binUtils from '../utils';
4-
import * as binOptions from '../utils/options';
5-
import * as binProcessors from '../utils/processors';
2+
import CommandPolykey from '../../CommandPolykey';
3+
import * as binUtils from '../../utils';
4+
import * as binOptions from '../../utils/options';
5+
import * as binProcessors from '../../utils/processors';
66

77
class CommandClear extends CommandPolykey {
88
constructor(...args: ConstructorParameters<typeof CommandPolykey>) {
99
super(...args);
1010
this.name('clear');
11-
this.description('Clear all Notifications');
11+
this.description('Clear Inbox Notifications');
1212
this.addOption(binOptions.nodeId);
1313
this.addOption(binOptions.clientHost);
1414
this.addOption(binOptions.clientPort);
@@ -45,7 +45,7 @@ class CommandClear extends CommandPolykey {
4545
});
4646
await binUtils.retryAuthentication(
4747
(auth) =>
48-
pkClient.rpcClient.methods.notificationsClear({
48+
pkClient.rpcClient.methods.notificationsInboxClear({
4949
metadata: auth,
5050
}),
5151
auth,
Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
import type { Notification } from 'polykey/dist/notifications/types';
22
import type PolykeyClient from 'polykey/dist/PolykeyClient';
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';
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';
77

88
class CommandRead extends CommandPolykey {
99
constructor(...args: ConstructorParameters<typeof CommandPolykey>) {
1010
super(...args);
1111
this.name('read');
12-
this.description('Display Notifications');
12+
this.description('Display Inbox Notifications');
1313
this.option(
1414
'-u, --unread',
1515
'(optional) Flag to only display unread notifications',
1616
);
1717
this.option(
18-
'-n, --number [number]',
18+
'-l, --limit [number]',
1919
'(optional) Number of notifications to read',
20-
'all',
2120
);
2221
this.option(
2322
'-o, --order [order]',
@@ -63,14 +62,13 @@ class CommandRead extends CommandPolykey {
6362
});
6463
const notificationReadMessages = await binUtils.retryAuthentication(
6564
async (auth) => {
66-
const response = await pkClient.rpcClient.methods.notificationsRead(
67-
{
65+
const response =
66+
await pkClient.rpcClient.methods.notificationsInboxRead({
6867
metadata: auth,
6968
unread: options.unread,
70-
number: options.number,
71-
order: options.order,
72-
},
73-
);
69+
limit: parseInt(options.limit),
70+
order: options.order === 'newest' ? 'desc' : 'asc',
71+
});
7472
const notificationReadMessages: Array<{
7573
notification: Notification;
7674
}> = [];
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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;

src/notifications/inbox/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import CommandClear from './CommandClear';
2+
import CommandRead from './CommandRead';
3+
import CommandRemove from './CommandRemove';
4+
import CommandPolykey from '../../CommandPolykey';
5+
6+
class CommandInbox extends CommandPolykey {
7+
constructor(...args: ConstructorParameters<typeof CommandPolykey>) {
8+
super(...args);
9+
this.name('inbox');
10+
this.description('Notifications Inbox Operations');
11+
this.addCommand(new CommandClear(...args));
12+
this.addCommand(new CommandRead(...args));
13+
this.addCommand(new CommandRemove(...args));
14+
}
15+
}
16+
17+
export default CommandInbox;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import type PolykeyClient from 'polykey/dist/PolykeyClient';
2+
import CommandPolykey from '../../CommandPolykey';
3+
import * as binUtils from '../../utils';
4+
import * as binOptions from '../../utils/options';
5+
import * as binProcessors from '../../utils/processors';
6+
7+
class CommandClear extends CommandPolykey {
8+
constructor(...args: ConstructorParameters<typeof CommandPolykey>) {
9+
super(...args);
10+
this.name('clear');
11+
this.description('Clear Outbox Notifications');
12+
this.addOption(binOptions.nodeId);
13+
this.addOption(binOptions.clientHost);
14+
this.addOption(binOptions.clientPort);
15+
this.action(async (options) => {
16+
const { default: PolykeyClient } = await import(
17+
'polykey/dist/PolykeyClient'
18+
);
19+
const clientOptions = await binProcessors.processClientOptions(
20+
options.nodePath,
21+
options.nodeId,
22+
options.clientHost,
23+
options.clientPort,
24+
this.fs,
25+
this.logger.getChild(binProcessors.processClientOptions.name),
26+
);
27+
const auth = await binProcessors.processAuthentication(
28+
options.passwordFile,
29+
this.fs,
30+
);
31+
32+
let pkClient: PolykeyClient;
33+
this.exitHandlers.handlers.push(async () => {
34+
if (pkClient != null) await pkClient.stop();
35+
});
36+
try {
37+
pkClient = await PolykeyClient.createPolykeyClient({
38+
nodeId: clientOptions.nodeId,
39+
host: clientOptions.clientHost,
40+
port: clientOptions.clientPort,
41+
options: {
42+
nodePath: options.nodePath,
43+
},
44+
logger: this.logger.getChild(PolykeyClient.name),
45+
});
46+
await binUtils.retryAuthentication(
47+
(auth) =>
48+
pkClient.rpcClient.methods.notificationsOutboxClear({
49+
metadata: auth,
50+
}),
51+
auth,
52+
);
53+
} finally {
54+
if (pkClient! != null) await pkClient.stop();
55+
}
56+
});
57+
}
58+
}
59+
60+
export default CommandClear;

0 commit comments

Comments
 (0)