2019-09-21 21:20:14 +00:00
|
|
|
'use strict'
|
|
|
|
const config = require('../config.json')
|
2019-09-23 17:52:22 +00:00
|
|
|
const pubConsts = require('./consts')
|
2019-09-21 21:20:14 +00:00
|
|
|
|
|
|
|
module.exports = {
|
2019-09-24 03:18:35 +00:00
|
|
|
actvityIdToIRI,
|
2019-09-22 05:20:37 +00:00
|
|
|
usernameToIRI,
|
|
|
|
toJSONLD,
|
|
|
|
arrayToCollection,
|
2019-09-23 17:52:22 +00:00
|
|
|
actorFromActivity,
|
|
|
|
objectIdToIRI,
|
|
|
|
validateActivity,
|
|
|
|
validateObject
|
2019-09-21 21:20:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function actorFromActivity (activity) {
|
2019-09-22 05:20:37 +00:00
|
|
|
if (Object.prototype.toString.call(activity.actor) === '[object String]') {
|
|
|
|
return activity.actor
|
|
|
|
}
|
|
|
|
if (activity.actor.type === 'Link') {
|
|
|
|
return activity.actor.href
|
|
|
|
}
|
|
|
|
return activity.actor.id
|
2019-09-21 21:20:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function arrayToCollection (arr, ordered) {
|
2019-09-22 05:20:37 +00:00
|
|
|
return {
|
2019-09-23 17:52:22 +00:00
|
|
|
'@context': pubConsts.ASContext,
|
2019-09-22 05:20:37 +00:00
|
|
|
totalItems: arr.length,
|
|
|
|
type: ordered ? 'orderedCollection' : 'collection',
|
|
|
|
[ordered ? 'orderedItems' : 'items']: arr
|
|
|
|
}
|
2019-09-21 21:20:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function toJSONLD (obj) {
|
2019-09-23 17:52:22 +00:00
|
|
|
obj['@context'] = obj['@context'] || pubConsts.ASContext
|
2019-09-22 05:20:37 +00:00
|
|
|
return obj
|
2019-09-21 21:20:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function usernameToIRI (user) {
|
2019-09-22 05:20:37 +00:00
|
|
|
return `https://${config.DOMAIN}/u/${user}`
|
2019-09-21 21:20:14 +00:00
|
|
|
}
|
2019-09-23 17:52:22 +00:00
|
|
|
|
|
|
|
function objectIdToIRI (oid) {
|
|
|
|
if (oid.toHexString) {
|
|
|
|
oid = oid.toHexString()
|
|
|
|
}
|
|
|
|
return `https://${config.DOMAIN}/o/${oid}`
|
|
|
|
}
|
|
|
|
|
2019-09-24 03:18:35 +00:00
|
|
|
function actvityIdToIRI (oid) {
|
|
|
|
if (oid.toHexString) {
|
|
|
|
oid = oid.toHexString()
|
|
|
|
}
|
|
|
|
return `https://${config.DOMAIN}/s/${oid}`
|
|
|
|
}
|
|
|
|
|
2019-09-23 17:52:22 +00:00
|
|
|
function validateObject (object) {
|
|
|
|
if (object && object.id) {
|
|
|
|
// object['@context'] = object['@context'] || pubConsts.ASContext
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function validateActivity (object) {
|
|
|
|
if (object && object.id && object.actor) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|