git ssb

0+

Dominic / ssb-feed



Tree: 704f26822aea683181bd1a94a30e08df76f5d8b3

Files: 704f26822aea683181bd1a94a30e08df76f5d8b3 / index.js

1517 bytesRaw
1var cont = require('cont')
2var util = require('./util')
3var Queue = require('./queue')
4
5var ssbKeys = require('ssb-keys')
6
7function isFunction (f) {
8 return 'function' === typeof f
9}
10
11function isObject (o) {
12 return (
13 o && 'object' === typeof o
14 && !Buffer.isBuffer(o)
15 && !Array.isArray(o)
16 )
17}
18
19module.exports = function (ssb, keys, opts) {
20 opts = opts || {}
21 if(!ssb.add)
22 throw new Error('*must* install feeds on ssb instance')
23
24 var sign_cap = util.toBuffer(opts.caps && opts.caps.sign)
25
26 var queue = Queue(function (msg, prev, cb) {
27 if(prev && !opts.remote) next(prev)
28 else
29 ssb.getLatest(keys.id, function (err, prev) {
30 if(err) cb(err)
31 else next(prev)
32 })
33
34 function next (prev) {
35 ssb.add(
36 util.create(
37 keys, null, msg,
38 prev && prev.value,
39 prev && prev.key,
40 sign_cap
41 ),
42 cb
43 )
44 }
45 })
46
47 var publish =
48 cont(function (type, message, cb) {
49 // argument variations
50 if (isFunction(message)) { cb = message; message = type } // add(msgObj, cbFn)
51 else if (isObject(message)) { message.type = type } // add(typeStr, mgObj, cbFn)
52 else { message = { type: type, value: message } } // add(typeStr, msgStr, cbFn)
53
54 var err = util.isInvalidContent(message)
55 if(err) return cb(err)
56
57 queue(message, cb)
58
59 return this
60 })
61
62 return {
63 id: keys.id,
64 keys: keys,
65 add: publish,
66 publish: publish
67 }
68}
69
70

Built with git-ssb-web