git ssb

0+

mixmix / ssb-meme



Tree: b848b4be1ba7f602add5be6e000f787eaee31a77

Files: b848b4be1ba7f602add5be6e000f787eaee31a77 / index.js

2060 bytesRaw
1const FlumeView = require('flumeview-search')
2const pull = require('pull-stream')
3const Validator = require('is-my-json-valid')
4
5const isBlobMention = Validator(require('./schema/mention'))
6
7const INDEX_VERSION = 2
8const SEARCH_TERM_MIN = 3
9
10module.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
60const imgExtRegEx = /\.(jpg|jpeg|png|gif|bmp|svg)$/i
61const spaceCharRegex = /(-|\.|_|\/|~)/g
62
63function 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
71function 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
77function getAuthor (msg) {
78 return msg.value.author
79}
80

Built with git-ssb-web