Add SEARCH_ALL_VISIBLE_TOOTS env flag

Context: https://docs.joinmastodon.org/user/network/#search

Vanilla Mastodon intentionally refuses to search outside a user's
own toots, favs, bookmarks, and mentions. This flag makes that
restricted search behavior a per-instance choice, defaulting
to the same behavior as vanilla Mastodon if the flag is absent.
This commit is contained in:
Vyr Cossont 2022-11-28 10:43:26 -08:00
parent 1a7aa37b60
commit 60654e8111
3 changed files with 14 additions and 1 deletions

View file

@ -283,6 +283,11 @@ MAX_POLL_OPTION_CHARS=100
# Customize the number of hashtags shown in 'Explore'
# MAX_TRENDING_TAGS=10
# Search all visible toots
# (Normally searches only a user's own toots, favs, bookmarks, and mentions)
# Only relevant when elasticsearch is installed
# SEARCH_ALL_VISIBLE_TOOTS=true
# Maximum custom emoji file sizes
# If undefined or smaller than MAX_EMOJI_SIZE, the value
# of MAX_EMOJI_SIZE will be used for MAX_REMOTE_EMOJI_SIZE

View file

@ -71,5 +71,6 @@ class StatusesIndex < Chewy::Index
end
field :searchable_by, type: 'long', value: ->(status, crutches) { status.searchable_by(crutches) }
field :searchable_by_anyone, type: 'boolean', value: ->(status) { status.public_visibility? or status.unlisted_visibility? }
end
end

View file

@ -1,6 +1,9 @@
# frozen_string_literal: true
class SearchService < BaseService
SEARCH_ALL_VISIBLE_TOOTS = ENV['SEARCH_ALL_VISIBLE_TOOTS'] == 'true'
def call(query, account, limit, options = {})
@query = query&.strip
@account = account
@ -35,7 +38,11 @@ class SearchService < BaseService
end
def perform_statuses_search!
definition = parsed_query.apply(StatusesIndex.filter(term: { searchable_by: @account.id }))
statuses_index = StatusesIndex.filter(term: { searchable_by: @account.id })
if SEARCH_ALL_VISIBLE_TOOTS
statuses_index = statuses_index.or.filter(term: { searchable_by_anyone: true })
end
definition = parsed_query.apply(statuses_index)
if @options[:account_id].present?
definition = definition.filter(term: { account_id: @options[:account_id] })