Compare commits

...

2 commits

Author SHA1 Message Date
Yarmo Mackenbach
0cdd42579f
feat: use nodeinfo for fediverse instance info 2023-09-14 16:18:27 +02:00
Yarmo Mackenbach
3df1bc7106
feat: better support both Person and Note data 2023-09-14 15:37:26 +02:00
2 changed files with 143 additions and 3 deletions

View file

@ -270,7 +270,7 @@ export class Claim {
const def = _data[claimData.about.id] const def = _data[claimData.about.id]
if (def.functions?.postprocess) { if (def.functions?.postprocess) {
try { try {
({ claimData, proofData } = def.functions.postprocess(claimData, proofData)) ({ claimData, proofData } = await def.functions.postprocess(claimData, proofData, opts))
} catch (_) {} } catch (_) {}
} }
} else { } else {

View file

@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import * as E from '../enums.js' import * as E from '../enums.js'
import { activitypub } from '../fetcher/index.js'
import { ServiceProvider } from '../serviceProvider.js' import { ServiceProvider } from '../serviceProvider.js'
export const reURI = /^https:\/\/(.*)\/?/ export const reURI = /^https:\/\/(.*)\/?/
@ -76,12 +77,151 @@ export function processURI (uri) {
} }
export const functions = { export const functions = {
postprocess: (claimData, proofData) => { postprocess: async (/** @type {ServiceProvider} */ claimData, proofData, opts) => {
claimData.profile.display = `@${proofData.result.preferredUsername}@${new URL(proofData.result.url).hostname}` switch (proofData.result.type) {
case 'Note': {
claimData.profile.uri = proofData.result.attributedTo
const personData = await activitypub.fn({ url: proofData.result.attributedTo }, opts)
claimData.profile.display = `@${personData.preferredUsername}@${new URL(proofData.result.url).hostname}`
break
}
case 'Person':
claimData.profile.display = `@${proofData.result.preferredUsername}@${new URL(proofData.result.url).hostname}`
break
default:
break
}
// Attempt to fetch and process the instance's NodeInfo data
const nodeinfo = await _processNodeinfo(new URL(proofData.result.url).hostname)
if (nodeinfo) {
claimData.about.name = nodeinfo.software.name
claimData.about.id = nodeinfo.software.name
claimData.about.homepage = nodeinfo.software.homepage
}
return { claimData, proofData } return { claimData, proofData }
} }
} }
const _processNodeinfo = async (/** @type {string} */ domain) => {
const nodeinfoRef = await fetch(`http://${domain}/.well-known/nodeinfo`)
.then(res => {
if (res.status !== 200) {
throw new Error('HTTP Status was not 200')
}
return res.json()
})
.catch(_ => {
return null
})
if (!nodeinfoRef) return null
// NodeInfo version 2.1
{
const nodeinfo = nodeinfoRef.links.find(x => { return x.rel === 'http://nodeinfo.diaspora.software/ns/schema/2.1' })
if (nodeinfo) {
return await fetch(nodeinfo.href)
.then(res => {
if (res.status !== 200) {
throw new Error('HTTP Status was not 200')
}
return res.json()
})
.then(res => {
return {
software: {
name: res.software.name,
version: res.software.version,
homepage: res.software.homepage || 'https://activitypub.rocks'
}
}
})
.catch(_ => {
return null
})
}
}
// NodeInfo version 2.0
{
const nodeinfo = nodeinfoRef.links.find(x => { return x.rel === 'http://nodeinfo.diaspora.software/ns/schema/2.0' })
if (nodeinfo) {
return await fetch(nodeinfo.href)
.then(res => {
if (res.status !== 200) {
throw new Error('HTTP Status was not 200')
}
return res.json()
})
.then(res => {
return {
software: {
name: res.software.name,
version: res.software.version,
homepage: 'https://activitypub.rocks'
}
}
})
.catch(_ => {
return null
})
}
}
// NodeInfo version 1.1
{
const nodeinfo = nodeinfoRef.links.find(x => { return x.rel === 'http://nodeinfo.diaspora.software/ns/schema/1.1' })
if (nodeinfo) {
return await fetch(nodeinfo.href)
.then(res => {
if (res.status !== 200) {
throw new Error('HTTP Status was not 200')
}
return res.json()
})
.then(res => {
return {
software: {
name: res.software.name,
version: res.software.version,
homepage: 'https://activitypub.rocks'
}
}
})
.catch(_ => {
return null
})
}
}
// NodeInfo version 1.0
{
const nodeinfo = nodeinfoRef.links.find(x => { return x.rel === 'http://nodeinfo.diaspora.software/ns/schema/1.0' })
if (nodeinfo) {
return await fetch(nodeinfo.href)
.then(res => {
if (res.status !== 200) {
throw new Error('HTTP Status was not 200')
}
return res.json()
})
.then(res => {
return {
software: {
name: res.software.name,
version: res.software.version,
homepage: 'https://activitypub.rocks'
}
}
})
.catch(_ => {
return null
})
}
}
}
export const tests = [ export const tests = [
{ {
uri: 'https://domain.org', uri: 'https://domain.org',