forked from CenterForDigitalHumanities/rerum_server_nodejs
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpredicates.js
More file actions
75 lines (70 loc) · 2.2 KB
/
predicates.js
File metadata and controls
75 lines (70 loc) · 2.2 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
/**
* Check this object for deleted status. deleted objects in RERUM look like {"@id":"{some-id}", __deleted:{object properties}}
*/
const isDeleted = function(obj){
return obj.hasOwnProperty("__deleted")
}
/**
* Check this object for released status. Released objects in RERUM look like {"@id":"{some-id}", __rerum:{"isReleased" : "ISO-DATE-TIME"}}
*/
const isReleased = function(obj){
let bool =
(obj.hasOwnProperty("__rerum") &&
obj.__rerum.hasOwnProperty("isReleased") &&
obj.__rerum.isReleased !== "")
return bool
}
/**
* Check to see if the agent from the request (req.user had decoded token) matches the generating agent of the object in mongodb.
*/
const isGenerator = function(origObj, changeAgent){
//If the object in mongo does not have a generator, something wrong. however, there is no permission to check, no generator is the same as any generator.
const generatingAgent = origObj.__rerum.generatedBy ?? changeAgent
//bots get a free pass through
return generatingAgent === changeAgent
}
/**
* Check if this object is of a known container type.
* If so, it requires a different header than a stand-alone resource object.
* return boolean
*/
const isContainerType = function(obj){
let answer = false
let typestring = obj["@type"] ?? obj.type ?? ""
const knownContainerTypes = [
"ItemList",
"AnnotationPage",
"AnnotationList",
"AnnotationCollection",
"Sequence",
"Range",
"Canvas",
"List",
"Set",
"Collection"
]
for(const t of knownContainerTypes){
//Dang those pesky prefixes...circumventing exact match for now
if(typestring.includes(t)){
answer = true
break
}
}
return answer
}
/**
* Check if this object is a Linked Data object.
* If so, it will have an @context -(TODO) that resolves!
* return boolean
*/
const isLD = function(obj){
//Note this is always false if obj is an array, like /since, /history or /query provide as a return.
return Array.isArray(obj) ? false : obj["@context"] ? true : false
}
export {
isDeleted,
isReleased,
isGenerator,
isContainerType,
isLD
}