solid-logic 1.3.17-615b75c1 → 1.3.17-6e0634d8

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 (127) hide show
  1. package/README.md +2 -2
  2. package/jest.config.js +1 -1
  3. package/lib/acl/aclLogic.d.ts +3 -30
  4. package/lib/acl/aclLogic.d.ts.map +1 -1
  5. package/lib/acl/aclLogic.js +150 -119
  6. package/lib/acl/aclLogic.js.map +1 -1
  7. package/lib/authn/SolidAuthnLogic.d.ts.map +1 -1
  8. package/lib/authn/SolidAuthnLogic.js +2 -2
  9. package/lib/authn/SolidAuthnLogic.js.map +1 -1
  10. package/lib/chat/chatLogic.d.ts +3 -0
  11. package/lib/chat/chatLogic.d.ts.map +1 -0
  12. package/lib/chat/{ChatLogic.js → chatLogic.js} +82 -86
  13. package/lib/chat/chatLogic.js.map +1 -0
  14. package/lib/inbox/inboxLogic.d.ts +3 -0
  15. package/lib/inbox/inboxLogic.d.ts.map +1 -0
  16. package/lib/inbox/{InboxLogic.js → inboxLogic.js} +59 -64
  17. package/lib/inbox/inboxLogic.js.map +1 -0
  18. package/lib/index.d.ts +6 -11
  19. package/lib/index.d.ts.map +1 -1
  20. package/lib/index.js +5 -25
  21. package/lib/index.js.map +1 -1
  22. package/lib/issuer/issuerLogic.js +1 -1
  23. package/lib/issuer/issuerLogic.js.map +1 -1
  24. package/lib/logic/CustomError.d.ts +4 -0
  25. package/lib/logic/CustomError.d.ts.map +1 -1
  26. package/lib/logic/CustomError.js +17 -1
  27. package/lib/logic/CustomError.js.map +1 -1
  28. package/lib/logic/solidLogic.d.ts +6 -0
  29. package/lib/logic/solidLogic.d.ts.map +1 -0
  30. package/lib/logic/solidLogic.js +92 -0
  31. package/lib/logic/solidLogic.js.map +1 -0
  32. package/lib/logic/solidLogicSingleton.d.ts +1 -2
  33. package/lib/logic/solidLogicSingleton.d.ts.map +1 -1
  34. package/lib/logic/solidLogicSingleton.js +3 -3
  35. package/lib/logic/solidLogicSingleton.js.map +1 -1
  36. package/lib/profile/profileLogic.d.ts +3 -0
  37. package/lib/profile/profileLogic.d.ts.map +1 -0
  38. package/lib/profile/profileLogic.js +246 -0
  39. package/lib/profile/profileLogic.js.map +1 -0
  40. package/lib/typeIndex/typeIndexLogic.d.ts +2 -21
  41. package/lib/typeIndex/typeIndexLogic.d.ts.map +1 -1
  42. package/lib/typeIndex/typeIndexLogic.js +304 -296
  43. package/lib/typeIndex/typeIndexLogic.js.map +1 -1
  44. package/lib/types.d.ts +82 -1
  45. package/lib/types.d.ts.map +1 -1
  46. package/lib/util/containerLogic.d.ts +11 -0
  47. package/lib/util/containerLogic.d.ts.map +1 -0
  48. package/lib/{profile/ProfileLogic.js → util/containerLogic.js} +51 -44
  49. package/lib/util/containerLogic.js.map +1 -0
  50. package/lib/util/ns.d.ts +2 -0
  51. package/lib/util/ns.d.ts.map +1 -0
  52. package/lib/util/ns.js +34 -0
  53. package/lib/util/ns.js.map +1 -0
  54. package/lib/util/utilityLogic.d.ts +15 -0
  55. package/lib/util/utilityLogic.d.ts.map +1 -0
  56. package/lib/util/utilityLogic.js +274 -0
  57. package/lib/util/utilityLogic.js.map +1 -0
  58. package/lib/util/utils.d.ts +8 -0
  59. package/lib/util/utils.d.ts.map +1 -0
  60. package/lib/util/utils.js +48 -0
  61. package/lib/util/utils.js.map +1 -0
  62. package/package.json +5 -1
  63. package/src/acl/aclLogic.ts +136 -118
  64. package/src/authn/SolidAuthnLogic.ts +3 -2
  65. package/src/chat/chatLogic.ts +225 -0
  66. package/src/inbox/inboxLogic.ts +58 -0
  67. package/src/index.ts +11 -29
  68. package/src/issuer/issuerLogic.ts +1 -1
  69. package/src/logic/CustomError.ts +5 -1
  70. package/src/logic/solidLogic.ts +75 -0
  71. package/src/logic/solidLogicSingleton.ts +3 -3
  72. package/src/profile/profileLogic.ts +126 -0
  73. package/src/typeIndex/typeIndexLogic.ts +175 -182
  74. package/src/types.ts +83 -4
  75. package/src/util/containerLogic.ts +53 -0
  76. package/src/util/ns.ts +5 -0
  77. package/src/util/utilityLogic.ts +156 -0
  78. package/src/util/utils.ts +52 -0
  79. package/test/aclLogic.test.ts +13 -4
  80. package/test/chatLogic.test.ts +70 -71
  81. package/test/container.test.ts +57 -0
  82. package/test/helpers/dataSetup.ts +134 -0
  83. package/test/helpers/setup.ts +1 -0
  84. package/test/inboxLogic.test.ts +40 -38
  85. package/test/logic.test.ts +10 -9
  86. package/test/profileLogic.test.ts +246 -0
  87. package/test/typeIndexLogic.test.ts +487 -30
  88. package/test/utilityLogic.test.ts +172 -126
  89. package/test/utils.test.ts +32 -0
  90. package/lib/chat/ChatLogic.d.ts +0 -26
  91. package/lib/chat/ChatLogic.d.ts.map +0 -1
  92. package/lib/chat/ChatLogic.js.map +0 -1
  93. package/lib/chat/determineChatContainer.d.ts +0 -3
  94. package/lib/chat/determineChatContainer.d.ts.map +0 -1
  95. package/lib/chat/determineChatContainer.js +0 -12
  96. package/lib/chat/determineChatContainer.js.map +0 -1
  97. package/lib/discovery/discoveryLogic.d.ts +0 -40
  98. package/lib/discovery/discoveryLogic.d.ts.map +0 -1
  99. package/lib/discovery/discoveryLogic.js +0 -494
  100. package/lib/discovery/discoveryLogic.js.map +0 -1
  101. package/lib/inbox/InboxLogic.d.ts +0 -18
  102. package/lib/inbox/InboxLogic.d.ts.map +0 -1
  103. package/lib/inbox/InboxLogic.js.map +0 -1
  104. package/lib/logic/SolidLogic.d.ts +0 -48
  105. package/lib/logic/SolidLogic.d.ts.map +0 -1
  106. package/lib/logic/SolidLogic.js +0 -321
  107. package/lib/logic/SolidLogic.js.map +0 -1
  108. package/lib/profile/ProfileLogic.d.ts +0 -13
  109. package/lib/profile/ProfileLogic.d.ts.map +0 -1
  110. package/lib/profile/ProfileLogic.js.map +0 -1
  111. package/lib/util/UtilityLogic.d.ts +0 -33
  112. package/lib/util/UtilityLogic.d.ts.map +0 -1
  113. package/lib/util/UtilityLogic.js +0 -240
  114. package/lib/util/UtilityLogic.js.map +0 -1
  115. package/lib/util/uri.d.ts +0 -3
  116. package/lib/util/uri.d.ts.map +0 -1
  117. package/lib/util/uri.js +0 -9
  118. package/lib/util/uri.js.map +0 -1
  119. package/src/chat/ChatLogic.ts +0 -244
  120. package/src/chat/determineChatContainer.ts +0 -14
  121. package/src/discovery/discoveryLogic.ts +0 -311
  122. package/src/inbox/InboxLogic.ts +0 -66
  123. package/src/logic/SolidLogic.ts +0 -262
  124. package/src/profile/ProfileLogic.ts +0 -44
  125. package/src/util/UtilityLogic.ts +0 -161
  126. package/src/util/uri.ts +0 -5
  127. package/test/discoveryLogic.test.ts +0 -740
package/lib/util/uri.js DELETED
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.newThing = void 0;
4
- var rdflib_1 = require("rdflib");
5
- function newThing(doc) {
6
- return (0, rdflib_1.sym)(doc.uri + "#" + "id" + ("" + Date.now()));
7
- }
8
- exports.newThing = newThing;
9
- //# sourceMappingURL=uri.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"uri.js","sourceRoot":"","sources":["../../src/util/uri.ts"],"names":[],"mappings":";;;AAAA,iCAAwC;AAExC,SAAgB,QAAQ,CAAC,GAAc;IACrC,OAAO,IAAA,YAAG,EAAC,GAAG,CAAC,GAAG,GAAG,GAAG,GAAG,IAAI,GAAG,CAAC,EAAE,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;AACvD,CAAC;AAFD,4BAEC"}
@@ -1,244 +0,0 @@
1
- import { NamedNode, Node, st, term, LiveStore } from "rdflib";
2
- import { ProfileLogic } from "../profile/ProfileLogic";
3
- import { SolidNamespace } from "../types";
4
- import { newThing } from "../util/uri";
5
- import { determineChatContainer } from "./determineChatContainer";
6
-
7
- const CHAT_LOCATION_IN_CONTAINER = "index.ttl#this";
8
-
9
- interface NewPaneOptions {
10
- me?: NamedNode;
11
- newInstance?: NamedNode;
12
- newBase: string;
13
- }
14
-
15
- interface CreatedPaneOptions {
16
- newInstance: NamedNode;
17
- }
18
-
19
- /**
20
- * Chat-related logic
21
- */
22
- export class ChatLogic {
23
- store: LiveStore;
24
- ns: SolidNamespace;
25
- profile: ProfileLogic;
26
-
27
- constructor(store: LiveStore, ns: SolidNamespace, profile: ProfileLogic) {
28
- this.store = store;
29
- this.ns = ns;
30
- this.profile = profile;
31
- }
32
-
33
- async setAcl(
34
- chatContainer: NamedNode,
35
- me: NamedNode,
36
- invitee: NamedNode
37
- ): Promise<void> {
38
- // Some servers don't present a Link http response header
39
- // if the container doesn't exist yet, so refetch the container
40
- // now that it has been created:
41
- await this.store.fetcher?.load(chatContainer);
42
-
43
- // FIXME: check the Why value on this quad:
44
- const chatAclDoc = this.store.any(
45
- chatContainer,
46
- new NamedNode("http://www.iana.org/assignments/link-relations/acl")
47
- );
48
- if (!chatAclDoc) {
49
- throw new Error("Chat ACL doc not found!");
50
- }
51
-
52
- const aclBody = `
53
- @prefix acl: <http://www.w3.org/ns/auth/acl#>.
54
- <#owner>
55
- a acl:Authorization;
56
- acl:agent <${me.value}>;
57
- acl:accessTo <.>;
58
- acl:default <.>;
59
- acl:mode
60
- acl:Read, acl:Write, acl:Control.
61
- <#invitee>
62
- a acl:Authorization;
63
- acl:agent <${invitee.value}>;
64
- acl:accessTo <.>;
65
- acl:default <.>;
66
- acl:mode
67
- acl:Read, acl:Append.
68
- `;
69
- await this.store.fetcher?.webOperation("PUT", chatAclDoc.value, {
70
- data: aclBody,
71
- contentType: "text/turtle",
72
- });
73
- }
74
-
75
- private async addToPrivateTypeIndex(chatThing, me) {
76
- // Add to private type index
77
- const privateTypeIndex = this.store.any(
78
- me,
79
- this.ns.solid("privateTypeIndex")
80
- ) as NamedNode | null;
81
- if (!privateTypeIndex) {
82
- throw new Error("Private type index not found!");
83
- }
84
- await this.store.fetcher?.load(privateTypeIndex);
85
- const reg = newThing(privateTypeIndex);
86
- const ins = [
87
- st(
88
- reg,
89
- this.ns.rdf("type"),
90
- this.ns.solid("TypeRegistration"),
91
- privateTypeIndex.doc()
92
- ),
93
- st(
94
- reg,
95
- this.ns.solid("forClass"),
96
- this.ns.meeting("LongChat"),
97
- privateTypeIndex.doc()
98
- ),
99
- st(reg, this.ns.solid("instance"), chatThing, privateTypeIndex.doc()),
100
- ];
101
- await new Promise((resolve, reject) => {
102
- this.store.updater?.update([], ins, function (_uri, ok, errm) {
103
- if (!ok) {
104
- reject(new Error(errm));
105
- } else {
106
- resolve(null);
107
- }
108
- });
109
- });
110
- }
111
-
112
- private async findChat(invitee: NamedNode) {
113
- const me = await this.profile.loadMe();
114
- const podRoot = await this.profile.getPodRoot(me);
115
- const chatContainer = determineChatContainer(invitee, podRoot);
116
- let exists = true;
117
- try {
118
- await this.store.fetcher?.load(
119
- new NamedNode(chatContainer.value + "index.ttl#this")
120
- );
121
- } catch (e) {
122
- exists = false;
123
- }
124
- return { me, chatContainer, exists };
125
- }
126
-
127
- private async createChatThing(
128
- chatContainer: NamedNode,
129
- me: NamedNode
130
- ): Promise<NamedNode> {
131
- const created = await this.mintNew({
132
- me,
133
- newBase: chatContainer.value,
134
- });
135
- return created.newInstance;
136
- }
137
-
138
- private mintNew(newPaneOptions: NewPaneOptions): Promise<CreatedPaneOptions> {
139
- const kb = this.store;
140
- const updater = kb.updater;
141
- if (newPaneOptions.me && !newPaneOptions.me.uri) {
142
- throw new Error("chat mintNew: Invalid userid " + newPaneOptions.me);
143
- }
144
-
145
- const newInstance = (newPaneOptions.newInstance =
146
- newPaneOptions.newInstance ||
147
- kb.sym(newPaneOptions.newBase + CHAT_LOCATION_IN_CONTAINER));
148
- const newChatDoc = newInstance.doc();
149
-
150
- kb.add(
151
- newInstance,
152
- this.ns.rdf("type"),
153
- this.ns.meeting("LongChat"),
154
- newChatDoc
155
- );
156
- kb.add(newInstance, this.ns.dc("title"), "Chat channel", newChatDoc);
157
- kb.add(
158
- newInstance,
159
- this.ns.dc("created"),
160
- term<Node>(new Date(Date.now())),
161
- newChatDoc
162
- );
163
- if (newPaneOptions.me) {
164
- kb.add(newInstance, this.ns.dc("author"), newPaneOptions.me, newChatDoc);
165
- }
166
-
167
- return new Promise(function (resolve, reject) {
168
- updater?.put(
169
- newChatDoc,
170
- kb.statementsMatching(undefined, undefined, undefined, newChatDoc),
171
- "text/turtle",
172
- function (uri2, ok, message) {
173
- if (ok) {
174
- resolve({
175
- ...newPaneOptions,
176
- newInstance,
177
- });
178
- } else {
179
- reject(
180
- new Error(
181
- "FAILED to save new chat channel at: " + uri2 + " : " + message
182
- )
183
- );
184
- }
185
- }
186
- );
187
- });
188
- }
189
-
190
- /**
191
- * Find (and optionally create) an individual chat between the current user and the given invitee
192
- * @param invitee - The person to chat with
193
- * @param createIfMissing - Whether the chat should be created, if missing
194
- * @returns null if missing, or a node referring to an already existing chat, or the newly created chat
195
- */
196
- async getChat(
197
- invitee: NamedNode,
198
- createIfMissing = true
199
- ): Promise<NamedNode | null> {
200
- const { me, chatContainer, exists } = await this.findChat(invitee);
201
- if (exists) {
202
- return new NamedNode(chatContainer.value + CHAT_LOCATION_IN_CONTAINER);
203
- }
204
-
205
- if (createIfMissing) {
206
- const chatThing = await this.createChatThing(chatContainer, me);
207
- await this.sendInvite(invitee, chatThing);
208
- await this.setAcl(chatContainer, me, invitee);
209
- await this.addToPrivateTypeIndex(chatThing, me);
210
- return chatThing;
211
- }
212
- return null;
213
- }
214
-
215
- private async sendInvite(invitee: NamedNode, chatThing: NamedNode) {
216
- await this.store.fetcher?.load(invitee.doc());
217
- const inviteeInbox = this.store.any(
218
- invitee,
219
- this.ns.ldp("inbox"),
220
- undefined,
221
- invitee.doc()
222
- );
223
- if (!inviteeInbox) {
224
- throw new Error(`Invitee inbox not found! ${invitee.value}`);
225
- }
226
- const inviteBody = `
227
- <> a <http://www.w3.org/ns/pim/meeting#LongChatInvite> ;
228
- ${this.ns.rdf("seeAlso")} <${chatThing.value}> .
229
- `;
230
-
231
- const inviteResponse = await this.store.fetcher?.webOperation(
232
- "POST",
233
- inviteeInbox.value,
234
- {
235
- data: inviteBody,
236
- contentType: "text/turtle",
237
- }
238
- );
239
- const locationStr = inviteResponse?.headers.get("location");
240
- if (!locationStr) {
241
- throw new Error(`Invite sending returned a ${inviteResponse?.status}`);
242
- }
243
- }
244
- }
@@ -1,14 +0,0 @@
1
- import { NamedNode } from "rdflib";
2
-
3
- export function determineChatContainer(
4
- invitee: NamedNode,
5
- podRoot: NamedNode
6
- ): NamedNode {
7
- // Create chat
8
- // See https://gitter.im/solid/chat-app?at=5f3c800f855be416a23ae74a
9
- const chatContainerStr = new URL(
10
- `IndividualChats/${new URL(invitee.value).host}/`,
11
- podRoot.value
12
- ).toString();
13
- return new NamedNode(chatContainerStr);
14
- }
@@ -1,311 +0,0 @@
1
- import { NamedNode, Namespace, LiveStore, sym, st } from "rdflib";
2
- // import * as debug from '../util/debug'
3
- // import { getContainerMembers } from '../util/UtilityLogic'
4
- import { solidLogicSingleton } from "../logic/solidLogicSingleton"
5
- import { newThing } from "../util/uri"
6
-
7
- const { authn } = solidLogicSingleton
8
- const { currentUser } = authn
9
-
10
- type TypeIndexScope = { label: string, index: NamedNode, agent: NamedNode } ;
11
- type ScopedApp = { instance: NamedNode, scope: TypeIndexScope }
12
-
13
- const ns = {
14
- dct: Namespace('http://purl.org/dc/terms/'),
15
- ldp: Namespace('http://www.w3.org/ns/ldp#'),
16
- meeting: Namespace('http://www.w3.org/ns/pim/meeting#'),
17
- rdf: Namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#'),
18
- schema: Namespace('http://schema.org/'),
19
- solid: Namespace('http://www.w3.org/ns/solid/terms#'),
20
- space: Namespace('http://www.w3.org/ns/pim/space#'),
21
- stat: Namespace('http://www.w3.org/ns/posix/stat#'),
22
- vcard: Namespace('http://www.w3.org/2006/vcard/ns#'),
23
- wf: Namespace('http://www.w3.org/2005/01/wf/flow#'),
24
- xsd: Namespace('http://www.w3.org/2001/XMLSchema#')
25
-
26
- }
27
-
28
- /** Create a resource if it really does not exist
29
- * Be absolutely sure something does not exist before creating a new empty file
30
- * as otherwise existing could be deleted.
31
- * @param doc {NamedNode} - The resource
32
- */
33
- export async function loadOrCreateIfNotExists (store: LiveStore, doc: NamedNode) {
34
- let response
35
- // console.log('@@ loadOrCreateIfNotExists doc ', doc)
36
- try {
37
- response = await store.fetcher.load(doc)
38
- } catch (err) {
39
- if (err.response.status === 404) {
40
- // console.log('createIfNotExists doc does NOT exist, will create: ' + doc)
41
- try {
42
- store.fetcher.webOperation('PUT', doc, {data: '', contentType: 'text/turtle'})
43
- } catch (err) {
44
- const msg = 'createIfNotExists: PUT FAILED: ' + doc + ': ' + err
45
- // console.log(msg)
46
- throw new Error(msg)
47
- }
48
- delete store.fetcher.requested[doc.uri] // delete cached 404 error
49
- // console.log('createIfNotExists doc created ok ' + doc)
50
- } else {
51
- const msg = 'createIfNotExists doc load error NOT 404: ' + doc + ': ' + err
52
- // console.log(msg)
53
- throw new Error(msg) // @@ add nested errors
54
- }
55
- }
56
- // console.log('createIfNotExists doc exists, all good ' + doc)
57
- return response
58
- }
59
-
60
- export function suggestPreferencesFile (me:NamedNode) {
61
- const stripped = me.uri.replace('/profile/', '/').replace('/public/', '/')
62
- // const stripped = me.uri.replace(\/[p|P]rofile/\g, '/').replace(\/[p|P]ublic/\g, '/')
63
- const folderURI = stripped.split('/').slice(0,-1).join('/') + '/Settings/'
64
- const fileURI = folderURI + 'Preferences.ttl'
65
- return sym(fileURI)
66
- }
67
-
68
- export function suggestPublicTypeIndex (me:NamedNode) {
69
- return sym(me.doc().dir()?.uri + 'publicTypeIndex.ttl')
70
- }
71
- // Note this one is based off the pref file not the profile
72
-
73
- export function suggestPrivateTypeIndex (preferencesFile:NamedNode) {
74
- return sym(preferencesFile.doc().dir()?.uri + 'privateTypeIndex.ttl')
75
- }
76
- /* Follow link from this doc to another thing, or else make a new link
77
- **
78
- ** return: null no ld one and failed to make a new one
79
- */
80
- export async function followOrCreateLink(store: LiveStore, subject: NamedNode, predicate: NamedNode,
81
- object: NamedNode, doc:NamedNode):Promise<NamedNode | null> {
82
- await store.fetcher.load(doc)
83
- const result = store.any(subject, predicate, null, doc)
84
- // console.log('@@ followOrCreateLink result ', result)
85
-
86
- if (result) return result as NamedNode
87
- if (!store.updater.editable(doc)) {
88
- // console.log(`followOrCreateLink: Can't modify ${doc} so can't make new link to ${object}.`)
89
- // console.log('followOrCreateLink @@ connectedStatements', store.connectedStatements(subject))
90
- return null
91
- }
92
- try {
93
- await store.updater.update([], [ st(subject, predicate, object, doc)])
94
- } catch (err) {
95
- console.warn(`followOrCreateLink: Error making link in ${doc} to ${object}: ${err}`)
96
- return null
97
- }
98
-
99
- // console.log(`Success making link in ${doc} to ${object}` )
100
-
101
- try {
102
- await loadOrCreateIfNotExists(store, object)
103
- // store.fetcher.webOperation('PUT', object, { data: '', contentType: 'text/turtle'})
104
- } catch (err) {
105
- console.warn(`followOrCreateLink: Error loading or saving new linked document: ${object}: ${err}`)
106
- }
107
- // console.log(`followOrCreateLink: Success loading or saving new linked document: ${object}.`)
108
- return object
109
- }
110
-
111
- export async function loadProfile(store: LiveStore, user: NamedNode) {
112
- // console.log(' @@ loadProfile: user', user)
113
- if (!user) {
114
- throw new Error(`loadProfile: no user given.`)
115
- }
116
- // try {
117
- await store.fetcher.load(user.doc())
118
- // } catch (err) {
119
- // throw new Error(`Unable to load profile of user ${user}: ${err}`)
120
- //}
121
- return user.doc()
122
- }
123
-
124
- export async function loadPreferences(store: LiveStore, user: NamedNode): Promise <NamedNode | undefined > {
125
- // console.log('loadPreferences @@ user', user)
126
- await loadProfile(store as LiveStore, user)
127
-
128
- const possiblePreferencesFile = suggestPreferencesFile(user)
129
-
130
- const preferencesFile = await followOrCreateLink(store, user, ns.space('preferencesFile') as NamedNode, possiblePreferencesFile, user.doc())
131
- // const preferencesFile = store.any(user, ns.space('preferencesFile'), undefined, profile)
132
-
133
- // console.log('loadPreferences @@ pref file', preferencesFile)
134
- if (!preferencesFile) {
135
- const message = `User ${user} has no pointer in profile to preferences file.`
136
- console.warn(message)
137
- // throw new Error()
138
- return undefined
139
- }
140
- try {
141
- await store.fetcher.load(preferencesFile as NamedNode)
142
- } catch (err) { // Mabeb a permission propblem or origin problem
143
- return undefined
144
- // throw new Error(`Unable to load preferences file ${preferencesFile} of user <${user}>: ${err}`)
145
- }
146
- return preferencesFile as NamedNode
147
- }
148
-
149
- export async function loadTypeIndexesFor(store: LiveStore, user:NamedNode): Promise<Array<TypeIndexScope>> {
150
- // console.log('@@ loadTypeIndexesFor user', user)
151
- if (!user) throw new Error(`loadTypeIndexesFor: No user given`)
152
- const profile = await loadProfile(store, user)
153
-
154
- const suggestion = suggestPublicTypeIndex(user)
155
-
156
- const publicTypeIndex = await followOrCreateLink(store, user, ns.solid('publicTypeIndex') as NamedNode, suggestion, profile)
157
-
158
- // const publicTypeIndex = store.any(user, ns.solid('publicTypeIndex'), undefined, profile)
159
- // console.log('@@ loadTypeIndexesFor publicTypeIndex', publicTypeIndex)
160
-
161
- const publicScopes = publicTypeIndex ? [ { label: 'public', index: publicTypeIndex as NamedNode, agent: user } ] : []
162
-
163
- let preferencesFile
164
- try {
165
- preferencesFile = await loadPreferences(store, user)
166
- } catch (err) {
167
- preferencesFile = null
168
- }
169
-
170
- let privateScopes
171
- if (preferencesFile) { // watch out - can be in either as spec was not clear. Legacy is profile.
172
- // If there is a legacy one linked from the profile, use that.
173
- // Otherwiae use or make one linked from Preferences
174
- const suggestedPrivateTypeIndex = suggestPrivateTypeIndex(preferencesFile)
175
-
176
- const privateTypeIndex = store.any(user, ns.solid('privateTypeIndex'), undefined, profile) ||
177
-
178
- await followOrCreateLink(store, user, ns.solid('privateTypeIndex') as NamedNode, suggestedPrivateTypeIndex, preferencesFile);
179
-
180
- privateScopes = privateTypeIndex ? [ { label: 'private', index: privateTypeIndex as NamedNode, agent: user } ] : []
181
- } else {
182
- privateScopes = []
183
- }
184
- const scopes = publicScopes.concat(privateScopes)
185
- if (scopes.length === 0) return scopes
186
- const files = scopes.map(scope => scope.index)
187
- // console.log('@@ loadTypeIndexesFor files ', files)
188
- try {
189
- await store.fetcher.load(files)
190
- } catch (err) {
191
- console.warn('Problems loading type index: ', err)
192
- }
193
- return scopes
194
- }
195
-
196
- export async function loadCommunityTypeIndexes (store:LiveStore, user:NamedNode): Promise<TypeIndexScope[][]> {
197
- const preferencesFile = await loadPreferences(store, user)
198
- if (preferencesFile) { // For now, pick up communities as simple links from the preferences file.
199
- const communities = store.each(user, ns.solid('community'), undefined, preferencesFile as NamedNode)
200
- // console.log('loadCommunityTypeIndexes communities: ',communities)
201
- let result = []
202
- for (const org of communities) {
203
- result = result.concat(await loadTypeIndexesFor(store, org as NamedNode) as any)
204
- }
205
- // const communityTypeIndexesPromises = communities.map(async community => await loadTypeIndexesFor(store, community as NamedNode))
206
- // const result1 = Promise.all(communityTypeIndexesPromises)
207
- return result
208
- }
209
- return [] // No communities
210
- }
211
-
212
- export async function loadAllTypeIndexes (store:LiveStore, user:NamedNode) {
213
- return (await loadTypeIndexesFor(store, user)).concat((await loadCommunityTypeIndexes(store, user)).flat())
214
- }
215
-
216
- // Utility: remove duplicates from Array of NamedNodes
217
-
218
- export function uniqueNodes (arr: NamedNode[]): NamedNode[] {
219
- const uris = arr.map(x => x.uri)
220
- const set = new Set(uris)
221
- const uris2 = Array.from(set)
222
- const arr2 = uris2.map(u => new NamedNode(u))
223
- return arr2 // Array.from(new Set(arr.map(x => x.uri))).map(u => sym(u))
224
- }
225
-
226
- export async function getScopedAppsfromIndex (store, scope, theClass: NamedNode) {
227
- // console.log(`getScopedAppsfromIndex agent ${scope.agent} index: ${scope.index}` )
228
- const index = scope.index
229
- const registrations = store.each(undefined, ns.solid('forClass'), theClass, index)
230
- // console.log(' registrations', registrations )
231
-
232
- const directInstances = registrations.map(reg => store.each(reg as NamedNode, ns.solid('instance'), null, index)).flat()
233
- // console.log(' directInstances', directInstances )
234
- let instances = uniqueNodes(directInstances)
235
-
236
- /*
237
- let instanceContainers = []
238
- for (const reg of registrations) {
239
- const cont = store.any(reg as NamedNode, ns.solid('instanceContainer'), null, index)
240
- if (cont) {
241
- // console.log(' @@ getScopedAppsfromIndex got one: ', cont)
242
- instanceContainers.push(cont)
243
- }
244
- }
245
- */
246
- const instanceContainers = registrations.map(
247
- reg => store.each(reg as NamedNode, ns.solid('instanceContainer'), null, index)).flat()
248
-
249
- // instanceContainers may be deprocatable if no one has used them
250
-
251
- const containers = uniqueNodes(instanceContainers)
252
- for (let i = 0; i < containers.length; i++) {
253
- const cont = containers[i]
254
- await store.fetcher.load(cont)
255
- const contents = store.each(cont, ns.ldp('contains'), null, cont)
256
- // if (contents.length) console.log('getScopedAppsfromIndex @@ instanceContainer contents:', contents)
257
- instances = instances.concat(contents)
258
- }
259
- return instances.map(instance => { return {instance, scope}})
260
- }
261
-
262
-
263
- export async function getScopedAppInstances (store:LiveStore, klass: NamedNode, user: NamedNode):Promise<ScopedApp[]> {
264
- // console.log('getScopedAppInstances @@ ' + user)
265
- const scopes = await loadAllTypeIndexes(store, user)
266
- let scopedApps = []
267
- for (const scope of scopes) {
268
- const scopedApps0 = await getScopedAppsfromIndex(store, scope, klass) as any
269
- scopedApps = scopedApps.concat(scopedApps0)
270
- }
271
- return scopedApps
272
- }
273
- // This is the function signature which used to be in solid-ui/logic
274
- // Recommended to use getScopedAppInstances instead as it provides more information.
275
- //
276
- export async function getAppInstances (store:LiveStore, klass: NamedNode): Promise<NamedNode[]> {
277
- const user = currentUser()
278
- if (!user) throw new Error('getAppInstances: Must be logged in to find apps.')
279
- const scopedAppInstances = await getScopedAppInstances(store, klass, user)
280
- return scopedAppInstances.map(scoped => scoped.instance)
281
- }
282
- /**
283
- * Register a new app in a type index
284
- * used in chat in bookmark.js (solid-ui)
285
- * Returns the registration object if successful else null
286
- */
287
- export async function registerInstanceInTypeIndex (
288
- store:LiveStore,
289
- instance: NamedNode,
290
- index: NamedNode,
291
- theClass: NamedNode,
292
- // agent: NamedNode
293
- ): Promise<NamedNode | null> {
294
- const registration = newThing(index)
295
- const ins = [
296
- // See https://github.com/solid/solid/blob/main/proposals/data-discovery.md
297
- st(registration, ns.rdf('type'), ns.solid('TypeRegistration'), index),
298
- st(registration, ns.solid('forClass'), theClass, index),
299
- st(registration, ns.solid('instance'), instance, index)
300
- ]
301
- try {
302
- console.log('patching index', ins)
303
- await store.updater.update([], ins)
304
- } catch (err) {
305
- const msg = `Unable to register ${instance} in index ${index}: ${err}`
306
- console.warn(msg)
307
- return null
308
- }
309
- return registration
310
- }
311
- // ENDS
@@ -1,66 +0,0 @@
1
- import { NamedNode, LiveStore } from "rdflib";
2
- import { ProfileLogic } from "../profile/ProfileLogic";
3
- import { SolidNamespace } from "../types";
4
- import { UtilityLogic } from "../util/UtilityLogic";
5
-
6
- /**
7
- * Inbox-related logic
8
- */
9
- export class InboxLogic {
10
- store: LiveStore;
11
- ns: SolidNamespace;
12
- profile: ProfileLogic;
13
- util: UtilityLogic;
14
-
15
- constructor(store: LiveStore, ns: SolidNamespace, profile: ProfileLogic, util: UtilityLogic) {
16
- this.store = store;
17
- this.ns = ns;
18
- this.profile = profile;
19
- this.util = util;
20
- }
21
-
22
- async getNewMessages(
23
- user?: NamedNode
24
- ): Promise<string[]> {
25
- if (!user) {
26
- user = await this.profile.loadMe();
27
- }
28
- const inbox = await this.profile.getMainInbox(user);
29
- const urls = await this.util.getContainerMembers(inbox.value);
30
- return urls.filter(url => !this.util.isContainer(url));
31
- }
32
- async createInboxFor(peerWebId: string, nick: string) {
33
- const myWebId: NamedNode = (await this.profile.loadMe());
34
- const podRoot: NamedNode = await this.profile.getPodRoot(myWebId);
35
- const ourInbox = `${podRoot.value}p2p-inboxes/${encodeURIComponent(nick)}/`;
36
- await this.util.createContainer(ourInbox);
37
- const aclDocUrl = await this.util.findAclDocUrl(ourInbox);
38
- await this.util.setSinglePeerAccess({
39
- ownerWebId: myWebId.value,
40
- peerWebId,
41
- accessToModes: 'acl:Append',
42
- target: ourInbox
43
- });
44
- return ourInbox;
45
- }
46
- async markAsRead(url: string, date: Date) {
47
- const downloaded = await this.util.underlyingFetch.fetch(url);
48
- if (downloaded.status !== 200) {
49
- throw new Error(`Not OK! ${url}`);
50
- }
51
- const archiveUrl = this.util.getArchiveUrl(url, date);
52
- const options = {
53
- method: 'PUT',
54
- body: await downloaded.text(),
55
- headers: [
56
- [ 'Content-Type', downloaded.headers.get('Content-Type') || 'application/octet-stream' ]
57
- ]
58
- };
59
- const uploaded = await this.util.underlyingFetch.fetch(archiveUrl, options);
60
- if (uploaded.status.toString()[0] === '2') {
61
- await this.store.fetcher?._fetch(url, {
62
- method: 'DELETE'
63
- });
64
- }
65
- }
66
- }