Commit db46d1cf47e3b75b3771f18847f3278ca8abd0d4
refactor search and suggest to be in terms of signified
Dominic Tarr committed on 7/31/2016, 10:50:54 AMParent: 0ea8e47dd13c972419f7ec529f51d81623acaea3
Files changed
modules/search-box.js | changed |
modules/suggest-mentions.js | changed |
modules/search-box.js | ||
---|---|---|
@@ -4,8 +4,13 @@ | ||
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 | 7 | |
8 | +var channels = [] | |
9 | + | |
10 | +var signified = require('../plugs').first(exports.signified = []) | |
11 | + | |
12 | + | |
8 | 13 | exports.search_box = function (go) { |
9 | 14 | |
10 | 15 | var suggestBox |
11 | 16 | var search = h('input.searchprompt', { |
@@ -42,11 +47,36 @@ | ||
42 | 47 | var suggestions = {} |
43 | 48 | |
44 | 49 | // delay until the element has a parent |
45 | 50 | setTimeout(function () { |
46 | - suggestBox = suggest(search, suggestions) | |
51 | + suggestBox = suggest(search, function (word, cb) { | |
52 | + if(/^#\w/.test(word)) | |
53 | + cb(null, channels.filter(function (chan) { | |
54 | + return ('#'+chan.name).substring(0, word.length) === word | |
55 | + }) | |
56 | + .map(function (e) { | |
57 | + var name = '#'+e.name | |
58 | + return { | |
59 | + title: name, | |
60 | + value: name, | |
61 | + subtitle: chan.posts | |
62 | + } | |
63 | + })) | |
64 | + else if(/^@\w/.test(word)) { | |
65 | + signified(word, function (_, names) { | |
66 | + cb(null, names.map(function (e) { | |
67 | + return { | |
68 | + title: e.name + ':'+e.id.substring(0, 10), | |
69 | + value: e.id, | |
70 | + subtitle: e.rank | |
71 | + } | |
72 | + })) | |
73 | + }) | |
74 | + } | |
75 | + }) | |
47 | 76 | }, 10) |
48 | 77 | |
78 | + | |
49 | 79 | pull( |
50 | 80 | sbot_query({query: [ |
51 | 81 | {$filter: {value: {content: {channel: {$gt: ''}}}}}, |
52 | 82 | {$reduce: { |
@@ -55,44 +85,13 @@ | ||
55 | 85 | }} |
56 | 86 | ]}), |
57 | 87 | pull.collect(function (err, chans) { |
58 | 88 | if (err) return console.error(err) |
59 | - suggestions['#'] = chans.map(function (chan) { | |
60 | - var name = '#' + chan.channel | |
61 | - return { | |
62 | - title: name, | |
63 | - value: name, | |
64 | - subtitle: chan.posts | |
65 | - } | |
66 | - }) | |
89 | + channels = chans | |
67 | 90 | }) |
68 | 91 | ) |
69 | 92 | |
70 | - pull( | |
71 | - sbot_links2({query: [ | |
72 | - {$filter: { | |
73 | - dest: {$prefix: '@'}, | |
74 | - rel: ['mentions', {$gt: '@'}]} | |
75 | - }, | |
76 | - {$reduce: { | |
77 | - id: 'dest', | |
78 | - name: ['rel', 1], | |
79 | - rank: {$count: true}} | |
80 | - } | |
81 | - ]}), | |
82 | - pull.collect(function (err, links) { | |
83 | - if (err) return console.error(err) | |
84 | - suggestions['@'] = links.map(function (e) { | |
85 | - return { | |
86 | - title: e.name, | |
87 | - value: e.id, | |
88 | - subtitle: e.id + ' (' + e.rank + ')', | |
89 | - rank: e.rank | |
90 | - } | |
91 | - }).sort(function (a, b) { | |
92 | - return b.rank - a.rank | |
93 | - }) | |
94 | - }) | |
95 | - ) | |
96 | - | |
97 | 93 | return search |
98 | 94 | } |
95 | + | |
96 | + | |
97 | + |
modules/suggest-mentions.js | ||
---|---|---|
@@ -5,41 +5,36 @@ | ||
5 | 5 | } |
6 | 6 | |
7 | 7 | var sbot_links2 = require('../plugs').first(exports.sbot_links2 = []) |
8 | 8 | var blob_url = require('../plugs').first(exports.blob_url = []) |
9 | +var signified = require('../plugs').first(exports.signified = []) | |
9 | 10 | |
10 | 11 | exports.suggest = cont.to(function (word, cb) { |
11 | - if(!/^[@%&!]/.test(word[0])) return cb() | |
12 | - if(word.length < 2) return cb() | |
12 | + if(!/^@\w/.test(word)) return cb() | |
13 | 13 | |
14 | - var sigil = word[0] | |
15 | - var embed = ((sigil === '!') ? '!' : '') | |
16 | - if(embed) sigil = '&' | |
17 | - if(word[0] !== '@') word = word.substring(1) | |
18 | 14 | |
19 | - pull( | |
20 | - sbot_links2({query: [ | |
21 | - {$filter: {rel: ['mentions', {$prefix: word}], dest: {$prefix: sigil}}}, | |
22 | - {$reduce: {id: 'dest', name: ['rel', 1], rank: {$count: true}}} | |
23 | - ]}), | |
24 | - pull.collect(function (err, ary) { | |
25 | - | |
26 | - ary = ary | |
27 | - .filter(function (e) { | |
28 | - if(!embed) return true | |
29 | - return isImage(e.name) | |
30 | - }).sort(function (a, b) { | |
31 | - return b.rank - a.rank | |
32 | - }).map(function (e) { | |
33 | - return { | |
34 | - title: e.name + ': ' + e.id.substring(0,10)+' ('+e.rank+')', | |
35 | - value: embed+'['+e.name+']('+e.id+')', | |
36 | - rank: e.rank, | |
37 | - image: isImage(e.name) ? blob_url(e.id) : undefined | |
38 | - } | |
39 | - }) | |
40 | - cb(null, ary) | |
41 | - }) | |
42 | - ) | |
15 | + signified(word, function (err, names) { | |
16 | + if(err) cb(err) | |
17 | + else cb(null, names.map(function (e) { | |
18 | + return { | |
19 | + title: e.name + ': ' + e.id.substring(0,10)+' ('+e.rank+')', | |
20 | + value: '['+e.name+']('+e.id+')', | |
21 | + rank: e.rank, | |
22 | + //TODO: avatar images... | |
23 | + } | |
24 | + })) | |
25 | + }) | |
43 | 26 | }) |
44 | 27 | |
45 | 28 | |
29 | + | |
30 | + | |
31 | + | |
32 | + | |
33 | + | |
34 | + | |
35 | + | |
36 | + | |
37 | + | |
38 | + | |
39 | + | |
40 | + |
Built with git-ssb-web