Commit 02874af884fe19a9d71733abcbfa5dc310ac9b71
add followers, better name ordering
mix irving committed on 1/31/2018, 4:40:17 AMParent: 9f05fffe1423a376666951a479b2f427ba0c4d68
Files changed
about/async/suggest.js | changed |
package.json | changed |
about/async/suggest.js | ||
---|---|---|
@@ -1,5 +1,6 @@ | ||
1 | 1 … | const nest = require('depnest') |
2 … | +const { isFeed } = require('ssb-ref') | |
2 | 3 … | const { h, Struct, map, concat, dictToCollection, computed, lookup, watch, keys, resolve } = require('mutant') |
3 | 4 … | |
4 | 5 … | const KEY_SAMPLE_LENGTH = 10 // includes @ |
5 | 6 … | |
@@ -9,8 +10,9 @@ | ||
9 | 10 … | 'about.obs.groupedValues': 'first', |
10 | 11 … | 'about.obs.name': 'first', |
11 | 12 … | 'about.obs.imageUrl': 'first', |
12 | 13 … | 'contact.obs.following': 'first', |
14 … | + 'contact.obs.followers': 'first', | |
13 | 15 … | 'feed.obs.recent': 'first', |
14 | 16 … | 'keys.sync.id': 'first' |
15 | 17 … | }) |
16 | 18 … | |
@@ -21,22 +23,34 @@ | ||
21 | 23 … | return nest('about.async.suggest', suggestedProfile) |
22 | 24 … | |
23 | 25 … | function suggestedProfile () { |
24 | 26 … | loadSuggestions() |
27 … | + | |
25 | 28 … | return function (word) { |
26 | 29 … | if (!word) return recentSuggestions() |
27 | 30 … | |
28 | - wordLower = word.toLowerCase() | |
31 … | + var wordNormed = normalise(word) | |
29 | 32 … | return suggestions() |
30 | - .filter(item => ~item.title.toLowerCase().indexOf(wordLower)) | |
33 … | + .filter(item => ~normalise(item.title).indexOf(wordNormed)) | |
31 | 34 … | .sort((a, b) => { |
35 … | + // where name is is an exact match | |
36 … | + if (a.title === word) return -1 | |
37 … | + if (b.title === word) return +1 | |
38 … | + | |
39 … | + const normedATitle = normalise(a.title) | |
40 … | + const normedBTitle = normalise(b.title) | |
41 … | + | |
42 … | + // where normalised name is an exact match | |
43 … | + if (normedATitle === wordNormed) return -1 | |
44 … | + if (normedBTitle === wordNormed) return +1 | |
45 … | + | |
32 | 46 … | // where name is matching exactly so far |
33 | 47 … | if (a.title.indexOf(word) === 0) return -1 |
34 | 48 … | if (b.title.indexOf(word) === 0) return +1 |
35 | 49 … | |
36 | 50 … | // where name is matching exactly so far (case insensitive) |
37 | - if (a.title.toLowerCase().indexOf(wordLower) === 0) return -1 | |
38 | - if (b.title.toLowerCase().indexOf(wordLower) === 0) return +1 | |
51 … | + if (normedATitle.indexOf(wordNormed) === 0) return -1 | |
52 … | + if (normedBTitle.indexOf(wordNormed) === 0) return +1 | |
39 | 53 … | }) |
40 | 54 … | .reduce((sofar, match) => { |
41 | 55 … | // prune down to the first instance of each id |
42 | 56 … | // this presumes if you were typing e.g. "dino" you don't need "ahdinosaur" as well |
@@ -54,21 +68,23 @@ | ||
54 | 68 … | |
55 | 69 … | function loadSuggestions () { |
56 | 70 … | if (suggestions) return |
57 | 71 … | |
58 | - var id = api.keys.sync.id() | |
59 | - var following = api.contact.obs.following(id) | |
72 … | + var myId = api.keys.sync.id() | |
73 … | + var following = api.contact.obs.following(myId) | |
74 … | + var followers = api.contact.obs.followers(myId) | |
60 | 75 … | var recentlyUpdated = api.feed.obs.recent() |
61 | - var contacts = computed([following, recentlyUpdated], (a, b) => { | |
76 … | + var contacts = computed([following, followers, recentlyUpdated], (a, b, c) => { | |
62 | 77 … | var result = new Set(a) |
63 | 78 … | b.forEach(item => result.add(item)) |
79 … | + c.forEach(item => result.add(item)) | |
64 | 80 … | |
65 | 81 … | return Array.from(result) |
66 | 82 … | }) |
67 | 83 … | |
68 | 84 … | recentSuggestions = map( |
69 | 85 … | computed(recentlyUpdated, (items) => Array.from(items).slice(0, 10)), |
70 | - toRecentSuggestion, | |
86 … | + buildSuggestion, | |
71 | 87 … | {idle: true} |
72 | 88 … | ) |
73 | 89 … | |
74 | 90 … | const suggestionsRecord = lookup(contacts, contact => { |
@@ -106,16 +122,16 @@ | ||
106 | 122 … | ], |
107 | 123 … | value: mention(name, id), |
108 | 124 … | image: api.about.obs.imageUrl(id), |
109 | 125 … | showBoth: true, |
110 | - _isPrefered: name === myNameForThem | |
126 … | + _isPrefered: normalise(name) === normalise(myNameForThem) | |
111 | 127 … | }) |
112 | 128 … | }) |
113 | 129 … | }) |
114 | 130 … | } |
115 | 131 … | |
116 | - // feeds recentSuggestions | |
117 | - function toRecentSuggestion (id) { | |
132 … | + // used to cobble together additional suggestions | |
133 … | + function buildSuggestion (id) { | |
118 | 134 … | var name = api.about.obs.name(id) |
119 | 135 … | return Struct({ |
120 | 136 … | id, |
121 | 137 … | title: name, |
@@ -126,8 +142,12 @@ | ||
126 | 142 … | }) |
127 | 143 … | } |
128 | 144 … | } |
129 | 145 … | |
146 … | +function normalise (word) { | |
147 … | + return word.toLowerCase().replace(/(\s|-)/g, '') | |
148 … | +} | |
149 … | + | |
130 | 150 … | function mention (name, id) { |
131 | 151 … | return `[@${name}](${id})` |
132 | 152 … | } |
133 | 153 … |
Built with git-ssb-web