sources: fix sources sometimes choosing wrong strategy (fix #3968)

Fix sources choosing the wrong strategy when the referer belongs to a
different site (for example, when uploading a twitter post with a pixiv
referer).

* Fix `match?` to only consider the main url, not the referer.

* Change `match?` to match against a list of domains given by the `domains` method.

* Change `match?` to an instance method.
This commit is contained in:
evazion
2018-11-04 13:00:17 -06:00
parent 4219163042
commit 5cf6a43918
13 changed files with 58 additions and 37 deletions

View File

@@ -14,14 +14,10 @@
module Sources
module Strategies
class Base
attr_reader :url, :referer_url
attr_reader :url, :referer_url, :urls, :parsed_url, :parsed_referer, :parsed_urls
extend Memoist
def self.match?(*urls)
false
end
# Should return true if all prerequisites for using the strategy are met.
# Return false if the strategy requires api keys that have not been configured.
def self.enabled?
@@ -41,10 +37,24 @@ module Sources
def initialize(url, referer_url = nil)
@url = url
@referer_url = referer_url
@urls = [url, referer_url].select(&:present?)
@parsed_url = Addressable::URI.heuristic_parse(url) rescue nil
@parsed_referer = Addressable::URI.heuristic_parse(referer_url) rescue nil
@parsed_urls = [parsed_url, parsed_referer].select(&:present?)
end
def urls
[url, referer_url].select(&:present?)
# Should return true if this strategy should be used. By default, checks
# if the main url belongs to any of the domains associated with this site.
def match?
return false if parsed_url.nil?
parsed_url.domain.in?(domains)
end
# The list of base domains belonging to this site. Subdomains are
# automatically included (i.e. "pixiv.net" matches "fanbox.pixiv.net").
def domains
[]
end
def site_name