solid-logic 1.3.17-a849582e → 1.3.17-a8db1c4b

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/lib/acl/aclLogic.d.ts +2 -11
  2. package/lib/acl/aclLogic.d.ts.map +1 -1
  3. package/lib/acl/aclLogic.js +5 -7
  4. package/lib/acl/aclLogic.js.map +1 -1
  5. package/lib/chat/chatLogic.d.ts +2 -15
  6. package/lib/chat/chatLogic.d.ts.map +1 -1
  7. package/lib/chat/chatLogic.js +8 -7
  8. package/lib/chat/chatLogic.js.map +1 -1
  9. package/lib/inbox/inboxLogic.d.ts +2 -6
  10. package/lib/inbox/inboxLogic.d.ts.map +1 -1
  11. package/lib/inbox/inboxLogic.js.map +1 -1
  12. package/lib/index.d.ts +7 -9
  13. package/lib/index.d.ts.map +1 -1
  14. package/lib/index.js +10 -74
  15. package/lib/index.js.map +1 -1
  16. package/lib/logic/solidLogic.d.ts +6 -0
  17. package/lib/logic/solidLogic.d.ts.map +1 -0
  18. package/lib/logic/solidLogic.js +92 -0
  19. package/lib/logic/solidLogic.js.map +1 -0
  20. package/lib/logic/solidLogicSingleton.d.ts +2 -35
  21. package/lib/logic/solidLogicSingleton.d.ts.map +1 -1
  22. package/lib/logic/solidLogicSingleton.js +7 -86
  23. package/lib/logic/solidLogicSingleton.js.map +1 -1
  24. package/lib/profile/profileLogic.d.ts +2 -12
  25. package/lib/profile/profileLogic.d.ts.map +1 -1
  26. package/lib/profile/profileLogic.js +1 -23
  27. package/lib/profile/profileLogic.js.map +1 -1
  28. package/lib/typeIndex/typeIndexLogic.d.ts +2 -31
  29. package/lib/typeIndex/typeIndexLogic.d.ts.map +1 -1
  30. package/lib/typeIndex/typeIndexLogic.js +12 -359
  31. package/lib/typeIndex/typeIndexLogic.js.map +1 -1
  32. package/lib/types.d.ts +65 -1
  33. package/lib/types.d.ts.map +1 -1
  34. package/lib/util/ns.d.ts +2 -0
  35. package/lib/util/ns.d.ts.map +1 -0
  36. package/lib/util/ns.js +34 -0
  37. package/lib/util/ns.js.map +1 -0
  38. package/package.json +1 -1
  39. package/src/acl/aclLogic.ts +8 -5
  40. package/src/chat/chatLogic.ts +7 -7
  41. package/src/inbox/inboxLogic.ts +2 -1
  42. package/src/index.ts +12 -83
  43. package/src/logic/solidLogic.ts +75 -0
  44. package/src/logic/solidLogicSingleton.ts +6 -159
  45. package/src/profile/profileLogic.ts +4 -12
  46. package/src/typeIndex/typeIndexLogic.ts +6 -277
  47. package/src/types.ts +76 -1
  48. package/src/util/{ns.js → ns.ts} +0 -0
  49. package/test/logic.test.ts +5 -6
  50. package/test/typeIndexLogic.test.ts +484 -54
  51. package/lib/discovery/discoveryLogic.d.ts +0 -37
  52. package/lib/discovery/discoveryLogic.d.ts.map +0 -1
  53. package/lib/discovery/discoveryLogic.js +0 -500
  54. package/lib/discovery/discoveryLogic.js.map +0 -1
  55. package/src/discovery/discoveryLogic.ts +0 -267
  56. package/test/discoveryLogic.test.ts +0 -712
  57. package/test/typeIndexLogicPart2.test.ts +0 -485
@@ -1,267 +0,0 @@
1
- import { NamedNode, LiveStore, sym, st } from 'rdflib'
2
- import * as $rdf from 'rdflib'
3
- import { newThing } from "../util/utils"
4
- import solidNamespace from 'solid-namespace'
5
- import { authn } from '../logic/solidLogicSingleton'
6
-
7
- const ns = solidNamespace($rdf)
8
-
9
- type TypeIndexScope = { label: string, index: NamedNode, agent: NamedNode }
10
- type ScopedApp = { instance: NamedNode, scope: TypeIndexScope }
11
-
12
- /**
13
- * Create a resource if it really does not exist
14
- * Be absolutely sure something does not exist before creating a new empty file
15
- * as otherwise existing could be deleted.
16
- * @param doc {NamedNode} - The resource
17
- */
18
- export async function loadOrCreateIfNotExists (store: LiveStore, doc: NamedNode) {
19
- let response
20
- try {
21
- response = await store.fetcher.load(doc)
22
- } catch (err) {
23
- if (err.response.status === 404) {
24
- try {
25
- store.fetcher.webOperation('PUT', doc, {data: '', contentType: 'text/turtle'})
26
- } catch (err) {
27
- const msg = 'createIfNotExists: PUT FAILED: ' + doc + ': ' + err
28
- throw new Error(msg)
29
- }
30
- delete store.fetcher.requested[doc.uri] // delete cached 404 error
31
- } else {
32
- const msg = 'createIfNotExists doc load error NOT 404: ' + doc + ': ' + err
33
- throw new Error(msg) // @@ add nested errors
34
- }
35
- }
36
- return response
37
- }
38
-
39
- export function suggestPreferencesFile (me:NamedNode) {
40
- const stripped = me.uri.replace('/profile/', '/').replace('/public/', '/')
41
- // const stripped = me.uri.replace(\/[p|P]rofile/\g, '/').replace(\/[p|P]ublic/\g, '/')
42
- const folderURI = stripped.split('/').slice(0,-1).join('/') + '/Settings/'
43
- const fileURI = folderURI + 'Preferences.ttl'
44
- return sym(fileURI)
45
- }
46
-
47
- export function suggestPublicTypeIndex (me:NamedNode) {
48
- return sym(me.doc().dir()?.uri + 'publicTypeIndex.ttl')
49
- }
50
- // Note this one is based off the pref file not the profile
51
-
52
- export function suggestPrivateTypeIndex (preferencesFile:NamedNode) {
53
- return sym(preferencesFile.doc().dir()?.uri + 'privateTypeIndex.ttl')
54
- }
55
-
56
- /* Follow link from this doc to another thing, or else make a new link
57
- **
58
- ** return: null no ld one and failed to make a new one
59
- */
60
- export async function followOrCreateLink (store: LiveStore, subject: NamedNode, predicate: NamedNode,
61
- object: NamedNode, doc:NamedNode):Promise<NamedNode | null> {
62
- await store.fetcher.load(doc)
63
- const result = store.any(subject, predicate, null, doc)
64
-
65
- if (result) return result as NamedNode
66
- if (!store.updater.editable(doc)) {
67
- return null
68
- }
69
- try {
70
- await store.updater.update([], [ st(subject, predicate, object, doc)])
71
- } catch (err) {
72
- console.warn(`followOrCreateLink: Error making link in ${doc} to ${object}: ${err}`)
73
- return null
74
- }
75
-
76
- try {
77
- await loadOrCreateIfNotExists(store, object)
78
- // store.fetcher.webOperation('PUT', object, { data: '', contentType: 'text/turtle'})
79
- } catch (err) {
80
- console.warn(`followOrCreateLink: Error loading or saving new linked document: ${object}: ${err}`)
81
- }
82
- return object
83
- }
84
-
85
- export async function loadProfile (store: LiveStore, user: NamedNode) {
86
- if (!user) {
87
- throw new Error(`loadProfile: no user given.`)
88
- }
89
- try {
90
- await store.fetcher.load(user.doc())
91
- } catch (err) {
92
- throw new Error(`Unable to load profile of user ${user}: ${err}`)
93
- }
94
- return user.doc()
95
- }
96
-
97
- export async function loadPreferences (store: LiveStore, user: NamedNode): Promise <NamedNode | undefined > {
98
- await loadProfile(store as LiveStore, user)
99
-
100
- const possiblePreferencesFile = suggestPreferencesFile(user)
101
-
102
- const preferencesFile = await followOrCreateLink(store, user, ns.space('preferencesFile') as NamedNode, possiblePreferencesFile, user.doc())
103
-
104
- if (!preferencesFile) {
105
- const message = `User ${user} has no pointer in profile to preferences file.`
106
- console.warn(message)
107
- return undefined
108
- }
109
- try {
110
- await store.fetcher.load(preferencesFile as NamedNode)
111
- } catch (err) { // Maybe a permission propblem or origin problem
112
- return undefined
113
- }
114
- return preferencesFile as NamedNode
115
- }
116
-
117
- export async function loadTypeIndexesFor (store: LiveStore, user:NamedNode): Promise<Array<TypeIndexScope>> {
118
- if (!user) throw new Error(`loadTypeIndexesFor: No user given`)
119
- const profile = await loadProfile(store, user)
120
-
121
- const suggestion = suggestPublicTypeIndex(user)
122
-
123
- const publicTypeIndex = await followOrCreateLink(store, user, ns.solid('publicTypeIndex') as NamedNode, suggestion, profile)
124
-
125
- const publicScopes = publicTypeIndex ? [ { label: 'public', index: publicTypeIndex as NamedNode, agent: user } ] : []
126
-
127
- let preferencesFile
128
- try {
129
- preferencesFile = await loadPreferences(store, user)
130
- } catch (err) {
131
- preferencesFile = null
132
- }
133
-
134
- let privateScopes
135
- if (preferencesFile) { // watch out - can be in either as spec was not clear. Legacy is profile.
136
- // If there is a legacy one linked from the profile, use that.
137
- // Otherwiae use or make one linked from Preferences
138
- const suggestedPrivateTypeIndex = suggestPrivateTypeIndex(preferencesFile)
139
-
140
- const privateTypeIndex = store.any(user, ns.solid('privateTypeIndex'), undefined, profile) ||
141
-
142
- await followOrCreateLink(store, user, ns.solid('privateTypeIndex') as NamedNode, suggestedPrivateTypeIndex, preferencesFile);
143
-
144
- privateScopes = privateTypeIndex ? [ { label: 'private', index: privateTypeIndex as NamedNode, agent: user } ] : []
145
- } else {
146
- privateScopes = []
147
- }
148
- const scopes = publicScopes.concat(privateScopes)
149
- if (scopes.length === 0) return scopes
150
- const files = scopes.map(scope => scope.index)
151
- try {
152
- await store.fetcher.load(files)
153
- } catch (err) {
154
- console.warn('Problems loading type index: ', err)
155
- }
156
- return scopes
157
- }
158
-
159
- export async function loadCommunityTypeIndexes (store:LiveStore, user:NamedNode): Promise<TypeIndexScope[][]> {
160
- const preferencesFile = await loadPreferences(store, user)
161
- if (preferencesFile) { // For now, pick up communities as simple links from the preferences file.
162
- const communities = store.each(user, ns.solid('community'), undefined, preferencesFile as NamedNode).concat(
163
- store.each(user, ns.solid('community'), undefined, user.doc() as NamedNode)
164
- )
165
- let result = []
166
- for (const org of communities) {
167
- result = result.concat(await loadTypeIndexesFor(store, org as NamedNode) as any)
168
- }
169
- return result
170
- }
171
- return [] // No communities
172
- }
173
-
174
- export async function loadAllTypeIndexes (store:LiveStore, user:NamedNode) {
175
- return (await loadTypeIndexesFor(store, user)).concat((await loadCommunityTypeIndexes(store, user)).flat())
176
- }
177
-
178
- // Utility: remove duplicates from Array of NamedNodes
179
- export function uniqueNodes (arr: NamedNode[]): NamedNode[] {
180
- const uris = arr.map(x => x.uri)
181
- const set = new Set(uris)
182
- const uris2 = Array.from(set)
183
- const arr2 = uris2.map(u => new NamedNode(u))
184
- return arr2 // Array.from(new Set(arr.map(x => x.uri))).map(u => sym(u))
185
- }
186
-
187
- export async function getScopedAppsFromIndex (store, scope, theClass: NamedNode | null) {
188
- const index = scope.index
189
- const registrations = store.statementsMatching(null, ns.solid('instance'), null, index)
190
- .concat(store.statementsMatching(null, ns.solid('instanceContainer'), null, index))
191
- .map(st => st.subject)
192
- const relevant = theClass ? registrations.filter(reg => store.any(reg, ns.solid('forClass'), null, index).sameTerm(theClass))
193
- : registrations
194
- const directInstances = relevant.map(reg => store.each(reg as NamedNode, ns.solid('instance'), null, index)).flat()
195
- let instances = uniqueNodes(directInstances)
196
-
197
- const instanceContainers = relevant.map(
198
- reg => store.each(reg as NamedNode, ns.solid('instanceContainer'), null, index)).flat()
199
-
200
- // instanceContainers may be deprocatable if no one has used them
201
- const containers = uniqueNodes(instanceContainers)
202
- if (containers.length > 0) { console.log('@@ getScopedAppsFromIndex containers ', containers)}
203
- for (let i = 0; i < containers.length; i++) {
204
- const cont = containers[i]
205
- await store.fetcher.load(cont)
206
- const contents = store.each(cont, ns.ldp('contains'), null, cont)
207
- instances = instances.concat(contents)
208
- }
209
- return instances.map(instance => { return {instance, scope}})
210
- }
211
-
212
- export async function getScopedAppInstances (store:LiveStore, klass: NamedNode, user: NamedNode):Promise<ScopedApp[]> {
213
- const scopes = await loadAllTypeIndexes(store, user)
214
- let scopedApps = []
215
- for (const scope of scopes) {
216
- const scopedApps0 = await getScopedAppsFromIndex(store, scope, klass) as any
217
- scopedApps = scopedApps.concat(scopedApps0)
218
- }
219
- return scopedApps
220
- }
221
-
222
- // This is the function signature which used to be in solid-ui/logic
223
- // Recommended to use getScopedAppInstances instead as it provides more information.
224
- //
225
- export async function getAppInstances (store:LiveStore, klass: NamedNode): Promise<NamedNode[]> {
226
- const user = authn.currentUser()
227
- if (!user) throw new Error('getAppInstances: Must be logged in to find apps.')
228
- const scopedAppInstances = await getScopedAppInstances(store, klass, user)
229
- return scopedAppInstances.map(scoped => scoped.instance)
230
- }
231
-
232
- /*
233
- * Register a new app in a type index
234
- * used in chat in bookmark.js (solid-ui)
235
- * Returns the registration object if successful else null
236
- */
237
- export async function registerInstanceInTypeIndex (
238
- store:LiveStore,
239
- instance: NamedNode,
240
- index: NamedNode,
241
- theClass: NamedNode,
242
- // agent: NamedNode
243
- ): Promise<NamedNode | null> {
244
- const registration = newThing(index)
245
- const ins = [
246
- // See https://github.com/solid/solid/blob/main/proposals/data-discovery.md
247
- st(registration, ns.rdf('type'), ns.solid('TypeRegistration'), index),
248
- st(registration, ns.solid('forClass'), theClass, index),
249
- st(registration, ns.solid('instance'), instance, index)
250
- ]
251
- try {
252
- await store.updater.update([], ins)
253
- } catch (err) {
254
- const msg = `Unable to register ${instance} in index ${index}: ${err}`
255
- console.warn(msg)
256
- return null
257
- }
258
- return registration
259
- }
260
-
261
- export async function deleteTypeIndexRegistration (store: LiveStore, item) {
262
- const reg = store.the(null, ns.solid('instance'), item.instance, item.scope.index) as NamedNode
263
- if (!reg) throw new Error(`deleteTypeIndexRegistration: No registration found for ${item.instance}`)
264
- const statements = store.statementsMatching(reg, null, null, item.scope.index)
265
- await store.updater.update(statements, [])
266
- }
267
- // ENDS