-
Notifications
You must be signed in to change notification settings - Fork 1
/
ContactsModuleRdfLib.ts
391 lines (348 loc) · 11.6 KB
/
ContactsModuleRdfLib.ts
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import {
Fetcher,
IndexedFormula,
NamedNode,
Node,
sym,
UpdateManager,
} from "rdflib";
import {
AddContactToGroupCommand,
AddNewEmailAddressCommand,
AddNewPhoneNumberCommand,
AddressBook,
AddressBookLists,
ContactsModule,
CreateAddressBookCommand,
CreateNewContactCommand,
CreateNewGroupCommand,
FullContact,
FullGroup,
NewContact,
RemoveContactFromGroupCommand,
RemoveEmailAddressCommand,
RemovePhoneNumberCommand,
RenameContactCommand,
UpdateEmailAddressCommand,
UpdatePhoneNumberCommand,
} from "../index.js";
import { AddressBookQuery, ContactQuery } from "./queries/index.js";
import {
createAddressBook,
createNewContact,
} from "./update-operations/index.js";
import { executeUpdate } from "./web-operations/executeUpdate.js";
import { fetchNode } from "./web-operations/fetchNode.js";
import { createNewGroup } from "./update-operations/createNewGroup.js";
import { GroupQuery } from "./queries/GroupQuery.js";
import { addContactToGroup } from "./update-operations/addContactToGroup.js";
import { removeContactFromGroup } from "./update-operations/removeContactFromGroup.js";
import { addNewPhoneNumber } from "./update-operations/addNewPhoneNumber.js";
import { addNewEmailAddress } from "./update-operations/addNewEmailAddress.js";
import { removePhoneNumber } from "./update-operations/removePhoneNumber.js";
import { removeEmailAddress } from "./update-operations/removeEmailAddress.js";
import { ProfileQuery } from "./queries/ProfileQuery.js";
import { TypeIndexQuery } from "./queries/TypeIndexQuery.js";
import { PreferencesQuery } from "./queries/PreferencesQuery.js";
import { renameContact } from "./update-operations/renameContact.js";
import { addAddressBookToTypeIndex } from "./update-operations/addAddressBookToTypeIndex.js";
import { updatePhoneNumber } from "./update-operations/updatePhoneNumber.js";
import { updateEmailAddress } from "./update-operations/updateEmailAddress.js";
interface ModuleConfig {
store: IndexedFormula;
fetcher: Fetcher;
updater: UpdateManager;
}
export class ContactsModuleRdfLib implements ContactsModule {
private readonly fetcher: Fetcher;
private readonly store: IndexedFormula;
private readonly updater: UpdateManager;
constructor(config: ModuleConfig) {
this.store = config.store;
this.fetcher = config.fetcher;
this.updater = config.updater;
}
async readAddressBook(uri: string): Promise<AddressBook> {
const addressBookNode = sym(uri);
await this.fetchNode(addressBookNode);
const query = new AddressBookQuery(this.store, addressBookNode);
const title = query.queryTitle();
const nameEmailIndex = query.queryNameEmailIndex();
const groupIndex = query.queryGroupIndex();
await Promise.allSettled([
this.fetchNode(nameEmailIndex),
this.fetchNode(groupIndex),
]);
await this.fetchAll([nameEmailIndex, groupIndex]);
const contacts = query.queryContacts();
const groups = query.queryGroups();
return {
uri,
title,
contacts,
groups,
};
}
private async fetchNode(node: Node | null) {
return fetchNode(this.fetcher, node);
}
private async fetchAll(nodes: (Node | null)[]) {
return Promise.all(nodes.map((it) => this.fetchNode(it)));
}
async createAddressBook({
containerUri,
name,
ownerWebId,
}: CreateAddressBookCommand) {
const operation = createAddressBook(containerUri, name);
await executeUpdate(this.fetcher, this.updater, operation);
if (ownerWebId) {
await this.updatePrivateTypeIndex(ownerWebId, operation.uri);
}
return operation.uri;
}
private async updatePrivateTypeIndex(
ownerWebId: string,
addressBookUri: string,
) {
const profileNode = sym(ownerWebId);
await this.fetchNode(profileNode);
const profileQuery = new ProfileQuery(profileNode, this.store);
const preferencesFile = profileQuery.queryPreferencesFile();
const privateTypeIndex = await this.fetchPrivateTypeIndex(
profileNode,
preferencesFile,
);
if (!privateTypeIndex) {
throw new Error(`Private type not found for WebID ${ownerWebId}.`);
}
const operation = addAddressBookToTypeIndex(
privateTypeIndex,
addressBookUri,
);
await executeUpdate(this.fetcher, this.updater, operation);
}
async createNewContact({
addressBookUri,
contact,
groupUris,
}: CreateNewContactCommand) {
const contactQuery = await this.executeCreateNewContact(
addressBookUri,
contact,
);
await this.executeAddContactToGroups(groupUris, contactQuery);
return contactQuery.contactNode.uri;
}
private async executeAddContactToGroups(
groupUris: string[] | undefined,
contactQuery: ContactQuery,
) {
const groupNodes = (groupUris ?? []).map((it) => sym(it));
await this.fetchAll(groupNodes);
const groupUpdates = groupNodes.map((groupNode) => {
const groupQuery = new GroupQuery(this.store, groupNode);
const operation = addContactToGroup(contactQuery, groupQuery);
return executeUpdate(this.fetcher, this.updater, operation);
});
await Promise.allSettled(groupUpdates);
}
private async executeCreateNewContact(
addressBookUri: string,
contact: NewContact,
) {
const addressBookNode = sym(addressBookUri);
await this.fetchNode(addressBookNode);
const operation = createNewContact(
new AddressBookQuery(this.store, addressBookNode),
contact,
);
await executeUpdate(this.fetcher, this.updater, operation);
return new ContactQuery(this.store, sym(operation.uri));
}
async readContact(uri: string): Promise<FullContact> {
const contactNode = sym(uri);
await this.fetchNode(contactNode);
const query = new ContactQuery(this.store, contactNode);
const name = query.queryName();
const emails = query.queryEmails();
const phoneNumbers = query.queryPhoneNumbers();
return {
uri,
name,
emails,
phoneNumbers,
};
}
async createNewGroup({ addressBookUri, groupName }: CreateNewGroupCommand) {
const addressBookNode = sym(addressBookUri);
await this.fetchNode(addressBookNode);
const query = new AddressBookQuery(this.store, addressBookNode);
const operation = createNewGroup(query, groupName);
await executeUpdate(this.fetcher, this.updater, operation);
return operation.uri;
}
async readGroup(uri: string): Promise<FullGroup> {
const groupNode = sym(uri);
await this.fetchNode(groupNode);
const query = new GroupQuery(this.store, groupNode);
const name = query.queryName();
const members = query.queryMembers();
return {
uri,
name,
members,
};
}
async addContactToGroup({ contactUri, groupUri }: AddContactToGroupCommand) {
const contactNode = sym(contactUri);
const groupNode = sym(groupUri);
await this.fetchNode(contactNode);
const contactQuery = new ContactQuery(this.store, contactNode);
const groupQuery = new GroupQuery(this.store, groupNode);
const operation = addContactToGroup(contactQuery, groupQuery);
await executeUpdate(this.fetcher, this.updater, operation);
}
async removeContactFromGroup({
contactUri,
groupUri,
}: RemoveContactFromGroupCommand) {
const contactNode = sym(contactUri);
const groupNode = sym(groupUri);
await this.fetchNode(groupNode);
const contactQuery = new ContactQuery(this.store, contactNode);
const groupQuery = new GroupQuery(this.store, groupNode);
const operation = removeContactFromGroup(contactQuery, groupQuery);
await executeUpdate(this.fetcher, this.updater, operation);
}
async addNewPhoneNumber({
contactUri,
newPhoneNumber,
}: AddNewPhoneNumberCommand) {
const contactNode = sym(contactUri);
const operation = addNewPhoneNumber(contactNode, newPhoneNumber);
await executeUpdate(this.fetcher, this.updater, operation);
return operation.uri;
}
async addNewEmailAddress({
contactUri,
newEmailAddress,
}: AddNewEmailAddressCommand) {
const contactNode = sym(contactUri);
const operation = addNewEmailAddress(contactNode, newEmailAddress);
await executeUpdate(this.fetcher, this.updater, operation);
return operation.uri;
}
async removePhoneNumber({
contactUri,
phoneNumberUri,
}: RemovePhoneNumberCommand) {
const contactNode = sym(contactUri);
const phoneNumberNode = sym(phoneNumberUri);
await this.fetchNode(phoneNumberNode);
const operation = removePhoneNumber(
contactNode,
phoneNumberNode,
this.store,
);
await executeUpdate(this.fetcher, this.updater, operation);
}
async removeEmailAddress({
contactUri,
emailAddressUri,
}: RemoveEmailAddressCommand) {
const contactNode = sym(contactUri);
const emailAddressNode = sym(emailAddressUri);
await this.fetchNode(emailAddressNode);
const operation = removeEmailAddress(
contactNode,
emailAddressNode,
this.store,
);
await executeUpdate(this.fetcher, this.updater, operation);
}
async updatePhoneNumber({
phoneNumberUri,
newPhoneNumber,
}: UpdatePhoneNumberCommand) {
const phoneNumberNode = sym(phoneNumberUri);
await this.fetchNode(phoneNumberNode);
const operation = updatePhoneNumber(
phoneNumberNode,
newPhoneNumber,
this.store,
);
await executeUpdate(this.fetcher, this.updater, operation);
}
async updateEmailAddress({
emailAddressUri,
newEmailAddress,
}: UpdateEmailAddressCommand) {
const emailAddressNode = sym(emailAddressUri);
await this.fetchNode(emailAddressNode);
const operation = updateEmailAddress(
emailAddressNode,
newEmailAddress,
this.store,
);
await executeUpdate(this.fetcher, this.updater, operation);
}
async listAddressBooks(webId: string): Promise<AddressBookLists> {
const profileNode = sym(webId);
await this.fetchNode(profileNode);
const profileQuery = new ProfileQuery(profileNode, this.store);
const publicTypeIndexNode = profileQuery.queryPublicTypeIndex();
const preferencesFile = profileQuery.queryPreferencesFile();
const promisePublicUris =
this.fetchIndexedAddressBooks(publicTypeIndexNode);
const promisePrivateTypeIndex = this.fetchPrivateTypeIndex(
profileNode,
preferencesFile,
);
const [publicUris, privateTypeIndex] = await Promise.allSettled([
promisePublicUris,
promisePrivateTypeIndex,
]);
const privateUris =
privateTypeIndex.status === "fulfilled"
? await this.fetchIndexedAddressBooks(privateTypeIndex.value)
: [];
return {
publicUris: publicUris.status === "fulfilled" ? publicUris.value : [],
privateUris,
};
}
private async fetchIndexedAddressBooks(
publicTypeIndexNode: NamedNode | null,
): Promise<string[]> {
if (!publicTypeIndexNode) {
return [];
}
await this.fetchNode(publicTypeIndexNode);
return new TypeIndexQuery(
this.store,
publicTypeIndexNode,
).queryAddressBookInstances();
}
private async fetchPrivateTypeIndex(
profileNode: NamedNode,
preferencesFile: NamedNode | null,
): Promise<NamedNode | null> {
if (!preferencesFile) {
return null;
}
await this.fetchNode(preferencesFile);
const preferencesQuery = new PreferencesQuery(
this.store,
profileNode,
preferencesFile,
);
return preferencesQuery.queryPrivateTypeIndex();
}
async renameContact({ contactUri, newName }: RenameContactCommand) {
const contactNode = sym(contactUri);
await this.fetchNode(contactNode);
const operation = renameContact(this.store, contactNode, newName);
await executeUpdate(this.fetcher, this.updater, operation);
}
}