Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/directory/directoryHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, ' ')
}

Expand Down
52 changes: 52 additions & 0 deletions src/directory/friendsDirectory.test.ts
Original file line number Diff line number Diff line change
@@ -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'
}))
})
})
24 changes: 3 additions & 21 deletions src/directory/friendsDirectory.ts
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -15,8 +15,7 @@ export function createFriendsDirectoryDiscovery (store: LiveStore) {
depth: number,
onEntry: (entry: DirectoryEntry) => void | Promise<void>
): Promise<void> {
const personName = storedLabelForNode(store, person)
if (!personName) return
const personName = labelForNode(store, person)

await onEntry({
kind: 'person',
Expand All @@ -34,7 +33,6 @@ export function createFriendsDirectoryDiscovery (store: LiveStore) {
onEntry: (entry: DirectoryEntry) => void | Promise<void>
): Promise<void> {
const visited = new Set<string>([user.value])
const emitted = new Set<string>()
const loadedDocs = new Set<string>()
let queue: Array<{ person: NamedNode, depth: number }> = [{ person: user, depth: 0 }]

Expand All @@ -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) {
Expand All @@ -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 })
Expand Down
Loading