Commit 85899d9c65af617c7746ff686142b1bf2e513f20
standard
mix irving committed on 2/16/2018, 10:56:41 PMParent: cd8ac2d0fc9c1f5e2e287e2a0f67c68c8b2e417d
Files changed
async/fetch.js | changed |
async/get.js | changed |
index.js | changed |
isBlog.js | changed |
lib/getMsgContent.js | changed |
lib/inject.js | changed |
obs/get.js | changed |
schema/blog.js | changed |
struct.js | changed |
sync/isBlog.js | changed |
tests/sync/isBlog.js | changed |
async/fetch.js | ||
---|---|---|
@@ -3,16 +3,16 @@ | ||
3 | 3 … | // TODO take blog or blogKey? |
4 | 4 … | |
5 | 5 … | module.exports = function (server) { |
6 | 6 … | return function fetch (blog, cb) { |
7 | - if (!isBlog(blog)) return cb(`Not a valid blog ${JSON.stringify(blog, null, 2)}`) | |
7 … | + if (!isBlog(blog)) return cb(new Error(`Not a valid blog ${JSON.stringify(blog, null, 2)}`)) | |
8 | 8 … | |
9 | 9 … | server.blobs.want(getBlob(blog), (err, success) => { |
10 | 10 … | if (err) { |
11 | 11 … | if (cb) return cb(err) |
12 | 12 … | else throw err |
13 | 13 … | } |
14 | - | |
14 … | + | |
15 | 15 … | cb(null, success) |
16 | 16 … | }) |
17 | 17 … | } |
18 | 18 … | } |
@@ -22,5 +22,4 @@ | ||
22 | 22 … | if (msg.value.content && msg.value.content.blog) return msg.value.content.blog |
23 | 23 … | // is just body of the message, 'msg content' |
24 | 24 … | else if (msg.blog) return msg.blog |
25 | 25 … | } |
26 | - |
index.js | ||
---|---|---|
@@ -1,14 +1,14 @@ | ||
1 | 1 … | const methods = { |
2 | 2 … | async: { |
3 | 3 … | create: require('./async/fetch'), |
4 | - get: require('./async/get'), | |
4 … | + get: require('./async/get') | |
5 | 5 … | }, |
6 | 6 … | obs: { |
7 | - get: require('./obs/get'), | |
7 … | + get: require('./obs/get') | |
8 | 8 … | }, |
9 | 9 … | sync: { |
10 | - isBlog: require('./sync/isBlog'), | |
10 … | + isBlog: require('./sync/isBlog') | |
11 | 11 … | } |
12 | 12 … | } |
13 | 13 … | |
14 | 14 … | // Note : if you don't like this export pattern, there's no reason we can't add different mappings !! |
@@ -16,6 +16,4 @@ | ||
16 | 16 … | |
17 | 17 … | module.exports = function Blog (server, opts) { |
18 | 18 … | return require('./lib/inject')(server, methods) |
19 | 19 … | } |
20 | - | |
21 | - |
lib/getMsgContent.js | ||
---|---|---|
@@ -2,5 +2,4 @@ | ||
2 | 2 … | if (obj.value && obj.value.content) return obj.value.content |
3 | 3 … | else if (obj.author && obj.timestamp && obj.content) return obj.content |
4 | 4 … | else return obj |
5 | 5 … | } |
6 | - |
lib/inject.js | ||
---|---|---|
@@ -1,11 +1,10 @@ | ||
1 | 1 … | const { map } = require('libnested') |
2 | -const { Value, onceTrue, watch } = require('mutant') | |
2 … | +const { onceTrue, watch } = require('mutant') | |
3 | 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) { |
7 | - | |
8 | 7 … | switch (typeof server) { |
9 | 8 … | case 'object': // just a calssic ssb server |
10 | 9 … | checkPlugins(server) |
11 | 10 … | return map(methods, (fn, path) => fn(server)) |
@@ -13,10 +12,9 @@ | ||
13 | 12 … | case 'function': // hopefully an observeable which will contain an ssb server |
14 | 13 … | return injectObsServer(server, methods) |
15 | 14 … | |
16 | 15 … | default: |
17 | - throw new Error ('scuttle-blog expects an ssb server (or obs that contains one)') | |
18 | - | |
16 … | + throw new Error('scuttle-blog expects an ssb server (or obs that contains one)') | |
19 | 17 … | } |
20 | 18 … | } |
21 | 19 … | |
22 | 20 … | function injectObsServer (server, methods) { |
@@ -34,12 +32,12 @@ | ||
34 | 32 … | |
35 | 33 … | // NOTE - both `obs` and `sync` methods will return observeables |
36 | 34 … | return function () { |
37 | 35 … | // var result = Value({}) |
38 | - var result = Struct({}) | |
36 … | + var result = Struct({}) // WARNING - this shouldn't be copied for other apps, only works with obs.get method here. Probably breaks sync.isBlog | |
39 | 37 … | onceTrue( |
40 | 38 … | server, |
41 | - server => { | |
39 … | + server => { | |
42 | 40 … | var res = fn(server).apply(null, arguments) |
43 | 41 … | watch(res, res => result.set(res)) |
44 | 42 … | } |
45 | 43 … | ) |
@@ -53,7 +51,5 @@ | ||
53 | 51 … | function checkPlugins (server) { |
54 | 52 … | PLUGIN_DEPS.forEach(p => { |
55 | 53 … | if (!server[p]) throw new Error(`scuttle-blog needs a scuttlebot server with the ${p} plugin installed`) |
56 | 54 … | }) |
57 | - | |
58 | 55 … | } |
59 | - |
obs/get.js | ||
---|---|---|
@@ -1,5 +1,4 @@ | ||
1 | -const { Struct, Value, Array: MutantArray } = require('mutant') | |
2 | 1 … | const pull = require('pull-stream') |
3 | 2 … | const fetch = require('../async/fetch') |
4 | 3 … | const isBlog = require('../isBlog') |
5 | 4 … | const Blog = require('../struct') |
@@ -27,6 +26,4 @@ | ||
27 | 26 … | |
28 | 27 … | return obs |
29 | 28 … | } |
30 | 29 … | } |
31 | - | |
32 | - |
schema/blog.js | |||
---|---|---|---|
@@ -4,13 +4,12 @@ | |||
4 | 4 … | $schema: 'http://json-schema.org/schema#', | |
5 | 5 … | type: 'object', | |
6 | 6 … | required: ['type', 'title', 'blog'], | |
7 | 7 … | properties: { | |
8 | - type: {type: 'string', pattern: 'blog'}, | ||
9 | - title: {type: 'string'}, | ||
10 | - blog: {type: 'string', pattern: blobIdRegex}, | ||
11 | - summary: {type: 'string'}, | ||
8 … | + type: {type: 'string', pattern: 'blog'}, | ||
9 … | + title: {type: 'string'}, | ||
10 … | + blog: {type: 'string', pattern: blobIdRegex}, | ||
11 … | + summary: {type: 'string'}, | ||
12 | 12 … | thumbnail: {type: 'string', pattern: blobIdRegex}, | |
13 | - channel: {type: 'string'}, | ||
13 … | + channel: {type: 'string'} | ||
14 | 14 … | } | |
15 | 15 … | } | |
16 | - |
struct.js | ||
---|---|---|
@@ -13,5 +13,4 @@ | ||
13 | 13 … | // sync: Value(false), |
14 | 14 … | errors: MutantArray() |
15 | 15 … | }) |
16 | 16 … | } |
17 | - |
tests/sync/isBlog.js | ||
---|---|---|
@@ -1,12 +1,10 @@ | ||
1 | 1 … | const test = require('tape') |
2 | -const validator = require('is-my-json-valid') | |
3 | 2 … | |
4 | 3 … | const isBlog = require('../../sync/isBlog')() |
5 | 4 … | // or const isBlog = require('../../isBlog') |
6 | 5 … | |
7 | 6 … | test('isBlog / blog schema', t => { |
8 | - | |
9 | 7 … | const notBlog = { |
10 | 8 … | type: 'post', |
11 | 9 … | title: 'An important idea', |
12 | 10 … | blog: '&sfoIYo0kKKGI+TJYnznVDSs3BM/HjMWdCPXirvj9BfE=.sha256' |
@@ -34,44 +32,43 @@ | ||
34 | 32 … | } |
35 | 33 … | t.notOk(isBlog(malformedBlog), 'invalidates malformed blog') |
36 | 34 … | |
37 | 35 … | const actualBlog = { |
38 | - "key": "%GKGFNHKeUVAVPNUapE41skF3ND3Etm76XO7Y7sVeTLI=.sha256", | |
39 | - "value": { | |
40 | - "previous": "%MxC4d1BHqtBiC+mC+BaaZOAZSg41nFGOdXDpJiU+tg4=.sha256", | |
41 | - "author": "@ye+QM09iPcDJD6YvQYjoQc7sLF/IFhmNbEqgdzQo3lQ=.ed25519", | |
42 | - "sequence": 13369, | |
43 | - "timestamp": 1518568914727, | |
44 | - "hash": "sha256", | |
45 | - "content": { | |
46 | - "type": "blog", | |
47 | - "channel": "cooperative", | |
48 | - "title": "Coop-source", | |
49 | - "summary": "a day in the life of open source (and cooperatives) ", | |
50 | - "mentions": [ | |
36 … | + 'key': '%GKGFNHKeUVAVPNUapE41skF3ND3Etm76XO7Y7sVeTLI=.sha256', | |
37 … | + 'value': { | |
38 … | + 'previous': '%MxC4d1BHqtBiC+mC+BaaZOAZSg41nFGOdXDpJiU+tg4=.sha256', | |
39 … | + 'author': '@ye+QM09iPcDJD6YvQYjoQc7sLF/IFhmNbEqgdzQo3lQ=.ed25519', | |
40 … | + 'sequence': 13369, | |
41 … | + 'timestamp': 1518568914727, | |
42 … | + 'hash': 'sha256', | |
43 … | + 'content': { | |
44 … | + 'type': 'blog', | |
45 … | + 'channel': 'cooperative', | |
46 … | + 'title': 'Coop-source', | |
47 … | + 'summary': 'a day in the life of open source (and cooperatives) ', | |
48 … | + 'mentions': [ | |
51 | 49 … | { |
52 | - "link": "&wpKZTZIifFeH0P0LY3Iztgzbj17VlKXIfQNjPnoas1g=.sha256", | |
53 | - "name": "at_mention.png" | |
50 … | + 'link': '&wpKZTZIifFeH0P0LY3Iztgzbj17VlKXIfQNjPnoas1g=.sha256', | |
51 … | + 'name': 'at_mention.png' | |
54 | 52 … | }, |
55 | 53 … | { |
56 | - "link": "&3S7gQpN8ZznmLzstY9AZiWZaZXFG2UaxmeE9SuKU5jk=.sha256", | |
57 | - "name": "protoflip.png" | |
54 … | + 'link': '&3S7gQpN8ZznmLzstY9AZiWZaZXFG2UaxmeE9SuKU5jk=.sha256', | |
55 … | + 'name': 'protoflip.png' | |
58 | 56 … | }, |
59 | 57 … | { |
60 | - "link": "&UgzlelvbBOTj6QGaTqi/PnfkjY8wUNSzUw37/yOxMLw=.sha256", | |
61 | - "name": "protozoa_logo.png" | |
58 … | + 'link': '&UgzlelvbBOTj6QGaTqi/PnfkjY8wUNSzUw37/yOxMLw=.sha256', | |
59 … | + 'name': 'protozoa_logo.png' | |
62 | 60 … | } |
63 | 61 … | ], |
64 | - "thumbnail": "&wpKZTZIifFeH0P0LY3Iztgzbj17VlKXIfQNjPnoas1g=.sha256", | |
65 | - "blog": "&+/mENZOe+RTSn9ZsTFG7Xw0dtv05YDUpIhazYQEj48w=.sha256" | |
62 … | + 'thumbnail': '&wpKZTZIifFeH0P0LY3Iztgzbj17VlKXIfQNjPnoas1g=.sha256', | |
63 … | + 'blog': '&+/mENZOe+RTSn9ZsTFG7Xw0dtv05YDUpIhazYQEj48w=.sha256' | |
66 | 64 … | }, |
67 | - "signature": "1WEhVT42LCdNhuf5ogpcnJfJWbFTUily666pj1XDKS07hjEKGuwZTy/pYRcl/XJJIHMH9MiRkR6FIiOIa7t1DQ==.sig.ed25519" | |
65 … | + 'signature': '1WEhVT42LCdNhuf5ogpcnJfJWbFTUily666pj1XDKS07hjEKGuwZTy/pYRcl/XJJIHMH9MiRkR6FIiOIa7t1DQ==.sig.ed25519' | |
68 | 66 … | }, |
69 | - "timestamp": 1518568915077, | |
70 | - "dest": "#cooperative", | |
71 | - "rts": 1518568914727 | |
67 … | + 'timestamp': 1518568915077, | |
68 … | + 'dest': '#cooperative', | |
69 … | + 'rts': 1518568914727 | |
72 | 70 … | } |
73 | 71 … | t.ok(isBlog(actualBlog), 'validates a blog from ticktack') |
74 | 72 … | |
75 | 73 … | t.end() |
76 | 74 … | }) |
77 | - |
Built with git-ssb-web