Commit 944aabb813d4b39e34dc79a025bb30fa8db414ec
Refactor suggestions
- Use async plug combiner to get suggestions for mentions & search. - Move channel suggestions into channel.jscel committed on 11/24/2016, 2:25:45 AM
Parent: 64015f26b256acb4385383829b1af1cbc9a32a0f
Files changed
modules_basic/compose.js | changed |
modules_basic/search-box.js | changed |
modules_basic/suggest-mentions.js | changed |
modules_extra/channel.js | changed |
plugs.js | changed |
modules_basic/compose.js | ||
---|---|---|
@@ -1,22 +1,19 @@ | ||
1 | 1 … | |
2 | 2 … | var h = require('hyperscript') |
3 | 3 … | var u = require('../util') |
4 | 4 … | var suggest = require('suggest-box') |
5 | -var cont = require('cont') | |
6 | 5 … | var mentions = require('ssb-mentions') |
7 | 6 … | var lightbox = require('hyperlightbox') |
8 | 7 … | |
9 | 8 … | var plugs = require('../plugs') |
10 | 9 … | |
11 | -//var suggest = plugs.map(exports.suggest = []) | |
10 … | +var suggest_mentions= plugs.asyncConcat(exports.suggest_mentions = []) | |
12 | 11 … | var publish = plugs.first(exports.sbot_publish = []) |
13 | 12 … | var message_content = plugs.first(exports.message_content = []) |
14 | 13 … | var message_confirm = plugs.first(exports.message_confirm = []) |
15 | 14 … | var file_input = plugs.first(exports.file_input = []) |
16 | 15 … | |
17 | -exports.suggest = [] | |
18 | - | |
19 | 16 … | function id (e) { return e } |
20 | 17 … | |
21 | 18 … | /* |
22 | 19 … | opts can take |
@@ -125,26 +122,11 @@ | ||
125 | 122 … | publishBtn) |
126 | 123 … | ) |
127 | 124 … | ) |
128 | 125 … | |
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, {}) | |
140 | 127 … | |
141 | - cb(null, results) | |
142 | - }) | |
143 | - }, {}) | |
144 | - | |
145 | 128 … | return composer |
146 | 129 … | |
147 | 130 … | } |
148 | 131 … | |
149 | 132 … | |
150 | - |
modules_basic/search-box.js | ||
---|---|---|
@@ -3,24 +3,13 @@ | ||
3 | 3 … | var pull = require('pull-stream') |
4 | 4 … | var plugs = require('../plugs') |
5 | 5 … | var sbot_query = plugs.first(exports.sbot_query = []) |
6 | 6 … | var sbot_links2 = plugs.first(exports.sbot_links2 = []) |
7 … | +var suggest_search = plugs.asyncConcat(exports.suggest_search = []) | |
7 | 8 … | |
8 | 9 … | var channels = [] |
9 | 10 … | |
10 | -var signified = require('../plugs').first(exports.signified = []) | |
11 | 11 … | |
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 | - | |
23 | 12 … | exports.search_box = function (go) { |
24 | 13 … | |
25 | 14 … | var suggestBox |
26 | 15 … | var search = h('input.searchprompt', { |
@@ -58,55 +47,9 @@ | ||
58 | 47 … | var suggestions = {} |
59 | 48 … | |
60 | 49 … | // delay until the element has a parent |
61 | 50 … | 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, {}) | |
91 | 52 … | }, 10) |
92 | 53 … | |
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 | - | |
108 | 54 … | return search |
109 | 55 … | } |
110 | - | |
111 | - | |
112 | - |
modules_basic/suggest-mentions.js | ||
---|---|---|
@@ -1,15 +1,14 @@ | ||
1 | 1 … | var pull = require('pull-stream') |
2 | -var cont = require('cont') | |
3 | 2 … | function isImage (filename) { |
4 | 3 … | return /\.(gif|jpg|png|svg)$/i.test(filename) |
5 | 4 … | } |
6 | 5 … | |
7 | 6 … | var sbot_links2 = require('../plugs').first(exports.sbot_links2 = []) |
8 | 7 … | var blob_url = require('../plugs').first(exports.blob_url = []) |
9 | 8 … | var signified = require('../plugs').first(exports.signified = []) |
10 | 9 … | |
11 | -exports.suggest = cont.to(function (word, cb) { | |
10 … | +exports.suggest_mentions = function (word, cb) { | |
12 | 11 … | if(!/^[%&@]\w/.test(word)) return cb() |
13 | 12 … | |
14 | 13 … | |
15 | 14 … | signified(word, function (err, names) { |
@@ -22,11 +21,40 @@ | ||
22 | 21 … | //TODO: avatar images... |
23 | 22 … | } |
24 | 23 … | })) |
25 | 24 … | }) |
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 … | + } | |
26 | 36 … | }) |
27 | 37 … | |
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 … | + }) | |
28 | 50 … | |
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 … | +} | |
29 | 57 … | |
30 | 58 … | |
31 | 59 … | |
32 | 60 … | |
@@ -37,4 +65,5 @@ | ||
37 | 65 … | |
38 | 66 … | |
39 | 67 … | |
40 | 68 … | |
69 … | + |
modules_extra/channel.js | ||
---|---|---|
@@ -7,8 +7,9 @@ | ||
7 | 7 … | var message_render = plugs.first(exports.message_render = []) |
8 | 8 … | var message_compose = plugs.first(exports.message_compose = []) |
9 | 9 … | var sbot_log = plugs.first(exports.sbot_log = []) |
10 | 10 … | var sbot_query = plugs.first(exports.sbot_query = []) |
11 … | +var mfr = require('map-filter-reduce') | |
11 | 12 … | |
12 | 13 … | exports.message_meta = function (msg) { |
13 | 14 … | var chan = msg.value.content.channel |
14 | 15 … | if (chan) |
@@ -49,4 +50,59 @@ | ||
49 | 50 … | |
50 | 51 … | return div |
51 | 52 … | } |
52 | 53 … | } |
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.js | ||
---|---|---|
@@ -16,5 +16,25 @@ | ||
16 | 16 … | }).filter(Boolean) |
17 | 17 … | } |
18 | 18 … | } |
19 | 19 … | |
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 … | +} | |
20 | 40 … |
Built with git-ssb-web