Files: 3451510316992d414ec76ba5b29681fe359b7428 / lib / plugins / backlinks.js
1622 bytesRaw
1 | const pull = require('pull-stream') |
2 | |
3 | exports.manifest = { |
4 | referencesStream: 'source', |
5 | forksStream: 'source' |
6 | } |
7 | |
8 | const excludeTypes = ['about', 'vote', 'tag'] |
9 | |
10 | exports.init = function (ssb) { |
11 | return { |
12 | referencesStream: function ({ id, since }) { |
13 | return getBacklinksStream(id, since, (msg) => { |
14 | if (msg.value && msg.value.content) { |
15 | return msg.value.content.root !== id && msg.value.content.fork !== id && !includeOrEqual(msg.value.content.branch, id) |
16 | } |
17 | }) |
18 | }, |
19 | |
20 | // don't call this for root messages, only replies |
21 | forksStream: function ({ id, since }) { |
22 | return getBacklinksStream(id, since, (msg) => { |
23 | if (msg.value && msg.value.content) { |
24 | return msg.value.content.root === id && includeOrEqual(msg.value.content.branch, id) |
25 | } |
26 | }) |
27 | } |
28 | } |
29 | |
30 | function getBacklinksStream (id, since, fn) { |
31 | return pull( |
32 | ssb.backlinks.read({ |
33 | live: true, |
34 | query: [{ |
35 | $filter: { |
36 | timestamp: { $gt: since || 0 }, |
37 | dest: id |
38 | } |
39 | }] |
40 | }), |
41 | pull.filter((msg) => { |
42 | if (msg.sync) return true |
43 | if (msg.value && msg.value.content && excludeTypes.includes(msg.value.content.type)) return false |
44 | return fn(msg) |
45 | }), |
46 | pull.map((msg) => { |
47 | if (msg.sync) return msg |
48 | return { id: msg.key, author: msg.value.author, timestamp: msg.timestamp } |
49 | }) |
50 | ) |
51 | } |
52 | } |
53 | |
54 | function includeOrEqual (valueOrArray, item) { |
55 | if (Array.isArray(valueOrArray)) { |
56 | return valueOrArray.includes(item) |
57 | } else { |
58 | return valueOrArray === item |
59 | } |
60 | } |
61 |
Built with git-ssb-web