git ssb

0+

mixmix / patch-suggest



Commit 02874af884fe19a9d71733abcbfa5dc310ac9b71

add followers, better name ordering

mix irving committed on 1/31/2018, 4:40:17 AM
Parent: 9f05fffe1423a376666951a479b2f427ba0c4d68

Files changed

about/async/suggest.jschanged
package.jsonchanged
about/async/suggest.jsView
@@ -1,5 +1,6 @@
11 const nest = require('depnest')
2 +const { isFeed } = require('ssb-ref')
23 const { h, Struct, map, concat, dictToCollection, computed, lookup, watch, keys, resolve } = require('mutant')
34
45 const KEY_SAMPLE_LENGTH = 10 // includes @
56
@@ -9,8 +10,9 @@
910 'about.obs.groupedValues': 'first',
1011 'about.obs.name': 'first',
1112 'about.obs.imageUrl': 'first',
1213 'contact.obs.following': 'first',
14 + 'contact.obs.followers': 'first',
1315 'feed.obs.recent': 'first',
1416 'keys.sync.id': 'first'
1517 })
1618
@@ -21,22 +23,34 @@
2123 return nest('about.async.suggest', suggestedProfile)
2224
2325 function suggestedProfile () {
2426 loadSuggestions()
27 +
2528 return function (word) {
2629 if (!word) return recentSuggestions()
2730
28- wordLower = word.toLowerCase()
31 + var wordNormed = normalise(word)
2932 return suggestions()
30- .filter(item => ~item.title.toLowerCase().indexOf(wordLower))
33 + .filter(item => ~normalise(item.title).indexOf(wordNormed))
3134 .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 +
3246 // where name is matching exactly so far
3347 if (a.title.indexOf(word) === 0) return -1
3448 if (b.title.indexOf(word) === 0) return +1
3549
3650 // 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
3953 })
4054 .reduce((sofar, match) => {
4155 // prune down to the first instance of each id
4256 // this presumes if you were typing e.g. "dino" you don't need "ahdinosaur" as well
@@ -54,21 +68,23 @@
5468
5569 function loadSuggestions () {
5670 if (suggestions) return
5771
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)
6075 var recentlyUpdated = api.feed.obs.recent()
61- var contacts = computed([following, recentlyUpdated], (a, b) => {
76 + var contacts = computed([following, followers, recentlyUpdated], (a, b, c) => {
6277 var result = new Set(a)
6378 b.forEach(item => result.add(item))
79 + c.forEach(item => result.add(item))
6480
6581 return Array.from(result)
6682 })
6783
6884 recentSuggestions = map(
6985 computed(recentlyUpdated, (items) => Array.from(items).slice(0, 10)),
70- toRecentSuggestion,
86 + buildSuggestion,
7187 {idle: true}
7288 )
7389
7490 const suggestionsRecord = lookup(contacts, contact => {
@@ -106,16 +122,16 @@
106122 ],
107123 value: mention(name, id),
108124 image: api.about.obs.imageUrl(id),
109125 showBoth: true,
110- _isPrefered: name === myNameForThem
126 + _isPrefered: normalise(name) === normalise(myNameForThem)
111127 })
112128 })
113129 })
114130 }
115131
116- // feeds recentSuggestions
117- function toRecentSuggestion (id) {
132 + // used to cobble together additional suggestions
133 + function buildSuggestion (id) {
118134 var name = api.about.obs.name(id)
119135 return Struct({
120136 id,
121137 title: name,
@@ -126,8 +142,12 @@
126142 })
127143 }
128144 }
129145
146 +function normalise (word) {
147 + return word.toLowerCase().replace(/(\s|-)/g, '')
148 +}
149 +
130150 function mention (name, id) {
131151 return `[@${name}](${id})`
132152 }
133153
package.jsonView
@@ -22,7 +22,8 @@
2222 },
2323 "homepage": "https://github.com/mixmix/patch-suggest",
2424 "dependencies": {
2525 "depnest": "^1.3.0",
26- "mutant": "^3.21.2"
26 + "mutant": "^3.21.2",
27 + "ssb-ref": "^2.9.1"
2728 }
2829 }

Built with git-ssb-web