git ssb

0+

ev / microbay



forked from Dominic / patchbay

Commit db46d1cf47e3b75b3771f18847f3278ca8abd0d4

refactor search and suggest to be in terms of signified

Dominic Tarr committed on 7/31/2016, 10:50:54 AM
Parent: 0ea8e47dd13c972419f7ec529f51d81623acaea3

Files changed

modules/search-box.jschanged
modules/suggest-mentions.jschanged
modules/search-box.jsView
@@ -4,8 +4,13 @@
44 var plugs = require('../plugs')
55 var sbot_query = plugs.first(exports.sbot_query = [])
66 var sbot_links2 = plugs.first(exports.sbot_links2 = [])
77
8+var channels = []
9+
10+var signified = require('../plugs').first(exports.signified = [])
11+
12+
813 exports.search_box = function (go) {
914
1015 var suggestBox
1116 var search = h('input.searchprompt', {
@@ -42,11 +47,36 @@
4247 var suggestions = {}
4348
4449 // delay until the element has a parent
4550 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+ })
4776 }, 10)
4877
78+
4979 pull(
5080 sbot_query({query: [
5181 {$filter: {value: {content: {channel: {$gt: ''}}}}},
5282 {$reduce: {
@@ -55,44 +85,13 @@
5585 }}
5686 ]}),
5787 pull.collect(function (err, chans) {
5888 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
6790 })
6891 )
6992
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-
9793 return search
9894 }
95+
96+
97+
modules/suggest-mentions.jsView
@@ -5,41 +5,36 @@
55 }
66
77 var sbot_links2 = require('../plugs').first(exports.sbot_links2 = [])
88 var blob_url = require('../plugs').first(exports.blob_url = [])
9+var signified = require('../plugs').first(exports.signified = [])
910
1011 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()
1313
14- var sigil = word[0]
15- var embed = ((sigil === '!') ? '!' : '')
16- if(embed) sigil = '&'
17- if(word[0] !== '@') word = word.substring(1)
1814
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+ })
4326 })
4427
4528
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
39+
40+

Built with git-ssb-web