git ssb

0+

mixmix / patch-suggest



Commit 6b51ef76387825aa524424f71d8d722fa7eba2cf

add ability to extend the about suggester by adding extra feed ids

mix irving committed on 1/31/2018, 11:01:27 AM
Parent: 02874af884fe19a9d71733abcbfa5dc310ac9b71

Files changed

README.mdchanged
about/async/suggest.jschanged
README.mdView
@@ -29,9 +29,9 @@
2929 function page (location) {
3030
3131 var textArea = h('textarea')
3232
33- var getProfileSuggestions = api.about.async.suggest()
33 + var getUserSuggestions = api.about.async.suggest()
3434 var getChannelSuggestions = api.channel.async.suggest()
3535 var getEmojiSuggestions = api.emoji.async.suggest()
3636
3737 suggestBox(
@@ -39,9 +39,9 @@
3939 (inputText, cb) => {
4040 const char = inputText[0]
4141 const wordFragment = inputText.slice(1)
4242
43- if (char === '@') cb(null, getProfileSuggestions(wordFragment))
43 + if (char === '@') cb(null, getUserSuggestions(wordFragment))
4444 if (char === '#') cb(null, getChannelSuggestions(wordFragment))
4545 if (char === ':') cb(null, getEmojiSuggestions(wordFragment))
4646 },
4747 {cls: 'PatchSuggest'}
@@ -51,8 +51,27 @@
5151 }
5252 }
5353 ```
5454
55 +## API
56 +
57 +Each of these depject methods is called to initialize it (starts pre-loading data in the pbackground) and returns a "suggester".
58 +The suggester functions return "suggestion" objects which are compatible with the `suggest-box` module, but you can use them for whatever!
59 +
60 +### `api.about.async.suggest() => suggester(word, extraIds)`
61 +
62 +`suggester` returns suggestions for users based on your followers, following, and people who've recently said things.
63 +
64 +`extraIds` (optional) you can add an _Array_ (or _MutantArray_) of FeedIds which you would like included in the suggestions. This is a great way to add the context (i.e. the people in the conversation) of a the current page you're in to the suggestions.
65 +
66 +### `api.channel.async.suggest() => suggester(word)`
67 +
68 +`suggester` returns suggestions for channels
69 +
70 +### `api.emoji.async.suggest() => suggester(word)`
71 +
72 +`suggester` returns suggestions for emojis
73 +
5574 ## Styling
5675
5776 To use the styles provided by patch-suggest (recommended), supply the options argument to suggest-box `{cls: 'PatchSuggest}` (see example above)
5877
about/async/suggest.jsView
@@ -10,33 +10,38 @@
1010 'about.obs.groupedValues': 'first',
1111 'about.obs.name': 'first',
1212 'about.obs.imageUrl': 'first',
1313 'contact.obs.following': 'first',
14- 'contact.obs.followers': 'first',
1514 'feed.obs.recent': 'first',
1615 'keys.sync.id': 'first'
1716 })
1817
1918 exports.create = function (api) {
19 + var recentSuggestions = null
2020 var suggestions = null
21- var recentSuggestions = null
2221
2322 return nest('about.async.suggest', suggestedProfile)
2423
2524 function suggestedProfile () {
2625 loadSuggestions()
2726
28- return function (word) {
29- if (!word) return recentSuggestions()
27 + return function (word, extraIds = []) {
28 + var moreSuggestions = buildSuggestions(extraIds)
3029
30 + if (!word && extraIds.length) return resolve(moreSuggestions)
31 + if (!word) return resolve(recentSuggestions)
32 +
3133 var wordNormed = normalise(word)
34 +
3235 return suggestions()
36 + .concat(resolve(moreSuggestions))
3337 .filter(item => ~normalise(item.title).indexOf(wordNormed))
3438 .sort((a, b) => {
3539 // where name is is an exact match
3640 if (a.title === word) return -1
3741 if (b.title === word) return +1
3842
43 + // TODO - move all this into the suggestion building and decorate the suggestion?
3944 const normedATitle = normalise(a.title)
4045 const normedBTitle = normalise(b.title)
4146
4247 // where normalised name is an exact match
@@ -65,38 +70,41 @@
6570 })
6671 }
6772 }
6873
74 +
75 + //// PRIVATE ////
76 +
6977 function loadSuggestions () {
7078 if (suggestions) return
7179
7280 var myId = api.keys.sync.id()
7381 var following = api.contact.obs.following(myId)
74- var followers = api.contact.obs.followers(myId)
7582 var recentlyUpdated = api.feed.obs.recent()
76- var contacts = computed([following, followers, recentlyUpdated], (a, b, c) => {
77- var result = new Set(a)
78- b.forEach(item => result.add(item))
79- c.forEach(item => result.add(item))
8083
81- return Array.from(result)
82- })
83-
8484 recentSuggestions = map(
8585 computed(recentlyUpdated, (items) => Array.from(items).slice(0, 10)),
8686 buildSuggestion,
8787 {idle: true}
8888 )
89 + watch(recentSuggestions)
8990
91 + var contacts = computed([following, recentlyUpdated], (a, b) => {
92 + var result = new Set(a)
93 + b.forEach(item => result.add(item))
94 +
95 + return Array.from(result)
96 + })
9097 const suggestionsRecord = lookup(contacts, contact => {
9198 return [contact, keys(api.about.obs.groupedValues(contact, 'name'))]
9299 })
93-
94100 suggestions = concat(
95- map(dictToCollection(suggestionsRecord), pluralSuggestions, {idle: true})
101 + map(
102 + dictToCollection(suggestionsRecord),
103 + pluralSuggestions,
104 + {idle: true}
105 + )
96106 )
97-
98- watch(recentSuggestions)
99107 watch(suggestions)
100108 }
101109
102110 function pluralSuggestions (item) {
@@ -128,12 +136,27 @@
128136 })
129137 })
130138 }
131139
140 + function buildSuggestions (idsObs) {
141 + return concat([ // (mix) urg, I don't know why this is needed, but it makes it behave the same as asuggestions - when you resolve this it resolves the whole structure
142 + computed([idsObs], ids => { // NOTE [] is needed here
143 + return ids
144 + .filter(isFeed)
145 + .reduce((sofar, feedId) => {
146 + if (sofar.includes(feedId)) return sofar
147 + return [...sofar, feedId]
148 + }, [])
149 + .map(buildSuggestion)
150 + })
151 + ])
152 + }
153 +
132154 // used to cobble together additional suggestions
133155 function buildSuggestion (id) {
134156 var name = api.about.obs.name(id)
135157 return Struct({
158 + // return {
136159 id,
137160 title: name,
138161 subtitle: h('div.key', id.substring(0, KEY_SAMPLE_LENGTH)),
139162 value: computed([name, id], mention),
@@ -143,9 +166,11 @@
143166 }
144167 }
145168
146169 function normalise (word) {
147- return word.toLowerCase().replace(/(\s|-)/g, '')
170 + // TODO - this shouldn't need a reslve.
171 + // It's generated by buildSuggestion title, but not pluralSuggestions title
172 + return resolve(word).toLowerCase().replace(/(\s|-)/g, '')
148173 }
149174
150175 function mention (name, id) {
151176 return `[@${name}](${id})`

Built with git-ssb-web