git ssb

0+

Matt McKegg / ssb-backlinks



Tree: 5a54f8ce3a5ceb141438521584923130a229f879

Files: 5a54f8ce3a5ceb141438521584923130a229f879 / emit-links.js

975 bytesRaw
1var ref = require('ssb-ref')
2var deepEqual = require('deep-equal')
3var extend = require('xtend')
4var matchChannel = /^#[^\s#]+$/
5
6module.exports = emitLinks
7
8function emitLinks (msg, emit) {
9 var links = new Set()
10 walk(msg.value.content, function (path, value) {
11 // HACK: handle legacy channel mentions
12 if (deepEqual(path, ['channel']) && typeof value === 'string' && value.length < 30) {
13 value = `#${value.replace(/\s/g, '')}`
14 }
15
16 // TODO: should add channel matching to ref.type
17 if (ref.type(value) || isChannel(value)) {
18 links.add(value)
19 }
20 })
21 links.forEach(link => {
22 emit(extend(msg, {
23 dest: link
24 }))
25 })
26}
27
28function isChannel (value) {
29 return typeof value === 'string' && value.length < 30 && matchChannel.test(value)
30}
31
32function walk (obj, fn, prefix) {
33 if (obj && typeof obj === 'object') {
34 for (var k in obj) {
35 walk(obj[k], fn, (prefix || []).concat(k))
36 }
37 } else {
38 fn(prefix, obj)
39 }
40}
41

Built with git-ssb-web