git ssb

0+

Matt McKegg / ssb-backlinks



Commit 5a54f8ce3a5ceb141438521584923130a229f879

extract emit-links out into own file

Matt McKegg committed on 11/8/2017, 5:05:06 AM
Parent: dd065a089572cf2f1e51c77d8af280934cc52fd2

Files changed

index.jschanged
emit-links.jsadded
index.jsView
@@ -1,11 +1,8 @@
11 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#]+$/
62 var ssbKeys = require('ssb-keys')
73 var toUrlFriendly = require('base64-url').escape
4 +var emitLinks = require('./emit-links')
85
96 var indexes = [
107 { key: 'DTS', value: [['dest'], ['timestamp']] },
118 { key: 'DTA', value: [['dest'], ['value', 'timestamp']] }, // asserted timestamp
@@ -22,9 +19,9 @@
2219
2320 exports.init = function (ssb, config) {
2421 return ssb._flumeUse(
2522 `backlinks-${toUrlFriendly(ssb.id.slice(1, 10))}`,
26- FlumeQueryLinks(indexes, extractLinks, indexVersion, unbox)
23 + FlumeQueryLinks(indexes, emitLinks, indexVersion, unbox)
2724 )
2825
2926 function unbox (msg) {
3027 if (typeof msg.value.content === 'string') {
@@ -54,38 +51,4 @@
5451 private: true
5552 }
5653 }
5754 }
58-
59-function extractLinks (msg, emit) {
60- var links = new Set()
61- walk(msg.value.content, function (path, value) {
62- // HACK: handle legacy channel mentions
63- if (deepEqual(path, ['channel']) && typeof value === 'string' && value.length < 30) {
64- value = `#${value.replace(/\s/g, '')}`
65- }
66-
67- // TODO: should add channel matching to ref.type
68- if (ref.type(value) || isChannel(value)) {
69- links.add(value)
70- }
71- })
72- links.forEach(link => {
73- emit(extend(msg, {
74- dest: link
75- }))
76- })
77-}
78-
79-function isChannel (value) {
80- return typeof value === 'string' && value.length < 30 && matchChannel.test(value)
81-}
82-
83-function walk (obj, fn, prefix) {
84- if (obj && typeof obj === 'object') {
85- for (var k in obj) {
86- walk(obj[k], fn, (prefix || []).concat(k))
87- }
88- } else {
89- fn(prefix, obj)
90- }
91-}
emit-links.jsView
@@ -1,0 +1,40 @@
1 +var ref = require('ssb-ref')
2 +var deepEqual = require('deep-equal')
3 +var extend = require('xtend')
4 +var matchChannel = /^#[^\s#]+$/
5 +
6 +module.exports = emitLinks
7 +
8 +function 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 +
28 +function isChannel (value) {
29 + return typeof value === 'string' && value.length < 30 && matchChannel.test(value)
30 +}
31 +
32 +function 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 +}

Built with git-ssb-web