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