git ssb

16+

Dominic / patchbay



Commit 944aabb813d4b39e34dc79a025bb30fa8db414ec

Refactor suggestions

- Use async plug combiner to get suggestions for mentions & search.
- Move channel suggestions into channel.js
cel committed on 11/24/2016, 2:25:45 AM
Parent: 64015f26b256acb4385383829b1af1cbc9a32a0f

Files changed

modules_basic/compose.jschanged
modules_basic/search-box.jschanged
modules_basic/suggest-mentions.jschanged
modules_extra/channel.jschanged
plugs.jschanged
modules_basic/compose.jsView
@@ -1,22 +1,19 @@
11 'use strict'
22 var h = require('hyperscript')
33 var u = require('../util')
44 var suggest = require('suggest-box')
5-var cont = require('cont')
65 var mentions = require('ssb-mentions')
76 var lightbox = require('hyperlightbox')
87
98 var plugs = require('../plugs')
109
11-//var suggest = plugs.map(exports.suggest = [])
10 +var suggest_mentions= plugs.asyncConcat(exports.suggest_mentions = [])
1211 var publish = plugs.first(exports.sbot_publish = [])
1312 var message_content = plugs.first(exports.message_content = [])
1413 var message_confirm = plugs.first(exports.message_confirm = [])
1514 var file_input = plugs.first(exports.file_input = [])
1615
17-exports.suggest = []
18-
1916 function id (e) { return e }
2017
2118 /*
2219 opts can take
@@ -125,26 +122,11 @@
125122 publishBtn)
126123 )
127124 )
128125
129- suggest(ta, function (word, cb) {
130- cont.para(exports.suggest.map(function (fn) {
131- return function (cb) { fn(word, cb) }
132- }))
133- (function (err, results) {
134- if(err) console.error(err)
135- results = results.reduce(function (ary, item) {
136- return ary.concat(item)
137- }, []).sort(function (a, b) {
138- return b.rank - a.rank
139- }).filter(Boolean)
126 + suggest(ta, suggest_mentions, {})
140127
141- cb(null, results)
142- })
143- }, {})
144-
145128 return composer
146129
147130 }
148131
149132
150-
modules_basic/search-box.jsView
@@ -3,24 +3,13 @@
33 var pull = require('pull-stream')
44 var plugs = require('../plugs')
55 var sbot_query = plugs.first(exports.sbot_query = [])
66 var sbot_links2 = plugs.first(exports.sbot_links2 = [])
7 +var suggest_search = plugs.asyncConcat(exports.suggest_search = [])
78
89 var channels = []
910
10-var signified = require('../plugs').first(exports.signified = [])
1111
12-//TODO: this list should be generated from plugs
13-var builtinTabs = [
14- '/public', '/private', '/notifications',
15- '/network', '/query', '/versions'
16-].map(function (name) {
17- return {
18- title: name,
19- value: name,
20- }
21-})
22-
2312 exports.search_box = function (go) {
2413
2514 var suggestBox
2615 var search = h('input.searchprompt', {
@@ -58,55 +47,9 @@
5847 var suggestions = {}
5948
6049 // delay until the element has a parent
6150 setTimeout(function () {
62- suggestBox = suggest(search, function (word, cb) {
63- if(/^#\w/.test(word))
64- cb(null, channels.filter(function (chan) {
65- return ('#'+chan.name).substring(0, word.length) === word
66- })
67- .map(function (chan) {
68- var name = '#'+chan.name
69- return {
70- title: name,
71- value: name,
72- subtitle: chan.rank
73- }
74- }))
75- else if(/^[@%]\w/.test(word)) {
76- signified(word, function (_, names) {
77- cb(null, names.map(function (e) {
78- return {
79- title: e.name + ':'+e.id.substring(0, 10),
80- value: e.id,
81- subtitle: e.rank
82- }
83- }))
84- })
85- } else if(/^\//.test(word)) {
86- cb(null, builtinTabs.filter(function (name) {
87- return name.value.substr(0, word.length) === word
88- }))
89- }
90- }, {})
51 + suggestBox = suggest(search, suggest_search, {})
9152 }, 10)
9253
93-
94- pull(
95- sbot_query({query: [
96- {$filter: {value: {content: {channel: {$gt: ''}}}}},
97- {$reduce: {
98- name: ['value', 'content', 'channel'],
99- rank: {$count: true}
100- }}
101- ]}),
102- pull.collect(function (err, chans) {
103- if (err) return console.error(err)
104- channels = chans
105- })
106- )
107-
10854 return search
10955 }
110-
111-
112-
modules_basic/suggest-mentions.jsView
@@ -1,15 +1,14 @@
11 var pull = require('pull-stream')
2-var cont = require('cont')
32 function isImage (filename) {
43 return /\.(gif|jpg|png|svg)$/i.test(filename)
54 }
65
76 var sbot_links2 = require('../plugs').first(exports.sbot_links2 = [])
87 var blob_url = require('../plugs').first(exports.blob_url = [])
98 var signified = require('../plugs').first(exports.signified = [])
109
11-exports.suggest = cont.to(function (word, cb) {
10 +exports.suggest_mentions = function (word, cb) {
1211 if(!/^[%&@]\w/.test(word)) return cb()
1312
1413
1514 signified(word, function (err, names) {
@@ -22,11 +21,40 @@
2221 //TODO: avatar images...
2322 }
2423 }))
2524 })
25 +}
26 +
27 +//TODO: this list should be generated from plugs
28 +var builtinTabs = [
29 + '/public', '/private', '/notifications',
30 + '/network', '/query', '/versions'
31 +].map(function (name) {
32 + return {
33 + title: name,
34 + value: name,
35 + }
2636 })
2737
38 +exports.suggest_search = function (query, cb) {
39 + if(/^[@%]\w/.test(query)) {
40 + signified(query, function (_, names) {
41 + cb(null, names.map(function (e) {
42 + return {
43 + title: e.name + ':'+e.id.substring(0, 10),
44 + value: e.id,
45 + subtitle: e.rank,
46 + rank: e.rank
47 + }
48 + }))
49 + })
2850
51 + } else if(/^\//.test(query)) {
52 + cb(null, builtinTabs.filter(function (name) {
53 + return name.value.substr(0, query.length) === query
54 + }))
55 + } else cb()
56 +}
2957
3058
3159
3260
@@ -37,4 +65,5 @@
3765
3866
3967
4068
69 +
modules_extra/channel.jsView
@@ -7,8 +7,9 @@
77 var message_render = plugs.first(exports.message_render = [])
88 var message_compose = plugs.first(exports.message_compose = [])
99 var sbot_log = plugs.first(exports.sbot_log = [])
1010 var sbot_query = plugs.first(exports.sbot_query = [])
11 +var mfr = require('map-filter-reduce')
1112
1213 exports.message_meta = function (msg) {
1314 var chan = msg.value.content.channel
1415 if (chan)
@@ -49,4 +50,59 @@
4950
5051 return div
5152 }
5253 }
54 +
55 +function compareRank(a, b) {
56 + return b.rank - a.rank
57 +}
58 +
59 +var channels
60 +
61 +var filter = {$filter: {value: {content: {channel: {$gt: ''}}}}}
62 +var map = {$map: {'name': ['value', 'content', 'channel']}}
63 +var reduce = {$reduce: {
64 + name: 'name',
65 + rank: {$count: true}
66 +}}
67 +
68 +exports.connection_status = function (err) {
69 + if(err) return
70 +
71 + channels = []
72 +
73 + pull(
74 + sbot_query({query: [filter, map, reduce]}),
75 + pull.collect(function (err, chans) {
76 + if (err) return console.error(err)
77 + channels = chans.concat(channels)
78 + })
79 + )
80 +
81 + pull(
82 + sbot_log({old: false}),
83 + mfr.filter(filter),
84 + mfr.map(map),
85 + pull.drain(function (chan) {
86 + var c = channels.find(function (e) {
87 + return e.name === chan.name
88 + })
89 + if (c) c.rank++
90 + else channels.push(chan)
91 + })
92 + )
93 +}
94 +
95 +exports.suggest_search = function (query, cb) {
96 + if(!/^#\w/.test(query)) return cb()
97 + cb(null, channels.filter(function (chan) {
98 + return ('#'+chan.name).substring(0, query.length) === query
99 + })
100 + .map(function (chan) {
101 + var name = '#'+chan.name
102 + return {
103 + title: name,
104 + value: name,
105 + subtitle: chan.rank
106 + }
107 + }))
108 +}
plugs.jsView
@@ -16,5 +16,25 @@
1616 }).filter(Boolean)
1717 }
1818 }
1919
20 +exports.asyncConcat = function (plug) {
21 + return function () {
22 + var args = [].slice.call(arguments)
23 + var cb = args.pop()
24 + var allResults = []
25 + var waiting = plug.length
26 + plug.forEach(function (fn) {
27 + if (!fn) return next()
28 + fn.apply(null, args.concat(next))
29 + })
30 + function next(err, results) {
31 + if (err) {
32 + waiting = 0
33 + return cb(err)
34 + }
35 + if (results) allResults = allResults.concat(results)
36 + if (--waiting === 0) cb(null, allResults)
37 + }
38 + }
39 +}
2040

Built with git-ssb-web