Improve searching for private toots from URL

Most of the time, when sharing toots, people use the toot URL rather than
the toot URI, which makes sense since it is the user-facing URL.

In Mastodon's case, the URL and URI are different, and Mastodon does not
have an index on URL, which means searching a private toot by URL is done
with a slow query that will only succeed for very recent toots.

This change gets rid of the slow query, and attempts to guess the URI from
URL instead, as Mastodon's are predictable.
rebase/4.0.0rc2
Thibaut Girka 2020-09-21 18:22:54 +02:00 committed by ThibG
parent 3b6e27978d
commit 10bd6f415d
1 changed files with 11 additions and 1 deletions

View File

@ -34,7 +34,17 @@ class ResolveURLService < BaseService
# It may happen that the resource is a private toot, and thus not fetchable, # It may happen that the resource is a private toot, and thus not fetchable,
# but we can return the toot if we already know about it. # but we can return the toot if we already know about it.
status = Status.find_by(uri: @url) || Status.find_by(url: @url) uris = [@url]
# We don't have an index on `url`, so try guessing the `uri` from `url`
parsed_url = Addressable::URI.parse(@url)
parsed_url.path.match(%r{/@(?<username>#{Account::USERNAME_RE})/(?<status_id>[0-9]+)\Z}) do |matched|
parsed_url.path = "/users/#{matched[:username]}/statuses/#{matched[:status_id]}"
uris << parsed_url.to_s
end
status = Status.find_by(uri: uris)
authorize_with @on_behalf_of, status, :show? unless status.nil? authorize_with @on_behalf_of, status, :show? unless status.nil?
status status
rescue Mastodon::NotPermittedError rescue Mastodon::NotPermittedError