[Misc] Add a method that returns the base url for network requests

This makes it easier to stub in tests.
Also add a method to the iqdb proxy to say if it is enabled or not and use it everywhere.
This commit is contained in:
Earlopain 2024-04-26 15:57:56 +02:00
parent 988b607c57
commit b058077cf0
No known key found for this signature in database
GPG Key ID: 48860312319ADF61
8 changed files with 21 additions and 13 deletions

View File

@ -50,6 +50,6 @@ class IqdbQueriesController < ApplicationController
end
def validate_enabled
raise FeatureUnavailable if Danbooru.config.iqdb_server.blank?
raise FeatureUnavailable unless IqdbProxy.enabled?
end
end

View File

@ -1,9 +1,13 @@
# frozen_string_literal: true
module CloudflareService
def self.endpoint
"https://api.cloudflare.com/client/v4/ips"
end
def self.ips
text, code = Cache.fetch("cloudflare_ips", expires_in: 24.hours) do
resp = HTTParty.get("https://api.cloudflare.com/client/v4/ips", Danbooru.config.httparty_options)
resp = HTTParty.get(endpoint, Danbooru.config.httparty_options)
[resp.body, resp.code]
end
return [] if code != 200

View File

@ -7,8 +7,16 @@ module IqdbProxy
module_function
def endpoint
Danbooru.config.iqdb_server
end
def enabled?
endpoint.present?
end
def make_request(path, request_type, params = {})
url = URI.parse(Danbooru.config.iqdb_server)
url = URI.parse(endpoint)
url.path = path
HTTParty.send(request_type, url, { body: params.to_json, headers: { "Content-Type" => "application/json" } })
rescue Errno::ECONNREFUSED, Errno::EADDRNOTAVAIL, Errno::EHOSTUNREACH

View File

@ -1491,19 +1491,15 @@ class Post < ApplicationRecord
extend ActiveSupport::Concern
module ClassMethods
def iqdb_enabled?
Danbooru.config.iqdb_server.present?
end
def remove_iqdb(post_id)
if iqdb_enabled?
if IqdbProxy.enabled?
IqdbRemoveJob.perform_later(post_id)
end
end
end
def update_iqdb_async
if Post.iqdb_enabled? && has_preview?
if IqdbProxy.enabled? && has_preview?
IqdbUpdateJob.perform_later(id)
end
end

View File

@ -48,7 +48,7 @@
<% end %>
<% if CurrentUser.is_admin? %>
<% if Danbooru.config.iqdb_server.present? %>
<% if IqdbProxy.enabled? %>
<li><%= link_to "Update IQDB", update_iqdb_post_path(@post) %></li>
<% end %>
<li><%= tag.a "Destroy", href: '#', id: 'destroy-post-link', 'data-pid': post.id %></li>

View File

@ -37,7 +37,7 @@
<h1>Related</h1>
<ul>
<li><%= link_to "Sets with this post", post_sets_path(post_id: @post.id), rel: "nofollow" %></li>
<% if Danbooru.config.iqdb_server.present? && @post.has_preview? %>
<% if IqdbProxy.enabled? && @post.has_preview? %>
<li><%= link_to "Visually similar on E6", iqdb_queries_path(search: { post_id: @post.id }), rel: "nofollow" %></li>
<% end %>
<% if @post.is_image? %>

View File

@ -5,7 +5,7 @@ require "test_helper"
class IqdbQueriesControllerTest < ActionDispatch::IntegrationTest
context "The iqdb controller" do
setup do
Danbooru.config.stubs(:iqdb_server).returns("https://karasuma.donmai.us")
IqdbProxy.stubs(:endpoint).returns("http://iqdb:5588")
@user = create(:user)
as(@user) do
@posts = create_list(:post, 2)

View File

@ -13,7 +13,7 @@ class CloudflareServiceTest < ActiveSupport::TestCase
ipv6_cidrs: [ipv6],
},
}
stub_request(:get, "https://api.cloudflare.com/client/v4/ips").to_return(status: 200, body: dummy_response.to_json)
stub_request(:get, CloudflareService.endpoint).to_return(status: 200, body: dummy_response.to_json)
assert_equal([IPAddr.new(ipv4), IPAddr.new(ipv6)], CloudflareService.ips)
end
end