Files: ceab09c9e34bbabf034b57309315733a28ec5e7a / modules / suggest_mentions.js
1128 bytesRaw
1 | var pull = require('pull-stream') |
2 | |
3 | function isImage (filename) { |
4 | return /\.(gif|jpg|png|svg)$/i.test(filename) |
5 | } |
6 | |
7 | exports.suggest = function (word, sbot, cb) { |
8 | |
9 | if(!/^[@%&!]/.test(word[0])) return cb() |
10 | if(word.length < 2) return cb() |
11 | |
12 | var sigil = word[0] |
13 | var embed = ((sigil === '!') ? '!' : '') |
14 | if(embed) sigil = '&' |
15 | if(word[0] !== '@') word = word.substring(1) |
16 | |
17 | pull( |
18 | sbot.links2.read({query: [ |
19 | {$filter: {rel: ['mentions', {$prefix: word}], dest: {$prefix: sigil}}}, |
20 | {$reduce: {id: 'dest', name: ['rel', 1], rank: {$count: true}}} |
21 | ]}), |
22 | pull.collect(function (err, ary) { |
23 | |
24 | ary = ary |
25 | .filter(function (e) { |
26 | if(!embed) return true |
27 | return isImage(e.name) |
28 | }).sort(function (a, b) { |
29 | return b.rank - a.rank |
30 | }).map(function (e) { |
31 | return { |
32 | title: e.name + ': ' + e.id.substring(0,10)+' ('+e.rank+')', |
33 | value: embed+'['+e.name+']('+e.id+')', |
34 | rank: e.rank, |
35 | image: isImage(e.name) ? 'http://localhost:7777/'+e.id : undefined |
36 | } |
37 | }) |
38 | console.log(ary) |
39 | cb(null, ary) |
40 | }) |
41 | ) |
42 | } |
43 | |
44 | |
45 |
Built with git-ssb-web