From 88f32708c3801c5be413641be092db6fcbe5b013 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Wed, 22 Mar 2017 04:09:09 +0100 Subject: Web UI support for the new omnisearch --- .../javascripts/components/actions/search.jsx | 8 +-- .../javascripts/components/reducers/search.jsx | 58 +++++++++++++--------- .../javascripts/components/reducers/statuses.jsx | 2 + 3 files changed, 41 insertions(+), 27 deletions(-) (limited to 'app/assets') diff --git a/app/assets/javascripts/components/actions/search.jsx b/app/assets/javascripts/components/actions/search.jsx index ceb0e4a0c..e4af716ee 100644 --- a/app/assets/javascripts/components/actions/search.jsx +++ b/app/assets/javascripts/components/actions/search.jsx @@ -18,11 +18,13 @@ export function clearSearchSuggestions() { }; }; -export function readySearchSuggestions(value, accounts) { +export function readySearchSuggestions(value, { accounts, hashtags, statuses }) { return { type: SEARCH_SUGGESTIONS_READY, value, - accounts + accounts, + hashtags, + statuses }; }; @@ -32,7 +34,7 @@ export function fetchSearchSuggestions(value) { return; } - api(getState).get('/api/v1/accounts/search', { + api(getState).get('/api/v1/search', { params: { q: value, resolve: true, diff --git a/app/assets/javascripts/components/reducers/search.jsx b/app/assets/javascripts/components/reducers/search.jsx index d835ef268..1767be2c6 100644 --- a/app/assets/javascripts/components/reducers/search.jsx +++ b/app/assets/javascripts/components/reducers/search.jsx @@ -11,28 +11,38 @@ const initialState = Immutable.Map({ suggestions: [] }); -const normalizeSuggestions = (state, value, accounts) => { - let newSuggestions = [ - { +const normalizeSuggestions = (state, value, accounts, hashtags, statuses) => { + let newSuggestions = []; + + if (accounts.length > 0) { + newSuggestions.push({ title: 'account', items: accounts.map(item => ({ type: 'account', id: item.id, value: item.acct })) + }); + } + + if (value.indexOf('@') === -1 && value.indexOf(' ') === -1 || hashtags.length > 0) { + let hashtagItems = hashtags.map(item => ({ + type: 'hashtag', + id: item, + value: `#${item}` + })); + + if (value.indexOf('@') === -1 && value.indexOf(' ') === -1 && hashtags.indexOf(value) === -1) { + hashtagItems.unshift({ + type: 'hashtag', + id: value, + value: `#${value}` + }); } - ]; - if (value.indexOf('@') === -1 && value.indexOf(' ') === -1) { newSuggestions.push({ title: 'hashtag', - items: [ - { - type: 'hashtag', - id: value, - value: `#${value}` - } - ] + items: hashtagItems }); } @@ -44,17 +54,17 @@ const normalizeSuggestions = (state, value, accounts) => { export default function search(state = initialState, action) { switch(action.type) { - case SEARCH_CHANGE: - return state.set('value', action.value); - case SEARCH_SUGGESTIONS_READY: - return normalizeSuggestions(state, action.value, action.accounts); - case SEARCH_RESET: - return state.withMutations(map => { - map.set('suggestions', []); - map.set('value', ''); - map.set('loaded_value', ''); - }); - default: - return state; + case SEARCH_CHANGE: + return state.set('value', action.value); + case SEARCH_SUGGESTIONS_READY: + return normalizeSuggestions(state, action.value, action.accounts, action.hashtags, action.statuses); + case SEARCH_RESET: + return state.withMutations(map => { + map.set('suggestions', []); + map.set('value', ''); + map.set('loaded_value', ''); + }); + default: + return state; } }; diff --git a/app/assets/javascripts/components/reducers/statuses.jsx b/app/assets/javascripts/components/reducers/statuses.jsx index ce791eab6..1669b8c65 100644 --- a/app/assets/javascripts/components/reducers/statuses.jsx +++ b/app/assets/javascripts/components/reducers/statuses.jsx @@ -32,6 +32,7 @@ import { FAVOURITED_STATUSES_FETCH_SUCCESS, FAVOURITED_STATUSES_EXPAND_SUCCESS } from '../actions/favourites'; +import { SEARCH_SUGGESTIONS_READY } from '../actions/search'; import Immutable from 'immutable'; const normalizeStatus = (state, status) => { @@ -108,6 +109,7 @@ export default function statuses(state = initialState, action) { case NOTIFICATIONS_EXPAND_SUCCESS: case FAVOURITED_STATUSES_FETCH_SUCCESS: case FAVOURITED_STATUSES_EXPAND_SUCCESS: + case SEARCH_SUGGESTIONS_READY: return normalizeStatuses(state, action.statuses); case TIMELINE_DELETE: return deleteStatus(state, action.id, action.references); -- cgit From 5aa3df017bcdefd15e041ca0a7e428f85887aff2 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Wed, 22 Mar 2017 17:36:34 +0100 Subject: Fix full-text search query quotation, improve tag search performance with an index, add ability to open status by URL from search (fix #53) --- .../compose/components/autosuggest_account.jsx | 7 ++++++- .../compose/components/autosuggest_status.jsx | 15 +++++++++++++++ .../components/features/compose/components/search.jsx | 9 +++++++-- .../containers/autosuggest_status_container.jsx | 15 +++++++++++++++ .../javascripts/components/reducers/accounts.jsx | 2 +- app/assets/javascripts/components/reducers/search.jsx | 19 ++++++++++++++++--- app/assets/stylesheets/components.scss | 10 ++++++++++ app/models/account.rb | 10 ++++++---- app/models/tag.rb | 5 +++-- app/services/fetch_remote_account_service.rb | 9 +++++++-- app/services/fetch_remote_resource_service.rb | 4 ++-- app/services/fetch_remote_status_service.rb | 9 +++++++-- db/migrate/20170322162804_add_search_index_to_tags.rb | 9 +++++++++ db/schema.rb | 3 ++- 14 files changed, 106 insertions(+), 20 deletions(-) create mode 100644 app/assets/javascripts/components/features/compose/components/autosuggest_status.jsx create mode 100644 app/assets/javascripts/components/features/compose/containers/autosuggest_status_container.jsx create mode 100644 db/migrate/20170322162804_add_search_index_to_tags.rb (limited to 'app/assets') diff --git a/app/assets/javascripts/components/features/compose/components/autosuggest_account.jsx b/app/assets/javascripts/components/features/compose/components/autosuggest_account.jsx index 9ea7f190f..5591b45cf 100644 --- a/app/assets/javascripts/components/features/compose/components/autosuggest_account.jsx +++ b/app/assets/javascripts/components/features/compose/components/autosuggest_account.jsx @@ -1,11 +1,16 @@ import Avatar from '../../../components/avatar'; import DisplayName from '../../../components/display_name'; +import ImmutablePropTypes from 'react-immutable-proptypes'; const AutosuggestAccount = ({ account }) => ( -
+
); +AutosuggestAccount.propTypes = { + account: ImmutablePropTypes.map.isRequired +}; + export default AutosuggestAccount; diff --git a/app/assets/javascripts/components/features/compose/components/autosuggest_status.jsx b/app/assets/javascripts/components/features/compose/components/autosuggest_status.jsx new file mode 100644 index 000000000..086488649 --- /dev/null +++ b/app/assets/javascripts/components/features/compose/components/autosuggest_status.jsx @@ -0,0 +1,15 @@ +import { FormattedMessage } from 'react-intl'; +import DisplayName from '../../../components/display_name'; +import ImmutablePropTypes from 'react-immutable-proptypes'; + +const AutosuggestStatus = ({ status }) => ( +
+ @{status.getIn(['account', 'acct'])} }} /> +
+); + +AutosuggestStatus.propTypes = { + status: ImmutablePropTypes.map.isRequired +}; + +export default AutosuggestStatus; diff --git a/app/assets/javascripts/components/features/compose/components/search.jsx b/app/assets/javascripts/components/features/compose/components/search.jsx index c1f23939d..a0e8f82fb 100644 --- a/app/assets/javascripts/components/features/compose/components/search.jsx +++ b/app/assets/javascripts/components/features/compose/components/search.jsx @@ -2,6 +2,7 @@ import PureRenderMixin from 'react-addons-pure-render-mixin'; import ImmutablePropTypes from 'react-immutable-proptypes'; import Autosuggest from 'react-autosuggest'; import AutosuggestAccountContainer from '../containers/autosuggest_account_container'; +import AutosuggestStatusContainer from '../containers/autosuggest_status_container'; import { debounce } from 'react-decoration'; import { defineMessages, injectIntl, FormattedMessage } from 'react-intl'; @@ -14,8 +15,10 @@ const getSuggestionValue = suggestion => suggestion.value; const renderSuggestion = suggestion => { if (suggestion.type === 'account') { return ; + } else if (suggestion.type === 'hashtag') { + return #{suggestion.id}; } else { - return #{suggestion.id} + return ; } }; @@ -78,8 +81,10 @@ const Search = React.createClass({ onSuggestionSelected (_, { suggestion }) { if (suggestion.type === 'account') { this.context.router.push(`/accounts/${suggestion.id}`); - } else { + } else if(suggestion.type === 'hashtag') { this.context.router.push(`/timelines/tag/${suggestion.id}`); + } else { + this.context.router.push(`/statuses/${suggestion.id}`); } }, diff --git a/app/assets/javascripts/components/features/compose/containers/autosuggest_status_container.jsx b/app/assets/javascripts/components/features/compose/containers/autosuggest_status_container.jsx new file mode 100644 index 000000000..ef46eb09c --- /dev/null +++ b/app/assets/javascripts/components/features/compose/containers/autosuggest_status_container.jsx @@ -0,0 +1,15 @@ +import { connect } from 'react-redux'; +import AutosuggestStatus from '../components/autosuggest_status'; +import { makeGetStatus } from '../../../selectors'; + +const makeMapStateToProps = () => { + const getStatus = makeGetStatus(); + + const mapStateToProps = (state, { id }) => ({ + status: getStatus(state, id) + }); + + return mapStateToProps; +}; + +export default connect(makeMapStateToProps)(AutosuggestStatus); diff --git a/app/assets/javascripts/components/reducers/accounts.jsx b/app/assets/javascripts/components/reducers/accounts.jsx index f3938cee1..6ce41670d 100644 --- a/app/assets/javascripts/components/reducers/accounts.jsx +++ b/app/assets/javascripts/components/reducers/accounts.jsx @@ -90,7 +90,6 @@ export default function accounts(state = initialState, action) { case REBLOGS_FETCH_SUCCESS: case FAVOURITES_FETCH_SUCCESS: case COMPOSE_SUGGESTIONS_READY: - case SEARCH_SUGGESTIONS_READY: case FOLLOW_REQUESTS_FETCH_SUCCESS: case FOLLOW_REQUESTS_EXPAND_SUCCESS: case BLOCKS_FETCH_SUCCESS: @@ -98,6 +97,7 @@ export default function accounts(state = initialState, action) { return normalizeAccounts(state, action.accounts); case NOTIFICATIONS_REFRESH_SUCCESS: case NOTIFICATIONS_EXPAND_SUCCESS: + case SEARCH_SUGGESTIONS_READY: return normalizeAccountsFromStatuses(normalizeAccounts(state, action.accounts), action.statuses); case TIMELINE_REFRESH_SUCCESS: case TIMELINE_EXPAND_SUCCESS: diff --git a/app/assets/javascripts/components/reducers/search.jsx b/app/assets/javascripts/components/reducers/search.jsx index 1767be2c6..e95f9ed79 100644 --- a/app/assets/javascripts/components/reducers/search.jsx +++ b/app/assets/javascripts/components/reducers/search.jsx @@ -32,7 +32,7 @@ const normalizeSuggestions = (state, value, accounts, hashtags, statuses) => { value: `#${item}` })); - if (value.indexOf('@') === -1 && value.indexOf(' ') === -1 && hashtags.indexOf(value) === -1) { + if (value.indexOf('@') === -1 && value.indexOf(' ') === -1 && !value.startsWith('http://') && !value.startsWith('https://') && hashtags.indexOf(value) === -1) { hashtagItems.unshift({ type: 'hashtag', id: value, @@ -40,9 +40,22 @@ const normalizeSuggestions = (state, value, accounts, hashtags, statuses) => { }); } + if (hashtagItems.length > 0) { + newSuggestions.push({ + title: 'hashtag', + items: hashtagItems + }); + } + } + + if (statuses.length > 0) { newSuggestions.push({ - title: 'hashtag', - items: hashtagItems + title: 'status', + items: statuses.map(item => ({ + type: 'status', + id: item.id, + value: item.id + })) }); } diff --git a/app/assets/stylesheets/components.scss b/app/assets/stylesheets/components.scss index 4b1e86aca..057c61f91 100644 --- a/app/assets/stylesheets/components.scss +++ b/app/assets/stylesheets/components.scss @@ -1421,3 +1421,13 @@ button.active i.fa-retweet { } } } + +.autosuggest-status { + overflow: hidden; + white-space: nowrap; + text-overflow: ellipsis; + + strong { + font-weight: 500; + } +} diff --git a/app/models/account.rb b/app/models/account.rb index c0cd2ff64..6968607a2 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -222,8 +222,9 @@ SQL end def search_for(terms, limit = 10) + terms = Arel.sql(connection.quote(terms.gsub(/['?\\:]/, ' '))) textsearch = '(setweight(to_tsvector(\'simple\', accounts.display_name), \'A\') || setweight(to_tsvector(\'simple\', accounts.username), \'B\') || setweight(to_tsvector(\'simple\', coalesce(accounts.domain, \'\')), \'C\'))' - query = 'to_tsquery(\'simple\', \'\'\' \' || ? || \' \'\'\' || \':*\')' + query = 'to_tsquery(\'simple\', \'\'\' \' || ' + terms + ' || \' \'\'\' || \':*\')' sql = <