Skip to content

Commit 3ac3fed

Browse files
Use triple in profile
1 parent ff6fe9a commit 3ac3fed

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

lib/payment-pointer-discovery.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
const express = require('express')
66
const { promisify } = require('util')
77
const fs = require('fs')
8+
const rdf = require('rdflib')
89

910
module.exports = paymentPointerDiscovery
1011

11-
const SETTING_FILE_PATH = '/settings/paymentPointer.json'
12+
const PROFILE_PATH = '/profile/card'
1213

1314
/**
1415
* Returns a set of routes to deal with server payment pointer discovery
@@ -35,9 +36,9 @@ function paymentPointerDocument () {
3536
return async (req, res) => {
3637
try {
3738
const ldp = req.app.locals.ldp
38-
const url = ldp.resourceMapper.resolveUrl(req.hostname, SETTING_FILE_PATH)
39-
const contentType = 'application/json'
40-
const createIfNotExists = true
39+
const url = ldp.resourceMapper.resolveUrl(req.hostname, PROFILE_PATH)
40+
const contentType = 'text/turtle'
41+
const createIfNotExists = false
4142
const { path } = await ldp.resourceMapper.mapUrlToFile({ url, contentType, createIfNotExists })
4243
let body
4344
try {
@@ -46,20 +47,31 @@ function paymentPointerDocument () {
4647
} catch (e) {
4748
if (e.message.startsWith('ENOENT: no such file or directory,')) {
4849
res.json({
49-
error: `Please create ${SETTING_FILE_PATH} on your pod`
50+
error: `Please create ${PROFILE_PATH} on your pod`
5051
})
5152
}
5253
}
53-
let obj
54+
const webid = rdf.Namespace(`${url}#`)('me')
55+
const pp = rdf.Namespace('http://paymentpointers.org/ns#')('PaymentPointer')
56+
let paymentPointer
5457
try {
55-
// Read the file from disk
56-
obj = JSON.parse(body)
58+
const graph = rdf.graph()
59+
// Parse the file as Turtle
60+
rdf.parse(body, graph, url, contentType)
61+
paymentPointer = graph.any(webid, pp)
5762
} catch (e) {
63+
console.error(e)
5864
res.json({
59-
error: `Please make sure ${SETTING_FILE_PATH} contains valid JSON`
65+
error: `Please make sure ${PROFILE_PATH} contains valid Turtle`
6066
})
6167
}
62-
res.json(obj)
68+
if (paymentPointer === null) {
69+
res.json({ fail: 'Add triple', subject: `<${webid.value}>`, predicate: `<${pp.value}>`, object: '$alice.example' })
70+
}
71+
if (paymentPointer.value.startsWith('$')) {
72+
paymentPointer.value = `https://${paymentPointer.value.substring(1)}`
73+
}
74+
res.redirect(paymentPointer.value)
6375
} catch (e) {
6476
res.json({ fail: e.message })
6577
}

0 commit comments

Comments
 (0)