Files: 1b789e63aa59f5d3e5539a4432c8b3c466977a0e / lib / filter-blocked.js
1757 bytesRaw
1 | const async = require('async') |
2 | const pull = require('pull-stream') |
3 | const paramap = require('pull-paramap') |
4 | const _ = require('lodash') |
5 | |
6 | module.exports = function (blockSources, { isBlocking, checkRoot = false, useRootAuthorBlocks = false }) { |
7 | // Maybe hacky but it seems true in practice |
8 | const me = blockSources[0] |
9 | return pull( |
10 | paramap((msg, cb) => { |
11 | const blocks = [] |
12 | |
13 | // It doesn't make sense to have the forEach here if blockSources would |
14 | // only contain you and the thread author, but I think this code is in |
15 | // preparation for block lists, in which case it makes sense |
16 | blockSources.forEach(source => { |
17 | if (msg.value) { |
18 | blocks.push({ source, dest: msg.value.author }) |
19 | } |
20 | if (checkRoot && msg.root && msg.root.value) { |
21 | blocks.push({ source, dest: msg.root.value.author }) |
22 | } |
23 | }) |
24 | |
25 | if (useRootAuthorBlocks && msg.root && msg.root.value) { |
26 | blocks.push({ source: msg.root.value.author, dest: msg.value.author }) |
27 | } |
28 | |
29 | async.map(blocks, isBlocking, (err, res) => { |
30 | if (err) return cb(err) |
31 | |
32 | // Important that we start with 0 (us), we're prioritized above thread |
33 | // authors |
34 | for (let i = 0; i < blocks.length; i++) { |
35 | if (res[i]) { |
36 | if (!msg.value.meta) msg.value.meta = {} |
37 | msg.value.meta.blockedBy = { |
38 | id: blocks[i].source, |
39 | role: blocks[i].source === me ? 'me' : 'threadAuthor' |
40 | } |
41 | return cb(null, msg) |
42 | } |
43 | } |
44 | cb(null, msg) |
45 | }) |
46 | }), |
47 | pull.filterNot(msg => { |
48 | // Only completely remove the msg if we are the blocker |
49 | return _.get(msg, 'value.meta.blockedBy.role') === 'me' |
50 | }) |
51 | ) |
52 | } |
53 |
Built with git-ssb-web