-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathIdentitiesInfoConnectedGet.ts
More file actions
114 lines (112 loc) · 3.61 KB
/
IdentitiesInfoConnectedGet.ts
File metadata and controls
114 lines (112 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import type { ContextTimed } from '@matrixai/contexts';
import type { JSONValue } from '@matrixai/rpc';
import type {
ClientRPCRequestParams,
ClientRPCResponseResult,
IdentityInfoMessage,
ProviderSearchMessage,
} from '../types';
import type { IdentityId, ProviderId } from '../../ids';
import type IdentitiesManager from '../../identities/IdentitiesManager';
import type { IdentityData } from '../../identities/types';
import { ServerHandler } from '@matrixai/rpc';
import * as ids from '../../ids';
import * as identitiesErrors from '../../identities/errors';
import { validateSync } from '../../validation';
import { matchSync } from '../../utils';
class IdentitiesInfoConnectedGet extends ServerHandler<
{
identitiesManager: IdentitiesManager;
},
ClientRPCRequestParams<ProviderSearchMessage>,
ClientRPCResponseResult<IdentityInfoMessage>
> {
public handle = async function* (
input: ClientRPCRequestParams<ProviderSearchMessage>,
_cancel: (reason?: any) => void,
_meta: Record<string, JSONValue>,
ctx: ContextTimed,
): AsyncGenerator<ClientRPCResponseResult<IdentityInfoMessage>> {
const { identitiesManager }: { identitiesManager: IdentitiesManager } =
this.container;
const {
providerIds,
}: {
providerIds: Array<ProviderId>;
} = validateSync(
(keyPath, value) => {
return matchSync(keyPath)(
[['providerIds'], () => value.map(ids.parseProviderId)],
() => value,
);
},
{
providerIds: input.providerIdList,
},
);
let identityId: IdentityId | undefined;
if (input.authIdentityId != null) {
identityId = validateSync(
(keyPath, value) => {
return matchSync(keyPath)(
[['identityId'], () => ids.parseIdentityId(value)],
() => value,
);
},
{
identityId: input.authIdentityId,
},
).identityId;
}
// Process options that were set
if (providerIds.length === 0) {
Object.keys(identitiesManager.getProviders()).forEach((id) =>
providerIds.push(id as ProviderId),
);
}
const getDisconnected = input.disconnected;
if (getDisconnected) {
// Can only get connected identities at this stage
throw new identitiesErrors.ErrorProviderUnimplemented();
}
const identities: Array<AsyncGenerator<IdentityData>> = [];
for (const providerId of providerIds) {
ctx.signal.throwIfAborted();
// Get provider from id
const provider = identitiesManager.getProvider(providerId);
if (provider === undefined) {
throw new identitiesErrors.ErrorProviderMissing();
}
// Get our own authenticated identity in order to query, skip provider
// if not authenticated
const authIdentities = await provider.getAuthIdentityIds();
if (authIdentities.length === 0) {
continue;
}
const authIdentityId =
identityId === undefined ? authIdentities[0] : identityId;
identities.push(
provider.getConnectedIdentityDatas(
authIdentityId,
input.searchTermList,
),
);
}
let count = 0;
for (const gen of identities) {
for await (const identity of gen) {
ctx.signal.throwIfAborted();
if (input.limit !== undefined && count >= input.limit) break;
yield {
providerId: identity.providerId,
identityId: identity.identityId,
name: identity.name ?? '',
email: identity.email ?? '',
url: identity.url ?? '',
};
count++;
}
}
};
}
export default IdentitiesInfoConnectedGet;