git ssb

0+

Piet / ssb-loomio



Tree: a945a5b2f7107dbaf98a64afca527a1ded419a0d

Files: a945a5b2f7107dbaf98a64afca527a1ded419a0d / position / async / buildPosition.js

1957 bytesRaw
1const pull = require('pull-stream')
2const pullAsync = require('pull-async')
3const { heads } = require('ssb-sort')
4const { isMsg } = require('ssb-ref')
5const { isPoll, isPosition, versionStrings: {V1_SCHEMA_VERSION_STRING} } = require('ssb-poll-schema')
6
7module.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 getPoll(typeof poll === 'string' ? poll : poll.key, cb)
17 }),
18 pull.map(build),
19 pull.drain(position => {
20 if (!isPosition(position)) return cb(isPosition.errors)
21
22 cb(null, position)
23 })
24 )
25
26 function build (poll) {
27 // NOTE - poll here is a decorated poll
28 const content = {
29 type: 'position',
30 version: V1_SCHEMA_VERSION_STRING,
31 root: poll.key,
32 details
33 }
34
35 content.branch = heads(poll.positions.concat(poll))
36
37 if (reason) content.reason = reason
38 if (poll.channel) content.channel = poll.channel
39 if (mentions) content.mentions = mentions
40
41 return content
42 }
43 }
44}
45
46// // NOTE mentions can be derived from text,
47// // or we could leave it so you can manually notify people without having to at-mention spam the text
48// if (mentions && (!Array.isArray(mentions) || mentions.length)) {
49// mentions = links(mentions)
50// if (!mentions || !mentions.length) { throw new Error('mentions are not valid links') }
51// content.mentions = mentions
52// }
53
54// // NOTE recps should be derived from the poll I think
55// if (recps && (!Array.isArray(recps) || recps.length)) {
56// recps = links(recps)
57// if (!recps || !recps.length) { throw new Error('recps are not valid links') }
58// content.recps = recps
59// }
60

Built with git-ssb-web