Merge branch 'master' into chore/linter
# Conflicts: # lib/contracts/contribution.js # lib/contracts/contributor.js # lib/contracts/proposal.js # lib/kredits.js # lib/serializers/contributor.js # yarn.lock
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
const schemas = require('kosmos-schemas');
|
||||
const validator = require('../utils/validator');
|
||||
/**
|
||||
* Handle serialization for JSON-LD object of the contributor, according to
|
||||
* https://github.com/67P/kosmos-schemas/blob/master/schemas/contributor.json
|
||||
@@ -6,41 +8,9 @@
|
||||
* @public
|
||||
*/
|
||||
class Contributor {
|
||||
/**
|
||||
* Deserialize JSON to object
|
||||
*
|
||||
* @method
|
||||
* @public
|
||||
*/
|
||||
static deserialize (serialized) {
|
||||
let {
|
||||
name,
|
||||
kind,
|
||||
url,
|
||||
accounts,
|
||||
} = JSON.parse(serialized.toString('utf8'));
|
||||
|
||||
let github_username, github_uid, wiki_username;
|
||||
let github = accounts.find((a) => a.site === 'github.com');
|
||||
let wiki = accounts.find((a) => a.site === 'wiki.kosmos.org');
|
||||
|
||||
if (github) {
|
||||
(({ username: github_username, uid: github_uid} = github));
|
||||
}
|
||||
if (wiki) {
|
||||
(({ username: wiki_username } = wiki));
|
||||
}
|
||||
|
||||
return {
|
||||
name,
|
||||
kind,
|
||||
url,
|
||||
accounts,
|
||||
github_uid,
|
||||
github_username,
|
||||
wiki_username,
|
||||
ipfsData: serialized,
|
||||
};
|
||||
constructor (attrs) {
|
||||
Object.keys(attrs).forEach(a => this[a] = attrs[a]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,15 +19,16 @@ class Contributor {
|
||||
* @method
|
||||
* @public
|
||||
*/
|
||||
static serialize (deserialized) {
|
||||
serialize () {
|
||||
let {
|
||||
name,
|
||||
kind,
|
||||
url,
|
||||
github_uid,
|
||||
github_username,
|
||||
gitea_username,
|
||||
wiki_username,
|
||||
} = deserialized;
|
||||
} = this;
|
||||
|
||||
let data = {
|
||||
"@context": "https://schema.kosmos.org",
|
||||
@@ -80,6 +51,14 @@ class Contributor {
|
||||
});
|
||||
}
|
||||
|
||||
if (gitea_username) {
|
||||
data.accounts.push({
|
||||
"site": "gitea.kosmos.org",
|
||||
"username": gitea_username,
|
||||
"url": `https://gitea.kosmos.org/${gitea_username}`
|
||||
});
|
||||
}
|
||||
|
||||
if (wiki_username) {
|
||||
data.accounts.push({
|
||||
"site": "wiki.kosmos.org",
|
||||
@@ -91,6 +70,60 @@ class Contributor {
|
||||
// Write it pretty to ipfs
|
||||
return JSON.stringify(data, null, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate serialized data against schema
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
validate () {
|
||||
const serialized = JSON.parse(this.serialize());
|
||||
const valid = validator.validate(serialized, schemas['contributor']);
|
||||
return valid ? Promise.resolve() : Promise.reject(validator.error);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserialize JSON to object
|
||||
*
|
||||
* @method
|
||||
* @public
|
||||
*/
|
||||
static deserialize (serialized) {
|
||||
let {
|
||||
name,
|
||||
kind,
|
||||
url,
|
||||
accounts,
|
||||
} = JSON.parse(serialized.toString('utf8'));
|
||||
|
||||
let github_username, github_uid, gitea_username, wiki_username;
|
||||
let github = accounts.find(a => a.site === 'github.com');
|
||||
let gitea = accounts.find(a => a.site === 'gitea.kosmos.org');
|
||||
let wiki = accounts.find(a => a.site === 'wiki.kosmos.org');
|
||||
|
||||
if (github) {
|
||||
(({ username: github_username, uid: github_uid} = github));
|
||||
}
|
||||
if (gitea) {
|
||||
(({ username: gitea_username } = gitea));
|
||||
}
|
||||
if (wiki) {
|
||||
(({ username: wiki_username } = wiki));
|
||||
}
|
||||
|
||||
return {
|
||||
name,
|
||||
kind,
|
||||
url,
|
||||
accounts,
|
||||
github_uid,
|
||||
github_username,
|
||||
gitea_username,
|
||||
wiki_username,
|
||||
ipfsData: serialized,
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = Contributor;
|
||||
|
||||
Reference in New Issue
Block a user