Commit d95ae6e1a02431fa8661487edc645af596a31fbe
add message.sync.isBlocked with persistent personal blocklist
mix irving committed on 10/1/2017, 5:37:15 AMParent: eea3b69d83cfcb21e841e49256b7d926d861ff9d
Files changed
contact/obs.js | changed |
feed/obs/thread.js | changed |
feed/pull/channel.js | changed |
feed/pull/mentions.js | changed |
feed/pull/public.js | changed |
feed/pull/rollup.js | changed |
feed/pull/type.js | changed |
message/sync/isBlocked.js | added |
contact/obs.js | ||
---|---|---|
@@ -3,9 +3,9 @@ | ||
3 | 3 | var pull = require('pull-stream') |
4 | 4 | var ref = require('ssb-ref') |
5 | 5 | |
6 | 6 | exports.needs = nest({ |
7 | - 'sbot.pull.stream': 'first' | |
7 | + 'sbot.pull.stream': 'first', | |
8 | 8 | }) |
9 | 9 | |
10 | 10 | exports.gives = nest({ |
11 | 11 | 'contact.obs': ['following', 'followers', 'blocking', 'blockers'], |
feed/obs/thread.js | ||
---|---|---|
@@ -7,10 +7,9 @@ | ||
7 | 7 | 'backlinks.obs.for': 'first', |
8 | 8 | 'sbot.async.get': 'first', |
9 | 9 | 'message.sync.unbox': 'first', |
10 | 10 | 'message.sync.root': 'first', |
11 | - 'contact.obs.blocking': 'first', | |
12 | - 'keys.sync.id': 'first' | |
11 | + 'message.sync.isBlocked': 'first', | |
13 | 12 | }) |
14 | 13 | |
15 | 14 | exports.gives = nest('feed.obs.thread') |
16 | 15 | |
@@ -19,29 +18,27 @@ | ||
19 | 18 | |
20 | 19 | function thread (rootId, { branch } = {}) { |
21 | 20 | if (!ref.isLink(rootId)) throw new Error('an id must be specified') |
22 | 21 | var sync = Value(false) |
22 | + const { isBlocked, unbox, root } = message.sync | |
23 | 23 | |
24 | - const blocking = api.contact.obs.blocking(api.keys.sync.id()) | |
25 | - | |
26 | 24 | var prepend = MutantArray() |
27 | 25 | api.sbot.async.get(rootId, (err, value) => { |
28 | 26 | sync.set(true) |
29 | 27 | if (!err) { |
30 | 28 | var msg = unboxIfNeeded({key: rootId, value}) |
31 | - if (blocking().includes(msg.value.author)) msg.isBlocked = true | |
29 | + if (isBlocked(msg)) msg.isBlocked = true | |
32 | 30 | prepend.push(Value(msg)) |
33 | 31 | } |
34 | 32 | }) |
35 | 33 | |
36 | 34 | var backlinks = api.backlinks.obs.for(rootId) |
37 | 35 | var replies = map(computed(backlinks, (msgs) => { |
38 | 36 | return msgs.filter(msg => { |
39 | - return !blocking().includes(msg.value.author) && | |
40 | - msg.value.content.type !== 'vote' && ( | |
41 | - api.message.sync.root(msg) === rootId || | |
42 | - matchAny(msg.value.content.branch, rootId) | |
43 | - ) | |
37 | + const { type, branch } = msg.value.content | |
38 | + return type !== 'vote' | |
39 | + && !isBlocked(msg) | |
40 | + && (root(msg) === rootId || matchAny(branch, rootId)) | |
44 | 41 | }) |
45 | 42 | }), x => Value(x), { |
46 | 43 | // avoid refresh of entire list when items added |
47 | 44 | comparer: (a, b) => a === b |
@@ -93,9 +90,9 @@ | ||
93 | 90 | } |
94 | 91 | |
95 | 92 | function unboxIfNeeded (msg) { |
96 | 93 | if (msg.value && typeof msg.value.content === 'string') { |
97 | - return api.message.sync.unbox(msg) || msg | |
94 | + return unbox(msg) || msg | |
98 | 95 | } else { |
99 | 96 | return msg |
100 | 97 | } |
101 | 98 | } |
feed/pull/channel.js | ||
---|---|---|
@@ -4,10 +4,9 @@ | ||
4 | 4 | |
5 | 5 | exports.gives = nest('feed.pull.channel') |
6 | 6 | exports.needs = nest({ |
7 | 7 | 'sbot.pull.backlinks': 'first', |
8 | - 'contact.obs.blocking': 'first', | |
9 | - 'keys.sync.id': 'first' | |
8 | + 'message.sync.isBlocked': 'first', | |
10 | 9 | }) |
11 | 10 | |
12 | 11 | exports.create = function (api) { |
13 | 12 | return nest('feed.pull.channel', function (channel) { |
@@ -27,17 +26,15 @@ | ||
27 | 26 | timestamp: typeof lt === 'number' ? {$lt: lt, $gt: 0} : {$gt: 0} |
28 | 27 | } |
29 | 28 | } |
30 | 29 | |
31 | - const blocking = api.contact.obs.blocking(api.keys.sync.id()) | |
32 | - | |
33 | 30 | return pull( |
34 | 31 | api.sbot.pull.backlinks(extend(opts, { |
35 | 32 | query: [ |
36 | 33 | {$filter: filter} |
37 | 34 | ] |
38 | 35 | })), |
39 | - pull.filter(msg => !blocking().includes(msg.value.author)) | |
36 | + pull.filter(msg => !api.message.sync.isBlocked(msg)) | |
40 | 37 | ) |
41 | 38 | } |
42 | 39 | }) |
43 | 40 | } |
feed/pull/mentions.js | ||
---|---|---|
@@ -4,10 +4,9 @@ | ||
4 | 4 | const ref = require('ssb-ref') |
5 | 5 | |
6 | 6 | exports.needs = nest({ |
7 | 7 | 'sbot.pull.backlinks': 'first', |
8 | - 'contact.obs.blocking': 'first', | |
9 | - 'keys.sync.id': 'first' | |
8 | + 'message.sync.isBlocked': 'first' | |
10 | 9 | }) |
11 | 10 | |
12 | 11 | exports.gives = nest('feed.pull.mentions') |
13 | 12 | |
@@ -30,13 +29,11 @@ | ||
30 | 29 | }} |
31 | 30 | ] |
32 | 31 | }) |
33 | 32 | |
34 | - const blocking = api.contact.obs.blocking(api.keys.sync.id()) | |
35 | - | |
36 | 33 | return pull( |
37 | 34 | api.sbot.pull.backlinks(opts), |
38 | - pull.filter((msg) => !blocking().includes(msg.value.author)) | |
35 | + pull.filter(msg => !api.message.sync.isBlocked(msg)) | |
39 | 36 | ) |
40 | 37 | } |
41 | 38 | }) |
42 | 39 | } |
feed/pull/public.js | ||
---|---|---|
@@ -3,10 +3,9 @@ | ||
3 | 3 | |
4 | 4 | exports.gives = nest('feed.pull.public') |
5 | 5 | exports.needs = nest({ |
6 | 6 | 'sbot.pull.feed': 'first', |
7 | - 'contact.obs.blocking': 'first', | |
8 | - 'keys.sync.id': 'first' | |
7 | + 'message.sync.isBlocked': 'first', | |
9 | 8 | }) |
10 | 9 | |
11 | 10 | exports.create = function (api) { |
12 | 11 | return nest('feed.pull.public', (opts) => { |
@@ -14,12 +13,10 @@ | ||
14 | 13 | opts.lt = (opts.lt && opts.lt.value) |
15 | 14 | ? opts.lt.value.timestamp |
16 | 15 | : opts.lt |
17 | 16 | |
18 | - const blocking = api.contact.obs.blocking(api.keys.sync.id()) | |
19 | - | |
20 | 17 | return pull( |
21 | 18 | api.sbot.pull.feed(opts), |
22 | - pull.filter(msg => !blocking().includes(msg.value.author)) | |
19 | + pull.filter(msg => !api.message.sync.isBlocked(msg)) | |
23 | 20 | ) |
24 | 21 | }) |
25 | 22 | } |
feed/pull/rollup.js | ||
---|---|---|
@@ -11,12 +11,11 @@ | ||
11 | 11 | |
12 | 12 | exports.needs = nest({ |
13 | 13 | 'backlinks.obs.for': 'first', |
14 | 14 | 'sbot.async.get': 'first', |
15 | + 'message.sync.isBlocked': 'first', | |
15 | 16 | 'message.sync.root': 'first', |
16 | 17 | 'message.sync.unbox': 'first', |
17 | - 'contact.obs.blocking': 'first', | |
18 | - 'keys.sync.id': 'first' | |
19 | 18 | }) |
20 | 19 | |
21 | 20 | exports.gives = nest('feed.pull.rollup', true) |
22 | 21 | |
@@ -24,10 +23,8 @@ | ||
24 | 23 | // cache mostly just to avoid reading the same roots over and over again |
25 | 24 | // not really big enough for multiple refresh cycles |
26 | 25 | var cache = HLRU(100) |
27 | 26 | |
28 | - const blocking = api.contact.obs.blocking(api.keys.sync.id()) | |
29 | - | |
30 | 27 | return nest('feed.pull.rollup', function (rootFilter) { |
31 | 28 | var seen = new Set() |
32 | 29 | return pull( |
33 | 30 | pull.map(msg => { |
@@ -87,19 +84,18 @@ | ||
87 | 84 | |
88 | 85 | // FILTER |
89 | 86 | pull.filter(msg => msg && msg.value && !api.message.sync.root(msg)), |
90 | 87 | pull.filter(rootFilter || (() => true)), |
88 | + pull.filter(msg => !api.message.sync.isBlocked(msg)) | |
91 | 89 | |
92 | - pull.filter(msg => !blocking().includes(msg.value.author)), | |
93 | - | |
94 | 90 | // ADD REPLIES |
95 | 91 | pull.asyncMap((rootMessage, cb) => { |
96 | 92 | // use global backlinks cache |
97 | 93 | var backlinks = api.backlinks.obs.for(rootMessage.key) |
98 | 94 | onceTrue(backlinks.sync, () => { |
99 | - var replies = resolve(backlinks).filter((msg) => { | |
100 | - return !blocking().includes(msg.value.author) && | |
101 | - api.message.sync.root(msg) === rootMessage.key | |
95 | + var replies = resolve(backlinks).filter(msg => { | |
96 | + return api.message.sync.root(msg) === rootMessage.key | |
97 | + && !api.message.sync.isBlocked(msg) | |
102 | 98 | }) |
103 | 99 | cb(null, extend(rootMessage, { replies })) |
104 | 100 | }) |
105 | 101 | }) |
feed/pull/type.js | ||
---|---|---|
@@ -4,10 +4,9 @@ | ||
4 | 4 | |
5 | 5 | exports.gives = nest('feed.pull.type') |
6 | 6 | exports.needs = nest({ |
7 | 7 | 'sbot.pull.messagesByType': 'first', |
8 | - 'contact.obs.blocking': 'first', | |
9 | - 'keys.sync.id': 'first' | |
8 | + 'message.sync.isBlocked': 'first', | |
10 | 9 | }) |
11 | 10 | |
12 | 11 | exports.create = function (api) { |
13 | 12 | return nest('feed.pull.type', (type) => { |
@@ -19,13 +18,11 @@ | ||
19 | 18 | // handle last item passed in as lt |
20 | 19 | lt: opts.lt && typeof opts.lt === 'object' ? opts.lt.timestamp : opts.lt |
21 | 20 | }) |
22 | 21 | |
23 | - const blocking = api.contact.obs.blocking(api.keys.sync.id()) | |
24 | - | |
25 | 22 | return pull( |
26 | 23 | api.sbot.pull.messagesByType(opts), |
27 | - pull.filter(msg => !blocking().includes(msg.value.author)) | |
24 | + pull.filter(msg => !api.message.sync.isBlocked(msg)) | |
28 | 25 | ) |
29 | 26 | } |
30 | 27 | }) |
31 | 28 | } |
message/sync/isBlocked.js | ||
---|---|---|
@@ -1,0 +1,22 @@ | ||
1 | +const nest = require('depnest') | |
2 | + | |
3 | +exports.gives = nest('message.sync.isBlocked') | |
4 | + | |
5 | +exports.needs = nest({ | |
6 | + 'contact.obs.blocking': 'first' | |
7 | + 'keys.sync.id': 'first' | |
8 | +}) | |
9 | + | |
10 | +exports.create = function (api) { | |
11 | + var _myBlocking | |
12 | + | |
13 | + return nest('message.sync.isBlocked', function isBlockedMessage (msg) { | |
14 | + if (!_myBlocking) { | |
15 | + const myKey = api.keys.sync.id() | |
16 | + _myBlocking = api.contact.obs.blocking(myKey) | |
17 | + } | |
18 | + | |
19 | + return _myBlocking.includes(msg.value.author) | |
20 | + }) | |
21 | +} | |
22 | + |
Built with git-ssb-web