diff --git a/src/directory/directoryHelpers.ts b/src/directory/directoryHelpers.ts index c506ea0..ac30b63 100644 --- a/src/directory/directoryHelpers.ts +++ b/src/directory/directoryHelpers.ts @@ -119,9 +119,18 @@ export function isHttpUri (value: string | null | undefined): boolean { export function fallbackLabel (uri: string): string { const withoutFragment = uri.split('#')[0] const fragment = uri.includes('#') ? uri.slice(uri.indexOf('#') + 1) : '' + let profileName = '' + try { + const url = new URL(withoutFragment) + const pathParts = url.pathname.split('/').filter(Boolean) + const profileIndex = pathParts.findIndex((part, index) => part === 'profile' && pathParts[index + 1] === 'card') + profileName = profileIndex > 0 ? pathParts[profileIndex - 1] : url.hostname + } catch { + // Keep using the URI path as a label for non-URL identifiers. + } const base = fragment && fragment !== 'this' && fragment !== 'me' ? fragment - : withoutFragment.slice(withoutFragment.lastIndexOf('/') + 1) + : profileName || withoutFragment.slice(withoutFragment.lastIndexOf('/') + 1) return decodeURIComponent(base || uri).replace(/[_-]+/g, ' ') } diff --git a/src/directory/friendsDirectory.test.ts b/src/directory/friendsDirectory.test.ts new file mode 100644 index 0000000..4886eb5 --- /dev/null +++ b/src/directory/friendsDirectory.test.ts @@ -0,0 +1,52 @@ +import { graph, st, sym } from 'rdflib' +import { describe, expect, it, vi } from 'vitest' +import { createDirectoryLogic } from './directoryLogic' +import { ns } from '../util/ns' + +describe('friends directory discovery', () => { + it('finds a friend by the WebID account name when no stored name is available', async () => { + const store = graph() as any + const user = sym('https://pod.example/profile/card#me') + const friend = sym('https://sharonsolidosplayground.com/timmy/profile/card#me') + store.add(st(user, ns.foaf('knows'), friend, user.doc())) + store.fetcher = { load: vi.fn().mockResolvedValue(undefined) } + const directory = createDirectoryLogic(store, {} as any, {} as any) + const entries = await directory.search({ + user, + sources: ['friends'], + maxFoafDistance: 1, + query: 'ti' + }) + + expect(entries).toContainEqual(expect.objectContaining({ + kind: 'person', + uri: friend.value, + label: 'timmy' + })) + }) + + it('finds a friend of a friend by the WebID account name', async () => { + const store = graph() as any + const user = sym('https://pod.example/profile/card#me') + const friend = sym('https://dimitri.example/profile/card#me') + const friendOfFriend = sym('https://pod.example/marcus/profile/card#me') + store.add(st(user, ns.foaf('knows'), friend, user.doc())) + store.add(st(friend, ns.foaf('knows'), friendOfFriend, friend.doc())) + store.fetcher = { load: vi.fn().mockResolvedValue(undefined) } + const directory = createDirectoryLogic(store, {} as any, {} as any) + + const entries = await directory.search({ + user, + sources: ['friends'], + maxFoafDistance: 3, + query: 'marcus' + }) + + expect(entries).toContainEqual(expect.objectContaining({ + kind: 'person', + uri: friendOfFriend.value, + label: 'marcus', + relationshipLabel: 'People' + })) + }) +}) diff --git a/src/directory/friendsDirectory.ts b/src/directory/friendsDirectory.ts index b5115b4..d4a4cf7 100644 --- a/src/directory/friendsDirectory.ts +++ b/src/directory/friendsDirectory.ts @@ -1,7 +1,7 @@ import { type DirectoryEntry } from '../types' import { type LiveStore, NamedNode } from 'rdflib' import { ns } from '../util/ns' -import { loadDocumentSilently, storedLabelForNode } from './directoryHelpers' +import { labelForNode, loadDocumentSilently } from './directoryHelpers' const PEOPLE_SEARCH_CONCURRENCY = 6 @@ -15,8 +15,7 @@ export function createFriendsDirectoryDiscovery (store: LiveStore) { depth: number, onEntry: (entry: DirectoryEntry) => void | Promise ): Promise { - const personName = storedLabelForNode(store, person) - if (!personName) return + const personName = labelForNode(store, person) await onEntry({ kind: 'person', @@ -34,7 +33,6 @@ export function createFriendsDirectoryDiscovery (store: LiveStore) { onEntry: (entry: DirectoryEntry) => void | Promise ): Promise { const visited = new Set([user.value]) - const emitted = new Set() const loadedDocs = new Set() let queue: Array<{ person: NamedNode, depth: number }> = [{ person: user, depth: 0 }] @@ -47,10 +45,7 @@ export function createFriendsDirectoryDiscovery (store: LiveStore) { } if (current.value !== user.value) { - if (!emitted.has(current.value)) { - emitted.add(current.value) - await emitPersonEntry(current, depth, onEntry) - } + await emitPersonEntry(current, depth, onEntry) } if (depth >= maxFoafDistance) { @@ -61,19 +56,6 @@ export function createFriendsDirectoryDiscovery (store: LiveStore) { const contacts = store.each(current, ns.foaf('knows')) as NamedNode[] for (const contact of contacts) { if (contact.termType === 'NamedNode') { - const contactName = storedLabelForNode(store, contact) - if (contact.value !== user.value && contactName && !emitted.has(contact.value)) { - emitted.add(contact.value) - await onEntry({ - kind: 'person', - uri: contact.value, - label: contactName, - subjectType: 'agent', - relationshipLabel: depth === 0 ? 'Friend' : 'People', - sources: ['friends'] - }) - } - if (!visited.has(contact.value)) { visited.add(contact.value) nextPeople.push({ person: contact, depth: depth + 1 })