Files: 862bba47be153d8955724bcb5e6b29a18d6ca29b / plugins / block.js
1814 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 | |
17 | var g = {} |
18 | |
19 | sbot.friends.post(function (_g) { |
20 | g = _g |
21 | }) |
22 | |
23 | function isBlocked (_opts) { |
24 | var opts |
25 | if(!g) return //only possible briefly at startup |
26 | if('string' === typeof _opts) |
27 | return g[sbot.id] ? g[sbot.id][_opts] === false : false |
28 | return g[_opts.source] ? g[_opts.source][_opts.dest] === false : false |
29 | } |
30 | |
31 | sbot.createHistoryStream.hook(function (fn, args) { |
32 | var opts = args[0], id = this.id |
33 | //reminder: this.id is the remote caller. |
34 | if(opts.id !== this.id && isBlocked({source: opts.id, dest: id})) { |
35 | return function (abort, cb) { |
36 | //just give them the cold shoulder |
37 | } |
38 | // return fn({id: null, sequence: 0}) |
39 | } else |
40 | return pull( |
41 | fn.apply(this, args), |
42 | //break off this feed if they suddenly block |
43 | //the recipient. |
44 | pull.take(function (msg) { |
45 | //handle when createHistoryStream is called with keys: true |
46 | if(!msg.content && msg.value.content) |
47 | msg = msg.value |
48 | if(msg.content.type !== 'contact') return true |
49 | return !( |
50 | (msg.content.flagged || msg.content.blocking) && |
51 | msg.content.contact === id |
52 | ) |
53 | }) |
54 | ) |
55 | }) |
56 | |
57 | sbot.auth.hook(function (fn, args) { |
58 | if(isBlocked(args[0])) args[1](new Error('client is blocked')) |
59 | else return fn.apply(this, args) |
60 | }) |
61 | |
62 | return {isBlocked: valid.sync(isBlocked, 'feedId|isBlockedOpts') } |
63 | |
64 | } |
65 | |
66 |
Built with git-ssb-web