Files: 8ea0672560e531ebb37c8697fcd37dad9a96986e / index.js
1778 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 = 1 |
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 | |
27 | pull( |
28 | view.query(opts), |
29 | pull.collect((err, data) => { |
30 | if (err) return cb(err) |
31 | |
32 | const result = data.reduce((soFar, msg) => { |
33 | getMentions(msg) |
34 | .filter(isBlobMention) |
35 | .filter(m => m.name.indexOf(opts.query) > -1) // only mentions relevant to the query |
36 | .forEach(({ link, name }) => { |
37 | if (!soFar[link]) soFar[link] = [] |
38 | |
39 | soFar[link].push({ name, author: getAuthor(msg), msg: msg.key }) |
40 | }) |
41 | |
42 | return soFar |
43 | }, {}) |
44 | |
45 | cb(null, result) |
46 | }) |
47 | ) |
48 | } |
49 | } |
50 | } |
51 | |
52 | const imgExtRegEx = /\.(jpg|jpeg|png|gif|bmp|svg)$/i |
53 | const spaceCharRegex = /(-|\.|_|\/|~)/g |
54 | |
55 | function map (msg) { |
56 | return getMentions(msg) |
57 | .filter(isBlobMention) |
58 | // .map(m => m.name) |
59 | .map(n => n.replace(imgExtRegEx, '').replace(spaceCharRegex, ' ')) |
60 | .join(' ') |
61 | } |
62 | |
63 | function getMentions (msg) { |
64 | if (!msg.value.content.mentions) return [] |
65 | else if (!Array.isArray(msg.value.content.mentions)) return [msg.value.content.mentions] |
66 | else return msg.value.content.mentions |
67 | } |
68 | |
69 | function getAuthor (msg) { |
70 | return msg.value.author |
71 | } |
72 |
Built with git-ssb-web