Skip to content

Commit 3f9d0bc

Browse files
committed
wip
wip wip: added limit to the notificaton read options wip
1 parent 3498f6a commit 3f9d0bc

11 files changed

Lines changed: 251 additions & 33 deletions

File tree

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
}
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import CommandClear from './CommandClear';
2+
import CommandRead from './CommandRead';
3+
import CommandPolykey from '../../CommandPolykey';
4+
5+
class CommandInbox extends CommandPolykey {
6+
constructor(...args: ConstructorParameters<typeof CommandPolykey>) {
7+
super(...args);
8+
this.name('inbox');
9+
this.description('Notifications Inbox Operations');
10+
this.addCommand(new CommandClear(...args));
11+
this.addCommand(new CommandRead(...args));
12+
}
13+
}
14+
15+
export default CommandInbox;
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
}> = [];

src/notifications/inbox/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from './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;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import CommandClear from './CommandClear';
2+
import CommandRead from './CommandRead';
3+
import CommandPolykey from '../../CommandPolykey';
4+
5+
class CommandOutbox extends CommandPolykey {
6+
constructor(...args: ConstructorParameters<typeof CommandPolykey>) {
7+
super(...args);
8+
this.name('outbox');
9+
this.description('Notifications Outbox Operations');
10+
this.addCommand(new CommandClear(...args));
11+
this.addCommand(new CommandRead(...args));
12+
}
13+
}
14+
15+
export default CommandOutbox;
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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;

src/notifications/outbox/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from './CommandOutbox';

tests/nodes/claim.test.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,11 @@ describe('claim', () => {
9696
expect(stderr).toContain(remoteIdEncoded);
9797
});
9898
test('sends a gestalt invite (force invite)', async () => {
99-
await remoteNode.notificationsManager.sendNotification(localId, {
100-
type: 'GestaltInvite',
99+
await remoteNode.notificationsManager.sendNotification({
100+
nodeId: localId,
101+
data: {
102+
type: 'GestaltInvite',
103+
},
101104
});
102105
const { exitCode, stdout, stderr } = await testUtils.pkStdio(
103106
['nodes', 'claim', remoteIdEncoded, '--force-invite', '--format', 'json'],
@@ -114,8 +117,11 @@ describe('claim', () => {
114117
expect(stderr).toContain(nodesUtils.encodeNodeId(remoteId));
115118
});
116119
test('claims a node', async () => {
117-
await remoteNode.notificationsManager.sendNotification(localId, {
118-
type: 'GestaltInvite',
120+
await remoteNode.notificationsManager.sendNotification({
121+
nodeId: localId,
122+
data: {
123+
type: 'GestaltInvite',
124+
},
119125
});
120126
const { exitCode, stdout, stderr } = await testUtils.pkStdio(
121127
['nodes', 'claim', remoteIdEncoded, '--format', 'json'],

0 commit comments

Comments
 (0)