From e681e8d14c6127284052c223d72cd0a8820fc549 Mon Sep 17 00:00:00 2001 From: 0x2019 <34298117+single-right-quote@users.noreply.github.com> Date: Fri, 8 Apr 2022 19:21:49 +0000 Subject: [PATCH] Fix error resposes for `from` search prefix (#17963) * Fix error responses in `from` search prefix (addresses mastodon/mastodon#17941) Using unsupported prefixes now reports a 422; searching for posts from an account the instance is not aware of reports a 404. TODO: The UI for this on the front end is abysmal. Searching `from:username@domain` now succeeds when `domain` is the local domain; searching `from:@username(@domain)?` now works as expected. * Remove unused methods on new Error classes as they are not being used Currently when `raise`d there are error messages being supplied, but this is not actually being used. The associated `raise`s have been edited accordingly. * Remove needless comments * Satisfy rubocop * Try fixing tests being unable to find AccountFindingConcern methods * Satisfy rubocop * Simplify `from` prefix logic This incorporates @ClearlyClaire's suggestion (see https://github.com/mastodon/mastodon/pull/17963#pullrequestreview-933986737). Accepctable account strings in `from:` clauses are more lenient than before this commit; for example, `from:@user@example.org@asnteo +cat` will not error, and return posts by @user@example.org containing the word "cat". This is more consistent with how Mastodon matches mentions in statuses. In addition, `from` clauses will not be checked for syntatically invalid usernames or domain names, simply 404ing when `Account.find_remote!` raises ActiveRecord::NotFound. New code for this PR that is no longer used has been removed. --- app/controllers/api/v2/search_controller.rb | 4 ++++ app/lib/search_query_transformer.rb | 8 ++++---- lib/exceptions.rb | 1 + 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/v2/search_controller.rb b/app/controllers/api/v2/search_controller.rb index f17431dd10..a305601333 100644 --- a/app/controllers/api/v2/search_controller.rb +++ b/app/controllers/api/v2/search_controller.rb @@ -11,6 +11,10 @@ class Api::V2::SearchController < Api::BaseController def index @search = Search.new(search_results) render json: @search, serializer: REST::SearchSerializer + rescue Mastodon::SyntaxError + unprocessable_entity + rescue ActiveRecord::RecordNotFound + not_found end private diff --git a/app/lib/search_query_transformer.rb b/app/lib/search_query_transformer.rb index c685d7b6fd..aef05e9d9d 100644 --- a/app/lib/search_query_transformer.rb +++ b/app/lib/search_query_transformer.rb @@ -88,14 +88,14 @@ class SearchQueryTransformer < Parslet::Transform 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 = term.gsub(/\A@/, '').split('@') + domain = nil if TagManager.instance.local_domain?(domain) + account = Account.find_remote!(username, domain) @term = account.id else - raise "Unknown prefix: #{prefix}" + raise Mastodon::SyntaxError end end end diff --git a/lib/exceptions.rb b/lib/exceptions.rb index eb472abaab..0c677b6605 100644 --- a/lib/exceptions.rb +++ b/lib/exceptions.rb @@ -10,6 +10,7 @@ module Mastodon class StreamValidationError < ValidationError; end class RaceConditionError < Error; end class RateLimitExceededError < Error; end + class SyntaxError < Error; end class UnexpectedResponseError < Error attr_reader :response