Files: b3f98f5d9fd193405803a810ea3664cb029a8b24 / index.js
1833 bytesRaw
1 | const FlumeView = require('flumeview-search') |
2 | const pull = require('pull-stream') |
3 | const Validator = require('is-my-json-valid') |
4 | |
5 | const isBlobMention = Validator(require('./schema/mention')) |
6 | |
7 | const INDEX_VERSION = 2 |
8 | |
9 | module.exports = { |
10 | name: 'meme', |
11 | version: require('./package.json').version, |
12 | manifest: { |
13 | query: 'source', |
14 | search: 'async' |
15 | }, |
16 | init: (sbot) => { |
17 | const view = sbot._flumeUse('meme', FlumeView(INDEX_VERSION, 3, map)) |
18 | |
19 | return { |
20 | query: view.query, |
21 | search |
22 | } |
23 | |
24 | function search (opts, cb) { |
25 | if (typeof opts === 'string') opts = { query: opts } |
26 | opts.query = opts.query.toLowerCase() |
27 | |
28 | pull( |
29 | view.query(opts), |
30 | pull.collect((err, data) => { |
31 | if (err) return cb(err) |
32 | |
33 | const result = data.reduce((soFar, msg) => { |
34 | getMentions(msg) |
35 | .filter(isBlobMention) |
36 | .filter(m => m.name.toLowerCase().indexOf(opts.query) > -1) // only mentions relevant to the query |
37 | .forEach(({ link, name }) => { |
38 | if (!soFar[link]) soFar[link] = [] |
39 | |
40 | soFar[link].push({ name, author: getAuthor(msg), msg: msg.key }) |
41 | }) |
42 | |
43 | return soFar |
44 | }, {}) |
45 | |
46 | cb(null, result) |
47 | }) |
48 | ) |
49 | } |
50 | } |
51 | } |
52 | |
53 | const imgExtRegEx = /\.(jpg|jpeg|png|gif|bmp|svg)$/i |
54 | const spaceCharRegex = /(-|\.|_|\/|~)/g |
55 | |
56 | function map (msg) { |
57 | return getMentions(msg) |
58 | .filter(isBlobMention) |
59 | .map(m => m.name) |
60 | .map(n => n.replace(imgExtRegEx, '').replace(spaceCharRegex, ' ')) |
61 | .join(' ') |
62 | } |
63 | |
64 | function getMentions (msg) { |
65 | if (!msg.value.content.mentions) return [] |
66 | else if (!Array.isArray(msg.value.content.mentions)) return [msg.value.content.mentions] |
67 | else return msg.value.content.mentions |
68 | } |
69 | |
70 | function getAuthor (msg) { |
71 | return msg.value.author |
72 | } |
73 |
Built with git-ssb-web