Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve from search prefix error handling #37

Merged
merged 3 commits into from
Apr 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/controllers/api/v2/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ class Api::V2::SearchController < Api::BaseController
def index
@search = Search.new(search_results)
render json: @search, serializer: REST::SearchSerializer

# TODO: in the front end, these will show a toast that is only barely helpful
# TODO: semantics?

# user searched with a prefix that does exist
rescue Mastodon::SyntaxError
unprocessable_entity
# user searched for posts from an account the instance is not aware of
rescue Mastodon::NotFound
marrus-sh marked this conversation as resolved.
Show resolved Hide resolved
not_found
end

private
Expand Down
10 changes: 6 additions & 4 deletions app/lib/search_query_transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,16 @@ def initialize(prefix, term)
case prefix
when 'from'
@filter = :account_id
username, domain = term.split('@')
account = Account.find_remote(username, domain)

raise "Account not found: #{term}" unless account
username, domain = Account.validate_account_string(term)
marrus-sh marked this conversation as resolved.
Show resolved Hide resolved
raise Mastodon::SyntaxError, "Account string invalid: #{term}" if username.nil?

account = Account.find_local_or_remote(username, domain)
raise Mastodon::NotFound, "Account not found: #{term}" if account.nil?

@term = account.id
else
raise "Unknown prefix: #{prefix}"
raise Mastodon::SyntaxError, "Unknown prefix: #{prefix}"
end
end
end
Expand Down
13 changes: 13 additions & 0 deletions app/models/concerns/account_finder_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,21 @@ def find_local(username)
def find_remote(username, domain)
AccountFinder.new(username, domain).account
end

# sigh
def find_local_or_remote(username, domain)
TagManager.instance.local_domain?(domain) ? find_local(username) : find_remote(username, domain)
end

def validate_account_string(account_string)
match = ACCOUNT_STRING_RE.match(account_string)

[match[:username], match[:domain]] if match
end
end

ACCOUNT_STRING_RE = /^@?(?<username>#{Account::USERNAME_RE})(?:@(?<domain>[[:word:]\.\-]+))?$/i
marrus-sh marked this conversation as resolved.
Show resolved Hide resolved

class AccountFinder
attr_reader :username, :domain

Expand Down
2 changes: 2 additions & 0 deletions lib/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class DimensionsValidationError < ValidationError; end
class StreamValidationError < ValidationError; end
class RaceConditionError < Error; end
class RateLimitExceededError < Error; end
class NotFound < Error; end
class SyntaxError < Error; end

class UnexpectedResponseError < Error
attr_reader :response
Expand Down