rubberguppe/pub/utils.js

62 lines
1.3 KiB
JavaScript
Raw Normal View History

2019-09-21 21:20:14 +00:00
'use strict'
const config = require('../config.json')
const pubConsts = require('./consts')
2019-09-21 21:20:14 +00:00
module.exports = {
2019-09-22 05:20:37 +00:00
usernameToIRI,
toJSONLD,
arrayToCollection,
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 {
'@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) {
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
}
function objectIdToIRI (oid) {
if (oid.toHexString) {
oid = oid.toHexString()
}
return `https://${config.DOMAIN}/o/${oid}`
}
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
}
}