Reapply changes to how disabling modeling works

```shell
git cherry-pick d82c7f817a --no-commit
```
This commit is contained in:
Emi Matchu 2024-11-10 11:39:51 -08:00
parent 54b25ef08e
commit 3242981eb2
6 changed files with 17 additions and 5 deletions

View file

@ -1,12 +1,10 @@
class PetsController < ApplicationController
rescue_from Neopets::CustomPets::PetNotFound, with: :pet_not_found
rescue_from Neopets::CustomPets::DownloadError, with: :pet_download_error
rescue_from Pet::ModelingDisabled, with: :modeling_disabled
rescue_from Pet::UnexpectedDataFormat, with: :unexpected_data_format
def load
# Uncomment this to temporarily disable modeling for most users.
# return modeling_disabled unless user_signed_in? && current_user.admin?
raise Neopets::CustomPets::PetNotFound unless params[:name]
@pet = Pet.load(params[:name])
points = contribute(current_user, @pet)

View file

@ -4,6 +4,8 @@ class Pet < ApplicationRecord
attr_reader :items, :pet_state, :alt_style
def load!(timeout: nil)
raise ModelingDisabled unless Rails.configuration.modeling_enabled
viewer_data_hash = Neopets::CustomPets.fetch_viewer_data(name, timeout:)
use_viewer_data(ViewerData.new(viewer_data_hash))
end
@ -59,6 +61,7 @@ class Pet < ApplicationRecord
end
class UnexpectedDataFormat < RuntimeError;end
class ModelingDisabled < RuntimeError;end
# A representation of a Neopets::CustomPets viewer data response, translated
# to DTI's database models!
@ -153,4 +156,3 @@ class Pet < ApplicationRecord
end
end
end

View file

@ -103,6 +103,10 @@ Rails.application.configure do
# Allow connections on Vagrant's private network.
config.web_console.permissions = '10.0.2.2'
# Allow pets to model new data. (If modeling is ever broken, disable this in
# production while we fix it!)
config.modeling_enabled = true
# Use a local copy of Impress 2020, presumably running on port 4000. (Can
# override this with the IMPRESS_2020_ORIGIN environment variable!)
config.impress_2020_origin = ENV.fetch("IMPRESS_2020_ORIGIN",

View file

@ -122,6 +122,10 @@ Rails.application.configure do
# Skip DNS rebinding protection for the default health check endpoint.
# config.host_authorization = { exclude: ->(request) { request.path == "/up" } }
# Allow pets to model new data. (If modeling is ever broken, disable this
# here while we fix it!)
config.modeling_enabled = false
# Use the live copy of Impress 2020. (Can override this with the
# IMPRESS_2020_ORIGIN environment variable!)
config.impress_2020_origin = ENV.fetch("IMPRESS_2020_ORIGIN",

View file

@ -62,6 +62,10 @@ Rails.application.configure do
# Raise error when a before_action's only/except options reference missing actions
config.action_controller.raise_on_missing_callback_actions = true
# Allow pets to model new data. (If modeling is ever broken, disable this in
# production while we fix it!)
config.modeling_enabled = true
# Use a local copy of Impress 2020, presumably running on port 4000. (Can
# override this with the IMPRESS_2020_ORIGIN environment variable!)
config.impress_2020_origin = ENV.fetch("IMPRESS_2020_ORIGIN",

View file

@ -605,7 +605,7 @@ RSpec.describe Pet, type: :model do
context "when modeling is disabled" do
before { allow(Rails.configuration).to receive(:modeling_enabled) { false } }
pending("raises an error") do
it("raises an error") do
expect { Pet.load("matts_bat") }.to raise_error(Pet::ModelingDisabled)
end
end