Files: f8196f783e989cd2c04642e1f2d7e1d21b4ef287 / modules / suggest-mentions.js
1248 bytesRaw
1 | var pull = require('pull-stream') |
2 | var cont = require('cont') |
3 | function isImage (filename) { |
4 | return /\.(gif|jpg|png|svg)$/i.test(filename) |
5 | } |
6 | |
7 | var sbot_links2 = require('../plugs').first(exports.sbot_links2 = []) |
8 | var blob_url = require('../plugs').first(exports.blob_url = []) |
9 | |
10 | exports.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