Files: 9e5a426e0a4859825b9743e57b563a7845310484 / position / async / buildPosition.js
2050 bytesRaw
1 | const pull = require('pull-stream') |
2 | const pullAsync = require('pull-async') |
3 | const { heads } = require('ssb-sort') |
4 | const { isMsg } = require('ssb-ref') |
5 | const { isPoll, isPosition, versionStrings: {V1_SCHEMA_VERSION_STRING} } = require('ssb-poll-schema') |
6 | |
7 | module.exports = function (server) { |
8 | return function Position ({ poll, details, reason, mentions }, cb) { |
9 | if (!isPoll(poll) && !isMsg(poll)) return cb(new Error('Position factory expects a valid poll')) |
10 | |
11 | // NOTE - getPoll has to be required here to avoid circular deps |
12 | const getPoll = require('../../poll/async/get')(server) |
13 | |
14 | pull( |
15 | pullAsync(cb => { |
16 | if (poll.decorated) cb(null, poll) |
17 | else getPoll(typeof poll === 'string' ? poll : poll.key, cb) |
18 | }), |
19 | pull.map(build), |
20 | pull.drain(position => { |
21 | if (!isPosition(position)) return cb(isPosition.errors) |
22 | |
23 | cb(null, position) |
24 | }) |
25 | ) |
26 | |
27 | function build (poll) { |
28 | // NOTE - poll here is a decorated poll |
29 | const content = { |
30 | type: 'position', |
31 | version: V1_SCHEMA_VERSION_STRING, |
32 | root: poll.key, |
33 | details |
34 | } |
35 | |
36 | content.branch = heads(poll.positions || []) |
37 | |
38 | if (reason) content.reason = reason |
39 | if (poll.channel) content.channel = poll.channel |
40 | if (mentions) content.mentions = mentions |
41 | |
42 | return content |
43 | } |
44 | } |
45 | } |
46 | |
47 | // // NOTE mentions can be derived from text, |
48 | // // or we could leave it so you can manually notify people without having to at-mention spam the text |
49 | // if (mentions && (!Array.isArray(mentions) || mentions.length)) { |
50 | // mentions = links(mentions) |
51 | // if (!mentions || !mentions.length) { throw new Error('mentions are not valid links') } |
52 | // content.mentions = mentions |
53 | // } |
54 | |
55 | // // NOTE recps should be derived from the poll I think |
56 | // if (recps && (!Array.isArray(recps) || recps.length)) { |
57 | // recps = links(recps) |
58 | // if (!recps || !recps.length) { throw new Error('recps are not valid links') } |
59 | // content.recps = recps |
60 | // } |
61 |
Built with git-ssb-web