Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
wip

wip: added limit to the notificaton read options

wip
  • Loading branch information
amydevs committed May 2, 2024
1 parent 3498f6a commit 3f9d0bc
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 33 deletions.
8 changes: 4 additions & 4 deletions src/notifications/CommandNotifications.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import CommandClear from './CommandClear';
import CommandRead from './CommandRead';
import CommandInbox from './inbox';
import CommandOutbox from './outbox';
import CommandSend from './CommandSend';
import CommandPolykey from '../CommandPolykey';

Expand All @@ -8,8 +8,8 @@ class CommandNotifications extends CommandPolykey {
super(...args);
this.name('notifications');
this.description('Notifications Operations');
this.addCommand(new CommandClear(...args));
this.addCommand(new CommandRead(...args));
this.addCommand(new CommandInbox(...args));
this.addCommand(new CommandOutbox(...args));
this.addCommand(new CommandSend(...args));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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';
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 all Notifications');
this.description('Clear Inbox Notifications');
this.addOption(binOptions.nodeId);
this.addOption(binOptions.clientHost);
this.addOption(binOptions.clientPort);
Expand Down Expand Up @@ -45,7 +45,7 @@ class CommandClear extends CommandPolykey {
});
await binUtils.retryAuthentication(
(auth) =>
pkClient.rpcClient.methods.notificationsClear({
pkClient.rpcClient.methods.notificationsInboxClear({
metadata: auth,
}),
auth,
Expand Down
15 changes: 15 additions & 0 deletions src/notifications/inbox/CommandInbox.ts
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;
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import type { Notification } from 'polykey/dist/notifications/types';
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';
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 Notifications');
this.description('Display Inbox Notifications');
this.option(
'-u, --unread',
'(optional) Flag to only display unread notifications',
);
this.option(
'-n, --number [number]',
'-l, --limit [number]',
'(optional) Number of notifications to read',
'all',
);
this.option(
'-o, --order [order]',
Expand Down Expand Up @@ -63,14 +62,13 @@ class CommandRead extends CommandPolykey {
});
const notificationReadMessages = await binUtils.retryAuthentication(
async (auth) => {
const response = await pkClient.rpcClient.methods.notificationsRead(
{
const response =
await pkClient.rpcClient.methods.notificationsInboxRead({
metadata: auth,
unread: options.unread,
number: options.number,
order: options.order,
},
);
limit: parseInt(options.limit),
order: options.order === 'newest' ? 'desc' : 'asc',
});
const notificationReadMessages: Array<{
notification: Notification;
}> = [];
Expand Down
1 change: 1 addition & 0 deletions src/notifications/inbox/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './CommandInbox';
60 changes: 60 additions & 0 deletions src/notifications/outbox/CommandClear.ts
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;
15 changes: 15 additions & 0 deletions src/notifications/outbox/CommandOutbox.ts
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;
115 changes: 115 additions & 0 deletions src/notifications/outbox/CommandRead.ts
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;
1 change: 1 addition & 0 deletions src/notifications/outbox/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './CommandOutbox';
14 changes: 10 additions & 4 deletions tests/nodes/claim.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,11 @@ describe('claim', () => {
expect(stderr).toContain(remoteIdEncoded);
});
test('sends a gestalt invite (force invite)', async () => {
await remoteNode.notificationsManager.sendNotification(localId, {
type: 'GestaltInvite',
await remoteNode.notificationsManager.sendNotification({
nodeId: localId,
data: {
type: 'GestaltInvite',
},
});
const { exitCode, stdout, stderr } = await testUtils.pkStdio(
['nodes', 'claim', remoteIdEncoded, '--force-invite', '--format', 'json'],
Expand All @@ -114,8 +117,11 @@ describe('claim', () => {
expect(stderr).toContain(nodesUtils.encodeNodeId(remoteId));
});
test('claims a node', async () => {
await remoteNode.notificationsManager.sendNotification(localId, {
type: 'GestaltInvite',
await remoteNode.notificationsManager.sendNotification({
nodeId: localId,
data: {
type: 'GestaltInvite',
},
});
const { exitCode, stdout, stderr } = await testUtils.pkStdio(
['nodes', 'claim', remoteIdEncoded, '--format', 'json'],
Expand Down
19 changes: 13 additions & 6 deletions tests/notifications/sendReadClear.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ describe('send/read/claim', () => {
expect(exitCode).toBe(0);
// Read notifications
({ exitCode, stdout } = await testUtils.pkExec(
['notifications', 'read', '--format', 'json'],
['notifications', 'inbox', 'read', '--format', 'json'],
{
env: {
PK_NODE_PATH: receiverAgentDir,
Expand Down Expand Up @@ -204,7 +204,7 @@ describe('send/read/claim', () => {
});
// Read only unread (none)
({ exitCode, stdout } = await testUtils.pkExec(
['notifications', 'read', '--unread', '--format', 'json'],
['notifications', 'inbox', 'read', '--unread', '--format', 'json'],
{
env: {
PK_NODE_PATH: receiverAgentDir,
Expand All @@ -218,7 +218,14 @@ describe('send/read/claim', () => {
expect(readNotificationMessages).toHaveLength(0);
// Read notifications on reverse order
({ exitCode, stdout } = await testUtils.pkExec(
['notifications', 'read', '--order=oldest', '--format', 'json'],
[
'notifications',
'inbox',
'read',
'--order=oldest',
'--format',
'json',
],
{
env: {
PK_NODE_PATH: receiverAgentDir,
Expand Down Expand Up @@ -259,7 +266,7 @@ describe('send/read/claim', () => {
});
// Read only one notification
({ exitCode, stdout } = await testUtils.pkExec(
['notifications', 'read', '--number=1', '--format', 'json'],
['notifications', 'inbox', 'read', '--limit=1', '--format', 'json'],
{
env: {
PK_NODE_PATH: receiverAgentDir,
Expand All @@ -281,7 +288,7 @@ describe('send/read/claim', () => {
isRead: true,
});
// Clear notifications
await testUtils.pkExec(['notifications', 'clear'], {
await testUtils.pkExec(['notifications', 'inbox', 'clear'], {
env: {
PK_NODE_PATH: receiverAgentDir,
PK_PASSWORD: receiverAgentPassword,
Expand All @@ -290,7 +297,7 @@ describe('send/read/claim', () => {
});
// Check there are no more notifications
({ exitCode, stdout } = await testUtils.pkExec(
['notifications', 'read', '--format', 'json'],
['notifications', 'inbox', 'read', '--format', 'json'],
{
env: {
PK_NODE_PATH: receiverAgentDir,
Expand Down

0 comments on commit 3f9d0bc

Please sign in to comment.