Fix request bug.

This commit is contained in:
BoHung Chiu 2022-05-10 10:00:50 +08:00
parent fd23167f98
commit 5240e84160
1 changed files with 17 additions and 2 deletions

View File

@ -86,6 +86,21 @@ class SiteFeed
def channel_title_for_cache
!self[:channel_title].to_s.empty? ? self[:channel_title] : I18n.t("feed.source")
end
def http_request(http, request)
response = http.request(request)
if response.code.to_i == 301 || response.code.to_i == 302
location = response["location"]
new_uri = URI(location)
http = Net::HTTP.new(new_uri.host, new_uri.port)
if location.include?('https')
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
request.instance_variable_set(:@path, new_uri.path)
response = http_request(http, request)
end
response
end
def add_notify
unless self.enable_notify
root_url = Site.first.root_url rescue ""
@ -98,7 +113,7 @@ class SiteFeed
http_req.open_timeout = 10
request = Net::HTTP::Post.new("/xhr/#{self.channel_key.pluralize}/feed_add_remote/#{self.feed_uid}", 'Content-Type' => 'application/json')
request.body = {"url"=>root_url}.to_json
response = http_req.request(request)
response = http_request( http_req , request )
if response.code.to_i == 200
self.update(:enable_notify=>true)
end
@ -117,7 +132,7 @@ class SiteFeed
http_req.open_timeout = 10
request = Net::HTTP::Post.new("/xhr/#{self.channel_key.pluralize}/feed_remove_remote/#{self.feed_uid}", 'Content-Type' => 'application/json')
request.body = {"url"=>root_url}.to_json
response = http_req.request(request)
response = http_request( http_req , request )
if response.code.to_i == 200
self.update(:enable_notify=>false)
end