Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backgrounding of Notifications Domain #695

Merged
merged 15 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/PolykeyAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ class PolykeyAgent {
acl,
db,
nodeManager,
taskManager,
keyRing,
logger: logger.getChild(NotificationsManager.name),
fresh,
Expand Down
1 change: 1 addition & 0 deletions src/bootstrap/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ async function bootstrapState({
acl,
db,
nodeManager,
taskManager,
keyRing,
logger: logger.getChild(NotificationsManager.name),
fresh,
Expand Down
24 changes: 18 additions & 6 deletions src/client/callers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ import nodesFind from './nodesFind';
import nodesGetAll from './nodesGetAll';
import nodesListConnections from './nodesListConnections';
import nodesPing from './nodesPing';
import notificationsClear from './notificationsClear';
import notificationsRead from './notificationsRead';
import notificationsInboxClear from './notificationsInboxClear';
import notificationsInboxRead from './notificationsInboxRead';
import notificationsInboxRemove from './notificationsInboxRemove';
import notificationsOutboxClear from './notificationsOutboxClear';
import notificationsOutboxRead from './notificationsOutboxRead';
import notificationsOutboxRemove from './notificationsOutboxRemove';
import notificationsSend from './notificationsSend';
import vaultsClone from './vaultsClone';
import vaultsCreate from './vaultsCreate';
Expand Down Expand Up @@ -122,8 +126,12 @@ const clientManifest = {
nodesGetAll,
nodesListConnections,
nodesPing,
notificationsClear,
notificationsRead,
notificationsInboxClear,
notificationsInboxRead,
notificationsInboxRemove,
notificationsOutboxClear,
notificationsOutboxRead,
notificationsOutboxRemove,
notificationsSend,
vaultsClone,
vaultsCreate,
Expand Down Expand Up @@ -198,8 +206,12 @@ export {
nodesGetAll,
nodesListConnections,
nodesPing,
notificationsClear,
notificationsRead,
notificationsInboxClear,
notificationsInboxRead,
notificationsInboxRemove,
notificationsOutboxClear,
notificationsOutboxRead,
notificationsOutboxRemove,
notificationsSend,
vaultsClone,
vaultsCreate,
Expand Down
12 changes: 0 additions & 12 deletions src/client/callers/notificationsClear.ts

This file was deleted.

12 changes: 12 additions & 0 deletions src/client/callers/notificationsInboxClear.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { HandlerTypes } from '@matrixai/rpc';
import type NotificationsInboxClear from '../handlers/NotificationsInboxClear';
import { UnaryCaller } from '@matrixai/rpc';

type CallerTypes = HandlerTypes<NotificationsInboxClear>;

const notificationsInboxClear = new UnaryCaller<
CallerTypes['input'],
CallerTypes['output']
>();

export default notificationsInboxClear;
12 changes: 12 additions & 0 deletions src/client/callers/notificationsInboxRead.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { HandlerTypes } from '@matrixai/rpc';
import type NotificationsInboxRead from '../handlers/NotificationsInboxRead';
import { ServerCaller } from '@matrixai/rpc';

type CallerTypes = HandlerTypes<NotificationsInboxRead>;

const notificationsInboxRead = new ServerCaller<
CallerTypes['input'],
CallerTypes['output']
>();

export default notificationsInboxRead;
12 changes: 12 additions & 0 deletions src/client/callers/notificationsInboxRemove.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { HandlerTypes } from '@matrixai/rpc';
import type NotificationsInboxRemove from '../handlers/NotificationsInboxRemove';
import { UnaryCaller } from '@matrixai/rpc';

type CallerTypes = HandlerTypes<NotificationsInboxRemove>;

const notificationsInboxRemove = new UnaryCaller<
CallerTypes['input'],
CallerTypes['output']
>();

export default notificationsInboxRemove;
12 changes: 12 additions & 0 deletions src/client/callers/notificationsOutboxClear.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { HandlerTypes } from '@matrixai/rpc';
import type NotificationsOutboxClear from '../handlers/NotificationsOutboxClear';
import { UnaryCaller } from '@matrixai/rpc';

type CallerTypes = HandlerTypes<NotificationsOutboxClear>;

const notificationsOutboxClear = new UnaryCaller<
CallerTypes['input'],
CallerTypes['output']
>();

export default notificationsOutboxClear;
12 changes: 12 additions & 0 deletions src/client/callers/notificationsOutboxRead.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { HandlerTypes } from '@matrixai/rpc';
import type NotificationsOutboxRead from '../handlers/NotificationsOutboxRead';
import { ServerCaller } from '@matrixai/rpc';

type CallerTypes = HandlerTypes<NotificationsOutboxRead>;

const notificationsOutboxRead = new ServerCaller<
CallerTypes['input'],
CallerTypes['output']
>();

export default notificationsOutboxRead;
12 changes: 12 additions & 0 deletions src/client/callers/notificationsOutboxRemove.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { HandlerTypes } from '@matrixai/rpc';
import type NotificationsOutboxRemove from '../handlers/NotificationsOutboxRemove';
import { UnaryCaller } from '@matrixai/rpc';

type CallerTypes = HandlerTypes<NotificationsOutboxRemove>;

const notificationsOutboxRemove = new UnaryCaller<
CallerTypes['input'],
CallerTypes['output']
>();

export default notificationsOutboxRemove;
12 changes: 0 additions & 12 deletions src/client/callers/notificationsRead.ts

This file was deleted.

7 changes: 5 additions & 2 deletions src/client/handlers/IdentitiesInvite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ class IdentitiesInvite extends UnaryHandler<
);
// Sending the notification, we don't care if it fails
try {
await notificationsManager.sendNotification(nodeId, {
type: 'GestaltInvite',
await notificationsManager.sendNotification({
nodeId,
data: {
type: 'GestaltInvite',
},
});
} catch {
logger.warn('Failed to send gestalt invitation to target node');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { ClientRPCRequestParams, ClientRPCResponseResult } from '../types';
import type NotificationsManager from '../../notifications/NotificationsManager';
import { UnaryHandler } from '@matrixai/rpc';

class NotificationsClear extends UnaryHandler<
class NotificationsInboxClear extends UnaryHandler<
{
db: DB;
notificationsManager: NotificationsManager;
Expand All @@ -14,10 +14,10 @@ class NotificationsClear extends UnaryHandler<
public handle = async (): Promise<ClientRPCResponseResult> => {
const { db, notificationsManager } = this.container;
await db.withTransactionF((tran) =>
notificationsManager.clearNotifications(tran),
notificationsManager.clearInboxNotifications(tran),
);
return {};
};
}

export default NotificationsClear;
export default NotificationsInboxClear;
64 changes: 64 additions & 0 deletions src/client/handlers/NotificationsInboxRead.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import type { DB } from '@matrixai/db';
import type {
ClientRPCRequestParams,
ClientRPCResponseResult,
NotificationInboxMessage,
NotificationReadMessage,
} from '../types';
import type { NotificationId } from '../../ids/types';
import type NotificationsManager from '../../notifications/NotificationsManager';
import { ServerHandler } from '@matrixai/rpc';
import * as notificationsUtils from '../../notifications/utils';

class NotificationsInboxRead extends ServerHandler<
{
db: DB;
notificationsManager: NotificationsManager;
},
ClientRPCRequestParams<NotificationReadMessage>,
ClientRPCResponseResult<NotificationInboxMessage>
> {
public handle(
input: ClientRPCRequestParams<NotificationReadMessage>,
_cancel,
_meta,
ctx,
): AsyncGenerator<ClientRPCResponseResult<NotificationInboxMessage>> {
if (ctx.signal.aborted) throw ctx.signal.reason;
const { db, notificationsManager } = this.container;
const { seek, seekEnd, unread, order, limit } = input;

let seek_: NotificationId | number | undefined;
if (seek != null) {
seek_ =
typeof seek === 'string'
? notificationsUtils.decodeNotificationId(seek)
: seek;
}
let seekEnd_: NotificationId | number | undefined;
if (seekEnd != null) {
seekEnd_ =
typeof seekEnd === 'string'
? notificationsUtils.decodeNotificationId(seekEnd)
: seekEnd;
}
return db.withTransactionG(async function* (tran) {
const notifications = notificationsManager.readInboxNotifications({
seek: seek_,
seekEnd: seekEnd_,
unread,
order,
limit,
tran,
});
for await (const notification of notifications) {
if (ctx.signal.aborted) throw ctx.signal.reason;
yield {
notification: notification,
};
}
});
}
}

export default NotificationsInboxRead;
39 changes: 39 additions & 0 deletions src/client/handlers/NotificationsInboxRemove.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { DB } from '@matrixai/db';
import type {
ClientRPCRequestParams,
ClientRPCResponseResult,
NotificationRemoveMessage,
} from '../types';
import type NotificationsManager from '../../notifications/NotificationsManager';
import { UnaryHandler } from '@matrixai/rpc';
import * as notificationsUtils from '../../notifications/utils';
import * as validationErrors from '../../validation/errors';

class NotificationsInboxRemove extends UnaryHandler<
{
db: DB;
notificationsManager: NotificationsManager;
},
ClientRPCRequestParams,
ClientRPCResponseResult
> {
public handle = async (
input: ClientRPCRequestParams<NotificationRemoveMessage>,
): Promise<ClientRPCResponseResult> => {
const { db, notificationsManager } = this.container;
const notificationId = notificationsUtils.decodeNotificationId(
input.notificationIdEncoded,
);
if (notificationId == null) {
throw new validationErrors.ErrorParse(
'`notificationIdEncoded` property must be an encoded notification ID',
);
}
await db.withTransactionF(async (tran) =>
notificationsManager.removeInboxNotification(notificationId, tran),
);
return {};
};
}

export default NotificationsInboxRemove;
23 changes: 23 additions & 0 deletions src/client/handlers/NotificationsOutboxClear.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { DB } from '@matrixai/db';
import type { ClientRPCRequestParams, ClientRPCResponseResult } from '../types';
import type NotificationsManager from '../../notifications/NotificationsManager';
import { UnaryHandler } from '@matrixai/rpc';

class NotificationsOutboxClear extends UnaryHandler<
{
db: DB;
notificationsManager: NotificationsManager;
},
ClientRPCRequestParams,
ClientRPCResponseResult
> {
public handle = async (): Promise<ClientRPCResponseResult> => {
const { db, notificationsManager } = this.container;
await db.withTransactionF((tran) =>
notificationsManager.clearOutboxNotifications(tran),
);
return {};
};
}

export default NotificationsOutboxClear;
63 changes: 63 additions & 0 deletions src/client/handlers/NotificationsOutboxRead.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import type { DB } from '@matrixai/db';
import type {
ClientRPCRequestParams,
ClientRPCResponseResult,
NotificationOutboxMessage,
NotificationOutboxReadMessage,
} from '../types';
import type { NotificationId } from '../../ids/types';
import type NotificationsManager from '../../notifications/NotificationsManager';
import { ServerHandler } from '@matrixai/rpc';
import * as notificationsUtils from '../../notifications/utils';

class NotificationsOutboxRead extends ServerHandler<
{
db: DB;
notificationsManager: NotificationsManager;
},
ClientRPCRequestParams<NotificationOutboxReadMessage>,
ClientRPCResponseResult<NotificationOutboxMessage>
> {
public handle(
input: ClientRPCRequestParams<NotificationOutboxReadMessage>,
_cancel,
_meta,
ctx,
): AsyncGenerator<ClientRPCResponseResult<NotificationOutboxMessage>> {
if (ctx.signal.aborted) throw ctx.signal.reason;
const { db, notificationsManager } = this.container;
const { seek, seekEnd, order, limit } = input;

let seek_: NotificationId | number | undefined;
if (seek != null) {
seek_ =
typeof seek === 'string'
? notificationsUtils.decodeNotificationId(seek)
: seek;
}
let seekEnd_: NotificationId | number | undefined;
if (seekEnd != null) {
seekEnd_ =
typeof seekEnd === 'string'
? notificationsUtils.decodeNotificationId(seekEnd)
: seekEnd;
}
return db.withTransactionG(async function* (tran) {
const notifications = notificationsManager.readOutboxNotifications({
seek: seek_,
seekEnd: seekEnd_,
order,
limit,
tran,
});
for await (const notification of notifications) {
if (ctx.signal.aborted) throw ctx.signal.reason;
yield {
notification: notification,
};
}
});
}
}

export default NotificationsOutboxRead;
Loading