Files: 0773ae21c1f01e75b8deed6d6dd2defc7f6e0f06 / index.js
1448 bytesRaw
1 | var FlumeQueryLinks = require('./lib/flumeview-links-raw') |
2 | var ref = require('ssb-ref') |
3 | var deepEqual = require('deep-equal') |
4 | var extend = require('xtend') |
5 | var matchChannel = /^#[^\s#]+$/ |
6 | |
7 | var indexes = [ |
8 | { key: 'DTS', value: [['dest'], ['timestamp']] }, |
9 | { key: 'DTY', value: [['dest'], ['value', 'content', 'type'], ['timestamp']] } |
10 | ] |
11 | |
12 | var indexVersion = 0 |
13 | |
14 | exports.name = 'backlinks' |
15 | exports.version = require('./package.json').version |
16 | exports.manifest = { |
17 | read: 'source' |
18 | } |
19 | |
20 | exports.init = function (ssb, config) { |
21 | return ssb._flumeUse( |
22 | 'backlinks', |
23 | FlumeQueryLinks(indexes, extractLinks, indexVersion) |
24 | ) |
25 | } |
26 | |
27 | function extractLinks (msg, emit) { |
28 | var links = new Set() |
29 | walk(msg.value.content, function (path, value) { |
30 | // HACK: handle legacy channel mentions |
31 | if (deepEqual(path, ['channel']) && typeof value === 'string' && value.length < 30) { |
32 | value = `#${value.replace(/\s/g, '')}` |
33 | } |
34 | |
35 | // TODO: should add channel matching to ref.type |
36 | if (ref.type(value) || isChannel(value)) { |
37 | links.add(value) |
38 | } |
39 | }) |
40 | links.forEach(link => { |
41 | emit(extend(msg, { |
42 | dest: link |
43 | })) |
44 | }) |
45 | } |
46 | |
47 | function isChannel (value) { |
48 | return typeof value === 'string' && value.length < 30 && matchChannel.test(value) |
49 | } |
50 | |
51 | function walk (obj, fn, prefix) { |
52 | if (obj && typeof obj === 'object') { |
53 | for (var k in obj) { |
54 | walk(obj[k], fn, (prefix || []).concat(k)) |
55 | } |
56 | } else { |
57 | fn(prefix, obj) |
58 | } |
59 | } |
60 |
Built with git-ssb-web