git ssb

0+

Piet / ssb-loomio



Tree: fd35bcd5410f1f010361327040a3f0a52b4bddd9

Files: fd35bcd5410f1f010361327040a3f0a52b4bddd9 / poll.js

1462 bytesRaw
1//TODO: pull out schema to seperate file
2//TODO: isPoll should be able to take a poll or a message that contains a poll.
3//TODO: copy Mix's pattern with constructors and inject.
4var Validate = require('is-my-json-valid')
5var { schema, validate } = require('./schema/poll.js')
6var { link } = require('ssb-msg-schemas/util')
7
8function create({text, mentions, recps, channel, pollType, root, branch}){
9 var content = { type: 'poll', text, pollType}
10 if (root) {
11 root = link(root)
12 if (!root)
13 throw new Error('root is not a valid link')
14 content.root = root
15 }
16 if (branch) {
17 if (!root)
18 throw new Error('root is not a valid link')
19 branch = Array.isArray(branch) ? branch.map(link) : link(branch)
20 if (!branch)
21 throw new Error('branch is not a valid link')
22 content.branch = branch
23 }
24 if (mentions && (!Array.isArray(mentions) || mentions.length)) {
25 mentions = links(mentions)
26 if (!mentions || !mentions.length)
27 throw new Error('mentions are not valid links')
28 content.mentions = mentions
29 }
30 if (recps && (!Array.isArray(recps) || recps.length)) {
31 recps = links(recps)
32 if (!recps || !recps.length)
33 throw new Error('recps are not valid links')
34 content.recps = recps
35 }
36 if (channel) {
37 if (typeof channel !== 'string')
38 throw new Error('channel must be a string')
39 content.channel = channel
40 }
41
42 return content
43}
44
45module.exports = {
46 create,
47 schema,
48 validate
49}
50

Built with git-ssb-web