-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypeIndexHelper.ts
267 lines (215 loc) · 9.53 KB
/
TypeIndexHelper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import {
SolidDataset,
ThingPersisted,
addNamedNode,
buildThing,
createSolidDataset,
createThing,
getNamedNode,
getSolidDataset,
getThing,
getThingAll,
saveSolidDatasetAt,
setThing
} from "@inrupt/solid-client";
import {
RDF,
} from "@inrupt/vocab-common-rdf";
import { namedNode } from '@rdfjs/data-model';
import { __foafPerson, __forClass, __privateTypeIndex, __publicTypeIndex, __schemaPerson, __solidTypeRegistration, __solid_instance, __solid_instance_container } from "./constants";
import { NamedNode } from '@rdfjs/types'
/**
* TypeIndexHelper provides helper methods for working with typeIndexes in Solid.
* This includes methods for getting and updating a user's public and private
* typeIndexes.
* @public
*/
export class TypeIndexHelper {
/**
* Retrieves the profile for the given WebID.
* If no profile exists, creates a default profile.
*
* @param webId - The WebID URL to retrieve the profile for
* @param fetch - The authenticated fetch function
* @returns The profile Thing for the given WebID, or null if not found
* @internal
*/
public static async getMeProfile(
webId: string,
fetch: any
): Promise<ThingPersisted | null> {
const profileDS = await getSolidDataset(webId, { fetch });
let profileMe = getThing(profileDS, webId);
if (!profileMe) {
const profileMeThing = buildThing(createThing({ name: "me" }))
.addUrl(RDF.type, __foafPerson)
.addUrl(RDF.type, __schemaPerson)
.build();
const updatedProfile = setThing(profileDS, profileMeThing);
const updatedProfileDS = await saveSolidDatasetAt(
webId!,
updatedProfile,
{ fetch }
);
profileMe = getThing(updatedProfileDS, webId);
}
return profileMe;
}
/**
* Retrieves the typeIndexe for the given WebID, creating one if it does not exist.
*
* @param webId - The WebID of the user
* @param fetch - The authenticated fetch function to use for requests
* @param isPrivate - Whether to get the private or public typeIndexe
* @returns A NamedNode containing the typeIndexe URL
* @internal
*/
public static async getTypeIndex(
webId: string,
fetch: any,
isPrivate: boolean
): Promise<NamedNode<string>> {
const profileMe = await this.getMeProfile(webId, fetch);
const typeIndexPredicate = TypeIndexHelper.getTypeIndexPredicate(isPrivate);
const typeIndexUrl = TypeIndexHelper.getDefaultTypeIndexURL(webId, isPrivate);
const profileDS = await getSolidDataset(webId, { fetch });
if (profileMe) {
let typeIndex = getNamedNode(profileMe, typeIndexPredicate);
if (typeIndex) return typeIndex;
await this.createTypeIndex(fetch, typeIndexUrl);
const updatedProfileMe = addNamedNode(
profileMe,
typeIndexPredicate,
namedNode(typeIndexUrl)
);
const updatedProfileDS = setThing(profileDS, updatedProfileMe);
await saveSolidDatasetAt(webId, updatedProfileDS, { fetch });
return namedNode(typeIndexUrl);
} else {
await this.createTypeIndex(fetch, typeIndexUrl);
const profileMeThing = buildThing(createThing({ name: "me" }))
.addNamedNode(typeIndexPredicate, namedNode(typeIndexUrl))
.addUrl(RDF.type, __foafPerson)
.addUrl(RDF.type, __schemaPerson)
.build();
const updatedProfileDS = setThing(profileDS, profileMeThing);
await saveSolidDatasetAt(webId, updatedProfileDS, { fetch });
return namedNode(typeIndexUrl);
}
}
/**
* Retrieves all instances of the given RDF class from the user's typeIndexe.
*
* @param webId - The user's WebID
* @param rdfClass - The RDF class to retrieve instances for, as a Valid URL
* @param fetch - Authenticated fetch function
* @param isPrivate - Whether the typeIndexe is private or public
* @returns Promise resolving to an object containing instance URLs and instanceContainers URLs.
*/
public static async getFromTypeIndex(webId: string, rdfClass: string, fetch: any, isPrivate: boolean): Promise<{ instanceContainers: string[]; instances: string[]; }> {
const typeIndex = await this.getTypeIndex(webId, fetch, isPrivate);
const typeIndexDS = await getSolidDataset(typeIndex?.value, { fetch });
const allRegisteries = getThingAll(typeIndexDS);
const instances: string[] = []
const instanceContainers: string[] = []
allRegisteries.forEach(registery => {
const forClass = getNamedNode(registery, __forClass)
if (forClass?.value === rdfClass) {
const instance = getNamedNode(registery, __solid_instance)?.value
const instanceContainer = getNamedNode(registery, __solid_instance_container)?.value
instance && instances?.push(instance)
instanceContainer && instanceContainers?.push(instanceContainer)
}
})
const instanceContainersPromises = instanceContainers.map(async (instanceContainer) => {
const instanceContainerDS = await getSolidDataset(instanceContainer, { fetch })
const all = getThingAll(instanceContainerDS); // all files under the instanceContainer
const urls = all.filter(x => x.url !== "").map(x => x.url) // all file urls
return urls.filter(url => url !== instanceContainer) // remove the instanceContainer itself, only file urls needed;
})
const innerInstances = (await Promise.all([...instanceContainersPromises])).flat();
return {
instanceContainers: instanceContainers,
instances: [...new Set([...instances, ...innerInstances])]
}
}
/**
* Registers a typeRegistration in the user's typeIndexe.
*
* @param webId - The WebID of the user
* @param typeRegistrationTitle - The title to use for the typeRegistration inside the typeIndex,
* @param rdfClass - The RDF class that this registration is for, as a Valid URL
* @param fetch - The authenticated fetch function
* @param registeryUrl - The URL of the solid:instance or solid:instanceContainer being registered
* @param isContainer - Whether to register a solid:instanceContainer or a solid:instance
* @param isPrivate - Whether to register in the private or public typeIndexe
* @returns A Promise resolving to the updated typeIndexe dataset
*/
public static async registerInTypeIndex(
webId: string,
typeRegistrationTitle: string,
rdfClass: string,
fetch: any,
registeryUrl: string,
isContainer: boolean,
isPrivate: boolean,
): Promise<SolidDataset> {
const typeIndex = await this.getTypeIndex(webId, fetch, isPrivate);
const typeIndexDS = await getSolidDataset(typeIndex?.value, { fetch });
const registeryThing = buildThing(createThing({ name: typeRegistrationTitle }))
.addNamedNode(__forClass, namedNode(rdfClass))
.addNamedNode(isContainer ? __solid_instance_container : __solid_instance, namedNode(registeryUrl))
.addUrl(RDF.type, __solidTypeRegistration)
.build();
const updatedTypeIndexDS = setThing(typeIndexDS, registeryThing);
await saveSolidDatasetAt(registeryUrl, createSolidDataset(), { fetch });
return await saveSolidDatasetAt(typeIndex?.value, updatedTypeIndexDS, { fetch });
}
/**
* Creates a new empty TypeIndex file at the given indexUrl.
*
* @param fetch - Authenticated fetch function
* @param typeIndexUrl - URL where the new TypeIndex file will be created
* @returns A Promise resolving to the created TypeIndex dataset if successful, or undefined if there was an error.
* @internal
*/
public static async createTypeIndex(
fetch: any,
typeIndexUrl: string
): Promise<SolidDataset | undefined> {
try {
await fetch(typeIndexUrl, {
method: "PUT",
headers: {
"Content-Type": "text/turtle",
},
body: `@prefix solid: <http://www.w3.org/ns/solid/terms#>.\n\n<> a solid:TypeIndex, solid:UnlistedDocument.`,
});
return await getSolidDataset(typeIndexUrl, { fetch });
} catch (error) { }
}
/**
* Returns the predicate to use for the typeIndexe based on whether it is private or public.
*
* @param isPrivate - Whether the typeIndexe is private or public.
* @returns The predicate to use - either __privateTypeIndex or __publicTypeIndex.
* @internal
*/
public static getTypeIndexPredicate(isPrivate: boolean): string {
return isPrivate ? __privateTypeIndex : __publicTypeIndex;
}
/**
* Generates the URL for the given user's typeIndexe file.
*
* @param webId - The user's WebID URL
* @param isPrivate - Whether the typeIndexe is private or public.
* @returns The full URL for the typeIndexe file in the user's /settings/ folder
* @internal
*/
public static getDefaultTypeIndexURL(
webId: string,
isPrivate: boolean,
): string {
return `${webId.split("/profile")[0]}/settings/${isPrivate ? "privateTypeIndex" : "publicTypeIndex"}.ttl`;
}
}