mirror of
https://github.com/IT4Change/Ocelot-Social.git
synced 2025-12-13 07:45:56 +00:00
Refactor: Use URL.host instead of hostname
This way, we don't have to manually add the port number. cc @Mastercuber Documentation: https://nodejs.org/api/url.html#url_urlobject_host
This commit is contained in:
parent
a55b253784
commit
ebb1c283b7
@ -22,9 +22,8 @@ let activityPub = null
|
||||
export { activityPub }
|
||||
|
||||
export default class ActivityPub {
|
||||
constructor (hostname, port, uri) {
|
||||
this.hostname = hostname
|
||||
this.port = port
|
||||
constructor (host, uri) {
|
||||
this.host = host
|
||||
this.dataSource = new NitroDataSource(uri)
|
||||
this.collections = new Collections(this.dataSource)
|
||||
}
|
||||
@ -33,7 +32,7 @@ export default class ActivityPub {
|
||||
if (!activityPub) {
|
||||
dotenv.config()
|
||||
const url = new URL(process.env.GRAPHQL_URI)
|
||||
activityPub = new ActivityPub(url.host || 'localhost', url.port || 4000, url.origin)
|
||||
activityPub = new ActivityPub(url.host || 'localhost:4000', url.origin)
|
||||
|
||||
// integrate into running graphql express server
|
||||
server.express.set('ap', activityPub)
|
||||
@ -59,7 +58,7 @@ export default class ActivityPub {
|
||||
}
|
||||
}, async (err, response, toActorObject) => {
|
||||
if (err) return reject(err)
|
||||
debug(`name = ${toActorName}@${this.hostname}`)
|
||||
debug(`name = ${toActorName}@${this.host}`)
|
||||
// save shared inbox
|
||||
toActorObject = JSON.parse(toActorObject)
|
||||
await this.dataSource.addSharedInboxEndpoint(toActorObject.endpoints.sharedInbox)
|
||||
@ -184,7 +183,7 @@ export default class ActivityPub {
|
||||
}
|
||||
|
||||
generateStatusId (slug) {
|
||||
return `https://${this.hostname}/activitypub/users/${slug}/status/${uuid()}`
|
||||
return `https://${this.host}/activitypub/users/${slug}/status/${uuid()}`
|
||||
}
|
||||
|
||||
async sendActivity (activity) {
|
||||
|
||||
@ -11,14 +11,14 @@ export function createNoteObject (text, name, id, published) {
|
||||
|
||||
return {
|
||||
'@context': 'https://www.w3.org/ns/activitystreams',
|
||||
'id': `https://${activityPub.hostname}/activitypub/users/${name}/status/${createUuid}`,
|
||||
'id': `https://${activityPub.host}/activitypub/users/${name}/status/${createUuid}`,
|
||||
'type': 'Create',
|
||||
'actor': `https://${activityPub.hostname}/activitypub/users/${name}`,
|
||||
'actor': `https://${activityPub.host}/activitypub/users/${name}`,
|
||||
'object': {
|
||||
'id': `https://${activityPub.hostname}/activitypub/users/${name}/status/${id}`,
|
||||
'id': `https://${activityPub.host}/activitypub/users/${name}/status/${id}`,
|
||||
'type': 'Note',
|
||||
'published': published,
|
||||
'attributedTo': `https://${activityPub.hostname}/activitypub/users/${name}`,
|
||||
'attributedTo': `https://${activityPub.host}/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.hostname}/activitypub/users/${name}/status/` + crypto.randomBytes(16).toString('hex'))
|
||||
.actor(`https://${activityPub.hostname}/activitypub/users/${name}`)
|
||||
.id(`https://${activityPub.host}/activitypub/users/${name}/status/` + crypto.randomBytes(16).toString('hex'))
|
||||
.actor(`https://${activityPub.host}/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.hostname}/activitypub/users/${name}/status/` + crypto.randomBytes(16).toString('hex'))
|
||||
.actor(`https://${activityPub.hostname}/activitypub/users/${name}`)
|
||||
.id(`https://${activityPub.host}/activitypub/users/${name}/status/` + crypto.randomBytes(16).toString('hex'))
|
||||
.actor(`https://${activityPub.host}/activitypub/users/${name}`)
|
||||
.object(theBody)
|
||||
.prettyWrite((err, doc) => {
|
||||
if (!err) {
|
||||
|
||||
@ -6,21 +6,21 @@ export function createActor (name, pubkey) {
|
||||
'https://www.w3.org/ns/activitystreams',
|
||||
'https://w3id.org/security/v1'
|
||||
],
|
||||
'id': `https://${activityPub.hostname}/activitypub/users/${name}`,
|
||||
'id': `https://${activityPub.host}/activitypub/users/${name}`,
|
||||
'type': 'Person',
|
||||
'preferredUsername': `${name}`,
|
||||
'name': `${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}`,
|
||||
'following': `https://${activityPub.host}/activitypub/users/${name}/following`,
|
||||
'followers': `https://${activityPub.host}/activitypub/users/${name}/followers`,
|
||||
'inbox': `https://${activityPub.host}/activitypub/users/${name}/inbox`,
|
||||
'outbox': `https://${activityPub.host}/activitypub/users/${name}/outbox`,
|
||||
'url': `https://${activityPub.host}/activitypub/@${name}`,
|
||||
'endpoints': {
|
||||
'sharedInbox': `https://${activityPub.hostname}/activitypub/inbox`
|
||||
'sharedInbox': `https://${activityPub.host}/activitypub/inbox`
|
||||
},
|
||||
'publicKey': {
|
||||
'id': `https://${activityPub.hostname}/activitypub/users/${name}#main-key`,
|
||||
'owner': `https://${activityPub.hostname}/activitypub/users/${name}`,
|
||||
'id': `https://${activityPub.host}/activitypub/users/${name}#main-key`,
|
||||
'owner': `https://${activityPub.host}/activitypub/users/${name}`,
|
||||
'publicKeyPem': pubkey
|
||||
}
|
||||
}
|
||||
@ -28,12 +28,12 @@ export function createActor (name, pubkey) {
|
||||
|
||||
export function createWebFinger (name) {
|
||||
return {
|
||||
'subject': `acct:${name}@${activityPub.hostname}`,
|
||||
'subject': `acct:${name}@${activityPub.host}`,
|
||||
'links': [
|
||||
{
|
||||
'rel': 'self',
|
||||
'type': 'application/activity+json',
|
||||
'href': `https://${activityPub.hostname}/activitypub/users/${name}`
|
||||
'href': `https://${activityPub.host}/activitypub/users/${name}`
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@ -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.hostname}/activitypub/users/${name}/${collectionName}`,
|
||||
'id': `https://${activityPub.host}/activitypub/users/${name}/${collectionName}`,
|
||||
'summary': `${name}s ${collectionName} collection`,
|
||||
'type': 'OrderedCollection',
|
||||
'first': `https://${activityPub.hostname}/activitypub/users/${name}/${collectionName}?page=true`,
|
||||
'first': `https://${activityPub.host}/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.hostname}/activitypub/users/${name}/${collectionName}?page=true`,
|
||||
'id': `https://${activityPub.host}/activitypub/users/${name}/${collectionName}?page=true`,
|
||||
'summary': `${name}s ${collectionName} collection`,
|
||||
'type': 'OrderedCollectionPage',
|
||||
'totalItems': 0,
|
||||
'partOf': `https://${activityPub.hostname}/activitypub/users/${name}/${collectionName}`,
|
||||
'partOf': `https://${activityPub.host}/activitypub/users/${name}/${collectionName}`,
|
||||
'orderedItems': []
|
||||
}
|
||||
}
|
||||
|
||||
@ -20,12 +20,12 @@ export function extractIdFromActivityId (uri) {
|
||||
return splitted[splitted.indexOf('status') + 1]
|
||||
}
|
||||
|
||||
export function constructIdFromName (name, fromDomain = activityPub.hostname) {
|
||||
export function constructIdFromName (name, fromDomain = activityPub.host) {
|
||||
return `http://${fromDomain}/activitypub/users/${name}`
|
||||
}
|
||||
|
||||
export function extractDomainFromUrl (url) {
|
||||
return new URL(url).hostname
|
||||
return new URL(url).host
|
||||
}
|
||||
|
||||
export function throwErrorIfApolloErrorOccurred (result) {
|
||||
@ -76,7 +76,7 @@ export function signAndSend (activity, fromName, targetDomain, url) {
|
||||
'Host': targetDomain,
|
||||
'Date': date,
|
||||
'Signature': createSignature({ privateKey,
|
||||
keyId: `http://${activityPub.hostname}/activitypub/users/${fromName}#main-key`,
|
||||
keyId: `http://${activityPub.host}/activitypub/users/${fromName}#main-key`,
|
||||
url,
|
||||
headers: {
|
||||
'Host': targetDomain,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user