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