Files: a732bc2a78bd9ce1b2bff429591d2b85f9a86306 / contact / async.js
1077 bytesRaw
1 | var nest = require('depnest') |
2 | var onceTrue = require('mutant/once-true') |
3 | var resolve = require('mutant/resolve') |
4 | var ref = require('ssb-ref') |
5 | |
6 | exports.needs = nest({ |
7 | 'contact.obs.following': 'first', |
8 | 'sbot.async.publish': 'first' |
9 | }) |
10 | |
11 | exports.gives = nest({ |
12 | 'contact.async': ['follow', 'unfollow', 'followerOf'] |
13 | }) |
14 | |
15 | exports.create = function (api) { |
16 | return nest({ |
17 | 'contact.async': {follow, unfollow, followerOf} |
18 | }) |
19 | |
20 | function followerOf (source, dest, cb) { |
21 | var following = api.contact.obs.following(source) |
22 | onceTrue(following.sync, () => { |
23 | var value = resolve(following) |
24 | cb(null, value && value.has(dest)) |
25 | }) |
26 | } |
27 | |
28 | function follow (id, cb) { |
29 | if (!ref.isFeed(id)) throw new Error('a feed id must be specified') |
30 | api.sbot.async.publish({ |
31 | type: 'contact', |
32 | contact: id, |
33 | following: true |
34 | }, cb) |
35 | } |
36 | |
37 | function unfollow (id, cb) { |
38 | if (!ref.isFeed(id)) throw new Error('a feed id must be specified') |
39 | api.sbot.async.publish({ |
40 | type: 'contact', |
41 | contact: id, |
42 | following: false |
43 | }, cb) |
44 | } |
45 | } |
46 |
Built with git-ssb-web