git ssb

16+

Dominic / patchbay



Tree: 1d2a8b5a23e6c1810b78b10c60f7c1a05c9d76ae

Files: 1d2a8b5a23e6c1810b78b10c60f7c1a05c9d76ae / modules / suggest-mentions.js

1248 bytesRaw
1var pull = require('pull-stream')
2var cont = require('cont')
3function isImage (filename) {
4 return /\.(gif|jpg|png|svg)$/i.test(filename)
5}
6
7var sbot_links2 = require('../plugs').first(exports.sbot_links2 = [])
8var blob_url = require('../plugs').first(exports.blob_url = [])
9
10exports.suggest = cont.to(function (word, cb) {
11 if(!/^[@%&!]/.test(word[0])) return cb()
12 if(word.length < 2) return cb()
13
14 var sigil = word[0]
15 var embed = ((sigil === '!') ? '!' : '')
16 if(embed) sigil = '&'
17 if(word[0] !== '@') word = word.substring(1)
18
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 )
43})
44
45
46

Built with git-ssb-web