Commit b85045658dceb1b66821e40f21509c2e1937c463
poll.async.get - scaffolding
mix irving committed on 3/5/2018, 11:41:27 PMParent: a3426ae4bc96b37cfb2795e1acb2b31d2b3da6ea
Files changed
README.md | changed |
index.js | changed |
lib/inject.js | changed |
poll/async/get.js | added |
README.md | ||
---|---|---|
@@ -40,8 +40,14 @@ | ||
40 | 40 | Note - `server` can also be an observeable which resolves to a scuttlebot instance |
41 | 41 | (this is more experimental, it will turn your sync functions into obs functions) |
42 | 42 | |
43 | 43 | |
44 | +## Dependencies | |
45 | + | |
46 | +Requires a scuttlebutt server with the following plugins installed: | |
47 | + - `ssb-backlinks` | |
48 | + | |
49 | + | |
44 | 50 | ## API |
45 | 51 | |
46 | 52 | ```js |
47 | 53 | var Scuttle = require('scuttle-poll') |
@@ -53,10 +59,32 @@ | ||
53 | 59 | #### `scuttle.poll.sync.isPoll(msg) => Boolean` |
54 | 60 | |
55 | 61 | takes a msg or the contents of a msg |
56 | 62 | |
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 | + | |
57 | 83 | #### `scuttle.poll.async.publishChooseOne(opts, cb)` |
58 | 84 | |
85 | +// NOT BUILT YET | |
86 | + | |
59 | 87 | where `opts` is an object of form: |
60 | 88 | ```js |
61 | 89 | { |
62 | 90 | title: String, // required |
index.js | ||
---|---|---|
@@ -1,5 +1,6 @@ | ||
1 | 1 | const raw = require('./methods') |
2 | +const PLUGIN_DEPS = ['backlinks'] | |
2 | 3 | |
3 | 4 | const niceMappings = { |
4 | 5 | isPoll: raw.poll.sync.isPoll |
5 | 6 | } |
@@ -7,6 +8,6 @@ | ||
7 | 8 | |
8 | 9 | module.exports = function (server, opts) { |
9 | 10 | const methods = Object.assign({}, raw, niceMappings) |
10 | 11 | |
11 | - return require('./lib/inject')(server, methods) | |
12 | + return require('./lib/inject')(server, methods, PLUGIN_DEPS) | |
12 | 13 | } |
lib/inject.js | ||
---|---|---|
@@ -2,12 +2,12 @@ | ||
2 | 2 | const { onceTrue, watch } = require('mutant') |
3 | 3 | const Struct = require('../struct') |
4 | 4 | |
5 | 5 | // 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 = []) { | |
7 | 7 | switch (typeof server) { |
8 | 8 | case 'object': // just a calssic ssb server |
9 | - checkPlugins(server) | |
9 | + checkPlugins(server, pluginDeps) | |
10 | 10 | return map(methods, (fn, path) => fn(server)) |
11 | 11 | |
12 | 12 | case 'function': // hopefully an observeable which will contain an ssb server |
13 | 13 | return injectObsServer(server, methods) |
@@ -16,10 +16,10 @@ | ||
16 | 16 | throw new Error('scuttle-blog expects an ssb server (or obs that contains one)') |
17 | 17 | } |
18 | 18 | } |
19 | 19 | |
20 | -function injectObsServer (server, methods) { | |
21 | - onceTrue(server, checkPlugins) | |
20 | +function injectObsServer (server, methods, pluginDeps) { | |
21 | + onceTrue(server, server => checkPlugins(server, pluginDeps)) | |
22 | 22 | |
23 | 23 | return map(methods, (fn, path) => { |
24 | 24 | if (path[0] === 'async') { |
25 | 25 | return function () { |
@@ -45,10 +45,8 @@ | ||
45 | 45 | } |
46 | 46 | }) |
47 | 47 | } |
48 | 48 | |
49 | -const PLUGIN_DEPS = ['blobs'] | |
50 | - | |
51 | 49 | function checkPlugins (server) { |
52 | 50 | PLUGIN_DEPS.forEach(p => { |
53 | 51 | if (!server[p]) throw new Error(`scuttle-blog needs a scuttlebot server with the ${p} plugin installed`) |
54 | 52 | }) |
poll/async/get.js | ||
---|---|---|
@@ -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