Files: e6a09b701a2e1d5b6260e3c9f4b075fc3cfe7a2a / poll.js
1556 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/pollTypes/chooseOne.js') |
4 | var { link } = require('ssb-msg-schemas/util') |
5 | |
6 | function create({text, mentions, recps, channel, pollType, root, branch}){ |
7 | var content = { type: 'poll', text, pollType} |
8 | if (root) { |
9 | root = link(root) |
10 | if (!root) |
11 | throw new Error('root is not a valid link') |
12 | content.root = root |
13 | } |
14 | if (branch) { |
15 | if (!root) |
16 | throw new Error('root is not a valid link') |
17 | branch = Array.isArray(branch) ? branch.map(link) : link(branch) |
18 | if (!branch) |
19 | throw new Error('branch is not a valid link') |
20 | content.branch = branch |
21 | } |
22 | if (mentions && (!Array.isArray(mentions) || mentions.length)) { |
23 | mentions = links(mentions) |
24 | if (!mentions || !mentions.length) |
25 | throw new Error('mentions are not valid links') |
26 | content.mentions = mentions |
27 | } |
28 | if (recps && (!Array.isArray(recps) || recps.length)) { |
29 | recps = links(recps) |
30 | if (!recps || !recps.length) |
31 | throw new Error('recps are not valid links') |
32 | content.recps = recps |
33 | } |
34 | if (channel) { |
35 | if (typeof channel !== 'string') |
36 | throw new Error('channel must be a string') |
37 | content.channel = channel |
38 | } |
39 | |
40 | if(isValidChooseOneTypeString(pollType.type)){ |
41 | content.pollType = chooseOneCreate({choices: pollType.choices}) |
42 | } |
43 | |
44 | return content |
45 | } |
46 | |
47 | module.exports = { |
48 | create, |
49 | schema, |
50 | validate |
51 | } |
52 |
Built with git-ssb-web