git ssb

0+

Matt McKegg / ssb-backlinks



Tree: 389fe124a06bd5e7915acd466ce8aaa047d4eaca

Files: 389fe124a06bd5e7915acd466ce8aaa047d4eaca / index.js

1448 bytesRaw
1var FlumeQueryLinks = require('./lib/flumeview-links-raw')
2var ref = require('ssb-ref')
3var deepEqual = require('deep-equal')
4var extend = require('xtend')
5var matchChannel = /^#[^\s#]+$/
6
7var indexes = [
8 { key: 'DTS', value: [['dest'], ['timestamp']] },
9 { key: 'DTY', value: [['dest'], ['value', 'content', 'type'], ['timestamp']] }
10]
11
12var indexVersion = 0
13
14exports.name = 'backlinks'
15exports.version = require('./package.json').version
16exports.manifest = {
17 read: 'source'
18}
19
20exports.init = function (ssb, config) {
21 return ssb._flumeUse(
22 'backlinks',
23 FlumeQueryLinks(indexes, extractLinks, indexVersion)
24 )
25}
26
27function 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
47function isChannel (value) {
48 return typeof value === 'string' && value.length < 30 && matchChannel.test(value)
49}
50
51function 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