git ssb

0+

Piet / ssb-loomio



Commit 9db352a29a6d5bac4344c081e9228bd03aaeb97c

rename chooseOnePollType, finish poll.async.get ?

mix irving committed on 3/7/2018, 2:09:43 AM
Parent: c732390a0378ece4cdd624206b83d7b2c46069e8

Files changed

README.mdchanged
package-lock.jsonchanged
poll/async/get.jschanged
poll/schema/details/chooseOne.jschanged
poll/sync/chooseOne.jschanged
poll/sync/isPoll.jschanged
position/schema/details/chooseOne.jschanged
position/sync/chooseOne.jschanged
test/poll/sync/chooseOne.test.jschanged
test/poll/sync/isPoll.test.jschanged
test/position/sync/isPosition.test.jschanged
types.jschanged
README.mdView
@@ -57,11 +57,17 @@
5757 ### Methods
5858
5959 #### `scuttle.poll.sync.isPoll(msg) => Boolean`
6060
61-takes a msg or the contents of a msg
61+Takes a msg or the contents of a msg
6262
63+You can also check for subtypes of poll e.g.
6364
65+```js
66+isPoll.chooseOne(msg)
67+// => Boolean
68+```
69+
6470 ### `scuttle.poll.async.get(key, cb)`
6571
6672 fetches all the messages associated with a particular poll, and returns a delightfully accessible object:
6773
package-lock.jsonView
@@ -307,13 +307,8 @@
307307 "version": "4.5.0",
308308 "resolved": "https://registry.npmjs.org/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz",
309309 "integrity": "sha1-4j8/nE+Pvd6HJSnBBxhXoIblzO8="
310310 },
311- "lodash.merge": {
312- "version": "4.6.1",
313- "resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.1.tgz",
314- "integrity": "sha512-AOYza4+Hf5z1/0Hztxpm2/xiPZgi/cjMqdnKTUWTBSKchJlxXXuUSxCCl8rJlf4g6yww/j6mA8nC8Hw/EZWxKQ=="
315- },
316311 "minimatch": {
317312 "version": "3.0.4",
318313 "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz",
319314 "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==",
poll/async/get.jsView
@@ -1,7 +1,10 @@
11 const pull = require('pull-stream')
22 const sort = require('ssb-sort')
33 const isPoll = require('../../isPoll')
4+const isPosition = require('../../isPosition')
5+const { ERROR_POSITION_TYPE } = require('../../types')
6+const results = require('../../position/sync/chooseOneResults')
47
58 module.exports = function (server) {
69 return function get (key, cb) {
710 server.get(key, (err, value) => {
@@ -32,22 +35,40 @@
3235 })
3336 }
3437 }
3538
36-function decoratedPoll (poll, msgs) {
37- msgs = sort(msgs)
38- // TODO add missingContext warnings
39+function decoratedPoll (rawPoll, msgs) {
40+ const { title, body } = rawPoll.value.content
3941
40- const pollType = poll.pollDetails.type
41- // const pollType = poll.details.type
42+ const poll = Object.assign({}, rawPoll, {
43+ title,
44+ body,
4245
43- const positions = msgs.filter(isPoll[pollType])
46+ positions: [],
47+ results: {},
48+ errors: []
49+ })
4450
45- return Object.assign({}, poll, {
46- title: poll.content.title,
47- body: poll.content.body,
51+ // filter position message into 'positions' and 'errors'
52+ // TODO schema checks right position shape, but needs to add e.g. acceptible position ranges based on poll
53+ // TODO add missingContext warnings
54+ msgs = sort(msgs)
55+ const type = poll.value.content.pollDetails.type
56+ msgs.forEach(msg => {
57+ if (isPosition[type](msg)) {
58+ poll.positions.push(msg)
59+ return
60+ }
4861
49- // positions: msgs.filter,
50- results: {}, // TODO add reduction of positions
51- errors: {}
62+ if (isPosition(msg)) {
63+ poll.errors.push({
64+ type: ERROR_POSITION_TYPE,
65+ message: `Position responses need to be off the ${type} type for this poll`,
66+ position: msg
67+ })
68+ }
5269 })
70+
71+ poll.results = results(poll.positions)
72+
73+ return poll
5374 }
poll/schema/details/chooseOne.jsView
@@ -1,6 +1,6 @@
1-const { chooseOnePollType } = require('../../../types')
2-const typeStringPattern = `^${chooseOnePollType}$`
1+const { CHOOSE_ONE } = require('../../../types')
2+const typeStringPattern = `^${CHOOSE_ONE}$`
33
44 var schema = {
55 type: 'object',
66 required: ['type', 'choices'],
poll/sync/chooseOne.jsView
@@ -1,12 +1,12 @@
11 const Poll = require('./poll')
2-const { chooseOnePollType } = require('../../types')
2+const { CHOOSE_ONE } = require('../../types')
33
44 function ChooseOne ({ choices, title, closesAt, body, channel, recps, mentions }) {
55 return Poll({
66 pollDetails: {
77 choices,
8- type: chooseOnePollType
8+ type: CHOOSE_ONE
99 },
1010 title,
1111 closesAt,
1212 body,
poll/sync/isPoll.jsView
@@ -2,15 +2,23 @@
22 const schema = require('../schema/poll')
33 const validator = Validator(schema, {verbose: true})
44 const getMsgContent = require('../../lib/getMsgContent')
55
6+const isChooseOnePoll = require('./isChooseOnePoll')()
7+
68 // server is not used here. Closure pattern is just for consistency of use with other functions.
79 module.exports = function (server) {
8- return function isPoll (obj) {
10+ function isPoll (obj) {
911 const result = validator(getMsgContent(obj))
1012
1113 // exposes error messages provided by is-my-json-valid
1214 isPoll.errors = validator.errors
1315
1416 return result
1517 }
18+
19+ isPoll.chooseOne = isChooseOnePoll
20+ // isPoll.dot = isDotPoll
21+ // isPoll.score = isScorePoll
22+
23+ return isPoll
1624 }
position/schema/details/chooseOne.jsView
@@ -1,6 +1,6 @@
1-const { chooseOnePositionType } = require('../../../types')
2-const typeStringPattern = `^${chooseOnePositionType}$`
1+const { CHOOSE_ONE } = require('../../../types')
2+const typeStringPattern = `^${CHOOSE_ONE}$`
33
44 var schema = {
55 type: 'object',
66 required: ['type', 'choice'],
position/sync/chooseOne.jsView
@@ -1,12 +1,12 @@
11 const Postion = require('./position')
2-const { chooseOnePositionType } = require('../../types')
2+const { CHOOSE_ONE } = require('../../types')
33
44 function ChooseOne ({ poll, choice, reason, channel, recps, mentions }) {
55 return Postion({
66 poll,
77 positionDetails: {
8- type: chooseOnePositionType,
8+ type: CHOOSE_ONE,
99 choice
1010 },
1111 reason,
1212 channel,
test/poll/sync/chooseOne.test.jsView
@@ -11,9 +11,10 @@
1111 choices: [1, 2, 'three'],
1212 title: 'how many food',
1313 closesAt: Date.now()
1414 })
15- t.true(isPoll(validPoll), 'simple')
15+ t.true(isPoll(validPoll), 'simple (passes isPoll)')
16+ t.true(isPoll.chooseOne(validPoll), 'simple (passes isPoll.chooseOne)')
1617
1718 var fullPollMsg = {
1819 key: '%somekey',
1920 value: {
test/poll/sync/isPoll.test.jsView
@@ -1,7 +1,8 @@
11 const test = require('tape')
22 const ChooseOne = require('../../../poll/sync/chooseOne')
33 const isPoll = require('../../../isPoll')
4+const { CHOOSE_ONE } = require('../../../types')
45
56 // this is for testing the attributes that are required for all polls
67
78 test('Poll - common requirements', function (t) {
@@ -28,9 +29,9 @@
2829
2930 var fullyFeatured = {
3031 type: 'poll',
3132 pollDetails: {
32- type: 'chooseOnePoll', // TODO change to chooseOne?
33+ type: CHOOSE_ONE,
3334 title: 'how many dogs?',
3435 choices: [1, 2, 3]
3536 },
3637 title: 'how many food',
test/position/sync/isPosition.test.jsView
@@ -1,6 +1,7 @@
11 const test = require('tape')
22 const isPosition = require('../../../isPosition')
3+const { CHOOSE_ONE } = require('../../../types')
34
45 // this is for testing the attributes that are required for all polls
56 test('position - common requirements', function (t) {
67 var missingDetails = {
@@ -13,9 +14,9 @@
1314
1415 var missingPoll = {
1516 type: 'position',
1617 positionDetails: {
17- type: 'chooseOnePosition',
18+ type: CHOOSE_ONE,
1819 choice: 0
1920 }
2021 }
2122 t.false(isPosition(missingPoll), 'needs poll')
types.jsView
@@ -1,7 +1,6 @@
11 module.exports = {
2- chooseOnePollType: 'chooseOnePoll',
3- chooseOnePositionType: 'chooseOnePosition'
2+ CHOOSE_ONE: 'chooseOne'
43 }
54
65 // Question: do these need to be different, could we just have 'chooseOne',
76 // because we already have:
@@ -17,8 +16,8 @@
1716 // I wonder if a position could just be ... but this would mean we would have to have the parent poll, and then based o nthe type there, run a isOneChoicePostion validator... which I wthink could be good? Maybe I don't understandthe complexity of the other types of positions.
1817 // { type: 'position', poll, choice }
1918 //
2019 //
21-// I not that there's also a validation thing where
20+// I not that there's also a validation thing where
2221 // { type: 'position', poll, choice: 9000 }
2322 //
2423 // is probably invalid, and that the choice should be an integer bound by the parent poll choices. Can probably think about that case later, but making out reducer we're gonna have to put some filtering logic somewhere. Could programatically generate a position schema based on a poll.... _WOAH there EASY MIX_

Built with git-ssb-web