Skip to content
Open
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "solid-logic",
"version": "6.0.0-0",
"version": "6.0.0-1",
"description": "Core business logic of SolidOS",
"main": "dist/index.cjs.js",
"module": "dist/index.esm.js",
Expand Down
6 changes: 5 additions & 1 deletion src/authn/SolidAuthnLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ export class SolidAuthnLogic implements AuthnLogic {
private checkUserInFlight: Promise<NamedNode | null> | null = null
private sessionRestoreHookAttached = false
private fallbackWebId: string | null = null
private readonly onSessionRestore?: () => void

constructor(solidAuthSession: SessionWithLegacyEvents) {
constructor(solidAuthSession: SessionWithLegacyEvents, onSessionRestore?: () => void) {
this.session = solidAuthSession
this.onSessionRestore = onSessionRestore
}

// we created authSession getter because we want to access it as authn.authSession externally
Expand Down Expand Up @@ -147,6 +149,7 @@ export class SolidAuthnLogic implements AuthnLogic {
const isNowActive = sessionAny?.isActive ?? Boolean(sessionAny?.webId)
if (!wasActive && isNowActive) {
sessionAny.events?.emit('sessionRestore', window.location.href)
this.onSessionRestore?.()
}
}
if (typeof sessionAny?.handleRedirectFromLogin === 'function') {
Expand All @@ -155,6 +158,7 @@ export class SolidAuthnLogic implements AuthnLogic {
const isNowActive = sessionAny?.isActive ?? Boolean(sessionAny?.webId)
if (!wasActive && isNowActive) {
sessionAny.events?.emit('login')
this.onSessionRestore?.()
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/logic/solidLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export function createSolidLogic(specialFetch: { fetch: (url: any, requestInit:
store.updater = new rdf.UpdateManager(store) // Add real-time live updates store.updater
store.features = [] // disable automatic node merging on store load

const authn: AuthnLogic = new SolidAuthnLogic(session)
const authn: AuthnLogic = new SolidAuthnLogic(session, () => {
store.updater.flagAuthorizationMetadata() as any
})

const acl = createAclLogic(store)
const containerLogic = createContainerLogic(store)
Expand Down
26 changes: 24 additions & 2 deletions src/resource/resourceLogic.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { NamedNode, sym } from 'rdflib'
import { NamedNode, sym, LiveStore } from 'rdflib'
import { ACL_LINK } from '../acl/aclLogic'
import { ns } from '../util/ns'
import { assertSuccessfulHttpResponse, isMissingError } from './resourceHttp'
import { readWacAccessInfo } from './resourceMetadata'
import { type AclLogic, type ResourceAccess, type ResourceAccessWithDelete, type ResourceDeleteOptions, type ResourceLogic, type ResourceMetadata, type ResourceMetadataWithDelete, type TypeIndexLogic } from '../types'

export function createResourceLogic(store, aclLogic: AclLogic, containerLogic, typeIndexLogic: TypeIndexLogic): ResourceLogic {
export function createResourceLogic(store: LiveStore, aclLogic: AclLogic, containerLogic, typeIndexLogic: TypeIndexLogic): ResourceLogic {
function createContainer(url: string) {
return containerLogic.createContainer(url)
}
Expand Down Expand Up @@ -168,11 +168,33 @@ export function createResourceLogic(store, aclLogic: AclLogic, containerLogic, t
await recursiveDelete(resourceNode, { deleteTypeIndexes: true, user })
}

async function checkAndRefreshEditable(resourceNode: NamedNode | null | undefined): Promise<boolean> {
if (!resourceNode || !store.updater || !store.fetcher || typeof store.fetcher.refresh !== 'function') return false

const resourceUri = resourceNode.uri || resourceNode.value || ''
if (!resourceUri) return false

const editable = store.updater.editable(resourceUri, store)
if (editable !== false && editable !== undefined) {
return true
}

try {
await store.fetcher.refresh(resourceNode)
} catch (error) {
throw error instanceof Error ? error : new Error(`Failed to refresh <${resourceUri}>`)
}

const editableAfterRefresh = store.updater.editable(resourceUri, store)
return editableAfterRefresh !== false && editableAfterRefresh !== undefined
}

return {
recursiveDelete,
deleteResourceAndTypeIndexIfExists,
fetchMetadata,
fetchMetadataWithDelete,
checkAndRefreshEditable,
createContainer,
isContainer,
getContainerMemberCount
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export interface ResourceLogic {
deleteResourceAndTypeIndexIfExists: (resource: NamedNode, user?: NamedNode | null) => Promise<void>,
fetchMetadata: (subject: NamedNode) => Promise<ResourceMetadata>,
fetchMetadataWithDelete: (subject: NamedNode) => Promise<ResourceMetadataWithDelete>,
checkAndRefreshEditable: (resource: NamedNode | null | undefined) => Promise<boolean>,
createContainer: (url: string) => Promise<void>,
isContainer: (resource: NamedNode) => boolean,
getContainerMemberCount: (resource: NamedNode) => number
Expand Down
17 changes: 17 additions & 0 deletions test/resourceLogic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,21 @@ describe('resourceLogic', () => {
await expect(resourceLogic.recursiveDelete(resource)).resolves.toBeUndefined()
expect(store.removeDocument).toHaveBeenCalledWith(resource)
})

it('refreshes a resource when editability is stale and returns the refreshed editability', async () => {
const resourceLogic = createResourceLogic(store, aclLogic, containerLogic, typeIndexLogic)
const resource = sym('https://example.com/profile/card')
const editable = vi.fn().mockReturnValueOnce(false).mockReturnValueOnce(true)
const refresh = vi.fn().mockResolvedValue(undefined)

store.updater = {
editable
} as unknown as LiveStore['updater']
store.fetcher.refresh = refresh as unknown as Fetcher['refresh']

await expect(resourceLogic.checkAndRefreshEditable(resource)).resolves.toBe(true)
expect(editable).toHaveBeenNthCalledWith(1, resource.uri, store)
expect(refresh).toHaveBeenCalledWith(resource)
expect(editable).toHaveBeenNthCalledWith(2, resource.uri, store)
})
})
Loading