Files: a4af2ae17277b41330b06887b35138e2fab83b39 / plugins / block.js
1852 bytesRaw
1 | var pull = require('pull-stream') |
2 | var valid = require('../lib/validators') |
3 | |
4 | exports.name = 'block' |
5 | exports.version = '1.0.0' |
6 | exports.manifest = { |
7 | isBlocked : 'sync', |
8 | } |
9 | |
10 | exports.init = function (sbot) { |
11 | |
12 | //TODO: move other blocking code in here, |
13 | // i think we'll need a hook system for this. |
14 | |
15 | //if a currently connected peer is blocked, disconnect them immediately. |
16 | pull( |
17 | sbot.friends.createFriendStream({graph: 'flag', live: true}), |
18 | pull.drain(function (blocked) { |
19 | if(sbot.peers[blocked]) { |
20 | sbot.peers[blocked].forEach(function (b) { |
21 | b.close(true, function () {}) |
22 | }) |
23 | } |
24 | }) |
25 | ) |
26 | |
27 | function isBlocked (_opts) { |
28 | var opts |
29 | |
30 | if('string' === typeof _opts) |
31 | opts = { |
32 | source: sbot.id, dest: _opts, graph:'flag' |
33 | } |
34 | else opts = { |
35 | source: _opts.source, dest: _opts.dest, graph: 'flag' |
36 | } |
37 | return sbot.friends.get(opts) |
38 | } |
39 | |
40 | sbot.createHistoryStream.hook(function (fn, args) { |
41 | var opts = args[0], id = this.id |
42 | if(opts.id !== this.id && isBlocked({source: opts.id, dest: this.id})) |
43 | return fn({id: null, sequence: 0}) |
44 | else |
45 | return pull( |
46 | fn.apply(this, args), |
47 | //break off this feed if they suddenly block |
48 | //the recipient. |
49 | pull.take(function (msg) { |
50 | //handle when createHistoryStream is called with keys: true |
51 | if(!msg.content && msg.value.content) |
52 | msg = msg.value |
53 | if(msg.content.type !== 'contact') return true |
54 | return !( |
55 | msg.content.flagged && |
56 | msg.content.contact === id |
57 | ) |
58 | }) |
59 | ) |
60 | }) |
61 | |
62 | sbot.auth.hook(function (fn, args) { |
63 | if(isBlocked(args[0])) args[1](new Error('client is blocked')) |
64 | else return fn.apply(this, args) |
65 | }) |
66 | |
67 | return {isBlocked: valid.sync(isBlocked, 'feedId|isBlockedOpts') } |
68 | |
69 | } |
70 |
Built with git-ssb-web