[Glitch] Restore hashtag suggestions from local tag history
Port 5ab1e0e738
to glitch-soc
Signed-off-by: Thibaut Girka <thib@sitedethib.com>
pull/1210/head
parent
e1810eed8e
commit
12c188f533
|
@ -383,6 +383,8 @@ const fetchComposeSuggestionsTags = throttle((dispatch, getState, token) => {
|
||||||
cancelFetchComposeSuggestionsTags();
|
cancelFetchComposeSuggestionsTags();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dispatch(updateSuggestionTags(token));
|
||||||
|
|
||||||
api(getState).get('/api/v2/search', {
|
api(getState).get('/api/v2/search', {
|
||||||
cancelToken: new CancelToken(cancel => {
|
cancelToken: new CancelToken(cancel => {
|
||||||
cancelFetchComposeSuggestionsTags = cancel;
|
cancelFetchComposeSuggestionsTags = cancel;
|
||||||
|
|
|
@ -9,18 +9,18 @@ export default class AutosuggestHashtag extends React.PureComponent {
|
||||||
tag: PropTypes.shape({
|
tag: PropTypes.shape({
|
||||||
name: PropTypes.string.isRequired,
|
name: PropTypes.string.isRequired,
|
||||||
url: PropTypes.string,
|
url: PropTypes.string,
|
||||||
history: PropTypes.array.isRequired,
|
history: PropTypes.array,
|
||||||
}).isRequired,
|
}).isRequired,
|
||||||
};
|
};
|
||||||
|
|
||||||
render () {
|
render () {
|
||||||
const { tag } = this.props;
|
const { tag } = this.props;
|
||||||
const weeklyUses = shortNumberFormat(tag.history.reduce((total, day) => total + (day.uses * 1), 0));
|
const weeklyUses = tag.history && shortNumberFormat(tag.history.reduce((total, day) => total + (day.uses * 1), 0));
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className='autosuggest-hashtag'>
|
<div className='autosuggest-hashtag'>
|
||||||
<div className='autosuggest-hashtag__name'>#<strong>{tag.name}</strong></div>
|
<div className='autosuggest-hashtag__name'>#<strong>{tag.name}</strong></div>
|
||||||
<div className='autosuggest-hashtag__uses'><FormattedMessage id='autosuggest_hashtag.per_week' defaultMessage='{count} per week' values={{ count: weeklyUses }} /></div>
|
{tag.history !== undefined && <div className='autosuggest-hashtag__uses'><FormattedMessage id='autosuggest_hashtag.per_week' defaultMessage='{count} per week' values={{ count: weeklyUses }} /></div>}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@ import {
|
||||||
COMPOSE_SUGGESTIONS_CLEAR,
|
COMPOSE_SUGGESTIONS_CLEAR,
|
||||||
COMPOSE_SUGGESTIONS_READY,
|
COMPOSE_SUGGESTIONS_READY,
|
||||||
COMPOSE_SUGGESTION_SELECT,
|
COMPOSE_SUGGESTION_SELECT,
|
||||||
|
COMPOSE_SUGGESTION_TAGS_UPDATE,
|
||||||
COMPOSE_TAG_HISTORY_UPDATE,
|
COMPOSE_TAG_HISTORY_UPDATE,
|
||||||
COMPOSE_ADVANCED_OPTIONS_CHANGE,
|
COMPOSE_ADVANCED_OPTIONS_CHANGE,
|
||||||
COMPOSE_SENSITIVITY_CHANGE,
|
COMPOSE_SENSITIVITY_CHANGE,
|
||||||
|
@ -286,16 +287,36 @@ const expiresInFromExpiresAt = expires_at => {
|
||||||
return [300, 1800, 3600, 21600, 86400, 259200, 604800].find(expires_in => expires_in >= delta) || 24 * 3600;
|
return [300, 1800, 3600, 21600, 86400, 259200, 604800].find(expires_in => expires_in >= delta) || 24 * 3600;
|
||||||
};
|
};
|
||||||
|
|
||||||
const normalizeSuggestions = (state, { accounts, emojis, tags }) => {
|
const mergeLocalHashtagResults = (suggestions, prefix, tagHistory) => {
|
||||||
|
prefix = prefix.toLowerCase();
|
||||||
|
if (suggestions.length < 4) {
|
||||||
|
const localTags = tagHistory.filter(tag => tag.toLowerCase().startsWith(prefix) && !suggestions.some(suggestion => suggestion.type === 'hashtag' && suggestion.name.toLowerCase() === tag.toLowerCase()));
|
||||||
|
return suggestions.concat(localTags.slice(0, 4 - suggestions.length).toJS().map(tag => ({ type: 'hashtag', name: tag })));
|
||||||
|
} else {
|
||||||
|
return suggestions;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const normalizeSuggestions = (state, { accounts, emojis, tags, token }) => {
|
||||||
if (accounts) {
|
if (accounts) {
|
||||||
return accounts.map(item => ({ id: item.id, type: 'account' }));
|
return accounts.map(item => ({ id: item.id, type: 'account' }));
|
||||||
} else if (emojis) {
|
} else if (emojis) {
|
||||||
return emojis.map(item => ({ ...item, type: 'emoji' }));
|
return emojis.map(item => ({ ...item, type: 'emoji' }));
|
||||||
} else {
|
} else {
|
||||||
return sortHashtagsByUse(state, tags.map(item => ({ ...item, type: 'hashtag' })));
|
return mergeLocalHashtagResults(sortHashtagsByUse(state, tags.map(item => ({ ...item, type: 'hashtag' }))), token.slice(1), state.get('tagHistory'));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const updateSuggestionTags = (state, token) => {
|
||||||
|
const prefix = token.slice(1);
|
||||||
|
|
||||||
|
const suggestions = state.get('suggestions').toJS();
|
||||||
|
return state.merge({
|
||||||
|
suggestions: ImmutableList(mergeLocalHashtagResults(suggestions, prefix, state.get('tagHistory'))),
|
||||||
|
suggestion_token: token,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
export default function compose(state = initialState, action) {
|
export default function compose(state = initialState, action) {
|
||||||
switch(action.type) {
|
switch(action.type) {
|
||||||
case STORE_HYDRATE:
|
case STORE_HYDRATE:
|
||||||
|
@ -429,6 +450,8 @@ export default function compose(state = initialState, action) {
|
||||||
return state.set('suggestions', ImmutableList(normalizeSuggestions(state, action))).set('suggestion_token', action.token);
|
return state.set('suggestions', ImmutableList(normalizeSuggestions(state, action))).set('suggestion_token', action.token);
|
||||||
case COMPOSE_SUGGESTION_SELECT:
|
case COMPOSE_SUGGESTION_SELECT:
|
||||||
return insertSuggestion(state, action.position, action.token, action.completion, action.path);
|
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:
|
case COMPOSE_TAG_HISTORY_UPDATE:
|
||||||
return state.set('tagHistory', fromJS(action.tags));
|
return state.set('tagHistory', fromJS(action.tags));
|
||||||
case TIMELINE_DELETE:
|
case TIMELINE_DELETE:
|
||||||
|
|
Loading…
Reference in New Issue