-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgog.js
More file actions
404 lines (391 loc) · 16.1 KB
/
gog.js
File metadata and controls
404 lines (391 loc) · 16.1 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/usr/bin/env node
/**
* Gallery of Glosses (GOG) controller for RERUM operations
* Handles specialized operations for the Gallery of Glosses application
* @author Claude Sonnet 4, cubap, thehabes
*/
import { newID, isValidID, db } from '../database/index.js'
import utils from '../utils.js'
import { _contextid, ObjectID, createExpressError, getAgentClaim, parseDocumentID, idNegotiation } from './utils.js'
/**
* THIS IS SPECIFICALLY FOR 'Gallery of Glosses'
* Starting from a ManuscriptWitness URI get all WitnessFragment entities that are a part of the Manuscript.
* The inbound request is a POST request with an Authorization header
* The Bearer Token in the header must be from TinyMatt.
* The body must be formatted correctly - {"ManuscriptWitness":"witness_uri_here"}
*
* TODO? Some sort of limit and skip for large responses?
*
* @return The set of {'@id':'123', '@type':'WitnessFragment'} objects that match this criteria, as an Array
* */
const _gog_fragments_from_manuscript = async function (req, res, next) {
res.set("Content-Type", "application/json; charset=utf-8")
const agent = getAgentClaim(req, next)
const agentID = agent.split("/").pop()
const manID = req.body["ManuscriptWitness"]
const limit = parseInt(req.query.limit ?? 50)
const skip = parseInt(req.query.skip ?? 0)
// This request can only be made my Gallery of Glosses production apps.
if (agentID !== "61043ad4ffce846a83e700dd") {
const err = {
message: `Only the Gallery of Glosses can make this request.`,
status: 403
}
next(createExpressError(err))
return
}
// Must have a properly formed body with a usable value
if(!manID || !manID.startsWith("http")){
const err = {
message: `The body must be JSON like {"ManuscriptWitness":"witness_uri_here"}.`,
status: 400
}
next(createExpressError(err))
return
}
try {
let matches = []
const partOfConditions = [
{"body.partOf.value": manID.replace(/^https?/, "http")},
{"body.partOf.value": manID.replace(/^https?/, "https")},
{"body.partOf": manID.replace(/^https?/, "http")},
{"body.partOf": manID.replace(/^https?/, "https")}
]
const generatorConditions = [
{"__rerum.generatedBy": agent.replace(/^https?/, "http")},
{"__rerum.generatedBy": agent.replace(/^https?/, "https")}
]
const fragmentTypeConditions = [
{"witnessFragment.type": "WitnessFragment"},
{"witnessFragment.@type": "WitnessFragment"}
]
const annoTypeConditions = [
{"type": "Annotation"},
{"@type": "Annotation"},
{"@type": "oa:Annotation"}
]
let witnessFragmentPipeline = [
// Step 1: Detect Annotations bodies noting their 'target' is 'partOf' this Manuscript
{
$match: {
"__rerum.history.next": { "$exists": true, "$size": 0 },
"$and":[
{"$or": annoTypeConditions},
{"$or": partOfConditions},
{"$or": generatorConditions}
]
}
},
// Step 1.1 through 1.3 for limit and skip functionality.
{ $sort : { _id: 1 } },
{ $skip : skip },
{ $limit : limit },
// Step 2: Using the target of those Annotations lookup the Entity they represent and store them in a witnessFragment property on the Annotation
// Note that $match had filtered down the alpha collection, so we use $lookup to look through the whole collection again.
// FIXME? a target that is http will not match an @id that is https
{
$lookup: {
from: "alpha",
localField: "target", // Field in `Annotation` referencing `@id` in `alpha` corresponding to a WitnessFragment @id
foreignField: "@id",
as: "witnessFragment"
}
},
// Step 3: Filter out anything that is not a WitnessFragment entity (and a leaf)
{
$match: {
"witnessFragment.__rerum.history.next": { "$exists": true, "$size": 0 },
"$or": fragmentTypeConditions
}
},
// Step 4: Unwrap the Annotation and just return its corresponding WitnessFragment entity
{
$project: {
"_id": 0,
"@id": "$witnessFragment.@id",
"@type": "WitnessFragment"
}
},
// Step 5: @id values are an Array of 1 and need to be a string instead
{
$unwind: { "path": "$@id" }
}
// Step 6: Cache it?
]
// console.log("Start GoG WitnessFragment Aggregator")
const start = Date.now()
let witnessFragments = await db.aggregate(witnessFragmentPipeline).toArray()
.then((fragments) => {
if (fragments instanceof Error) {
throw fragments
}
return fragments
})
const fragmentSet = new Set(witnessFragments)
witnessFragments = Array.from(fragmentSet.values())
// Note that a server side expand() is available and could be used to expand these fragments here.
// console.log("End GoG WitnessFragment Aggregator")
// console.log(witnessFragments.length+" fragments found for this Manuscript")
// const end = Date.now()
// console.log(`Total Execution time: ${end - start} ms`)
res.set(utils.configureLDHeadersFor(witnessFragments))
res.json(witnessFragments)
}
catch (error) {
console.error(error)
next(createExpressError(error))
}
}
/**
* THIS IS SPECIFICALLY FOR 'Gallery of Glosses'
* Starting from a ManuscriptWitness URI get all Gloss entities that are a part of the Manuscript.
* The inbound request is a POST request with an Authorization header.
* The Bearer Token in the header must be from TinyMatt.
* The body must be formatted correctly - {"ManuscriptWitness":"witness_uri_here"}
*
* TODO? Some sort of limit and skip for large responses?
*
* @return The set of {'@id':'123', '@type':'Gloss'} objects that match this criteria, as an Array
* */
const _gog_glosses_from_manuscript = async function (req, res, next) {
res.set("Content-Type", "application/json; charset=utf-8")
const agent = getAgentClaim(req, next)
const agentID = agent.split("/").pop()
const manID = req.body["ManuscriptWitness"]
const limit = parseInt(req.query.limit ?? 50)
const skip = parseInt(req.query.skip ?? 0)
let err = { message: `` }
// This request can only be made my Gallery of Glosses production apps.
if (!agentID === "61043ad4ffce846a83e700dd") {
err = Object.assign(err, {
message: `Only the Gallery of Glosses can make this request.`,
status: 403
})
}
// Must have a properly formed body with a usable value
else if(!manID || !manID.startsWith("http")){
err = Object.assign(err, {
message: `The body must be JSON like {"ManuscriptWitness":"witness_uri_here"}.`,
status: 400
})
}
if (err.status) {
next(createExpressError(err))
return
}
try {
let matches = []
const partOfConditions = [
{"body.partOf.value": manID.replace(/^https?/, "http")},
{"body.partOf.value": manID.replace(/^https?/, "https")},
{"body.partOf": manID.replace(/^https?/, "http")},
{"body.partOf": manID.replace(/^https?/, "https")}
]
const generatorConditions = [
{"__rerum.generatedBy": agent.replace(/^https?/, "http")},
{"__rerum.generatedBy": agent.replace(/^https?/, "https")}
]
const fragmentTypeConditions = [
{"witnessFragment.type": "WitnessFragment"},
{"witnessFragment.@type": "WitnessFragment"}
]
const annoTypeConditions = [
{"type": "Annotation"},
{"@type": "Annotation"},
{"@type": "oa:Annotation"}
]
let glossPipeline = [
// Step 1: Detect Annotations bodies noting their 'target' is 'partOf' this Manuscript
{
$match: {
"__rerum.history.next": { $exists: true, $size: 0 },
"$and":[
{"$or": annoTypeConditions},
{"$or": partOfConditions},
{"$or": generatorConditions}
]
}
},
// Step 1.1 through 1.3 for limit and skip functionality.
{ $sort : { _id: 1 } },
{ $skip : skip },
{ $limit : limit },
// Step 2: Using the target of those Annotations lookup the Entity they represent and store them in a witnessFragment property on the Annotation
// Note that $match had filtered down the alpha collection, so we use $lookup to look through the whole collection again.
// FIXME? a target that is http will not match an @id that is https
{
$lookup: {
from: "alpha",
localField: "target", // Field in `Annotation` referencing `@id` in `alpha` corresponding to a WitnessFragment @id
foreignField: "@id",
as: "witnessFragment"
}
},
// Step 3: Filter Annotations to be only those which are for a WitnessFragment Entity
{
$match: {
"$or": fragmentTypeConditions
}
},
// Step 4: Unwrap the Annotation and just return its corresponding WitnessFragment entity
{
$project: {
"_id": 0,
"@id": "$witnessFragment.@id",
"@type": "WitnessFragment"
}
},
// Step 5: @id values are an Array of 1 and need to be a string instead
{
$unwind: { "path": "$@id" }
},
// Step 6: Using the WitnessFragment ids lookup their references Annotations
// Note that $match had filtered down the alpha collection, so we use $lookup to look through the whole collection again.
{
$lookup: {
from: "alpha",
localField: "@id", // Field in `WitnessFragment` referencing `target` in `alpha` corresponding to a Gloss @id
foreignField: "target",
as: "anno"
}
},
// Step 7: Filter Annos down to those that are the 'references' Annotations
{
$match: {
"anno.body.references":{ "$exists": true }
}
},
// Step 7: Collect together the body.references.value[] of those Annotations. Those are the relevant Gloss URIs.
{
$project: {
"_id": 0,
"@id": "$anno.body.references.value",
"@type": "Gloss"
}
},
// Step 8: @id values are an Array of and Array 1 because references.value is an Array
{
$unwind: { "path": "$@id" }
},
// Step 9: @id values are now an Array of 1 and need to be a string instead
{
$unwind: { "path": "$@id" }
}
]
// console.log("Start GoG Gloss Aggregator")
// const start = Date.now()
let glosses = await db.aggregate(glossPipeline).toArray()
.then((fragments) => {
if (fragments instanceof Error) {
throw fragments
}
return fragments
})
const glossSet = new Set(glosses)
glosses = Array.from(glossSet.values())
// Note that a server side expand() is available and could be used to expand these fragments here.
// console.log("End GoG Gloss Aggregator")
// console.log(glosses.length+" Glosses found for this Manuscript")
// const end = Date.now()
// console.log(`Total Execution time: ${end - start} ms`)
res.set(utils.configureLDHeadersFor(glosses))
res.json(glosses)
}
catch (error) {
console.error(error)
next(createExpressError(error))
}
}
/**
* Find relevant Annotations targeting a primitive RERUM entity. This is a 'full' expand.
* Add the descriptive information in the Annotation bodies to the primitive object.
*
* Anticipate likely Annotation body formats
* - anno.body
* - anno.body.value
*
* Anticipate likely Annotation target formats
* - target: 'uri'
* - target: {'id':'uri'}
* - target: {'@id':'uri'}
*
* Anticipate likely Annotation type formats
* - {"type": "Annotation"}
* - {"@type": "Annotation"}
* - {"@type": "oa:Annotation"}
*
* @param primitiveEntity - An existing RERUM object
* @param GENERATOR - A registered RERUM app's User Agent
* @param CREATOR - Some kind of string representing a specific user. Often combined with GENERATOR.
* @return the expanded entity object
*
*/
const expand = async function(primitiveEntity, GENERATOR=undefined, CREATOR=undefined){
if(!primitiveEntity?.["@id"] || primitiveEntity?.id) return primitiveEntity
const targetId = primitiveEntity["@id"] ?? primitiveEntity.id ?? "unknown"
let queryObj = {
"__rerum.history.next": { $exists: true, $size: 0 }
}
let targetPatterns = ["target", "target.@id", "target.id"]
let targetConditions = []
let annoTypeConditions = [{"type": "Annotation"}, {"@type":"Annotation"}, {"@type":"oa:Annotation"}]
if (targetId.startsWith("http")) {
for(const targetKey of targetPatterns){
targetConditions.push({ [targetKey]: targetId.replace(/^https?/, "http") })
targetConditions.push({ [targetKey]: targetId.replace(/^https?/, "https") })
}
queryObj["$and"] = [{"$or": targetConditions}, {"$or": annoTypeConditions}]
}
else{
queryObj["$or"] = annoTypeConditions
queryObj.target = targetId
}
// Only expand with data from a specific app
if(GENERATOR) {
// Need to check http:// and https://
const generatorConditions = [
{"__rerum.generatedBy": GENERATOR.replace(/^https?/, "http")},
{"__rerum.generatedBy": GENERATOR.replace(/^https?/, "https")}
]
if (GENERATOR.startsWith("http")) {
queryObj["$and"].push({"$or": generatorConditions })
}
else{
// It should be a URI, but this can be a fallback.
queryObj["__rerum.generatedBy"] = GENERATOR
}
}
// Only expand with data from a specific creator
if(CREATOR) {
// Need to check http:// and https://
const creatorConditions = [
{"creator": CREATOR.replace(/^https?/, "http")},
{"creator": CREATOR.replace(/^https?/, "https")}
]
if (CREATOR.startsWith("http")) {
queryObj["$and"].push({"$or": creatorConditions })
}
else{
// It should be a URI, but this can be a fallback.
queryObj["creator"] = CREATOR
}
}
// Get the Annotations targeting this Entity from the db. Remove _id property.
let matches = await db.find(queryObj).toArray()
matches = matches.map(o => {
delete o._id
return o
})
// Combine the Annotation bodies with the primitive object
let expandedEntity = JSON.parse(JSON.stringify(primitiveEntity))
for(const anno of matches){
const body = anno.body
let keys = Object.keys(body)
if(!keys || keys.length !== 1) return
let key = keys[0]
let val = body[key].value ?? body[key]
expandedEntity[key] = val
}
return expandedEntity
}
export { _gog_fragments_from_manuscript, _gog_glosses_from_manuscript, expand }