git ssb

0+

mixmix / ssb-meme



Tree: 1f83e2637d5e118ea1d1ee4f5b59c81359d321c5

Files: 1f83e2637d5e118ea1d1ee4f5b59c81359d321c5 / index.js

1833 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
8
9module.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
53const imgExtRegEx = /\.(jpg|jpeg|png|gif|bmp|svg)$/i
54const spaceCharRegex = /(-|\.|_|\/|~)/g
55
56function 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
64function 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
70function getAuthor (msg) {
71 return msg.value.author
72}
73

Built with git-ssb-web