Files: b85045658dceb1b66821e40f21509c2e1937c463 / poll / async / get.js
1075 bytesRaw
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 | } |
45 |
Built with git-ssb-web