From 08e5d96a4a07305660044e53657a52f935c3eee3 Mon Sep 17 00:00:00 2001 From: KMY Date: Wed, 5 Apr 2023 13:55:37 +0900 Subject: [PATCH 1/6] Expand search range --- app/chewy/statuses_index.rb | 5 +++++ app/controllers/api/v2/search_controller.rb | 2 +- app/models/status.rb | 2 ++ app/services/search_service.rb | 3 ++- 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/app/chewy/statuses_index.rb b/app/chewy/statuses_index.rb index 6dd4fb18b0..a185b7ce29 100644 --- a/app/chewy/statuses_index.rb +++ b/app/chewy/statuses_index.rb @@ -47,6 +47,11 @@ class StatusesIndex < Chewy::Index data.each.with_object({}) { |(id, name), result| (result[id] ||= []).push(name) } end + crutch :emoji_reactions do |collection| + data = ::EmojiReaction.where(status_id: collection.map(&:id)).where(account: Account.local).pluck(:status_id, :account_id) + data.each.with_object({}) { |(id, name), result| (result[id] ||= []).push(name) } + end + crutch :reblogs do |collection| data = ::Status.where(reblog_of_id: collection.map(&:id)).where(account: Account.local).pluck(:reblog_of_id, :account_id) data.each.with_object({}) { |(id, name), result| (result[id] ||= []).push(name) } diff --git a/app/controllers/api/v2/search_controller.rb b/app/controllers/api/v2/search_controller.rb index 4d20aeb10f..6098fc6abd 100644 --- a/app/controllers/api/v2/search_controller.rb +++ b/app/controllers/api/v2/search_controller.rb @@ -3,7 +3,7 @@ class Api::V2::SearchController < Api::BaseController include Authorization - RESULTS_LIMIT = 20 + RESULTS_LIMIT = 40 before_action -> { authorize_if_got_token! :read, :'read:search' } before_action :validate_search_params! diff --git a/app/models/status.rb b/app/models/status.rb index f2f71f5415..800147f26a 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -150,12 +150,14 @@ class Status < ApplicationRecord if preloaded.nil? ids += mentions.joins(:account).merge(Account.local).active.pluck(:account_id) ids += favourites.joins(:account).merge(Account.local).pluck(:account_id) + ids += emoji_reactions.joins(:account).merge(Account.local).pluck(:account_id) ids += reblogs.joins(:account).merge(Account.local).pluck(:account_id) ids += bookmarks.joins(:account).merge(Account.local).pluck(:account_id) ids += poll.votes.joins(:account).merge(Account.local).pluck(:account_id) if poll.present? else ids += preloaded.mentions[id] || [] ids += preloaded.favourites[id] || [] + ids += preloaded.emoji_reactions[id] || [] ids += preloaded.reblogs[id] || [] ids += preloaded.bookmarks[id] || [] ids += preloaded.votes[id] || [] diff --git a/app/services/search_service.rb b/app/services/search_service.rb index 93b72fa0c4..0d2383a53c 100644 --- a/app/services/search_service.rb +++ b/app/services/search_service.rb @@ -35,7 +35,8 @@ class SearchService < BaseService end def perform_statuses_search! - definition = parsed_query.apply(StatusesIndex.filter(term: { searchable_by: @account.id })) + # definition = parsed_query.apply(StatusesIndex.filter(term: { searchable_by: @account.id })).order(id: :desc) + definition = parsed_query.apply(StatusesIndex).order(id: :desc) definition = definition.filter(term: { account_id: @options[:account_id] }) if @options[:account_id].present? From 5e8bdaaab6998ca0f105fa40d4e00e95dc762257 Mon Sep 17 00:00:00 2001 From: KMY Date: Wed, 5 Apr 2023 14:29:24 +0900 Subject: [PATCH 2/6] Except for unlisted from search results --- app/lib/status_filter.rb | 14 ++++++++++++++ app/policies/status_policy.rb | 4 ++++ app/services/search_service.rb | 2 +- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app/lib/status_filter.rb b/app/lib/status_filter.rb index c0e6f33313..05b818f73a 100644 --- a/app/lib/status_filter.rb +++ b/app/lib/status_filter.rb @@ -15,6 +15,12 @@ class StatusFilter blocked_by_policy? || (account_present? && filtered_status?) || silenced_account? end + def search_filtered? + return false if !account.nil? && account.id == status.account_id + + blocked_by_policy_search? || (account_present? && filtered_status?) || silenced_account? + end + private def account_present? @@ -53,7 +59,15 @@ class StatusFilter !policy_allows_show? end + def blocked_by_policy_search? + !policy_allows_search? + end + def policy_allows_show? StatusPolicy.new(account, status, @preloaded_relations).show? end + + def policy_allows_search? + StatusPolicy.new(account, status, @preloaded_relations).search? + end end diff --git a/app/policies/status_policy.rb b/app/policies/status_policy.rb index 694997788d..81e60fccba 100644 --- a/app/policies/status_policy.rb +++ b/app/policies/status_policy.rb @@ -19,6 +19,10 @@ class StatusPolicy < ApplicationPolicy end end + def search? + show? && (record.public_visibility? || record.public_unlisted_visibility?) + end + def reblog? !requires_mention? && (!private? || owned?) && show? && !blocking_author? end diff --git a/app/services/search_service.rb b/app/services/search_service.rb index 0d2383a53c..a7adada258 100644 --- a/app/services/search_service.rb +++ b/app/services/search_service.rb @@ -52,7 +52,7 @@ class SearchService < BaseService account_domains = results.map(&:account_domain) preloaded_relations = relations_map_for_account(@account, account_ids, account_domains) - results.reject { |status| StatusFilter.new(status, @account, preloaded_relations).filtered? } + results.reject { |status| StatusFilter.new(status, @account, preloaded_relations).search_filtered? } rescue Faraday::ConnectionFailed, Parslet::ParseFailed [] end From fdf3b93c3658ba07144255516e2a50604d2b30d5 Mon Sep 17 00:00:00 2001 From: KMY Date: Wed, 5 Apr 2023 14:36:15 +0900 Subject: [PATCH 3/6] Fix search policy --- app/policies/status_policy.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/app/policies/status_policy.rb b/app/policies/status_policy.rb index 81e60fccba..74709647a8 100644 --- a/app/policies/status_policy.rb +++ b/app/policies/status_policy.rb @@ -20,7 +20,15 @@ class StatusPolicy < ApplicationPolicy end def search? - show? && (record.public_visibility? || record.public_unlisted_visibility?) + return false if author.suspended? + + if requires_mention? + owned? || mention_exists? + elsif !public? + owned? || following_author? || mention_exists? + else + current_account.nil? || (!author_blocking? && !author_blocking_domain?) + end end def reblog? @@ -59,6 +67,10 @@ class StatusPolicy < ApplicationPolicy record.private_visibility? end + def public? + record.public_visibility? || record.public_unlisted_visibility? + end + def mention_exists? return false if current_account.nil? From 073304aa9e1969fac7a9589072c7649f8cd1539c Mon Sep 17 00:00:00 2001 From: KMY Date: Wed, 5 Apr 2023 16:59:05 +0900 Subject: [PATCH 4/6] Fix search show more bar --- app/javascript/mastodon/actions/search.js | 2 +- .../features/compose/components/search_results.jsx | 9 ++++++--- app/javascript/mastodon/reducers/search.js | 9 ++++++++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/app/javascript/mastodon/actions/search.js b/app/javascript/mastodon/actions/search.js index 666c6c223b..0012808e5b 100644 --- a/app/javascript/mastodon/actions/search.js +++ b/app/javascript/mastodon/actions/search.js @@ -43,7 +43,7 @@ export function submitSearch() { params: { q: value, resolve: signedIn, - limit: 5, + limit: 10, }, }).then(response => { if (response.data.accounts) { diff --git a/app/javascript/mastodon/features/compose/components/search_results.jsx b/app/javascript/mastodon/features/compose/components/search_results.jsx index 44ab43638b..3882dec362 100644 --- a/app/javascript/mastodon/features/compose/components/search_results.jsx +++ b/app/javascript/mastodon/features/compose/components/search_results.jsx @@ -25,6 +25,7 @@ class SearchResults extends ImmutablePureComponent { dismissSuggestion: PropTypes.func.isRequired, searchTerm: PropTypes.string, intl: PropTypes.object.isRequired, + noMoreResults: ImmutablePropTypes.map, }; componentDidMount () { @@ -45,6 +46,8 @@ class SearchResults extends ImmutablePureComponent { handleLoadMoreHashtags = () => this.props.expandSearch('hashtags'); + showMoreResults = (searchType) => this.props.noMoreResults ? !this.props.noMoreResults.get(searchType) : true; + render () { const { intl, results, suggestions, dismissSuggestion, searchTerm } = this.props; @@ -82,7 +85,7 @@ class SearchResults extends ImmutablePureComponent { {results.get('accounts').map(accountId => )} - {results.get('accounts').size >= 5 && } + {this.showMoreResults('accounts') && } ); } @@ -95,7 +98,7 @@ class SearchResults extends ImmutablePureComponent { {results.get('statuses').map(statusId => )} - {results.get('statuses').size >= 5 && } + {this.showMoreResults('statuses') && } ); } else if(results.get('statuses') && results.get('statuses').size === 0 && !searchEnabled && !(searchTerm.startsWith('@') || searchTerm.startsWith('#') || searchTerm.includes(' '))) { @@ -118,7 +121,7 @@ class SearchResults extends ImmutablePureComponent { {results.get('hashtags').map(hashtag => )} - {results.get('hashtags').size >= 5 && } + {this.showMoreResults('hashtags') && } ); } diff --git a/app/javascript/mastodon/reducers/search.js b/app/javascript/mastodon/reducers/search.js index d3e71da9d9..56ca3070c5 100644 --- a/app/javascript/mastodon/reducers/search.js +++ b/app/javascript/mastodon/reducers/search.js @@ -19,6 +19,7 @@ const initialState = ImmutableMap({ submitted: false, hidden: false, results: ImmutableMap(), + noMoreResults: ImmutableMap(), isLoading: false, searchTerm: '', }); @@ -31,6 +32,7 @@ export default function search(state = initialState, action) { return state.withMutations(map => { map.set('value', ''); map.set('results', ImmutableMap()); + map.set('noMoreResults', ImmutableMap()); map.set('submitted', false); map.set('hidden', false); }); @@ -54,13 +56,18 @@ export default function search(state = initialState, action) { statuses: ImmutableList(action.results.statuses.map(item => item.id)), hashtags: fromJS(action.results.hashtags), })); + map.set('noMoreResults', ImmutableMap({ + accounts: action.results.accounts.length <= 0, + statuses: action.results.statuses.length <= 0, + hashtags: false, + })); map.set('searchTerm', action.searchTerm); map.set('isLoading', false); }); case SEARCH_EXPAND_SUCCESS: const results = action.searchType === 'hashtags' ? fromJS(action.results.hashtags) : action.results[action.searchType].map(item => item.id); - return state.updateIn(['results', action.searchType], list => list.concat(results)); + return state.updateIn(['results', action.searchType], list => list.concat(results)).updateIn(['noMoreResults', action.searchType], results.size <= 0); default: return state; } From 232339db528873d847ceaa0085836f3563f49bcb Mon Sep 17 00:00:00 2001 From: KMY Date: Wed, 5 Apr 2023 17:28:14 +0900 Subject: [PATCH 5/6] Fix updatein errors --- .../features/compose/components/search_results.jsx | 9 ++++++--- app/javascript/mastodon/reducers/search.js | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/app/javascript/mastodon/features/compose/components/search_results.jsx b/app/javascript/mastodon/features/compose/components/search_results.jsx index 3882dec362..ffdf6cf692 100644 --- a/app/javascript/mastodon/features/compose/components/search_results.jsx +++ b/app/javascript/mastodon/features/compose/components/search_results.jsx @@ -79,26 +79,28 @@ class SearchResults extends ImmutablePureComponent { if (results.get('accounts') && results.get('accounts').size > 0) { count += results.get('accounts').size; + const showMore = this.showMoreResults('accounts'); accounts = (
{results.get('accounts').map(accountId => )} - {this.showMoreResults('accounts') && } + {showMore && }
); } if (results.get('statuses') && results.get('statuses').size > 0) { count += results.get('statuses').size; + const showMore = this.showMoreResults('statuses'); statuses = (
{results.get('statuses').map(statusId => )} - {this.showMoreResults('statuses') && } + {showMore && }
); } else if(results.get('statuses') && results.get('statuses').size === 0 && !searchEnabled && !(searchTerm.startsWith('@') || searchTerm.startsWith('#') || searchTerm.includes(' '))) { @@ -115,13 +117,14 @@ class SearchResults extends ImmutablePureComponent { if (results.get('hashtags') && results.get('hashtags').size > 0) { count += results.get('hashtags').size; + const showMore = this.showMoreResults('hashtags'); hashtags = (
{results.get('hashtags').map(hashtag => )} - {this.showMoreResults('hashtags') && } + {showMore && }
); } diff --git a/app/javascript/mastodon/reducers/search.js b/app/javascript/mastodon/reducers/search.js index 56ca3070c5..73d7eabef4 100644 --- a/app/javascript/mastodon/reducers/search.js +++ b/app/javascript/mastodon/reducers/search.js @@ -67,7 +67,7 @@ export default function search(state = initialState, action) { }); case SEARCH_EXPAND_SUCCESS: const results = action.searchType === 'hashtags' ? fromJS(action.results.hashtags) : action.results[action.searchType].map(item => item.id); - return state.updateIn(['results', action.searchType], list => list.concat(results)).updateIn(['noMoreResults', action.searchType], results.size <= 0); + return state.updateIn(['results', action.searchType], list => list.concat(results)).setIn(['noMoreResults', action.searchType], results.size <= 0); default: return state; } From a2e674af51c1a26bfaef48f4cdb049c3c8919ffc Mon Sep 17 00:00:00 2001 From: KMY Date: Wed, 5 Apr 2023 18:22:00 +0900 Subject: [PATCH 6/6] Update sudachi settings --- app/chewy/statuses_index.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app/chewy/statuses_index.rb b/app/chewy/statuses_index.rb index a185b7ce29..28dace0f98 100644 --- a/app/chewy/statuses_index.rb +++ b/app/chewy/statuses_index.rb @@ -30,7 +30,20 @@ class StatusesIndex < Chewy::Index english_stemmer ), }, + sudachi_analyzer: { + filter: [], + type: 'custom', + tokenizer: 'sudachi_tokenizer', + }, }, + tokenizer: { + sudachi_tokenizer: { + resources_path: '/etc/elasticsearch/sudachi', + split_mode: 'C', + type: 'sudachi_tokenizer', + discard_punctuation: 'true', + } + } } # We do not use delete_if option here because it would call a method that we