[Glitch] Add autosuggestions for hashtags

Port cfb2ed7823 to glitch-soc

Signed-off-by: Thibaut Girka <thib@sitedethib.com>
This commit is contained in:
Eugen Rochko
2019-07-28 14:37:52 +02:00
committed by ThibG
parent 147e90f35d
commit 3380e96449
6 changed files with 105 additions and 26 deletions

View File

@ -18,7 +18,6 @@ import {
COMPOSE_SUGGESTIONS_CLEAR,
COMPOSE_SUGGESTIONS_READY,
COMPOSE_SUGGESTION_SELECT,
COMPOSE_SUGGESTION_TAGS_UPDATE,
COMPOSE_TAG_HISTORY_UPDATE,
COMPOSE_ADVANCED_OPTIONS_CHANGE,
COMPOSE_SENSITIVITY_CHANGE,
@ -231,15 +230,20 @@ const insertSuggestion = (state, position, token, completion, path) => {
});
};
const updateSuggestionTags = (state, token) => {
const prefix = token.slice(1);
const sortHashtagsByUse = (state, tags) => {
const personalHistory = state.get('tagHistory');
return state.merge({
suggestions: state.get('tagHistory')
.filter(tag => tag.toLowerCase().startsWith(prefix.toLowerCase()))
.slice(0, 4)
.map(tag => '#' + tag),
suggestion_token: token,
return tags.sort((a, b) => {
const usedA = personalHistory.includes(a.name);
const usedB = personalHistory.includes(b.name);
if (usedA === usedB) {
return 0;
} else if (usedA && !usedB) {
return 1;
} else {
return -1;
}
});
};
@ -282,6 +286,16 @@ const expiresInFromExpiresAt = expires_at => {
return [300, 1800, 3600, 21600, 86400, 259200, 604800].find(expires_in => expires_in >= delta) || 24 * 3600;
};
const normalizeSuggestions = (state, { accounts, emojis, tags }) => {
if (accounts) {
return accounts.map(item => item.id);
} else if (emojis) {
return emojis;
} else {
return sortHashtagsByUse(state, tags);
}
};
export default function compose(state = initialState, action) {
switch(action.type) {
case STORE_HYDRATE:
@ -412,11 +426,9 @@ export default function compose(state = initialState, action) {
case COMPOSE_SUGGESTIONS_CLEAR:
return state.update('suggestions', ImmutableList(), list => list.clear()).set('suggestion_token', null);
case COMPOSE_SUGGESTIONS_READY:
return state.set('suggestions', ImmutableList(action.accounts ? action.accounts.map(item => item.id) : action.emojis)).set('suggestion_token', action.token);
return state.set('suggestions', ImmutableList(normalizeSuggestions(state, action))).set('suggestion_token', action.token);
case COMPOSE_SUGGESTION_SELECT:
return insertSuggestion(state, action.position, action.token, action.completion, action.path);
case COMPOSE_SUGGESTION_TAGS_UPDATE:
return updateSuggestionTags(state, action.token);
case COMPOSE_TAG_HISTORY_UPDATE:
return state.set('tagHistory', fromJS(action.tags));
case TIMELINE_DELETE: