Files: 6b40ec5cf68a4d01c36667b30a3801ff6d313c4e / poll.js
4458 bytesRaw
1 | var Validate = require('is-my-json-valid') |
2 | const { msgIdRegex, feedIdRegex, blobIdRegex } = require('ssb-ref') |
3 | |
4 | function create(text, root, branch, mentions, recps, channel, pollType){ |
5 | var content = { type: 'poll', text, pollType} |
6 | if (root) { |
7 | root = link(root) |
8 | if (!root) |
9 | throw new Error('root is not a valid link') |
10 | content.root = root |
11 | } |
12 | if (branch) { |
13 | if (!root) |
14 | throw new Error('root is not a valid link') |
15 | branch = Array.isArray(branch) ? branch.map(link) : link(branch) |
16 | if (!branch) |
17 | throw new Error('branch is not a valid link') |
18 | content.branch = branch |
19 | } |
20 | if (mentions && (!Array.isArray(mentions) || mentions.length)) { |
21 | mentions = links(mentions) |
22 | if (!mentions || !mentions.length) |
23 | throw new Error('mentions are not valid links') |
24 | content.mentions = mentions |
25 | } |
26 | if (recps && (!Array.isArray(recps) || recps.length)) { |
27 | recps = links(recps) |
28 | if (!recps || !recps.length) |
29 | throw new Error('recps are not valid links') |
30 | content.recps = recps |
31 | } |
32 | if (channel) { |
33 | if (typeof channel !== 'string') |
34 | throw new Error('channel must be a string') |
35 | content.channel = channel |
36 | } |
37 | |
38 | return content |
39 | } |
40 | |
41 | const schema = { |
42 | $schema: 'http://json-schema.org/schema#', |
43 | type: 'object', |
44 | required: ['type', 'pollType'], |
45 | properties: { |
46 | type: { |
47 | type: 'string', |
48 | pattern: '^poll$' |
49 | }, |
50 | pollType: { |
51 | oneOf: [ |
52 | { $ref: '#/definitions/pollTypes/dot'}, |
53 | { $ref: '#/definitions/pollTypes/proposal'}, |
54 | { $ref: '#/definitions/pollTypes/score'}, |
55 | //{ $ref: '#/definitions/pollTypes/rsvp'}, |
56 | //{ $ref: '#/definitions/pollTypes/meeting'}, |
57 | ] |
58 | }, |
59 | text: { type: 'string' }, |
60 | mentions: { |
61 | oneOf: [ |
62 | { type: 'null' }, |
63 | { |
64 | type: 'array', |
65 | items: { |
66 | oneOf: [ |
67 | { $ref: '#/definitions/mentions/message' }, |
68 | { $ref: '#/definitions/mentions/feed' }, |
69 | { $ref: '#/definitions/mentions/blob' } |
70 | ] |
71 | } |
72 | } |
73 | ] |
74 | }, |
75 | recps: { |
76 | oneOf: [ |
77 | { type: 'null' }, |
78 | { |
79 | type: 'array', |
80 | items: { |
81 | oneOf: [ |
82 | { $ref: '#/definitions/feedId' }, |
83 | { $ref: '#/definitions/mentions/feed' }, |
84 | ] |
85 | } |
86 | } |
87 | ] |
88 | } |
89 | }, |
90 | definitions: { |
91 | |
92 | messageId: { |
93 | type: 'string', |
94 | pattern: msgIdRegex |
95 | }, |
96 | feedId: { |
97 | type: 'string', |
98 | pattern: feedIdRegex |
99 | }, |
100 | blobId: { |
101 | type: 'string', |
102 | pattern: blobIdRegex |
103 | }, |
104 | pollTypes: { |
105 | type: 'object', |
106 | dot: { |
107 | type: 'object', |
108 | required: ['type', 'maxStanceScore', 'choices'], |
109 | properties: { |
110 | type: { |
111 | type: 'string', |
112 | pattern: '^dot$' |
113 | }, |
114 | maxStanceScore: { |
115 | type: 'integer', |
116 | minimum: 0 |
117 | }, |
118 | maxChoiceScore: { |
119 | type: 'integer', |
120 | minimum: 0 |
121 | }, |
122 | choices: { |
123 | type: 'array', |
124 | } |
125 | } |
126 | }, |
127 | proposal: { |
128 | type: 'object', |
129 | required: ['type', 'proposal'], |
130 | properties: { |
131 | type: { |
132 | type: 'string', |
133 | pattern: '^proposal$' |
134 | }, |
135 | proposal: { |
136 | type: 'string', |
137 | } |
138 | } |
139 | }, |
140 | score: { |
141 | type: 'object', |
142 | required: ['type', 'maxChoiceScore', 'choices'], |
143 | properties: { |
144 | type: { |
145 | type: 'string', |
146 | pattern: '^score$' |
147 | }, |
148 | maxChoiceScore: { |
149 | type: 'integer', |
150 | minimum: 2 |
151 | }, |
152 | choices: { |
153 | type: 'array', |
154 | } |
155 | } |
156 | }, |
157 | }, |
158 | mentions: { |
159 | message: { |
160 | type: 'object', |
161 | required: ['link'], |
162 | properties: { |
163 | link: { $ref: '#/definitions/messageId'} |
164 | } |
165 | }, |
166 | feed: { |
167 | type: 'object', |
168 | required: ['link', 'name'], |
169 | properties: { |
170 | link: { $ref: '#/definitions/feedId'}, |
171 | name: { type: 'string' } |
172 | } |
173 | }, |
174 | blob: { |
175 | type: 'object', |
176 | required: ['link', 'name'], |
177 | properties: { |
178 | link: { $ref: '#/definitions/blobId'}, |
179 | name: { type: 'string' } |
180 | } |
181 | } |
182 | } |
183 | }, |
184 | } |
185 | |
186 | const validate = Validate(schema, { verbose: true }) |
187 | |
188 | module.exports = { |
189 | create, |
190 | schema, |
191 | validate |
192 | } |
193 |
Built with git-ssb-web