Files: 9062bc4c96853910b299d0c0fe37db5b50dc7baa / contact / async.js
1332 bytesRaw
1 | var nest = require('depnest') |
2 | var pull = require('pull-stream') |
3 | var ref = require('ssb-ref') |
4 | |
5 | exports.needs = nest({ |
6 | 'sbot.pull.query': 'first', |
7 | 'sbot.async.publish': 'first' |
8 | }) |
9 | |
10 | exports.gives = nest({ |
11 | 'contact.async': ['follow', 'unfollow', 'followerOf'] |
12 | }) |
13 | |
14 | exports.create = function (api) { |
15 | return nest({ |
16 | 'contact.async': {follow, unfollow, followerOf} |
17 | }) |
18 | |
19 | function followerOf (source, dest, cb) { |
20 | pull( |
21 | api.sbot.pull.query({query: [ |
22 | makeQuery(source, dest), |
23 | {$map: ['value', 'content', 'following']} |
24 | ]}), |
25 | pull.collect(function (err, ary) { |
26 | if (err) return cb(err) |
27 | else cb(null, ary.pop()) // will be true, or undefined/false |
28 | }) |
29 | ) |
30 | } |
31 | |
32 | function follow (id, cb) { |
33 | if (!ref.isFeed(id)) throw new Error('a feed id must be specified') |
34 | api.sbot.async.publish({ |
35 | type: 'contact', |
36 | contact: id, |
37 | following: true |
38 | }, cb) |
39 | } |
40 | |
41 | function unfollow (id, cb) { |
42 | if (!ref.isFeed(id)) throw new Error('a feed id must be specified') |
43 | api.sbot.async.publish({ |
44 | type: 'contact', |
45 | contact: id, |
46 | following: false |
47 | }, cb) |
48 | } |
49 | } |
50 | |
51 | function makeQuery (a, b) { |
52 | return {'$filter': { |
53 | value: { |
54 | author: a, |
55 | content: { |
56 | type: 'contact', |
57 | contact: b, |
58 | following: true |
59 | } |
60 | } |
61 | }} |
62 | } |
63 |
Built with git-ssb-web