git ssb

4+

Dominic / scuttlebot



Tree: 765a31b854726f3c3f4e39689b4e4d4b8f86bcf8

Files: 765a31b854726f3c3f4e39689b4e4d4b8f86bcf8 / plugins / block.js

1679 bytesRaw
1var pull = require('pull-stream')
2var valid = require('../lib/validators')
3
4exports.name = 'block'
5exports.version = '1.0.0'
6exports.manifest = {
7 isBlocked : 'sync',
8}
9
10exports.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 if(opts.id !== this.id && isBlocked({source: opts.id, dest: this.id}))
34 return fn({id: null, sequence: 0})
35 else
36 return pull(
37 fn.apply(this, args),
38 //break off this feed if they suddenly block
39 //the recipient.
40 pull.take(function (msg) {
41 //handle when createHistoryStream is called with keys: true
42 if(!msg.content && msg.value.content)
43 msg = msg.value
44 if(msg.content.type !== 'contact') return true
45 return !(
46 (msg.content.flagged || msg.content.blocking) &&
47 msg.content.contact === id
48 )
49 })
50 )
51 })
52
53 sbot.auth.hook(function (fn, args) {
54 if(isBlocked(args[0])) args[1](new Error('client is blocked'))
55 else return fn.apply(this, args)
56 })
57
58 return {isBlocked: valid.sync(isBlocked, 'feedId|isBlockedOpts') }
59
60}
61

Built with git-ssb-web