diff --git a/app/services/problem_check/access_token_invalid.rb b/app/services/problem_check/access_token_invalid.rb new file mode 100644 index 0000000..a62e7a2 --- /dev/null +++ b/app/services/problem_check/access_token_invalid.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class ProblemCheck::AccessTokenInvalid < ::ProblemCheck::InlineProblemCheck + self.priority = "high" + + private + + def translation_key + "dashboard.patreon.access_token_invalid" + end +end diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index c2752a2..b09dd60 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -18,7 +18,7 @@ en: errors: patreon_creator_username_not_set: "You need to set the Patreon Creator Discourse Username setting before enabling Patreon Login." dashboard: - patreon: + problem: access_token_invalid: "Patreon Creator's access and refresh token values are incorrect. You must copy-paste new tokens from Patreon website to site settings." patreon: error: diff --git a/lib/api.rb b/lib/api.rb index e663bfa..7a46d86 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -21,9 +21,6 @@ def self.get(uri) RateLimiter.new(nil, "patreon_api_hr", SiteSetting.max_patreon_api_reqs_per_hr, 1.hour) limiter_day = RateLimiter.new(nil, "patreon_api_day", SiteSetting.max_patreon_api_reqs_per_day, 1.day) - if AdminDashboardData.problem_message_check(ACCESS_TOKEN_INVALID) - AdminDashboardData.clear_problem_message(ACCESS_TOKEN_INVALID) - end limiter_hr.performed! unless limiter_hr.can_perform? @@ -44,7 +41,7 @@ def self.get(uri) when 200 return JSON.parse response.body when 401 - AdminDashboardData.add_problem_message(ACCESS_TOKEN_INVALID, 7.hours) + ProblemCheckTracker[:access_token_invalid].problem! else e = ::Patreon::InvalidApiResponse.new(response.body.presence || "") e.set_backtrace(caller) diff --git a/plugin.rb b/plugin.rb index 1cae535..6140b45 100644 --- a/plugin.rb +++ b/plugin.rb @@ -69,6 +69,7 @@ def self.all require_relative "app/jobs/regular/sync_patron_groups" require_relative "app/jobs/scheduled/patreon_sync_patrons_to_groups" require_relative "app/jobs/scheduled/patreon_update_tokens" + require_relative "app/services/problem_check/access_token_invalid" require_relative "lib/api" require_relative "lib/seed" require_relative "lib/campaign" @@ -76,8 +77,6 @@ def self.all require_relative "lib/patron" require_relative "lib/tokens" - AdminDashboardData.problem_messages << ::Patreon::Api::ACCESS_TOKEN_INVALID - Patreon::Engine.routes.draw do get "/rewards" => "patreon_admin#rewards", :constraints => AdminConstraint.new get "/list" => "patreon_admin#list", :constraints => AdminConstraint.new @@ -144,6 +143,8 @@ def self.all add_to_serializer(:current_user, :show_donation_prompt?) do Patreon.show_donation_prompt_to_user?(object) end + + register_problem_check ProblemCheck::AccessTokenInvalid end # Authentication with Patreon diff --git a/spec/lib/api_spec.rb b/spec/lib/api_spec.rb index bfb9a9b..955cd94 100644 --- a/spec/lib/api_spec.rb +++ b/spec/lib/api_spec.rb @@ -13,23 +13,23 @@ def stub(status) stub_request(:get, url).to_return(content) end + before { SiteSetting.stubs(patreon_enabled: true) } + it "should add admin warning message for invalid api response" do stub(401) - expect(described_class.get(url)).to eq(error: I18n.t(described_class::INVALID_RESPONSE)) - expect( - AdminDashboardData.problem_message_check(described_class::ACCESS_TOKEN_INVALID).sub( - "%{base_path}", - "", - ), - ).to eq(I18n.t(described_class::ACCESS_TOKEN_INVALID, base_path: "")) + described_class.get(url) + + expect(ProblemCheckTracker[:access_token_invalid].blips).to eq(1) + expect(AdminNotice.find_by(identifier: :access_token_invalid).message).to eq( + I18n.t("dashboard.problem.access_token_invalid", base_path: Discourse.base_path), + ) + end + it "should not add admin warning message for valid api response" do stub(200) - expect(described_class.get(url)).to eq({}) - expect(AdminDashboardData.problem_message_check(described_class::ACCESS_TOKEN_INVALID)).to eq( - nil, - ) + expect(ProblemCheckTracker[:access_token_invalid].blips).to eq(0) end it "should add warning log" do