solid-logic 1.3.17-ec29db42 → 1.3.17-f02a2f1a

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 (117) hide show
  1. package/lib/acl/aclLogic.d.ts +3 -30
  2. package/lib/acl/aclLogic.d.ts.map +1 -1
  3. package/lib/acl/aclLogic.js +152 -119
  4. package/lib/acl/aclLogic.js.map +1 -1
  5. package/lib/authn/SolidAuthnLogic.d.ts.map +1 -1
  6. package/lib/authn/SolidAuthnLogic.js +2 -2
  7. package/lib/authn/SolidAuthnLogic.js.map +1 -1
  8. package/lib/chat/chatLogic.d.ts +3 -0
  9. package/lib/chat/chatLogic.d.ts.map +1 -0
  10. package/lib/chat/{ChatLogic.js → chatLogic.js} +82 -86
  11. package/lib/chat/chatLogic.js.map +1 -0
  12. package/lib/inbox/inboxLogic.d.ts +3 -0
  13. package/lib/inbox/inboxLogic.d.ts.map +1 -0
  14. package/lib/inbox/{InboxLogic.js → inboxLogic.js} +58 -64
  15. package/lib/inbox/inboxLogic.js.map +1 -0
  16. package/lib/index.d.ts +6 -11
  17. package/lib/index.d.ts.map +1 -1
  18. package/lib/index.js +5 -25
  19. package/lib/index.js.map +1 -1
  20. package/lib/logic/CustomError.d.ts +4 -0
  21. package/lib/logic/CustomError.d.ts.map +1 -1
  22. package/lib/logic/CustomError.js +17 -1
  23. package/lib/logic/CustomError.js.map +1 -1
  24. package/lib/logic/SolidLogic.d.ts +4 -46
  25. package/lib/logic/SolidLogic.d.ts.map +1 -1
  26. package/lib/logic/SolidLogic.js +44 -273
  27. package/lib/logic/SolidLogic.js.map +1 -1
  28. package/lib/logic/solidLogicSingleton.d.ts +1 -2
  29. package/lib/logic/solidLogicSingleton.d.ts.map +1 -1
  30. package/lib/logic/solidLogicSingleton.js +2 -2
  31. package/lib/logic/solidLogicSingleton.js.map +1 -1
  32. package/lib/profile/profileLogic.d.ts +3 -0
  33. package/lib/profile/profileLogic.d.ts.map +1 -0
  34. package/lib/profile/profileLogic.js +246 -0
  35. package/lib/profile/profileLogic.js.map +1 -0
  36. package/lib/typeIndex/typeIndexLogic.d.ts +2 -21
  37. package/lib/typeIndex/typeIndexLogic.d.ts.map +1 -1
  38. package/lib/typeIndex/typeIndexLogic.js +304 -296
  39. package/lib/typeIndex/typeIndexLogic.js.map +1 -1
  40. package/lib/types.d.ts +82 -1
  41. package/lib/types.d.ts.map +1 -1
  42. package/lib/util/containerLogic.d.ts +11 -0
  43. package/lib/util/containerLogic.d.ts.map +1 -0
  44. package/lib/{profile/ProfileLogic.js → util/containerLogic.js} +53 -44
  45. package/lib/util/containerLogic.js.map +1 -0
  46. package/lib/util/ns.d.ts +2 -0
  47. package/lib/util/ns.d.ts.map +1 -0
  48. package/lib/util/ns.js +34 -0
  49. package/lib/util/ns.js.map +1 -0
  50. package/lib/util/utilityLogic.d.ts +15 -0
  51. package/lib/util/utilityLogic.d.ts.map +1 -0
  52. package/lib/util/utilityLogic.js +272 -0
  53. package/lib/util/utilityLogic.js.map +1 -0
  54. package/lib/util/utils.d.ts +8 -0
  55. package/lib/util/utils.d.ts.map +1 -0
  56. package/lib/util/utils.js +48 -0
  57. package/lib/util/utils.js.map +1 -0
  58. package/package.json +3 -1
  59. package/src/acl/aclLogic.ts +137 -118
  60. package/src/authn/SolidAuthnLogic.ts +3 -2
  61. package/src/chat/chatLogic.ts +225 -0
  62. package/src/inbox/inboxLogic.ts +58 -0
  63. package/src/index.ts +11 -30
  64. package/src/logic/CustomError.ts +5 -1
  65. package/src/logic/SolidLogic.ts +42 -229
  66. package/src/logic/solidLogicSingleton.ts +3 -3
  67. package/src/profile/profileLogic.ts +126 -0
  68. package/src/typeIndex/typeIndexLogic.ts +175 -182
  69. package/src/types.ts +83 -4
  70. package/src/util/containerLogic.ts +54 -0
  71. package/src/util/ns.ts +5 -0
  72. package/src/util/utilityLogic.ts +155 -0
  73. package/src/util/utils.ts +52 -0
  74. package/test/aclLogic.test.ts +13 -4
  75. package/test/chatLogic.test.ts +70 -71
  76. package/test/container.test.ts +56 -0
  77. package/test/helpers/dataSetup.ts +134 -0
  78. package/test/helpers/setup.ts +4 -0
  79. package/test/inboxLogic.test.ts +39 -38
  80. package/test/logic.test.ts +10 -9
  81. package/test/profileLogic.test.ts +246 -0
  82. package/test/typeIndexLogic.test.ts +487 -30
  83. package/test/utilityLogic.test.ts +172 -126
  84. package/test/utils.test.ts +32 -0
  85. package/lib/chat/ChatLogic.d.ts +0 -26
  86. package/lib/chat/ChatLogic.d.ts.map +0 -1
  87. package/lib/chat/ChatLogic.js.map +0 -1
  88. package/lib/chat/determineChatContainer.d.ts +0 -3
  89. package/lib/chat/determineChatContainer.d.ts.map +0 -1
  90. package/lib/chat/determineChatContainer.js +0 -12
  91. package/lib/chat/determineChatContainer.js.map +0 -1
  92. package/lib/discovery/discoveryLogic.d.ts +0 -37
  93. package/lib/discovery/discoveryLogic.d.ts.map +0 -1
  94. package/lib/discovery/discoveryLogic.js +0 -507
  95. package/lib/discovery/discoveryLogic.js.map +0 -1
  96. package/lib/inbox/InboxLogic.d.ts +0 -18
  97. package/lib/inbox/InboxLogic.d.ts.map +0 -1
  98. package/lib/inbox/InboxLogic.js.map +0 -1
  99. package/lib/profile/ProfileLogic.d.ts +0 -13
  100. package/lib/profile/ProfileLogic.d.ts.map +0 -1
  101. package/lib/profile/ProfileLogic.js.map +0 -1
  102. package/lib/util/UtilityLogic.d.ts +0 -33
  103. package/lib/util/UtilityLogic.d.ts.map +0 -1
  104. package/lib/util/UtilityLogic.js +0 -240
  105. package/lib/util/UtilityLogic.js.map +0 -1
  106. package/lib/util/uri.d.ts +0 -3
  107. package/lib/util/uri.d.ts.map +0 -1
  108. package/lib/util/uri.js +0 -9
  109. package/lib/util/uri.js.map +0 -1
  110. package/src/chat/ChatLogic.ts +0 -244
  111. package/src/chat/determineChatContainer.ts +0 -14
  112. package/src/discovery/discoveryLogic.ts +0 -288
  113. package/src/inbox/InboxLogic.ts +0 -66
  114. package/src/profile/ProfileLogic.ts +0 -44
  115. package/src/util/UtilityLogic.ts +0 -161
  116. package/src/util/uri.ts +0 -5
  117. package/test/discoveryLogic.test.ts +0 -712
@@ -3,6 +3,7 @@ import { appContext, offlineTestID } from "./authUtil";
3
3
  import * as debug from '../util/debug'
4
4
  import { Session } from "@inrupt/solid-client-authn-browser";
5
5
  import { AuthenticationContext, AuthnLogic } from "../types";
6
+
6
7
  export class SolidAuthnLogic implements AuthnLogic {
7
8
  private session: Session;
8
9
 
@@ -58,10 +59,10 @@ export class SolidAuthnLogic implements AuthnLogic {
58
59
  const curUrl = new URL(window.location.href)
59
60
  if (curUrl.hash !== postLoginRedirectHash) {
60
61
  if (history.pushState) {
61
- // console.log('Setting window.location.has using pushState')
62
+ // debug.log('Setting window.location.has using pushState')
62
63
  history.pushState(null, document.title, postLoginRedirectHash)
63
64
  } else {
64
- // console.warn('Setting window.location.has using location.hash')
65
+ // debug.warn('Setting window.location.has using location.hash')
65
66
  location.hash = postLoginRedirectHash
66
67
  }
67
68
  curUrl.hash = postLoginRedirectHash
@@ -0,0 +1,225 @@
1
+ import { NamedNode, Node, st, term } from "rdflib"
2
+ import { ChatLogic, CreatedPaneOptions, NewPaneOptions, Chat } from "../types"
3
+ import { ns as namespace } from "../util/ns";
4
+ import { determineChatContainer, newThing } from "../util/utils"
5
+
6
+ const CHAT_LOCATION_IN_CONTAINER = "index.ttl#this"
7
+
8
+ export function createChatLogic(store, profileLogic): ChatLogic {
9
+ const ns = namespace
10
+
11
+ async function setAcl(
12
+ chatContainer: NamedNode,
13
+ me: NamedNode,
14
+ invitee: NamedNode
15
+ ): Promise<void> {
16
+ // Some servers don't present a Link http response header
17
+ // if the container doesn't exist yet, so refetch the container
18
+ // now that it has been created:
19
+ await store.fetcher.load(chatContainer);
20
+
21
+ // FIXME: check the Why value on this quad:
22
+ const chatAclDoc = store.any(
23
+ chatContainer,
24
+ new NamedNode("http://www.iana.org/assignments/link-relations/acl")
25
+ );
26
+ if (!chatAclDoc) {
27
+ throw new Error("Chat ACL doc not found!");
28
+ }
29
+
30
+ const aclBody = `
31
+ @prefix acl: <http://www.w3.org/ns/auth/acl#>.
32
+ <#owner>
33
+ a acl:Authorization;
34
+ acl:agent <${me.value}>;
35
+ acl:accessTo <.>;
36
+ acl:default <.>;
37
+ acl:mode
38
+ acl:Read, acl:Write, acl:Control.
39
+ <#invitee>
40
+ a acl:Authorization;
41
+ acl:agent <${invitee.value}>;
42
+ acl:accessTo <.>;
43
+ acl:default <.>;
44
+ acl:mode
45
+ acl:Read, acl:Append.
46
+ `;
47
+ await store.fetcher.webOperation("PUT", chatAclDoc.value, {
48
+ data: aclBody,
49
+ contentType: "text/turtle",
50
+ });
51
+ }
52
+
53
+ async function addToPrivateTypeIndex(chatThing, me) {
54
+ // Add to private type index
55
+ const privateTypeIndex = store.any(
56
+ me,
57
+ ns.solid("privateTypeIndex")
58
+ ) as NamedNode | null;
59
+ if (!privateTypeIndex) {
60
+ throw new Error("Private type index not found!");
61
+ }
62
+ await store.fetcher.load(privateTypeIndex);
63
+ const reg = newThing(privateTypeIndex);
64
+ const ins = [
65
+ st(
66
+ reg,
67
+ ns.rdf("type"),
68
+ ns.solid("TypeRegistration"),
69
+ privateTypeIndex.doc()
70
+ ),
71
+ st(
72
+ reg,
73
+ ns.solid("forClass"),
74
+ ns.meeting("LongChat"),
75
+ privateTypeIndex.doc()
76
+ ),
77
+ st(reg, ns.solid("instance"), chatThing, privateTypeIndex.doc()),
78
+ ];
79
+ await new Promise((resolve, reject) => {
80
+ store.updater.update([], ins, function (_uri, ok, errm) {
81
+ if (!ok) {
82
+ reject(new Error(errm));
83
+ } else {
84
+ resolve(null);
85
+ }
86
+ });
87
+ });
88
+ }
89
+
90
+ async function findChat(invitee: NamedNode): Promise<Chat> {
91
+ const me = await profileLogic.loadMe();
92
+ const podRoot = await profileLogic.getPodRoot(me);
93
+ const chatContainer = determineChatContainer(invitee, podRoot);
94
+ let exists = true;
95
+ try {
96
+ await store.fetcher.load(
97
+ new NamedNode(chatContainer.value + "index.ttl#this")
98
+ );
99
+ } catch (e) {
100
+ exists = false;
101
+ }
102
+ return { me, chatContainer, exists };
103
+ }
104
+
105
+ async function createChatThing(
106
+ chatContainer: NamedNode,
107
+ me: NamedNode
108
+ ): Promise<NamedNode> {
109
+ const created = await mintNew({
110
+ me,
111
+ newBase: chatContainer.value,
112
+ });
113
+ return created.newInstance;
114
+ }
115
+
116
+ function mintNew(newPaneOptions: NewPaneOptions): Promise<CreatedPaneOptions> {
117
+ const kb = store;
118
+ const updater = kb.updater;
119
+ if (newPaneOptions.me && !newPaneOptions.me.uri) {
120
+ throw new Error("chat mintNew: Invalid userid " + newPaneOptions.me);
121
+ }
122
+
123
+ const newInstance = (newPaneOptions.newInstance =
124
+ newPaneOptions.newInstance ||
125
+ kb.sym(newPaneOptions.newBase + CHAT_LOCATION_IN_CONTAINER));
126
+ const newChatDoc = newInstance.doc();
127
+
128
+ kb.add(
129
+ newInstance,
130
+ ns.rdf("type"),
131
+ ns.meeting("LongChat"),
132
+ newChatDoc
133
+ );
134
+ kb.add(newInstance, ns.dc("title"), "Chat channel", newChatDoc);
135
+ kb.add(
136
+ newInstance,
137
+ ns.dc("created"),
138
+ term<Node>(new Date(Date.now())),
139
+ newChatDoc
140
+ );
141
+ if (newPaneOptions.me) {
142
+ kb.add(newInstance, ns.dc("author"), newPaneOptions.me, newChatDoc);
143
+ }
144
+
145
+ return new Promise(function (resolve, reject) {
146
+ updater?.put(
147
+ newChatDoc,
148
+ kb.statementsMatching(undefined, undefined, undefined, newChatDoc),
149
+ "text/turtle",
150
+ function (uri2, ok, message) {
151
+ if (ok) {
152
+ resolve({
153
+ ...newPaneOptions,
154
+ newInstance,
155
+ });
156
+ } else {
157
+ reject(
158
+ new Error(
159
+ "FAILED to save new chat channel at: " + uri2 + " : " + message
160
+ )
161
+ );
162
+ }
163
+ }
164
+ );
165
+ });
166
+ }
167
+
168
+ /**
169
+ * Find (and optionally create) an individual chat between the current user and the given invitee
170
+ * @param invitee - The person to chat with
171
+ * @param createIfMissing - Whether the chat should be created, if missing
172
+ * @returns null if missing, or a node referring to an already existing chat, or the newly created chat
173
+ */
174
+ async function getChat(
175
+ invitee: NamedNode,
176
+ createIfMissing = true
177
+ ): Promise<NamedNode | null> {
178
+ const { me, chatContainer, exists } = await findChat(invitee);
179
+ if (exists) {
180
+ return new NamedNode(chatContainer.value + CHAT_LOCATION_IN_CONTAINER);
181
+ }
182
+
183
+ if (createIfMissing) {
184
+ const chatThing = await createChatThing(chatContainer, me);
185
+ await sendInvite(invitee, chatThing);
186
+ await setAcl(chatContainer, me, invitee);
187
+ await addToPrivateTypeIndex(chatThing, me);
188
+ return chatThing;
189
+ }
190
+ return null;
191
+ }
192
+
193
+ async function sendInvite(invitee: NamedNode, chatThing: NamedNode) {
194
+ await store.fetcher.load(invitee.doc());
195
+ const inviteeInbox = store.any(
196
+ invitee,
197
+ ns.ldp("inbox"),
198
+ undefined,
199
+ invitee.doc()
200
+ );
201
+ if (!inviteeInbox) {
202
+ throw new Error(`Invitee inbox not found! ${invitee.value}`);
203
+ }
204
+ const inviteBody = `
205
+ <> a <http://www.w3.org/ns/pim/meeting#LongChatInvite> ;
206
+ ${ns.rdf("seeAlso")} <${chatThing.value}> .
207
+ `;
208
+
209
+ const inviteResponse = await store.fetcher?.webOperation(
210
+ "POST",
211
+ inviteeInbox.value,
212
+ {
213
+ data: inviteBody,
214
+ contentType: "text/turtle",
215
+ }
216
+ );
217
+ const locationStr = inviteResponse?.headers.get("location");
218
+ if (!locationStr) {
219
+ throw new Error(`Invite sending returned a ${inviteResponse?.status}`);
220
+ }
221
+ }
222
+ return {
223
+ setAcl, addToPrivateTypeIndex, findChat, createChatThing, getChat, sendInvite, mintNew
224
+ }
225
+ }
@@ -0,0 +1,58 @@
1
+ import { NamedNode } from "rdflib";
2
+ import { InboxLogic } from "../types";
3
+ import { getArchiveUrl } from "../util/utils";
4
+
5
+ export function createInboxLogic(store, profileLogic, utilityLogic, containerLogic, aclLogic): InboxLogic {
6
+
7
+ async function createInboxFor(peerWebId: string, nick: string) {
8
+ const myWebId: NamedNode = await profileLogic.loadMe();
9
+ const podRoot: NamedNode = await profileLogic.getPodRoot(myWebId);
10
+ const ourInbox = `${podRoot.value}p2p-inboxes/${encodeURIComponent(nick)}/`;
11
+ await containerLogic.createContainer(ourInbox);
12
+ const aclDocUrl = await aclLogic.findAclDocUrl(ourInbox);
13
+ await utilityLogic.setSinglePeerAccess({
14
+ ownerWebId: myWebId.value,
15
+ peerWebId,
16
+ accessToModes: 'acl:Append',
17
+ target: ourInbox
18
+ });
19
+ return ourInbox;
20
+ }
21
+
22
+ async function getNewMessages(
23
+ user?: NamedNode
24
+ ): Promise<string[]> {
25
+ if (!user) {
26
+ user = await profileLogic.loadMe();
27
+ }
28
+ const inbox = await profileLogic.getMainInbox(user);
29
+ const urls = await containerLogic.getContainerMembers(inbox.value);
30
+ return urls.filter(url => !containerLogic.isContainer(url));
31
+ }
32
+
33
+ async function markAsRead(url: string, date: Date) {
34
+ const downloaded = await store.fetcher._fetch(url);
35
+ if (downloaded.status !== 200) {
36
+ throw new Error(`Not OK! ${url}`);
37
+ }
38
+ const archiveUrl = getArchiveUrl(url, date);
39
+ const options = {
40
+ method: 'PUT',
41
+ body: await downloaded.text(),
42
+ headers: [
43
+ ['Content-Type', downloaded.headers.get('Content-Type') || 'application/octet-stream']
44
+ ]
45
+ };
46
+ const uploaded = await store.fetcher._fetch(archiveUrl, options);
47
+ if (uploaded.status.toString()[0] === '2') {
48
+ await store.fetcher._fetch(url, {
49
+ method: 'DELETE'
50
+ });
51
+ }
52
+ }
53
+ return {
54
+ createInboxFor,
55
+ getNewMessages,
56
+ markAsRead
57
+ }
58
+ }
package/src/index.ts CHANGED
@@ -1,39 +1,20 @@
1
1
  // Make these variables directly accessible as it is what you need most of the time
2
2
  // This also makes these variable globaly accesible in mashlib
3
3
  import { solidLogicSingleton } from './logic/solidLogicSingleton'
4
+
4
5
  const authn = solidLogicSingleton.authn
5
6
  const authSession = solidLogicSingleton.authn.authSession
6
7
  const store = solidLogicSingleton.store
7
8
 
8
- const chat = solidLogicSingleton.chat
9
- const profile = solidLogicSingleton.profile
10
-
11
- export {
12
- setACLUserPublic,
13
- genACLText
14
- } from './acl/aclLogic'
15
-
16
- export {
17
- ensureTypeIndexes,
18
- loadTypeIndexes,
19
- registerInTypeIndex,
20
- loadIndex
21
- } from './typeIndex/typeIndexLogic'
22
-
23
- export {
24
- loadProfile,
25
- loadPreferences,
26
- loadTypeIndexesFor,
27
- loadCommunityTypeIndexes,
28
- loadAllTypeIndexes
29
- } from './discovery/discoveryLogic'
30
-
31
- export { SolidLogic } from './logic/SolidLogic'
9
+ export { ACL_LINK } from './acl/aclLogic'
32
10
  export { offlineTestID, appContext } from './authn/authUtil'
33
- export { ACL_LINK } from './util/UtilityLogic'
34
11
  export { getSuggestedIssuers } from './issuer/issuerLogic'
35
- export { AppDetails, SolidNamespace, AuthenticationContext } from './types'
36
- // solidLogicSingleton is exported entirely because it is used in solid-panes
37
- export { solidLogicSingleton } from './logic/solidLogicSingleton'
38
- export { UnauthorizedError, CrossOriginForbiddenError, SameOriginForbiddenError, NotFoundError, FetchError } from './logic/CustomError'
39
- export { authn, authSession, store, chat, profile }
12
+ export { AppDetails, SolidNamespace, AuthenticationContext, SolidLogic } from './types'
13
+ export { UnauthorizedError, CrossOriginForbiddenError, SameOriginForbiddenError, NotFoundError, FetchError, NotEditableError, WebOperationError } from './logic/CustomError'
14
+
15
+ export {
16
+ solidLogicSingleton, // solidLogicSingleton is exported entirely because it is used in solid-panes
17
+ store,
18
+ authn,
19
+ authSession
20
+ }
@@ -6,7 +6,7 @@ class CustomError extends Error {
6
6
  this.name = new.target.name; // stack traces display correctly now
7
7
  }
8
8
  }
9
-
9
+
10
10
  export class UnauthorizedError extends CustomError {}
11
11
 
12
12
  export class CrossOriginForbiddenError extends CustomError {}
@@ -15,6 +15,10 @@ export class SameOriginForbiddenError extends CustomError {}
15
15
 
16
16
  export class NotFoundError extends CustomError {}
17
17
 
18
+ export class NotEditableError extends CustomError { }
19
+
20
+ export class WebOperationError extends CustomError {}
21
+
18
22
  export class FetchError extends CustomError {
19
23
  status: number;
20
24
 
@@ -1,232 +1,52 @@
1
1
  import { Session } from "@inrupt/solid-client-authn-browser";
2
2
  import * as rdf from "rdflib";
3
- import { NamedNode, Statement, LiveStore } from "rdflib";
4
- import solidNamespace from "solid-namespace";
3
+ import { LiveStore, NamedNode, Statement } from "rdflib";
4
+ import { createAclLogic } from "../acl/aclLogic";
5
5
  import { SolidAuthnLogic } from "../authn/SolidAuthnLogic";
6
- import { ChatLogic } from "../chat/ChatLogic";
7
- import { ProfileLogic } from "../profile/ProfileLogic";
8
- import { AuthnLogic, SolidNamespace } from "../types";
6
+ import { createChatLogic } from "../chat/chatLogic";
7
+ import { createInboxLogic } from "../inbox/inboxLogic";
8
+ import { createProfileLogic } from "../profile/profileLogic";
9
+ import { createTypeIndexLogic } from "../typeIndex/typeIndexLogic";
10
+ import { createContainerLogic } from "../util/containerLogic";
11
+ import { createUtilityLogic } from "../util/utilityLogic";
12
+ import { AuthnLogic, SolidLogic } from "../types";
9
13
  import * as debug from "../util/debug";
10
- import { UtilityLogic } from "../util/UtilityLogic";
11
- import { CrossOriginForbiddenError, FetchError, NotFoundError, SameOriginForbiddenError, UnauthorizedError } from "./CustomError";
12
14
  /*
13
15
  ** It is important to distinquish `fetch`, a function provided by the browser
14
16
  ** and `Fetcher`, a helper object for the rdflib Store which turns it
15
17
  ** into a `ConnectedStore` or a `LiveStore`. A Fetcher object is
16
18
  ** available at store.fetcher, and `fetch` function at `store.fetcher._fetch`,
17
19
  */
20
+ export function createSolidLogic(specialFetch: { fetch: (url: any, requestInit: any) => any }, session: Session): SolidLogic {
18
21
 
19
- const ns: SolidNamespace = solidNamespace(rdf);
22
+ debug.log("SolidLogic: Unique instance created. There should only be one of these.")
23
+ const store: LiveStore = rdf.graph() as LiveStore
24
+ rdf.fetcher(store, {fetch: specialFetch.fetch}); // Attach a web I/O module, store.fetcher
25
+ store.updater = new rdf.UpdateManager(store); // Add real-time live updates store.updater
26
+ store.features = [] // disable automatic node merging on store load
20
27
 
21
- export class SolidLogic {
22
- cache: {
23
- profileDocument: {
24
- [WebID: string]: NamedNode;
25
- };
26
- preferencesFile: {
27
- [WebID: string]: NamedNode;
28
- };
29
- };
28
+ const authn: AuthnLogic = new SolidAuthnLogic(session)
29
+
30
+ const acl = createAclLogic(store)
31
+ const containerLogic = createContainerLogic(store)
32
+ const utilityLogic = createUtilityLogic(store, acl, containerLogic)
33
+ const profile = createProfileLogic(store, authn, utilityLogic)
34
+ const chat = createChatLogic(store, profile)
35
+ const inbox = createInboxLogic(store, profile, utilityLogic, containerLogic, acl)
36
+ const typeIndex = createTypeIndexLogic(store, authn, profile, utilityLogic)
37
+ debug.log('SolidAuthnLogic initialized')
30
38
 
31
- store: LiveStore;
32
- me: string | undefined;
33
- underlyingFetch: { fetch: (url: string, options?: any) => any };
34
-
35
- chat: ChatLogic;
36
- profile: ProfileLogic;
37
- authn: AuthnLogic;
38
- util: UtilityLogic;
39
-
40
- constructor(specialFetch: { fetch: (url: any, requestInit: any) => any }, session: Session) {
41
- // would xpect to be able to do it this way: but get TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation status: 999
42
- // this.store = new rdf.LiveStore({})
43
- // this.store.fetcher._fetch = fetch
44
- console.log("SolidLogic: Unique instance created. There should only be one of these.")
45
- this.store = rdf.graph() as LiveStore; // Make a Quad store
46
- rdf.fetcher(this.store, { fetch: specialFetch.fetch}); // Attach a web I/O module, store.fetcher
47
- this.store.updater = new rdf.UpdateManager(this.store); // Add real-time live updates store.updater
48
-
49
- this.store.features = [] // disable automatic node merging on store load
50
- this.cache = {
51
- profileDocument: {},
52
- preferencesFile: {},
53
- };
54
- this.underlyingFetch = { fetch: fetch }; // Note global one not the one passed
55
- this.authn = new SolidAuthnLogic(session);
56
- debug.log('SolidAuthnLogic initialized')
57
- this.profile = new ProfileLogic(this.store, ns, this.authn);
58
- this.chat = new ChatLogic(this.store, ns, this.profile);
59
- this.util = new UtilityLogic(this.store, ns, this.underlyingFetch);
60
- }
61
-
62
- findAclDocUrl(url: string) {
63
- return this.util.findAclDocUrl(url);
64
- }
65
-
66
- async loadProfile(me: NamedNode): Promise<NamedNode> {
67
- /*
68
- // console.log('loadProfile cache ', this.cache)
69
- if (this.cache.profileDocument[me.value]) {
70
- return this.cache.profileDocument[me.value];
71
- } @@ just use the cache in the store
72
- */
73
- console.log('loadProfile me ', me)
74
- const profileDocument = me.doc()
75
- try {
76
- await this.store.fetcher.load(profileDocument);
77
- return profileDocument;
78
- } catch (err) {
79
- const message = `Cannot load profile ${profileDocument} : ${err}`;
80
- throw new Error(message);
81
- }
82
- }
83
-
84
- async loadPreferences(me: NamedNode): Promise<NamedNode> {
85
- console.log('loadPreferences cache ', this.cache)
86
- if (this.cache.preferencesFile[me.value]) {
87
- return this.cache.preferencesFile[me.value];
88
- }
89
- await this.loadProfile(me) // Load pointer to pref file
90
- const preferencesFile = this.store.any(me, ns.space('preferencesFile'), null, me.doc());
91
-
92
- // console.log('this.store.any()', this.store.any())
93
- /**
94
- * Are we working cross-origin?
95
- * Returns True if we are in a webapp at an origin, and the file origin is different
96
- */
97
- function differentOrigin(): boolean {
98
- if (!preferencesFile) {
99
- return true;
100
- }
101
- return (
102
- `${window.location.origin}/` !== new URL(preferencesFile.value).origin
103
- );
104
- }
105
-
106
- if (!preferencesFile) {
107
- throw new Error(
108
- `Can't find a preference file pointer in profile ${me.doc()}`
109
- );
110
- }
111
-
112
- // //// Load preference file
113
- try {
114
- await this.store.fetcher.load(preferencesFile as NamedNode, {
115
- withCredentials: true,
116
- });
117
- } catch (err) {
118
- // Really important to look at why
119
- const status = err.status;
120
- debug.log(`HTTP status ${status} for preference file ${preferencesFile}`);
121
- if (status === 401) {
122
- throw new UnauthorizedError();
123
- }
124
- if (status === 403) {
125
- if (differentOrigin()) {
126
- throw new CrossOriginForbiddenError();
127
- }
128
- throw new SameOriginForbiddenError();
129
- }
130
- if (status === 404) {
131
- throw new NotFoundError(preferencesFile.value);
132
- }
133
- throw new FetchError(err.status, err.message);
134
- }
135
- return preferencesFile as NamedNode;
136
- }
137
-
138
- getTypeIndex(
139
- me: NamedNode | string,
140
- preferencesFile: NamedNode | string,
141
- isPublic: boolean
142
- ): NamedNode[] {
143
- // console.log('getTypeIndex', this.store.each(me, undefined, undefined, preferencesFile), isPublic, preferencesFile)
144
- return this.store.each(
145
- me as NamedNode,
146
- isPublic ? ns.solid("publicTypeIndex") : ns.solid("privateTypeIndex"),
147
- undefined,
148
- preferencesFile as NamedNode
149
- ) as NamedNode[];
150
- }
151
-
152
- getRegistrations(instance, theClass) {
153
- return this.store
154
- .each(undefined, ns.solid("instance"), instance)
155
- .filter((r) => {
156
- return this.store.holds(r, ns.solid("forClass"), theClass);
157
- });
158
- }
159
-
160
- load(doc: NamedNode | NamedNode[] | string) {
161
- return this.store.fetcher.load(doc);
162
- }
163
-
164
- async loadIndexes(
165
- me: NamedNode | string,
166
- publicProfile: NamedNode | string | null,
167
- preferencesFile: NamedNode | string | null,
168
- onWarning = async (_err: Error) => {
169
- return undefined;
170
- }
171
- ): Promise<{
172
- private: any;
173
- public: any;
174
- }> {
175
- let privateIndexes: any[] = [];
176
- let publicIndexes: any[] = [];
177
- if (publicProfile) {
178
- publicIndexes = this.getTypeIndex(me, publicProfile, true);
179
- try {
180
- await this.load(publicIndexes as NamedNode[]);
181
- } catch (err) {
182
- onWarning(new Error(`loadIndex: loading public type index(es) ${err}`));
183
- }
184
- }
185
- if (preferencesFile) {
186
- privateIndexes = this.getTypeIndex(me, preferencesFile, false);
187
- // console.log({ privateIndexes })
188
- if (privateIndexes.length === 0) {
189
- await onWarning(
190
- new Error(
191
- `Your preference file ${preferencesFile} does not point to a private type index.`
192
- )
193
- );
194
- } else {
195
- try {
196
- await this.load(privateIndexes);
197
- } catch (err) {
198
- onWarning(
199
- new Error(`loadIndex: loading private type index(es) ${err}`)
200
- );
201
- }
202
- }
203
- // } else {
204
- // debug.log(
205
- // 'We know your preference file is not available, so we are not bothering with private type indexes.'
206
- // )
207
- }
208
-
209
- return {
210
- private: privateIndexes,
211
- public: publicIndexes,
212
- };
213
- }
214
-
215
- async createEmptyRdfDoc(doc: NamedNode, comment: string) {
216
- await this.store.fetcher.webOperation("PUT", doc.uri, {
217
- data: `# ${new Date()} ${comment}
218
- `,
219
- contentType: "text/turtle",
220
- });
39
+ function load(doc: NamedNode | NamedNode[] | string) {
40
+ return store.fetcher.load(doc);
221
41
  }
222
42
 
223
43
  // @@@@ use the one in rdflib.js when it is available and delete this
224
- updatePromise(
44
+ function updatePromise(
225
45
  del: Array<Statement>,
226
46
  ins: Array<Statement> = []
227
47
  ): Promise<void> {
228
48
  return new Promise((resolve, reject) => {
229
- this.store.updater.update(del, ins, function (_uri, ok, errorBody) {
49
+ store.updater.update(del, ins, function (_uri, ok, errorBody) {
230
50
  if (!ok) {
231
51
  reject(new Error(errorBody));
232
52
  } else {
@@ -236,27 +56,20 @@ export class SolidLogic {
236
56
  }); // promise
237
57
  }
238
58
 
239
- isContainer(url: string) {
240
- return this.util.isContainer(url);
241
- }
242
-
243
- getContainerElements(containerNode: NamedNode): NamedNode[] {
244
- return this.util.getContainerElements(containerNode);
245
- }
246
-
247
- getContainerMembers(containerUrl: string): Promise<string[]> {
248
- return this.util.getContainerMembers(containerUrl);
249
- }
250
-
251
- async recursiveDelete(url: string) {
252
- return this.util.recursiveDelete(url);
253
- }
254
-
255
- clearStore() {
256
- return this.util.clearStore();
59
+ function clearStore() {
60
+ store.statements.slice().forEach(store.remove.bind(store));
257
61
  }
258
62
 
259
- async fetch(url: string, options?: any) {
260
- return this.underlyingFetch.fetch(url, options);
63
+ return {
64
+ store,
65
+ authn,
66
+ acl,
67
+ inbox,
68
+ chat,
69
+ profile,
70
+ typeIndex,
71
+ load,
72
+ updatePromise,
73
+ clearStore
261
74
  }
262
75
  }