-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
handler.js
76 lines (64 loc) · 2.41 KB
/
handler.js
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
const TrustNet = require("./index.js")
function TrustHandler (areas=[]) {
if (!(this instanceof TrustHandler)) return new TrustHandler(areas)
this.trustAreas = {}
// trustAreas was passed in as a list
if (typeof areas[Symbol.iterator] === "function") {
areas.forEach((area) => this.add(area))
} else if (typeof areas === "object") {
// trustAreas was passed in as mapping of {area: TrustNet opts}
Object.entries(areas).forEach((area, opts) => this.add(area, opts))
}
}
TrustHandler.prototype.add = function (area, opts=null) {
let tnet = new TrustNet(opts)
this.trustAreas[area] = tnet
return tnet
}
TrustHandler.prototype.loadAll = async function (areaMapping) {
const promises = Object.keys(areaMapping).map((area) => {
const rootid = areaMapping[area].rootid
const assignments = areaMapping[area].assignments
const distrusted = areaMapping[area].distrusted
return this.load(area, rootid, assignments, distrusted)
})
return Promises.all(promises)
}
TrustHandler.prototype.load = async function (area, rootid, assignments, distrusted=[]) {
if (!this.trustAreas[area]) this.add(area)
return this.trustAreas[area].load(rootid, assignments, distrusted)
}
TrustHandler.prototype.getMostTrusted = function (area) {
area = this._processArea(area)
if (!area) return null
return this.get(area).getMostTrusted()
}
TrustHandler.prototype.getRankings = function (area) {
area = this._processArea(area)
if (!area) return null
return this.get(area).getRankings()
}
TrustHandler.prototype.getAllTrusted = function (area) {
area = this._processArea(area)
if (!area) return null
return this.get(area).getAllTrusted()
}
// handle parameter-less calls to the trust handler. can be useful if there's only a single trust area
TrustHandler.prototype._processArea = function (area) {
if (typeof area === "undefined" || !this.trustAreas[area]) {
const areas = this.list()
if (areas.length === 0 || areas.length > 1) return null
return areas[0]
}
}
TrustHandler.prototype.get = function (area) {
return this.trustAreas[area] || null
}
TrustHandler.prototype.list = function () {
return Object.keys(this.trustAreas).sort()
}
TrustHandler.prototype.remove = function (area) {
// TODO: add destroy method to trustnet?
delete this.trustAreas[area]
}
module.exports = TrustHandler