From 6a6f0b821f7d42f8ea24c3bc6b6ad61ce422e8d4 Mon Sep 17 00:00:00 2001 From: cel Date: Tue, 17 Nov 2020 19:26:00 -0500 Subject: Render patchgraph updates --- lib/render-msg.js | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) (limited to 'lib/render-msg.js') 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) +} -- cgit v1.2.3