Commit 06b7d90bbe07c6d38c6ad24e196ba7d3d08108ec
allow search term to be a phrase
mix irving committed on 5/28/2018, 10:44:59 PMParent: 1f83e2637d5e118ea1d1ee4f5b59c81359d321c5
Files changed
index.js | changed |
index.js | ||
---|---|---|
@@ -4,8 +4,9 @@ | ||
4 | 4 … | |
5 | 5 … | const isBlobMention = Validator(require('./schema/mention')) |
6 | 6 … | |
7 | 7 … | const INDEX_VERSION = 2 |
8 … | +const SEARCH_TERM_MIN = 3 | |
8 | 9 … | |
9 | 10 … | module.exports = { |
10 | 11 … | name: 'meme', |
11 | 12 … | version: require('./package.json').version, |
@@ -13,9 +14,9 @@ | ||
13 | 14 … | query: 'source', |
14 | 15 … | search: 'async' |
15 | 16 … | }, |
16 | 17 … | init: (sbot) => { |
17 | - const view = sbot._flumeUse('meme', FlumeView(INDEX_VERSION, 3, map)) | |
18 … | + const view = sbot._flumeUse('meme', FlumeView(INDEX_VERSION, SEARCH_TERM_MIN, map)) | |
18 | 19 … | |
19 | 20 … | return { |
20 | 21 … | query: view.query, |
21 | 22 … | search |
@@ -23,8 +24,9 @@ | ||
23 | 24 … | |
24 | 25 … | function search (opts, cb) { |
25 | 26 … | if (typeof opts === 'string') opts = { query: opts } |
26 | 27 … | opts.query = opts.query.toLowerCase() |
28 … | + const validTerms = opts.query.split(' ').filter(s => s.length >= SEARCH_TERM_MIN) | |
27 | 29 … | |
28 | 30 … | pull( |
29 | 31 … | view.query(opts), |
30 | 32 … | pull.collect((err, data) => { |
@@ -32,9 +34,9 @@ | ||
32 | 34 … | |
33 | 35 … | const result = data.reduce((soFar, msg) => { |
34 | 36 … | getMentions(msg) |
35 | 37 … | .filter(isBlobMention) |
36 | - .filter(m => m.name.toLowerCase().indexOf(opts.query) > -1) // only mentions relevant to the query | |
38 … | + .filter(containsSearchTerms) | |
37 | 39 … | .forEach(({ link, name }) => { |
38 | 40 … | if (!soFar[link]) soFar[link] = [] |
39 | 41 … | |
40 | 42 … | soFar[link].push({ name, author: getAuthor(msg), msg: msg.key }) |
@@ -45,8 +47,13 @@ | ||
45 | 47 … | |
46 | 48 … | cb(null, result) |
47 | 49 … | }) |
48 | 50 … | ) |
51 … | + | |
52 … | + function containsSearchTerms (mention) { | |
53 … | + const name = mention.name.toLowerCase() | |
54 … | + return validTerms.every(term => name.indexOf(term) > -1) | |
55 … | + } | |
49 | 56 … | } |
50 | 57 … | } |
51 | 58 … | } |
52 | 59 … |
Built with git-ssb-web