Commit d022ea7ba118fcb624a4afe57ec5b1be07d30313
all the changes needed to actually use this module
mix irving committed on 3/8/2018, 10:31:48 AMParent: 4360b02f347aa3bea29d3e95dda409fe21bc3c45
Files changed
index.js | changed |
lib/inject.js | changed |
methods.js | changed |
package-lock.json | changed |
package.json | changed |
poll/async/chooseOne.js | added |
poll/schema/poll.js | changed |
test/poll/async/get.test.js | changed |
test/poll/sync/chooseOne.test.js | changed |
test/poll/sync/isPoll.test.js | changed |
index.js | ||
---|---|---|
@@ -1,9 +1,12 @@ | ||
1 | 1 … | const raw = require('./methods') |
2 | 2 … | const PLUGIN_DEPS = ['backlinks'] |
3 | 3 … | |
4 | 4 … | const niceMappings = { |
5 | - isPoll: raw.poll.sync.isPoll | |
5 … | + isPoll: raw.poll.sync.isPoll, | |
6 … | + // publish: { | |
7 … | + // chooseOne: raw.poll.async.chooseOne | |
8 … | + // } | |
6 | 9 … | } |
7 | 10 … | // by following this pattern you can write your own API |
8 | 11 … | |
9 | 12 … | module.exports = function (server, opts) { |
lib/inject.js | ||
---|---|---|
@@ -1,7 +1,7 @@ | ||
1 | 1 … | const { map } = require('libnested') |
2 | -const { onceTrue, watch } = require('mutant') | |
3 | -const Struct = require('../struct') | |
2 … | +const { onceTrue, watch, Value } = require('mutant') | |
3 … | +// const Struct = require('../struct') | |
4 | 4 … | |
5 | 5 … | // auto-inject the ssb-server to all methods to reduce repitition |
6 | 6 … | module.exports = function inject (server, methods, pluginDeps = []) { |
7 | 7 … | switch (typeof server) { |
@@ -16,13 +16,14 @@ | ||
16 | 16 … | throw new Error('scuttle-blog expects an ssb server (or obs that contains one)') |
17 | 17 … | } |
18 | 18 … | } |
19 | 19 … | |
20 | -function injectObsServer (server, methods, pluginDeps) { | |
20 … | +//TODO fix this plugins Deps thing D: | |
21 … | +function injectObsServer (server, methods, pluginDeps = []) { | |
21 | 22 … | onceTrue(server, server => checkPlugins(server, pluginDeps)) |
22 | 23 … | |
23 | 24 … | return map(methods, (fn, path) => { |
24 | - if (path[0] === 'async') { | |
25 … | + if (path.includes('async')) { | |
25 | 26 … | return function () { |
26 | 27 … | onceTrue( |
27 | 28 … | server, |
28 | 29 … | server => fn(server).apply(null, arguments) |
@@ -31,10 +32,10 @@ | ||
31 | 32 … | } |
32 | 33 … | |
33 | 34 … | // NOTE - both `obs` and `sync` methods will return observeables |
34 | 35 … | return function () { |
35 | - // var result = Value({}) | |
36 | - var result = Struct({}) // WARNING - this shouldn't be copied for other apps, only works with obs.get method here. Probably breaks sync.isBlog | |
36 … | + var result = Value({}) | |
37 … | + // var result = Struct({}) // WARNING - this shouldn't be copied for other apps, only works with obs.get method here. Probably breaks sync.isBlog | |
37 | 38 … | onceTrue( |
38 | 39 … | server, |
39 | 40 … | server => { |
40 | 41 … | var res = fn(server).apply(null, arguments) |
@@ -45,9 +46,9 @@ | ||
45 | 46 … | } |
46 | 47 … | }) |
47 | 48 … | } |
48 | 49 … | |
49 | -function checkPlugins (server) { | |
50 | - PLUGIN_DEPS.forEach(p => { | |
50 … | +function checkPlugins (server, pluginDeps) { | |
51 … | + pluginDeps.forEach(p => { | |
51 | 52 … | if (!server[p]) throw new Error(`scuttle-blog needs a scuttlebot server with the ${p} plugin installed`) |
52 | 53 … | }) |
53 | 54 … | } |
methods.js | ||
---|---|---|
@@ -2,9 +2,10 @@ | ||
2 | 2 … | |
3 | 3 … | module.exports = { |
4 | 4 … | poll: { |
5 | 5 … | async: { |
6 | - // publishPoll: require('./async/publishPoll') | |
6 … | + chooseOne: require('./poll/async/chooseOne'), | |
7 … | + get: require('./poll/async/get') | |
7 | 8 … | }, |
8 | 9 … | sync: { |
9 | 10 … | isPoll: require('./poll/sync/isPoll') |
10 | 11 … | // Poll: // this is not exported - doesn't follow the inject pattern atm |
package-lock.json | ||
---|---|---|
The diff is too large to show. Use a local git client to view these changes. Old file size: 174269 bytes New file size: 174788 bytes |
package.json | ||
---|---|---|
@@ -33,8 +33,9 @@ | ||
33 | 33 … | "is-my-json-valid": "^2.17.1", |
34 | 34 … | "isarray": "^2.0.4", |
35 | 35 … | "libnested": "^1.2.1", |
36 | 36 … | "lodash.clonedeep": "^4.5.0", |
37 … | + "mutant": "^3.22.1", | |
37 | 38 … | "pull-stream": "^3.6.2", |
38 | 39 … | "ssb-msg-schemas": "^6.3.0", |
39 | 40 … | "ssb-ref": "^2.9.0" |
40 | 41 … | } |
poll/async/chooseOne.js | ||
---|---|---|
@@ -1,0 +1,13 @@ | ||
1 … | +const ChooseOne = require('../sync/chooseOne') | |
2 … | +const isPoll = require('../../isPoll') | |
3 … | + | |
4 … | +module.exports = function (server) { | |
5 … | + return function publishChooseOne (opts, cb) { | |
6 … | + const poll = ChooseOne(opts) | |
7 … | + console.log('poll', poll) | |
8 … | + console.log('isChooseOnePoll', require('../../isPoll')) | |
9 … | + if (!isPoll.chooseOne(poll)) return cb(isPoll.chooseOne.errors) | |
10 … | + | |
11 … | + server.publish(poll, cb) | |
12 … | + } | |
13 … | +} |
poll/schema/poll.js | ||
---|---|---|
@@ -28,9 +28,9 @@ | ||
28 | 28 … | // { $ref: '#/definitions/pollDetails/meeting'}, |
29 | 29 … | ] |
30 | 30 … | }, |
31 | 31 … | title: { type: 'string' }, |
32 | - closesAt: { type: 'integer' }, | |
32 … | + closesAt: { type: 'string', format: 'date-time' }, | |
33 | 33 … | body: { type: 'string' }, |
34 | 34 … | mentions: { $ref: '#/definitions/mentions/any' }, |
35 | 35 … | recps: { $ref: '#/definitions/recps' } |
36 | 36 … | }, |
test/poll/async/get.test.js | ||
---|---|---|
@@ -67,7 +67,8 @@ | ||
67 | 67 … | } |
68 | 68 … | |
69 | 69 … | function nDaysTime (n) { |
70 | 70 … | var d = new Date() |
71 | - return d.setDate(d.getDate() + n) | |
72 | - // returns integer! | |
71 … | + d.setDate(d.getDate() + n) | |
72 … | + | |
73 … | + return d.toISOString() | |
73 | 74 … | } |
test/poll/sync/chooseOne.test.js | ||
---|---|---|
@@ -9,9 +9,9 @@ | ||
9 | 9 … | |
10 | 10 … | var validPoll = ChooseOne({ |
11 | 11 … | title: 'how many food', |
12 | 12 … | choices: [1, 2, 'three'], |
13 | - closesAt: Date.now() | |
13 … | + closesAt: new Date().toISOString() | |
14 | 14 … | }) |
15 | 15 … | t.true(isPoll(validPoll), 'simple (passes isPoll)') |
16 | 16 … | t.true(isPoll.chooseOne(validPoll), 'simple (passes isPoll.chooseOne)') |
17 | 17 … |
test/poll/sync/isPoll.test.js | ||
---|---|---|
@@ -7,9 +7,9 @@ | ||
7 | 7 … | |
8 | 8 … | test('Poll - common requirements', function (t) { |
9 | 9 … | var missingTitle = ChooseOne({ |
10 | 10 … | choices: [1, 2, 'three'], |
11 | - closesAt: Date.now() | |
11 … | + closesAt: new Date().toISOString() | |
12 | 12 … | }) |
13 | 13 … | t.false(isPoll(missingTitle), 'needs title') |
14 | 14 … | |
15 | 15 … | var missingClosesAt = ChooseOne({ |
@@ -17,13 +17,20 @@ | ||
17 | 17 … | title: 'how many food' |
18 | 18 … | }) |
19 | 19 … | t.false(isPoll(missingClosesAt), 'needs closes at') |
20 | 20 … | |
21 … | + var malformedClosesAt = ChooseOne({ | |
22 … | + choices: [1, 2, 'three'], | |
23 … | + title: 'how many food', | |
24 … | + closesAt: 'tomorrow' | |
25 … | + }) | |
26 … | + t.false(isPoll(missingClosesAt), 'needs ISOString closes at') | |
27 … | + | |
21 | 28 … | var missingDetails = { |
22 | 29 … | type: 'poll', |
23 | 30 … | pollDetails: undefined, |
24 | 31 … | title: 'how many food', |
25 | - closesAt: Date.now() | |
32 … | + closesAt: new Date().toISOString() | |
26 | 33 … | } |
27 | 34 … | t.false(isPoll(missingDetails), 'needs details') |
28 | 35 … | t.true(isPoll.errors, 'failing validations have errors') |
29 | 36 … | |
@@ -44,9 +51,9 @@ | ||
44 | 51 … | recps: [ |
45 | 52 … | '@ye+QM09iPcDJD6YvQYjoQc7sLF/IFhmNbEqgdzQo3lQ=.ed25519', |
46 | 53 … | {name: 'mix', link: '@ye+QM09iPcDJD6YvQYjoQc7sLF/IFhmNbEqgdzQo3lQ=.ed25519'} |
47 | 54 … | ], |
48 | - closesAt: Date.now() | |
55 … | + closesAt: new Date().toISOString() | |
49 | 56 … | } |
50 | 57 … | t.true(isPoll(fullyFeatured), 'fully featured') |
51 | 58 … | |
52 | 59 … | t.end() |
Built with git-ssb-web