Files: 8818456f68cdf8d623639cc4bc69b81a61f90869 / poll.js
1564 bytesRaw
1 | var Validate = require('is-my-json-valid') |
2 | var { schema, validate } = require('./schema/poll.js') |
3 | var { schema: chooseOneSchema, create: chooseOneCreate, isValidTypeString: isValidChooseOneTypeString } = require('./schema/pollDetails/chooseOne.js') |
4 | var { link } = require('ssb-msg-schemas/util') |
5 | |
6 | function create ({text, mentions, recps, channel, pollOptions, root, branch}) { |
7 | var content = { type: 'poll', text, pollOptions} |
8 | if (root) { |
9 | root = link(root) |
10 | if (!root) { throw new Error('root is not a valid link') } |
11 | content.root = root |
12 | } |
13 | if (branch) { |
14 | if (!root) { throw new Error('root is not a valid link') } |
15 | branch = Array.isArray(branch) ? branch.map(link) : link(branch) |
16 | if (!branch) { throw new Error('branch is not a valid link') } |
17 | content.branch = branch |
18 | } |
19 | if (mentions && (!Array.isArray(mentions) || mentions.length)) { |
20 | mentions = links(mentions) |
21 | if (!mentions || !mentions.length) { throw new Error('mentions are not valid links') } |
22 | content.mentions = mentions |
23 | } |
24 | if (recps && (!Array.isArray(recps) || recps.length)) { |
25 | recps = links(recps) |
26 | if (!recps || !recps.length) { throw new Error('recps are not valid links') } |
27 | content.recps = recps |
28 | } |
29 | if (channel) { |
30 | if (typeof channel !== 'string') { throw new Error('channel must be a string') } |
31 | content.channel = channel |
32 | } |
33 | |
34 | if (isValidChooseOneTypeString(pollOptions.type)) { |
35 | content.pollOptions = chooseOneCreate({choices: pollOptions.choices}) |
36 | } |
37 | |
38 | return content |
39 | } |
40 | |
41 | module.exports = { |
42 | create, |
43 | schema, |
44 | validate |
45 | } |
46 |
Built with git-ssb-web