This repository has been archived by the owner on Oct 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Endorse Badge Class.txt
68 lines (67 loc) · 7.31 KB
/
Endorse Badge Class.txt
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
Type: Endorse Badge Class
Version: 2.0
Description: (Un)endorse a badge class.
Template: {"badgeClass":{"type":"str","desc":"ID van de badge klasse.","name":"Badge klasse"},"endorsementComment":{"type":"str","desc":"Comment over deze endorsement.","name":"Comment"},"endorse":{"type":"bool","desc":"Endorse deze badge klasse of niet meer.","name":"Endorse"}}
Init: YXdhaXQgcXVlcnkoIkNSRUFURSIsICJlbmRvcnNlbWVudGNsYXNzZXMiLCAiKGlkIEJJR0lOVCBQUklNQVJZIEtFWSwgIiArDQogICAgImVudGl0eSBWQVJDSEFSKDM1KSBOT1QgTlVMTCBSRUZFUkVOQ0VTIGVudGl0aWVzKGVudGl0eSksIGNsYXNzIFZBUkNIQVIoMjU2KSBOT1QgTlVMTCwgIiArDQogICAgImpzb24gSlNPTiBOT1QgTlVMTCwgaXNzdWVkX29uIEJJR0lOVCBOT1QgTlVMTCwgcmV2b2tlZCBCSUdJTlQpOyIsIFtdKTsNCmF3YWl0IHF1ZXJ5KCJJTkRFWCIsICJlbmRvcnNlbWVudGNsYXNzZXMiLCAiKGNsYXNzKTsiLCBbImVuZG9yc2VtZW50Y2xhc3Nlc19jbGFzc19pbmRleCJdKTs=
Code: aWYgKHBheWxvYWQuYmFkZ2VDbGFzcy5sZW5ndGggPT09IDAgfHwgcGF5bG9hZC5iYWRnZUNsYXNzLmxlbmd0aCA+IDI1Nikgew0KICAgIHJldHVybiAiSW52YWxpZCBiYWRnZSBjbGFzcyBpZC4iOw0KfQ0KLy9JZiB3ZSBhcmUgcmV2b2tpbmc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bnRpdHksIGpzb24sIGlzc3VlZF9vbikgVkFMVUVTICgkMSwgJDIsICQzLCAkNCwgJDUpOyIsIFtuZXdJZCwgcGF5bG9hZC5iYWRnZUNsYXNzLCBmcm9tLCBKU09OLnN0cmluZ2lmeShlbmRvcnNlbWVudCksIHByZXZpb3VzQmxvY2tUaW1lc3RhbXBdKTsNCnJldHVybiAiT0siOw==
Init as text:
await query("CREATE", "endorsementclasses", "(id BIGINT PRIMARY KEY, " +
"entity VARCHAR(35) NOT NULL REFERENCES entities(entity), class VARCHAR(256) NOT NULL, " +
"json JSON NOT NULL, issued_on BIGINT NOT NULL, revoked BIGINT);", []);
await query("INDEX", "endorsementclasses", "(class);", ["endorsementclasses_class_index"]);
Code as text:
if (payload.badgeClass.length === 0 || payload.badgeClass.length > 256) {
return "Invalid badge class id.";
}
//If we are revoking an endorsement do not do all the extra checks.
if (!payload.endorse) {
const revoked = await query("UPDATE", "endorsementclasses", "SET revoked = $3 WHERE class = $1 AND entity = $2 AND revoked IS NULL;", [payload.badgeClass, from, previousBlockTimestamp]);
if (revoked.rowCount === 0) {
return "No unrevoked endorsement for this badgeclass by this user exists.";
}
return "OK";
}
//Check if the entity exists
const entity = (await query("SELECT", "entities", "WHERE entity = $1;", [from])).rows[0];
if (entity === undefined) {
return "Entity does not exists or user is not an entity.";
}
if (entity.revoked !== null) {
return "Entity is withdrawn.";
}
//Check if the institution is not withdrawn (we are sure it exists if the entity exists)
const institution = (await query("SELECT", "institutions", "WHERE institution = $1;", [entity.institution])).rows[0];
if (institution.revoked !== null) {
return "Institution of entity is withdrawn.";
}
//Check if there is an endorsement already
const oldEndorsement = (await query("SELECT", "endorsementclasses", "WHERE class = $1 AND entity = $2 AND revoked IS NULL;", [payload.badgeClass, from])).rows[0];
if (oldEndorsement !== undefined) {
return "Badge already endorsed by entity, to update revoke current endorsement and create a new one.";
}
//The the url on which the endorsement needs to be made available
const url = (await query("SELECT", "settings", "WHERE key = 'url';", [])).rows[0];
if (url === undefined) {
return "URL not set by Surf.";
}
//Assign an unique id to the endorsement (we avoid using SERIAL as it doesn't work in combination with ROLLBACK)
const oldId = (await query("SELECT", "endorsementclasses", "ORDER BY id DESC LIMIT 1;", [])).rows[0];
const newId = oldId === undefined ? 0 : oldId.id + 1;
//Create the endorsement json
const endorsement = {
"@context": "https://w3id.org/openbadges/v2",
"type": "Endorsement",
"id": `${url.value}endorsementbadgeclass?${newId}`,
"issuer": institution.iri,
"claim": {
id: payload.badgeClass,
endorsementComment: payload.endorsementComment
},
"issuedOn": new Date(previousBlockTimestamp).toISOString().split(".")[0] + "Z",
"verification": {
type: "hosted"
}
};
//Create the endorsement for the badge class
await query("INSERT", "endorsementclasses", "(id, class, entity, json, issued_on) VALUES ($1, $2, $3, $4, $5);", [newId, payload.badgeClass, from, JSON.stringify(endorsement), previousBlockTimestamp]);
return "OK";