|
| 1 | +import { type DirectoryEntry, type DirectorySource, type TypeIndexLogic } from '../types' |
| 2 | +import { type LiveStore, NamedNode } from 'rdflib' |
| 3 | +import { ns } from '../util/ns' |
| 4 | +import { isHttpUri, labelForNode, loadDocumentSilently, uniqueNamedNodes } from './directoryHelpers' |
| 5 | + |
| 6 | +const CONTACT_CARD_CONCURRENCY = 8 |
| 7 | + |
| 8 | +export function createContactsDirectoryDiscovery ( |
| 9 | + store: LiveStore, |
| 10 | + typeIndexLogic: TypeIndexLogic |
| 11 | +) { |
| 12 | + const personalAddressBooksCache = new Map<string, Promise<NamedNode[]>>() |
| 13 | + const contactWebIdCache = new Map<string, Promise<string | null>>() |
| 14 | + |
| 15 | + async function loadPersonalAddressBooks (user: NamedNode): Promise<NamedNode[]> { |
| 16 | + const cached = personalAddressBooksCache.get(user.value) |
| 17 | + if (cached) { |
| 18 | + return cached |
| 19 | + } |
| 20 | + |
| 21 | + const promise = (async () => { |
| 22 | + const scopes = await typeIndexLogic.loadTypeIndexesFor(user) |
| 23 | + let scopedApps = [] as Awaited<ReturnType<TypeIndexLogic['getScopedAppsFromIndex']>> |
| 24 | + |
| 25 | + for (const scope of scopes) { |
| 26 | + const apps = await typeIndexLogic.getScopedAppsFromIndex(scope, ns.vcard('AddressBook')) |
| 27 | + scopedApps = scopedApps.concat(apps) |
| 28 | + } |
| 29 | + |
| 30 | + return uniqueNamedNodes(scopedApps.map(app => app.instance)) |
| 31 | + })().catch(error => { |
| 32 | + personalAddressBooksCache.delete(user.value) |
| 33 | + throw error |
| 34 | + }) |
| 35 | + |
| 36 | + personalAddressBooksCache.set(user.value, promise) |
| 37 | + return promise |
| 38 | + } |
| 39 | + |
| 40 | + async function webIdForAddressBookContact (contact: NamedNode): Promise<string | null> { |
| 41 | + const cached = contactWebIdCache.get(contact.value) |
| 42 | + if (cached) { |
| 43 | + return cached |
| 44 | + } |
| 45 | + |
| 46 | + const promise = (async () => { |
| 47 | + if (!await loadDocumentSilently(store, contact)) { |
| 48 | + return null |
| 49 | + } |
| 50 | + |
| 51 | + const urlNodes = store.each(contact, ns.vcard('url'), null, contact.doc()) as NamedNode[] |
| 52 | + for (const urlNode of urlNodes) { |
| 53 | + const webId = resolveWebIdFromAddressBookUrlNode(urlNode, contact.doc()) |
| 54 | + if (webId) { |
| 55 | + return webId |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return null |
| 60 | + })().catch(error => { |
| 61 | + contactWebIdCache.delete(contact.value) |
| 62 | + throw error |
| 63 | + }) |
| 64 | + |
| 65 | + contactWebIdCache.set(contact.value, promise) |
| 66 | + return promise |
| 67 | + } |
| 68 | + |
| 69 | + function resolveWebIdFromAddressBookUrlNode (urlNode: NamedNode, doc: NamedNode): string | null { |
| 70 | + if (!store.holds(urlNode, ns.rdf('type'), ns.vcard('WebID'), doc)) { |
| 71 | + return null |
| 72 | + } |
| 73 | + |
| 74 | + const valueNode = store.any(urlNode, ns.vcard('value'), null, doc) |
| 75 | + const webId = valueNode?.value || '' |
| 76 | + return isHttpUri(webId) ? webId : null |
| 77 | + } |
| 78 | + |
| 79 | + async function discoverAddressBookContacts ( |
| 80 | + user: NamedNode, |
| 81 | + onEntry: (entry: DirectoryEntry) => void | Promise<void> |
| 82 | + ): Promise<void> { |
| 83 | + const books = await loadPersonalAddressBooks(user) |
| 84 | + |
| 85 | + for (const book of books) { |
| 86 | + if (await loadDocumentSilently(store, book)) { |
| 87 | + const nameEmailIndex = store.any(book, ns.vcard('nameEmailIndex'), null, book.doc()) as NamedNode | null |
| 88 | + if (nameEmailIndex) { |
| 89 | + if (await loadDocumentSilently(store, nameEmailIndex)) { |
| 90 | + const contacts = store.each(undefined, ns.vcard('inAddressBook'), book, nameEmailIndex) as NamedNode[] |
| 91 | + for (let index = 0; index < contacts.length; index += CONTACT_CARD_CONCURRENCY) { |
| 92 | + const batch = contacts.slice(index, index + CONTACT_CARD_CONCURRENCY) |
| 93 | + const entries = await Promise.all(batch.map(async contact => { |
| 94 | + const webId = await webIdForAddressBookContact(contact) |
| 95 | + if (!webId) { |
| 96 | + return null |
| 97 | + } |
| 98 | + |
| 99 | + return { |
| 100 | + kind: 'person' as const, |
| 101 | + uri: webId, |
| 102 | + label: store.anyValue(contact, ns.vcard('fn'), null, nameEmailIndex) || labelForNode(store, contact), |
| 103 | + subjectType: 'agent' as const, |
| 104 | + relationshipLabel: 'Contact' as const, |
| 105 | + sources: ['contacts'] as DirectorySource[] |
| 106 | + } |
| 107 | + })) |
| 108 | + |
| 109 | + for (const entry of entries.filter((entry): entry is NonNullable<typeof entry> => Boolean(entry))) { |
| 110 | + await onEntry(entry) |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + async function discoverAddressBookGroups ( |
| 120 | + user: NamedNode, |
| 121 | + onEntry: (entry: DirectoryEntry) => void | Promise<void> |
| 122 | + ): Promise<void> { |
| 123 | + const books = await loadPersonalAddressBooks(user) |
| 124 | + |
| 125 | + for (const book of books) { |
| 126 | + if (await loadDocumentSilently(store, book)) { |
| 127 | + const groupIndex = store.any(book, ns.vcard('groupIndex'), null, book.doc()) as NamedNode | null |
| 128 | + if (groupIndex) { |
| 129 | + if (await loadDocumentSilently(store, groupIndex)) { |
| 130 | + const groups = uniqueNamedNodes([ |
| 131 | + ...(store.each(book, ns.vcard('includesGroup'), null, groupIndex) as NamedNode[]), |
| 132 | + ...(store.each(book, ns.vcard('includesGroup'), null, book.doc()) as NamedNode[]) |
| 133 | + ]) |
| 134 | + |
| 135 | + for (const group of groups) { |
| 136 | + await loadDocumentSilently(store, group) |
| 137 | + |
| 138 | + await onEntry({ |
| 139 | + kind: 'group', |
| 140 | + uri: group.value, |
| 141 | + label: labelForNode(store, group), |
| 142 | + subjectType: 'agentGroup', |
| 143 | + relationshipLabel: 'Group', |
| 144 | + sources: ['groups'] |
| 145 | + }) |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + return { |
| 154 | + discoverAddressBookContacts, |
| 155 | + discoverAddressBookGroups |
| 156 | + } |
| 157 | +} |
0 commit comments