git ssb

0+

Piet / ssb-loomio



Commit b85045658dceb1b66821e40f21509c2e1937c463

poll.async.get - scaffolding

mix irving committed on 3/5/2018, 11:41:27 PM
Parent: a3426ae4bc96b37cfb2795e1acb2b31d2b3da6ea

Files changed

README.mdchanged
index.jschanged
lib/inject.jschanged
poll/async/get.jsadded
README.mdView
@@ -40,8 +40,14 @@
4040 Note - `server` can also be an observeable which resolves to a scuttlebot instance
4141 (this is more experimental, it will turn your sync functions into obs functions)
4242
4343
44+## Dependencies
45+
46+Requires a scuttlebutt server with the following plugins installed:
47+ - `ssb-backlinks`
48+
49+
4450 ## API
4551
4652 ```js
4753 var Scuttle = require('scuttle-poll')
@@ -53,10 +59,32 @@
5359 #### `scuttle.poll.sync.isPoll(msg) => Boolean`
5460
5561 takes a msg or the contents of a msg
5662
63+
64+### `scuttle.poll.async.get(key, cb)`
65+
66+fetches all the messages associated with a particular poll, and returns a delightfully accessible object:
67+
68+```js
69+{
70+ key: MessageKey,
71+ value: MessageValue,
72+ author: FeedId,
73+ title: String,
74+ body: (String|Null),
75+
76+ positions: Array,
77+ results: Object,
78+ errors: Object
79+}
80+```
81+
82+
5783 #### `scuttle.poll.async.publishChooseOne(opts, cb)`
5884
85+// NOT BUILT YET
86+
5987 where `opts` is an object of form:
6088 ```js
6189 {
6290 title: String, // required
index.jsView
@@ -1,5 +1,6 @@
11 const raw = require('./methods')
2+const PLUGIN_DEPS = ['backlinks']
23
34 const niceMappings = {
45 isPoll: raw.poll.sync.isPoll
56 }
@@ -7,6 +8,6 @@
78
89 module.exports = function (server, opts) {
910 const methods = Object.assign({}, raw, niceMappings)
1011
11- return require('./lib/inject')(server, methods)
12+ return require('./lib/inject')(server, methods, PLUGIN_DEPS)
1213 }
lib/inject.jsView
@@ -2,12 +2,12 @@
22 const { onceTrue, watch } = require('mutant')
33 const Struct = require('../struct')
44
55 // auto-inject the ssb-server to all methods to reduce repitition
6-module.exports = function inject (server, methods) {
6+module.exports = function inject (server, methods, pluginDeps = []) {
77 switch (typeof server) {
88 case 'object': // just a calssic ssb server
9- checkPlugins(server)
9+ checkPlugins(server, pluginDeps)
1010 return map(methods, (fn, path) => fn(server))
1111
1212 case 'function': // hopefully an observeable which will contain an ssb server
1313 return injectObsServer(server, methods)
@@ -16,10 +16,10 @@
1616 throw new Error('scuttle-blog expects an ssb server (or obs that contains one)')
1717 }
1818 }
1919
20-function injectObsServer (server, methods) {
21- onceTrue(server, checkPlugins)
20+function injectObsServer (server, methods, pluginDeps) {
21+ onceTrue(server, server => checkPlugins(server, pluginDeps))
2222
2323 return map(methods, (fn, path) => {
2424 if (path[0] === 'async') {
2525 return function () {
@@ -45,10 +45,8 @@
4545 }
4646 })
4747 }
4848
49-const PLUGIN_DEPS = ['blobs']
50-
5149 function checkPlugins (server) {
5250 PLUGIN_DEPS.forEach(p => {
5351 if (!server[p]) throw new Error(`scuttle-blog needs a scuttlebot server with the ${p} plugin installed`)
5452 })
poll/async/get.jsView
@@ -1,0 +1,44 @@
1+const pull = require('pull-stream')
2+const sort = require('ssb-sort')
3+const isPoll = require('../../isPoll')
4+
5+module.exports = function (server) {
6+ return function get (key, cb) {
7+ server.get(key, (err, poll) => {
8+ if (err) return cb(err)
9+ if (!isPoll(poll)) return cb(new Error('scuttle-poll could not fetch, key provided was not a valid poll key'))
10+
11+ pull(
12+ createBacklinkStream(key),
13+ pull.collect(msgs => {
14+ msgs = sort(msgs)
15+ // TODO add missingContext warnings
16+
17+ Object.assign({}, poll, {
18+ key,
19+ value: poll,
20+ title: poll.content.title,
21+ body: poll.content.body,
22+
23+ // positions: msgs.filter,
24+ results: {}, // TODO add reduction of positions
25+ errors: {}
26+ })
27+ })
28+ )
29+ })
30+ }
31+
32+ function createBacklinkStream (key) {
33+ var filterQuery = {
34+ $filter: {
35+ dest: key
36+ }
37+ }
38+
39+ return server.backlinks.read({
40+ query: [filterQuery],
41+ index: 'DTA' // use asserted timestamps
42+ })
43+ }
44+}

Built with git-ssb-web