2019-06-11 21:18:09 +03:00
|
|
|
/**
|
|
|
|
* suggest - generates a suggestor function to be used by emoji-input
|
|
|
|
* data: object providing source information for specific types of suggestions:
|
|
|
|
* data.emoji - optional, an array of all emoji available i.e.
|
2022-01-08 16:55:00 -05:00
|
|
|
* (getters.standardEmojiList + state.instance.customEmoji)
|
2019-06-11 21:18:09 +03:00
|
|
|
* data.users - optional, an array of all known users
|
2019-07-18 03:40:02 +00:00
|
|
|
* updateUsersList - optional, a function to search and append to users
|
2019-06-11 21:18:09 +03:00
|
|
|
*
|
|
|
|
* Depending on data present one or both (or none) can be present, so if field
|
|
|
|
* doesn't support user linking you can just provide only emoji.
|
|
|
|
*/
|
2019-07-18 03:40:02 +00:00
|
|
|
|
2020-11-17 17:46:26 +02:00
|
|
|
export default data => {
|
|
|
|
const emojiCurry = suggestEmoji(data.emoji)
|
2020-11-18 18:43:24 +02:00
|
|
|
const usersCurry = data.store && suggestUsers(data.store)
|
2022-09-21 23:16:33 -04:00
|
|
|
return (input, nameKeywordLocalizer) => {
|
2020-11-17 17:46:26 +02:00
|
|
|
const firstChar = input[0]
|
|
|
|
if (firstChar === ':' && data.emoji) {
|
2022-09-21 23:16:33 -04:00
|
|
|
return emojiCurry(input, nameKeywordLocalizer)
|
2020-11-17 17:46:26 +02:00
|
|
|
}
|
2020-11-18 18:43:24 +02:00
|
|
|
if (firstChar === '@' && usersCurry) {
|
2020-11-17 17:46:26 +02:00
|
|
|
return usersCurry(input)
|
|
|
|
}
|
|
|
|
return []
|
2019-06-18 22:13:03 +03:00
|
|
|
}
|
2019-06-07 00:17:49 +03:00
|
|
|
}
|
|
|
|
|
2022-09-21 23:16:33 -04:00
|
|
|
export const suggestEmoji = emojis => (input, nameKeywordLocalizer) => {
|
2019-06-18 21:30:35 +03:00
|
|
|
const noPrefix = input.toLowerCase().substr(1)
|
|
|
|
return emojis
|
2022-09-21 23:16:33 -04:00
|
|
|
.map(emoji => ({ ...emoji, ...nameKeywordLocalizer(emoji) }))
|
|
|
|
.filter((emoji) => (emoji.names.concat(emoji.keywords)).filter(kw => kw.toLowerCase().match(noPrefix)).length)
|
|
|
|
.map(k => {
|
|
|
|
let score = 0
|
2019-06-09 20:41:12 +03:00
|
|
|
|
2020-02-10 09:32:07 -05:00
|
|
|
// An exact match always wins
|
2022-09-21 23:16:33 -04:00
|
|
|
score += Math.max(...k.names.map(name => name.toLowerCase() === noPrefix ? 200 : 0), 0)
|
2020-02-10 09:32:07 -05:00
|
|
|
|
|
|
|
// Prioritize custom emoji a lot
|
2022-09-21 23:16:33 -04:00
|
|
|
score += k.imageUrl ? 100 : 0
|
2020-02-10 09:32:07 -05:00
|
|
|
|
|
|
|
// Prioritize prefix matches somewhat
|
2022-09-21 23:16:33 -04:00
|
|
|
score += Math.max(...k.names.map(kw => kw.toLowerCase().startsWith(noPrefix) ? 10 : 0), 0)
|
2019-06-09 20:41:12 +03:00
|
|
|
|
2020-02-09 17:25:24 -05:00
|
|
|
// Sort by length
|
2022-09-21 23:16:33 -04:00
|
|
|
score -= k.displayText.length
|
2020-02-09 17:25:24 -05:00
|
|
|
|
2022-09-21 23:16:33 -04:00
|
|
|
k.score = score
|
|
|
|
return k
|
|
|
|
})
|
|
|
|
.sort((a, b) => {
|
2020-02-09 17:25:24 -05:00
|
|
|
// Break ties alphabetically
|
|
|
|
const alphabetically = a.displayText > b.displayText ? 0.5 : -0.5
|
2019-06-09 20:41:12 +03:00
|
|
|
|
2022-09-21 23:16:33 -04:00
|
|
|
return b.score - a.score + alphabetically
|
2019-06-18 21:30:35 +03:00
|
|
|
})
|
2019-06-07 00:17:49 +03:00
|
|
|
}
|
|
|
|
|
2020-11-18 18:43:24 +02:00
|
|
|
export const suggestUsers = ({ dispatch, state }) => {
|
2020-11-19 12:35:21 +02:00
|
|
|
// Keep some persistent values in closure, most importantly for the
|
|
|
|
// custom debounce to work. Lodash debounce does not return a promise.
|
2020-11-17 17:46:26 +02:00
|
|
|
let suggestions = []
|
|
|
|
let previousQuery = ''
|
|
|
|
let timeout = null
|
2020-11-18 18:43:24 +02:00
|
|
|
let cancelUserSearch = null
|
2020-11-17 17:46:26 +02:00
|
|
|
|
2020-11-18 18:43:24 +02:00
|
|
|
const userSearch = (query) => dispatch('searchUsers', { query })
|
2020-11-17 17:46:26 +02:00
|
|
|
const debounceUserSearch = (query) => {
|
2020-11-18 18:43:24 +02:00
|
|
|
cancelUserSearch && cancelUserSearch()
|
2020-11-17 17:46:26 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
timeout = setTimeout(() => {
|
|
|
|
userSearch(query).then(resolve).catch(reject)
|
|
|
|
}, 300)
|
2020-11-18 18:43:24 +02:00
|
|
|
cancelUserSearch = () => {
|
|
|
|
clearTimeout(timeout)
|
|
|
|
resolve([])
|
|
|
|
}
|
2020-11-17 17:46:26 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return async input => {
|
|
|
|
const noPrefix = input.toLowerCase().substr(1)
|
|
|
|
if (previousQuery === noPrefix) return suggestions
|
|
|
|
|
|
|
|
suggestions = []
|
|
|
|
previousQuery = noPrefix
|
2020-11-18 18:43:24 +02:00
|
|
|
// Fetch more and wait, don't fetch if there's the 2nd @ because
|
|
|
|
// the backend user search can't deal with it.
|
|
|
|
// Reference semantics make it so that we get the updated data after
|
|
|
|
// the await.
|
|
|
|
if (!noPrefix.includes('@')) {
|
|
|
|
await debounceUserSearch(noPrefix)
|
|
|
|
}
|
2020-11-17 17:46:26 +02:00
|
|
|
|
2020-11-18 18:43:24 +02:00
|
|
|
const newSuggestions = state.users.users.filter(
|
2020-11-17 17:46:26 +02:00
|
|
|
user =>
|
|
|
|
user.screen_name.toLowerCase().startsWith(noPrefix) ||
|
|
|
|
user.name.toLowerCase().startsWith(noPrefix)
|
|
|
|
).slice(0, 20).sort((a, b) => {
|
|
|
|
let aScore = 0
|
|
|
|
let bScore = 0
|
|
|
|
|
|
|
|
// Matches on screen name (i.e. user@instance) makes a priority
|
|
|
|
aScore += a.screen_name.toLowerCase().startsWith(noPrefix) ? 2 : 0
|
|
|
|
bScore += b.screen_name.toLowerCase().startsWith(noPrefix) ? 2 : 0
|
|
|
|
|
|
|
|
// Matches on name takes second priority
|
|
|
|
aScore += a.name.toLowerCase().startsWith(noPrefix) ? 1 : 0
|
|
|
|
bScore += b.name.toLowerCase().startsWith(noPrefix) ? 1 : 0
|
|
|
|
|
|
|
|
const diff = (bScore - aScore) * 10
|
|
|
|
|
|
|
|
// Then sort alphabetically
|
|
|
|
const nameAlphabetically = a.name > b.name ? 1 : -1
|
|
|
|
const screenNameAlphabetically = a.screen_name > b.screen_name ? 1 : -1
|
|
|
|
|
|
|
|
return diff + nameAlphabetically + screenNameAlphabetically
|
|
|
|
/* eslint-disable camelcase */
|
2022-08-29 18:46:41 -04:00
|
|
|
}).map((user) => ({
|
|
|
|
user,
|
|
|
|
displayText: user.screen_name_ui,
|
|
|
|
detailText: user.name,
|
|
|
|
imageUrl: user.profile_image_url_original,
|
|
|
|
replacement: '@' + user.screen_name + ' '
|
2020-11-17 17:46:26 +02:00
|
|
|
}))
|
2020-11-18 18:43:24 +02:00
|
|
|
/* eslint-enable camelcase */
|
2020-11-17 17:46:26 +02:00
|
|
|
|
2020-11-18 18:43:24 +02:00
|
|
|
suggestions = newSuggestions || []
|
2020-11-17 17:46:26 +02:00
|
|
|
return suggestions
|
2019-07-18 03:40:02 +00:00
|
|
|
}
|
2019-06-07 00:17:49 +03:00
|
|
|
}
|