forked from fedi/mastodon
Fix full-text search query quotation, improve tag search performance with an index,
add ability to open status by URL from search (fix #53)
This commit is contained in:
parent
c89ccbab09
commit
5aa3df017b
|
@ -1,11 +1,16 @@
|
||||||
import Avatar from '../../../components/avatar';
|
import Avatar from '../../../components/avatar';
|
||||||
import DisplayName from '../../../components/display_name';
|
import DisplayName from '../../../components/display_name';
|
||||||
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
||||||
|
|
||||||
const AutosuggestAccount = ({ account }) => (
|
const AutosuggestAccount = ({ account }) => (
|
||||||
<div style={{ overflow: 'hidden' }}>
|
<div style={{ overflow: 'hidden' }} className='autosuggest-account'>
|
||||||
<div style={{ float: 'left', marginRight: '5px' }}><Avatar src={account.get('avatar')} size={18} /></div>
|
<div style={{ float: 'left', marginRight: '5px' }}><Avatar src={account.get('avatar')} size={18} /></div>
|
||||||
<DisplayName account={account} />
|
<DisplayName account={account} />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
AutosuggestAccount.propTypes = {
|
||||||
|
account: ImmutablePropTypes.map.isRequired
|
||||||
|
};
|
||||||
|
|
||||||
export default AutosuggestAccount;
|
export default AutosuggestAccount;
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
import { FormattedMessage } from 'react-intl';
|
||||||
|
import DisplayName from '../../../components/display_name';
|
||||||
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
||||||
|
|
||||||
|
const AutosuggestStatus = ({ status }) => (
|
||||||
|
<div style={{ overflow: 'hidden' }} className='autosuggest-status'>
|
||||||
|
<FormattedMessage id='search.status_by' defaultMessage='Status by {name}' values={{ name: <strong>@{status.getIn(['account', 'acct'])}</strong> }} />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
|
||||||
|
AutosuggestStatus.propTypes = {
|
||||||
|
status: ImmutablePropTypes.map.isRequired
|
||||||
|
};
|
||||||
|
|
||||||
|
export default AutosuggestStatus;
|
|
@ -2,6 +2,7 @@ import PureRenderMixin from 'react-addons-pure-render-mixin';
|
||||||
import ImmutablePropTypes from 'react-immutable-proptypes';
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
||||||
import Autosuggest from 'react-autosuggest';
|
import Autosuggest from 'react-autosuggest';
|
||||||
import AutosuggestAccountContainer from '../containers/autosuggest_account_container';
|
import AutosuggestAccountContainer from '../containers/autosuggest_account_container';
|
||||||
|
import AutosuggestStatusContainer from '../containers/autosuggest_status_container';
|
||||||
import { debounce } from 'react-decoration';
|
import { debounce } from 'react-decoration';
|
||||||
import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
|
import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
|
||||||
|
|
||||||
|
@ -14,8 +15,10 @@ const getSuggestionValue = suggestion => suggestion.value;
|
||||||
const renderSuggestion = suggestion => {
|
const renderSuggestion = suggestion => {
|
||||||
if (suggestion.type === 'account') {
|
if (suggestion.type === 'account') {
|
||||||
return <AutosuggestAccountContainer id={suggestion.id} />;
|
return <AutosuggestAccountContainer id={suggestion.id} />;
|
||||||
|
} else if (suggestion.type === 'hashtag') {
|
||||||
|
return <span>#{suggestion.id}</span>;
|
||||||
} else {
|
} else {
|
||||||
return <span>#{suggestion.id}</span>
|
return <AutosuggestStatusContainer id={suggestion.id} />;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -78,8 +81,10 @@ const Search = React.createClass({
|
||||||
onSuggestionSelected (_, { suggestion }) {
|
onSuggestionSelected (_, { suggestion }) {
|
||||||
if (suggestion.type === 'account') {
|
if (suggestion.type === 'account') {
|
||||||
this.context.router.push(`/accounts/${suggestion.id}`);
|
this.context.router.push(`/accounts/${suggestion.id}`);
|
||||||
} else {
|
} else if(suggestion.type === 'hashtag') {
|
||||||
this.context.router.push(`/timelines/tag/${suggestion.id}`);
|
this.context.router.push(`/timelines/tag/${suggestion.id}`);
|
||||||
|
} else {
|
||||||
|
this.context.router.push(`/statuses/${suggestion.id}`);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -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);
|
|
@ -90,7 +90,6 @@ export default function accounts(state = initialState, action) {
|
||||||
case REBLOGS_FETCH_SUCCESS:
|
case REBLOGS_FETCH_SUCCESS:
|
||||||
case FAVOURITES_FETCH_SUCCESS:
|
case FAVOURITES_FETCH_SUCCESS:
|
||||||
case COMPOSE_SUGGESTIONS_READY:
|
case COMPOSE_SUGGESTIONS_READY:
|
||||||
case SEARCH_SUGGESTIONS_READY:
|
|
||||||
case FOLLOW_REQUESTS_FETCH_SUCCESS:
|
case FOLLOW_REQUESTS_FETCH_SUCCESS:
|
||||||
case FOLLOW_REQUESTS_EXPAND_SUCCESS:
|
case FOLLOW_REQUESTS_EXPAND_SUCCESS:
|
||||||
case BLOCKS_FETCH_SUCCESS:
|
case BLOCKS_FETCH_SUCCESS:
|
||||||
|
@ -98,6 +97,7 @@ export default function accounts(state = initialState, action) {
|
||||||
return normalizeAccounts(state, action.accounts);
|
return normalizeAccounts(state, action.accounts);
|
||||||
case NOTIFICATIONS_REFRESH_SUCCESS:
|
case NOTIFICATIONS_REFRESH_SUCCESS:
|
||||||
case NOTIFICATIONS_EXPAND_SUCCESS:
|
case NOTIFICATIONS_EXPAND_SUCCESS:
|
||||||
|
case SEARCH_SUGGESTIONS_READY:
|
||||||
return normalizeAccountsFromStatuses(normalizeAccounts(state, action.accounts), action.statuses);
|
return normalizeAccountsFromStatuses(normalizeAccounts(state, action.accounts), action.statuses);
|
||||||
case TIMELINE_REFRESH_SUCCESS:
|
case TIMELINE_REFRESH_SUCCESS:
|
||||||
case TIMELINE_EXPAND_SUCCESS:
|
case TIMELINE_EXPAND_SUCCESS:
|
||||||
|
|
|
@ -32,7 +32,7 @@ const normalizeSuggestions = (state, value, accounts, hashtags, statuses) => {
|
||||||
value: `#${item}`
|
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({
|
hashtagItems.unshift({
|
||||||
type: 'hashtag',
|
type: 'hashtag',
|
||||||
id: value,
|
id: value,
|
||||||
|
@ -40,11 +40,24 @@ const normalizeSuggestions = (state, value, accounts, hashtags, statuses) => {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (hashtagItems.length > 0) {
|
||||||
newSuggestions.push({
|
newSuggestions.push({
|
||||||
title: 'hashtag',
|
title: 'hashtag',
|
||||||
items: hashtagItems
|
items: hashtagItems
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (statuses.length > 0) {
|
||||||
|
newSuggestions.push({
|
||||||
|
title: 'status',
|
||||||
|
items: statuses.map(item => ({
|
||||||
|
type: 'status',
|
||||||
|
id: item.id,
|
||||||
|
value: item.id
|
||||||
|
}))
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return state.withMutations(map => {
|
return state.withMutations(map => {
|
||||||
map.set('suggestions', newSuggestions);
|
map.set('suggestions', newSuggestions);
|
||||||
|
|
|
@ -1421,3 +1421,13 @@ button.active i.fa-retweet {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.autosuggest-status {
|
||||||
|
overflow: hidden;
|
||||||
|
white-space: nowrap;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
|
||||||
|
strong {
|
||||||
|
font-weight: 500;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -222,8 +222,9 @@ SQL
|
||||||
end
|
end
|
||||||
|
|
||||||
def search_for(terms, limit = 10)
|
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\'))'
|
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 = <<SQL
|
sql = <<SQL
|
||||||
SELECT
|
SELECT
|
||||||
|
@ -235,12 +236,13 @@ SQL
|
||||||
LIMIT ?
|
LIMIT ?
|
||||||
SQL
|
SQL
|
||||||
|
|
||||||
Account.find_by_sql([sql, terms, terms, limit])
|
Account.find_by_sql([sql, limit])
|
||||||
end
|
end
|
||||||
|
|
||||||
def advanced_search_for(terms, account, limit = 10)
|
def advanced_search_for(terms, account, 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\'))'
|
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 = <<SQL
|
sql = <<SQL
|
||||||
SELECT
|
SELECT
|
||||||
|
@ -254,7 +256,7 @@ SQL
|
||||||
LIMIT ?
|
LIMIT ?
|
||||||
SQL
|
SQL
|
||||||
|
|
||||||
Account.find_by_sql([sql, terms, account.id, account.id, terms, limit])
|
Account.find_by_sql([sql, account.id, account.id, limit])
|
||||||
end
|
end
|
||||||
|
|
||||||
def following_map(target_account_ids, account_id)
|
def following_map(target_account_ids, account_id)
|
||||||
|
|
|
@ -13,8 +13,9 @@ class Tag < ApplicationRecord
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
def search_for(terms, limit = 5)
|
def search_for(terms, limit = 5)
|
||||||
|
terms = Arel.sql(connection.quote(terms.gsub(/['?\\:]/, ' ')))
|
||||||
textsearch = 'to_tsvector(\'simple\', tags.name)'
|
textsearch = 'to_tsvector(\'simple\', tags.name)'
|
||||||
query = 'to_tsquery(\'simple\', \'\'\' \' || ? || \' \'\'\' || \':*\')'
|
query = 'to_tsquery(\'simple\', \'\'\' \' || ' + terms + ' || \' \'\'\' || \':*\')'
|
||||||
|
|
||||||
sql = <<SQL
|
sql = <<SQL
|
||||||
SELECT
|
SELECT
|
||||||
|
@ -26,7 +27,7 @@ class Tag < ApplicationRecord
|
||||||
LIMIT ?
|
LIMIT ?
|
||||||
SQL
|
SQL
|
||||||
|
|
||||||
Tag.find_by_sql([sql, terms, terms, limit])
|
Tag.find_by_sql([sql, limit])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,8 +1,13 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class FetchRemoteAccountService < BaseService
|
class FetchRemoteAccountService < BaseService
|
||||||
def call(url)
|
def call(url, prefetched_body = nil)
|
||||||
|
if prefetched_body.nil?
|
||||||
atom_url, body = FetchAtomService.new.call(url)
|
atom_url, body = FetchAtomService.new.call(url)
|
||||||
|
else
|
||||||
|
atom_url = url
|
||||||
|
body = prefetched_body
|
||||||
|
end
|
||||||
|
|
||||||
return nil if atom_url.nil?
|
return nil if atom_url.nil?
|
||||||
process_atom(atom_url, body)
|
process_atom(atom_url, body)
|
||||||
|
|
|
@ -10,9 +10,9 @@ class FetchRemoteResourceService < BaseService
|
||||||
xml.encoding = 'utf-8'
|
xml.encoding = 'utf-8'
|
||||||
|
|
||||||
if xml.root.name == 'feed'
|
if xml.root.name == 'feed'
|
||||||
FetchRemoteAccountService.new.call(atom_url)
|
FetchRemoteAccountService.new.call(atom_url, body)
|
||||||
elsif xml.root.name == 'entry'
|
elsif xml.root.name == 'entry'
|
||||||
FetchRemoteStatusService.new.call(atom_url)
|
FetchRemoteStatusService.new.call(atom_url, body)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,8 +1,13 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class FetchRemoteStatusService < BaseService
|
class FetchRemoteStatusService < BaseService
|
||||||
def call(url)
|
def call(url, prefetched_body = nil)
|
||||||
|
if prefetched_body.nil?
|
||||||
atom_url, body = FetchAtomService.new.call(url)
|
atom_url, body = FetchAtomService.new.call(url)
|
||||||
|
else
|
||||||
|
atom_url = url
|
||||||
|
body = prefetched_body
|
||||||
|
end
|
||||||
|
|
||||||
return nil if atom_url.nil?
|
return nil if atom_url.nil?
|
||||||
process_atom(atom_url, body)
|
process_atom(atom_url, body)
|
||||||
|
|
9
db/migrate/20170322162804_add_search_index_to_tags.rb
Normal file
9
db/migrate/20170322162804_add_search_index_to_tags.rb
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
class AddSearchIndexToTags < ActiveRecord::Migration[5.0]
|
||||||
|
def up
|
||||||
|
execute 'CREATE INDEX hashtag_search_index ON tags USING gin(to_tsvector(\'simple\', tags.name));'
|
||||||
|
end
|
||||||
|
|
||||||
|
def down
|
||||||
|
remove_index :tags, name: :hashtag_search_index
|
||||||
|
end
|
||||||
|
end
|
|
@ -10,7 +10,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(version: 20170322143850) do
|
ActiveRecord::Schema.define(version: 20170322162804) do
|
||||||
|
|
||||||
# These are extensions that must be enabled in order to support this database
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "plpgsql"
|
enable_extension "plpgsql"
|
||||||
|
@ -259,6 +259,7 @@ ActiveRecord::Schema.define(version: 20170322143850) do
|
||||||
t.string "name", default: "", null: false
|
t.string "name", default: "", null: false
|
||||||
t.datetime "created_at", null: false
|
t.datetime "created_at", null: false
|
||||||
t.datetime "updated_at", null: false
|
t.datetime "updated_at", null: false
|
||||||
|
t.index "to_tsvector('simple'::regconfig, (name)::text)", name: "hashtag_search_index", using: :gin
|
||||||
t.index ["name"], name: "index_tags_on_name", unique: true, using: :btree
|
t.index ["name"], name: "index_tags_on_name", unique: true, using: :btree
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue