Files: 3147be9a99a30d20113b37a3e39f6f53e9ffaf0b / meme / async / suggest.js
1181 bytesRaw
1 | const nest = require('depnest') |
2 | const { h, onceTrue } = require('mutant') |
3 | const map = require('lodash.map') |
4 | |
5 | exports.gives = nest('meme.async.suggest') |
6 | |
7 | exports.needs = nest({ |
8 | 'blob.sync.url': 'first', |
9 | 'sbot.obs.connection': 'first' |
10 | }) |
11 | |
12 | exports.create = function (api) { |
13 | return nest('meme.async.suggest', suggestedMeme) |
14 | |
15 | function suggestedMeme (word, cb) { |
16 | if (word.length < 3) return |
17 | // TODO suggest a random 10? |
18 | |
19 | word = word.toLowerCase() |
20 | |
21 | onceTrue(api.sbot.obs.connection, sbot => { |
22 | sbot.meme.search(word, (err, data) => { |
23 | if (err) return console.error(err) |
24 | |
25 | cb(null, map(data, toSuggestion)) |
26 | }) |
27 | }) |
28 | } |
29 | |
30 | function toSuggestion (value, key) { |
31 | const topPick = value[0] |
32 | // TODO get a better topPick (filter by user, frequency etc?) |
33 | // |
34 | // return { |
35 | // image: api.blob.sync.url(key), |
36 | // title: topPick.name, |
37 | // // subtitle: topPick.name, |
38 | // value: '![' + topPick.name + '](' + key + ')' |
39 | // } |
40 | // |
41 | return { |
42 | // image: |
43 | title: h('img.meme', { src: api.blob.sync.url(key) }), |
44 | subtitle: topPick.name, |
45 | value: '![' + topPick.name + '](' + key + ')' |
46 | } |
47 | } |
48 | } |
49 |
Built with git-ssb-web