diff options
author | cel <cel@lOUVT+Phkvai9a/cCS/RKo+S9hnPAQdVixms/7ldpPA=.ed25519> | 2020-11-17 19:26:00 -0500 |
---|---|---|
committer | cel <cel@lOUVT+Phkvai9a/cCS/RKo+S9hnPAQdVixms/7ldpPA=.ed25519> | 2020-11-27 10:27:52 -0500 |
commit | 6a6f0b821f7d42f8ea24c3bc6b6ad61ce422e8d4 (patch) | |
tree | 3c46a6ba65802a164857007ff737172f728953f8 /lib | |
parent | 1bf5278d3088c9b366c19937130c030b41c7cee0 (diff) | |
download | patchfoo-6a6f0b821f7d42f8ea24c3bc6b6ad61ce422e8d4.tar.gz patchfoo-6a6f0b821f7d42f8ea24c3bc6b6ad61ce422e8d4.zip |
Render patchgraph updates
Diffstat (limited to 'lib')
-rw-r--r-- | lib/render-msg.js | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/lib/render-msg.js b/lib/render-msg.js index 250719d..3957694 100644 --- a/lib/render-msg.js +++ b/lib/render-msg.js @@ -5,6 +5,7 @@ var u = require('./util') var mdInline = require('./markdown-inline') var ssbKeys = require('ssb-keys') var qs = require('querystring') +var sparql = require('sparqljs') module.exports = RenderMsg @@ -392,6 +393,7 @@ RenderMsg.prototype.message = function (cb) { case 'cesium_geoPoint.lat': case 'cesium_geoPoint.lon': return this.astroport(cb) case 'delete': return this.delete(cb) + case 'patchgraph-update': return this.patchgraphUpdate(cb) default: return this.object(cb) } } @@ -2521,3 +2523,74 @@ RenderMsg.prototype.delete = function (cb) { ], cb) }) } + +RenderMsg.prototype.rdfLiteral = function (term) { + if (term.datatype && term.datatype.termType === 'NamedNode' && + term.datatype === 'http://www.w3.org/2001/XMLSchema#string') { + return h('div', {innerHTML: term.value}) + } + return this.valueTable(term.value, 2, function () {}) +} + +RenderMsg.prototype.rdfTerm = function (term) { + switch (term.termType) { + case 'Literal': return this.rdfLiteral(term) + case 'NamedNode': return this.valueTable(term.value, 2, function () {}) + default: return this.valueTable(term, 2, function () {}) + } +} + +RenderMsg.prototype.patchgraphUpdate = function (cb) { + var self = this + var query + try { + var parser = new sparql.Parser() + query = parser.parse(self.c.value) + } catch(e) { + return self.wrap(h('div', [ + h('div', 'patchgraph update:'), + h('pre', u.toString(self.c.value)) + ]), cb) + } + + if (query && query.type === 'update' && Array.isArray(query.updates) + && query.updates.every(function (update) { + return update && update.updateType === 'insert' + && Array.isArray(update.insert) + && update.insert.every(function (insert) { + return insert.type === 'bgp' + && Array.isArray(insert.triples) + }) + })) { + var triples = [] + query.updates.forEach(function (update) { + update.insert.forEach(function (insert) { + insert.triples.forEach(function (triple) { + triples.push(triple) + }) + }) + }) + return self.wrap(h('div', [ + h('div', 'patchgraph insert:'), + h('table.ssb-object', [ + h('tr', [ + h('th', 'subject'), + h('th', 'predicate'), + h('th', 'object') + ]), + triples.map(function (triple) { + return h('tr', [ + h('td', self.rdfTerm(triple.subject)), + h('td', self.rdfTerm(triple.predicate)), + h('td', self.rdfTerm(triple.object)), + ]) + }) + ]), + ]), cb) + } + + self.wrap(h('div', [ + h('div', 'patchgraph update:'), + self.valueTable(query, 1, function () {}) + ]), cb) +} |