From 6a6f0b821f7d42f8ea24c3bc6b6ad61ce422e8d4 Mon Sep 17 00:00:00 2001 From: cel Date: Tue, 17 Nov 2020 19:26:00 -0500 Subject: Render patchgraph updates --- README.md | 8 +++--- doc/install-guide.html | 4 +-- lib/render-msg.js | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + 4 files changed, 80 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8921571..58c596d 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,7 @@ Running as a standalone process this way is useful if you can use git-ssb and ss ```sh git clone ssb://%YAg1hicat+2GELjE2QJzDwlAWcx0ML+1sXEdsWwvdt8=.sha256 patchfoo cd patchfoo -ssb-npm install --production --branch %HWJLkLhiapPIaWn2yBOsFIeLNwyrYdSn/Z8cDqiBBi4=.sha256 +ssb-npm install --production --branch %HWJLkLhiapPIaWn2yBOsFIeLNwyrYdSn/Z8cDqiBBi4=.sha256 --branch %eNFBTxGjgxmiQMzNUnHrQqXuBmZ8lwdwlnCbvSG8Rb0=.sha256 npm start ``` @@ -94,7 +94,7 @@ This is the traditional way to install and run patchfoo. cd ~/.ssb/node_modules git clone ssb://%YAg1hicat+2GELjE2QJzDwlAWcx0ML+1sXEdsWwvdt8=.sha256 patchfoo cd patchfoo -ssb-npm install --production --branch %HWJLkLhiapPIaWn2yBOsFIeLNwyrYdSn/Z8cDqiBBi4=.sha256 +ssb-npm install --production --branch %HWJLkLhiapPIaWn2yBOsFIeLNwyrYdSn/Z8cDqiBBi4=.sha256 --branch %eNFBTxGjgxmiQMzNUnHrQqXuBmZ8lwdwlnCbvSG8Rb0=.sha256 node enable-plugin.js ``` @@ -118,7 +118,7 @@ This installs patchfoo globally and gives you a `patchfoo` executable. Note: if you previously installed a patchfoo binary release, [ssb-]npm will not overwrite it. You must manually remove it (e.g. `rm ~/.local/bin/patchfoo`) before installing via ssb-npm. ```sh -ssb-npm install -g patchfoo --branch %HWJLkLhiapPIaWn2yBOsFIeLNwyrYdSn/Z8cDqiBBi4=.sha256 +ssb-npm install -g patchfoo --branch %HWJLkLhiapPIaWn2yBOsFIeLNwyrYdSn/Z8cDqiBBi4=.sha256 --branch %eNFBTxGjgxmiQMzNUnHrQqXuBmZ8lwdwlnCbvSG8Rb0=.sha256 ``` ### as a ssb-server plugin @@ -126,7 +126,7 @@ ssb-npm install -g patchfoo --branch %HWJLkLhiapPIaWn2yBOsFIeLNwyrYdSn/Z8cDqiBBi This does a global ssb-npm install and then symlinks it into place and installs it with ssb-server as a plugin. ```sh -ssb-npm install -g patchfoo --branch %HWJLkLhiapPIaWn2yBOsFIeLNwyrYdSn/Z8cDqiBBi4=.sha256 +ssb-npm install -g patchfoo --branch %HWJLkLhiapPIaWn2yBOsFIeLNwyrYdSn/Z8cDqiBBi4=.sha256 --branch %eNFBTxGjgxmiQMzNUnHrQqXuBmZ8lwdwlnCbvSG8Rb0=.sha256 dir=$(dirname $(realpath $(which patchfoo))) ln -s "$dir" ~/.ssb/node_modules/ node "$dir/enable-plugin.js" diff --git a/doc/install-guide.html b/doc/install-guide.html index c11f6ce..edc963c 100644 --- a/doc/install-guide.html +++ b/doc/install-guide.html @@ -42,7 +42,7 @@ ssb-npm install -g git-ssb@2.8.0 \
 git clone ssb://%YAg1hicat+2GELjE2QJzDwlAWcx0ML+1sXEdsWwvdt8=.sha256 patchfoo
 cd patchfoo
-ssb-npm install --production --branch %HWJLkLhiapPIaWn2yBOsFIeLNwyrYdSn/Z8cDqiBBi4=.sha256
+ssb-npm install --production --branch %HWJLkLhiapPIaWn2yBOsFIeLNwyrYdSn/Z8cDqiBBi4=.sha256 --branch %eNFBTxGjgxmiQMzNUnHrQqXuBmZ8lwdwlnCbvSG8Rb0=.sha256
 npm start
 
@@ -52,7 +52,7 @@ mkdir ~/.ssb/node_modules cd ~/.ssb/node_modules git clone ssb://%YAg1hicat+2GELjE2QJzDwlAWcx0ML+1sXEdsWwvdt8=.sha256 patchfoo cd patchfoo -ssb-npm install --production --branch %HWJLkLhiapPIaWn2yBOsFIeLNwyrYdSn/Z8cDqiBBi4=.sha256 +ssb-npm install --production --branch %HWJLkLhiapPIaWn2yBOsFIeLNwyrYdSn/Z8cDqiBBi4=.sha256 --branch %eNFBTxGjgxmiQMzNUnHrQqXuBmZ8lwdwlnCbvSG8Rb0=.sha256 node enable-plugin.js

Restart ssb-server or Patchwork.

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) +} diff --git a/package.json b/package.json index fc51f63..de3512b 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "pull-split": "^0.2.0", "pull-stream": "^3.5.0", "pull-utf8-decoder": "^1.0.2", + "sparqljs": "^3.2.0", "ssb-client": "^4.7.8", "ssb-git": "^1.1.0", "ssb-markdown": "^6.0.0", -- cgit v1.2.3