Skip to content

Commit

Permalink
wip: adding gestalts handlers
Browse files Browse the repository at this point in the history
[ci skip]
  • Loading branch information
tegefaulkes committed Feb 28, 2023
1 parent 7c31f8f commit b734d2d
Show file tree
Hide file tree
Showing 15 changed files with 854 additions and 37 deletions.
7 changes: 2 additions & 5 deletions src/clientRPC/handlers/agentLockAll.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type Logger from '@matrixai/logger';
import type { RPCRequestParams, RPCResponseResult } from '../types';
import type { DB } from '@matrixai/db';
import type SessionManager from '../../sessions/SessionManager';
Expand All @@ -14,15 +13,13 @@ class AgentLockAllHandler extends UnaryHandler<
{
sessionManager: SessionManager;
db: DB;
logger: Logger;
},
RPCRequestParams,
RPCResponseResult
> {
public async handle(): Promise<RPCResponseResult> {
await this.container.db.withTransactionF((tran) =>
this.container.sessionManager.resetKey(tran),
);
const { db, sessionManager } = this.container;
await db.withTransactionF((tran) => sessionManager.resetKey(tran));
return {};
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/clientRPC/handlers/agentStatus.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import type KeyRing from '../../keys/KeyRing';
import type CertManager from '../../keys/CertManager';
import type Logger from '@matrixai/logger';
import type { NodeIdEncoded } from '../../ids';
import type { RPCRequestParams, RPCResponseResult } from '../types';
import * as nodesUtils from '../../nodes/utils';
Expand All @@ -22,18 +20,17 @@ const agentStatusCaller = new UnaryCaller<
class AgentStatusHandler extends UnaryHandler<
{
keyRing: KeyRing;
certManager: CertManager;
logger: Logger;
},
RPCRequestParams,
RPCResponseResult<StatusResult>
> {
public async handle(): Promise<RPCResponseResult<StatusResult>> {
const { keyRing } = this.container;
return {
pid: process.pid,
nodeId: nodesUtils.encodeNodeId(this.container.keyRing.getNodeId()),
nodeId: nodesUtils.encodeNodeId(keyRing.getNodeId()),
publicJwk: JSON.stringify(
keysUtils.publicKeyToJWK(this.container.keyRing.keyPair.publicKey),
keysUtils.publicKeyToJWK(keyRing.keyPair.publicKey),
),
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/clientRPC/handlers/agentStop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class AgentStopHandler extends UnaryHandler<
RPCResponseResult
> {
public async handle(): Promise<RPCResponseResult> {
const pkAgent = this.container.pkAgent;
const { pkAgent } = this.container;
// If not running or in stopping status, then respond successfully
if (!pkAgent[running] || pkAgent[status] === 'stopping') {
return {};
Expand Down
4 changes: 2 additions & 2 deletions src/clientRPC/handlers/agentUnlock.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type Logger from '@matrixai/logger';
import type { RPCRequestParams, RPCResponseResult } from '../types';
import type { ContainerType } from 'RPC/types';
import { UnaryHandler } from '../../RPC/handlers';
import { UnaryCaller } from '../../RPC/callers';

Expand All @@ -9,7 +9,7 @@ const agentUnlockCaller = new UnaryCaller<
>();

class AgentUnlockHandler extends UnaryHandler<
{ logger: Logger },
ContainerType,
RPCRequestParams,
RPCResponseResult
> {
Expand Down
78 changes: 78 additions & 0 deletions src/clientRPC/handlers/gestaltsActionsGetByIdentity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import type { RPCRequestParams, RPCResponseResult } from '../types';
import type GestaltGraph from 'gestalts/GestaltGraph';
import type { DB } from '@matrixai/db';
import type { IdentityId, ProviderId } from 'ids/index';
import type { GestaltAction } from 'gestalts/types';
import { UnaryCaller } from '../../RPC/callers';
import { UnaryHandler } from '../../RPC/handlers';
import { validateSync } from '../../validation/index';
import { matchSync } from '../../utils/index';
import * as validationUtils from '../../validation/utils';

const gestaltsActionsGetByIdentityCaller = new UnaryCaller<
RPCRequestParams<{
providerId: string;
identityId: string;
}>,
RPCResponseResult<{
actionsList: Array<string>;
}>
>();

class GestaltsActionsGetByIdentityHandler extends UnaryHandler<
{
gestaltGraph: GestaltGraph;
db: DB;
},
RPCRequestParams<{
providerId: string;
identityId: string;
}>,
RPCResponseResult<{
actionsList: Array<GestaltAction>;
}>
> {
public async handle(
input: RPCRequestParams<{
providerId: string;
identityId: string;
}>,
): Promise<
RPCResponseResult<{
actionsList: Array<GestaltAction>;
}>
> {
const { db, gestaltGraph } = this.container;
const {
providerId,
identityId,
}: { providerId: ProviderId; identityId: IdentityId } = validateSync(
(keyPath, value) => {
return matchSync(keyPath)(
[['providerId'], () => validationUtils.parseProviderId(value)],
[['identityId'], () => validationUtils.parseIdentityId(value)],
() => value,
);
},
{
providerId: input.providerId,
identityId: input.identityId,
},
);
const result = await db.withTransactionF((tran) =>
gestaltGraph.getGestaltActions(
['identity', [providerId, identityId]],
tran,
),
);

return {
actionsList: Object.keys(result) as Array<GestaltAction>,
};
}
}

export {
gestaltsActionsGetByIdentityCaller,
GestaltsActionsGetByIdentityHandler,
};
64 changes: 64 additions & 0 deletions src/clientRPC/handlers/gestaltsActionsGetByNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import type { RPCRequestParams, RPCResponseResult } from '../types';
import type GestaltGraph from 'gestalts/GestaltGraph';
import type { DB } from '@matrixai/db';
import type { GestaltAction } from 'gestalts/types';
import type { NodeId, NodeIdEncoded } from 'ids/index';
import { UnaryCaller } from '../../RPC/callers';
import { UnaryHandler } from '../../RPC/handlers';
import { validateSync } from '../../validation/index';
import { matchSync } from '../../utils/index';
import * as validationUtils from '../../validation/utils';

const gestaltsActionsGetByNodeCaller = new UnaryCaller<
RPCRequestParams<{
nodeIdEncoded: NodeIdEncoded;
}>,
RPCResponseResult<{
actionsList: Array<GestaltAction>;
}>
>();

class GestaltsActionsGetByNodeHandler extends UnaryHandler<
{
gestaltGraph: GestaltGraph;
db: DB;
},
RPCRequestParams<{
nodeIdEncoded: NodeIdEncoded;
}>,
RPCResponseResult<{
actionsList: Array<GestaltAction>;
}>
> {
public async handle(
input: RPCRequestParams<{
nodeIdEncoded: NodeIdEncoded;
}>,
): Promise<
RPCResponseResult<{
actionsList: Array<GestaltAction>;
}>
> {
const { db, gestaltGraph } = this.container;
const { nodeId }: { nodeId: NodeId } = validateSync(
(keyPath, value) => {
return matchSync(keyPath)(
[['nodeId'], () => validationUtils.parseNodeId(value)],
() => value,
);
},
{
nodeId: input.nodeIdEncoded,
},
);
const result = await db.withTransactionF((tran) =>
gestaltGraph.getGestaltActions(['node', nodeId], tran),
);

return {
actionsList: Object.keys(result) as Array<GestaltAction>,
};
}
}

export { gestaltsActionsGetByNodeCaller, GestaltsActionsGetByNodeHandler };
78 changes: 78 additions & 0 deletions src/clientRPC/handlers/gestaltsActionsSetByIdentity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import type { RPCRequestParams, RPCResponseResult } from '../types';
import type { DB } from '@matrixai/db';
import type { IdentityId, ProviderId } from 'ids/index';
import type { GestaltAction } from '../../gestalts/types';
import type GestaltGraph from 'gestalts/GestaltGraph';
import { UnaryCaller } from '../../RPC/callers';
import { UnaryHandler } from '../../RPC/handlers';
import { validateSync } from '../../validation/index';
import { matchSync } from '../../utils/index';
import * as validationUtils from '../../validation/utils';

const gestaltsActionsSetByIdentityCaller = new UnaryCaller<
RPCRequestParams<{
action: GestaltAction;
providerId: string;
identityId: string;
}>,
RPCResponseResult
>();

class GestaltsActionsSetByIdentityHandler extends UnaryHandler<
{
gestaltGraph: GestaltGraph;
db: DB;
},
RPCRequestParams<{
action: GestaltAction;
providerId: string;
identityId: string;
}>,
RPCResponseResult
> {
public async handle(
input: RPCRequestParams<{
action: GestaltAction;
providerId: string;
identityId: string;
}>,
): Promise<RPCResponseResult> {
const { db, gestaltGraph } = this.container;
const {
action,
providerId,
identityId,
}: {
action: GestaltAction;
providerId: ProviderId;
identityId: IdentityId;
} = validateSync(
(keyPath, value) => {
return matchSync(keyPath)(
[['action'], () => validationUtils.parseGestaltAction(value)],
[['providerId'], () => validationUtils.parseProviderId(value)],
[['identityId'], () => validationUtils.parseIdentityId(value)],
() => value,
);
},
{
action: input.action,
providerId: input.providerId,
identityId: input.identityId,
},
);
await db.withTransactionF((tran) =>
gestaltGraph.setGestaltAction(
['identity', [providerId, identityId]],
action,
tran,
),
);
return {};
}
}

export {
gestaltsActionsSetByIdentityCaller,
GestaltsActionsSetByIdentityHandler,
};
59 changes: 59 additions & 0 deletions src/clientRPC/handlers/gestaltsActionsSetByNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import type { RPCRequestParams, RPCResponseResult } from '../types';
import type { DB } from '@matrixai/db';
import type { GestaltAction } from '../../gestalts/types';
import type GestaltGraph from 'gestalts/GestaltGraph';
import type { NodeId } from 'ids/index';
import { UnaryCaller } from '../../RPC/callers';
import { UnaryHandler } from '../../RPC/handlers';
import { validateSync } from '../../validation/index';
import { matchSync } from '../../utils/index';
import * as validationUtils from '../../validation/utils';

const gestaltsActionsSetByNodeCaller = new UnaryCaller<
RPCRequestParams<{
action: GestaltAction;
nodeIdEncoded: string;
}>,
RPCResponseResult
>();

class GestaltsActionsSetByNodeHandler extends UnaryHandler<
{
gestaltGraph: GestaltGraph;
db: DB;
},
RPCRequestParams<{
action: GestaltAction;
nodeIdEncoded: string;
}>,
RPCResponseResult
> {
public async handle(
input: RPCRequestParams<{
action: GestaltAction;
nodeIdEncoded: string;
}>,
): Promise<RPCResponseResult> {
const { db, gestaltGraph } = this.container;
const { nodeId, action }: { nodeId: NodeId; action: GestaltAction } =
validateSync(
(keyPath, value) => {
return matchSync(keyPath)(
[['nodeId'], () => validationUtils.parseNodeId(value)],
[['action'], () => validationUtils.parseGestaltAction(value)],
() => value,
);
},
{
nodeId: input.nodeIdEncoded,
action: input.action,
},
);
await db.withTransactionF((tran) =>
gestaltGraph.setGestaltAction(['node', nodeId], action, tran),
);
return {};
}
}

export { gestaltsActionsSetByNodeCaller, GestaltsActionsSetByNodeHandler };
Loading

0 comments on commit b734d2d

Please sign in to comment.