Should fix #234 + refactor domain to hostname

This commit is contained in:
Armin 2019-03-25 13:15:37 +01:00
parent 47e67fe334
commit 1d5a8d3917
6 changed files with 33 additions and 30 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
.env
.idea
*.iml
.vscode
.DS_Store
npm-debug.log*

View File

@ -22,8 +22,8 @@ let activityPub = null
export { activityPub }
export default class ActivityPub {
constructor (domain, port, uri) {
if (domain === 'localhost') { this.domain = `${domain}:${port}` } else { this.domain = domain }
constructor (hostname, port, uri) {
if (hostname === 'localhost') { this.hostname = `${hostname}:${port}` } else { this.hostname = hostname }
this.port = port
this.dataSource = new NitroDataSource(uri)
this.collections = new Collections(this.dataSource)
@ -33,7 +33,9 @@ export default class ActivityPub {
if (!activityPub) {
dotenv.config()
const url = new URL(process.env.GRAPHQL_URI)
activityPub = new ActivityPub(url.hostname || 'localhost', url.port || 4000, url.origin)
// TODO Check why the hostname attribute in the prod env not containing the tld! Following line is a quick fix!!
const hostname = url.hostname.endsWith('.org') ? url.hostname : url.hostname.concat('.org')
activityPub = new ActivityPub(hostname || 'localhost', url.port || 4000, url.origin)
// integrate into running graphql express server
server.express.set('ap', activityPub)
@ -59,7 +61,7 @@ export default class ActivityPub {
}
}, async (err, response, toActorObject) => {
if (err) return reject(err)
debug(`name = ${toActorName}@${this.domain}`)
debug(`name = ${toActorName}@${this.hostname}`)
// save shared inbox
toActorObject = JSON.parse(toActorObject)
await this.dataSource.addSharedInboxEndpoint(toActorObject.endpoints.sharedInbox)
@ -184,7 +186,7 @@ export default class ActivityPub {
}
generateStatusId (slug) {
return `http://${this.domain}/activitypub/users/${slug}/status/${uuid()}`
return `https://${this.hostname}/activitypub/users/${slug}/status/${uuid()}`
}
async sendActivity (activity) {

View File

@ -11,14 +11,14 @@ export function createNoteObject (text, name, id, published) {
return {
'@context': 'https://www.w3.org/ns/activitystreams',
'id': `https://${activityPub.domain}/activitypub/users/${name}/status/${createUuid}`,
'id': `https://${activityPub.hostname}/activitypub/users/${name}/status/${createUuid}`,
'type': 'Create',
'actor': `https://${activityPub.domain}/activitypub/users/${name}`,
'actor': `https://${activityPub.hostname}/activitypub/users/${name}`,
'object': {
'id': `https://${activityPub.domain}/activitypub/users/${name}/status/${id}`,
'id': `https://${activityPub.hostname}/activitypub/users/${name}/status/${id}`,
'type': 'Note',
'published': published,
'attributedTo': `https://${activityPub.domain}/activitypub/users/${name}`,
'attributedTo': `https://${activityPub.hostname}/activitypub/users/${name}`,
'content': text,
'to': 'https://www.w3.org/ns/activitystreams#Public'
}
@ -64,8 +64,8 @@ export async function getActorId (name) {
export function sendAcceptActivity (theBody, name, targetDomain, url) {
as.accept()
.id(`https://${activityPub.domain}/activitypub/users/${name}/status/` + crypto.randomBytes(16).toString('hex'))
.actor(`https://${activityPub.domain}/activitypub/users/${name}`)
.id(`https://${activityPub.hostname}/activitypub/users/${name}/status/` + crypto.randomBytes(16).toString('hex'))
.actor(`https://${activityPub.hostname}/activitypub/users/${name}`)
.object(theBody)
.prettyWrite((err, doc) => {
if (!err) {
@ -79,8 +79,8 @@ export function sendAcceptActivity (theBody, name, targetDomain, url) {
export function sendRejectActivity (theBody, name, targetDomain, url) {
as.reject()
.id(`https://${activityPub.domain}/activitypub/users/${name}/status/` + crypto.randomBytes(16).toString('hex'))
.actor(`https://${activityPub.domain}/activitypub/users/${name}`)
.id(`https://${activityPub.hostname}/activitypub/users/${name}/status/` + crypto.randomBytes(16).toString('hex'))
.actor(`https://${activityPub.hostname}/activitypub/users/${name}`)
.object(theBody)
.prettyWrite((err, doc) => {
if (!err) {

View File

@ -6,21 +6,21 @@ export function createActor (name, pubkey) {
'https://www.w3.org/ns/activitystreams',
'https://w3id.org/security/v1'
],
'id': `https://${activityPub.domain}/activitypub/users/${name}`,
'id': `https://${activityPub.hostname}/activitypub/users/${name}`,
'type': 'Person',
'preferredUsername': `${name}`,
'name': `${name}`,
'following': `https://${activityPub.domain}/activitypub/users/${name}/following`,
'followers': `https://${activityPub.domain}/activitypub/users/${name}/followers`,
'inbox': `https://${activityPub.domain}/activitypub/users/${name}/inbox`,
'outbox': `https://${activityPub.domain}/activitypub/users/${name}/outbox`,
'url': `https://${activityPub.domain}/activitypub/@${name}`,
'following': `https://${activityPub.hostname}/activitypub/users/${name}/following`,
'followers': `https://${activityPub.hostname}/activitypub/users/${name}/followers`,
'inbox': `https://${activityPub.hostname}/activitypub/users/${name}/inbox`,
'outbox': `https://${activityPub.hostname}/activitypub/users/${name}/outbox`,
'url': `https://${activityPub.hostname}/activitypub/@${name}`,
'endpoints': {
'sharedInbox': `https://${activityPub.domain}/activitypub/inbox`
'sharedInbox': `https://${activityPub.hostname}/activitypub/inbox`
},
'publicKey': {
'id': `https://${activityPub.domain}/activitypub/users/${name}#main-key`,
'owner': `https://${activityPub.domain}/activitypub/users/${name}`,
'id': `https://${activityPub.hostname}/activitypub/users/${name}#main-key`,
'owner': `https://${activityPub.hostname}/activitypub/users/${name}`,
'publicKeyPem': pubkey
}
}
@ -28,12 +28,12 @@ export function createActor (name, pubkey) {
export function createWebFinger (name) {
return {
'subject': `acct:${name}@${activityPub.domain}`,
'subject': `acct:${name}@${activityPub.hostname}`,
'links': [
{
'rel': 'self',
'type': 'application/activity+json',
'href': `https://${activityPub.domain}/users/${name}`
'href': `https://${activityPub.hostname}/activitypub/users/${name}`
}
]
}

View File

@ -5,10 +5,10 @@ const debug = require('debug')('ea:utils:collections')
export function createOrderedCollection (name, collectionName) {
return {
'@context': 'https://www.w3.org/ns/activitystreams',
'id': `https://${activityPub.domain}/activitypub/users/${name}/${collectionName}`,
'id': `https://${activityPub.hostname}/activitypub/users/${name}/${collectionName}`,
'summary': `${name}s ${collectionName} collection`,
'type': 'OrderedCollection',
'first': `https://${activityPub.domain}/activitypub/users/${name}/${collectionName}?page=true`,
'first': `https://${activityPub.hostname}/activitypub/users/${name}/${collectionName}?page=true`,
'totalItems': 0
}
}
@ -16,11 +16,11 @@ export function createOrderedCollection (name, collectionName) {
export function createOrderedCollectionPage (name, collectionName) {
return {
'@context': 'https://www.w3.org/ns/activitystreams',
'id': `https://${activityPub.domain}/activitypub/users/${name}/${collectionName}?page=true`,
'id': `https://${activityPub.hostname}/activitypub/users/${name}/${collectionName}?page=true`,
'summary': `${name}s ${collectionName} collection`,
'type': 'OrderedCollectionPage',
'totalItems': 0,
'partOf': `https://${activityPub.domain}/activitypub/users/${name}/${collectionName}`,
'partOf': `https://${activityPub.hostname}/activitypub/users/${name}/${collectionName}`,
'orderedItems': []
}
}

View File

@ -20,7 +20,7 @@ export function extractIdFromActivityId (uri) {
return splitted[splitted.indexOf('status') + 1]
}
export function constructIdFromName (name, fromDomain = activityPub.domain) {
export function constructIdFromName (name, fromDomain = activityPub.hostname) {
return `http://${fromDomain}/activitypub/users/${name}`
}
@ -76,7 +76,7 @@ export function signAndSend (activity, fromName, targetDomain, url) {
'Host': targetDomain,
'Date': date,
'Signature': createSignature({ privateKey,
keyId: `http://${activityPub.domain}/activitypub/users/${fromName}#main-key`,
keyId: `http://${activityPub.hostname}/activitypub/users/${fromName}#main-key`,
url,
headers: {
'Host': targetDomain,