diff --git a/config/application.rb b/config/application.rb index 92976e87ab..88c7b029bf 100644 --- a/config/application.rb +++ b/config/application.rb @@ -35,6 +35,7 @@ require_relative '../lib/paperclip/type_corrector' require_relative '../lib/paperclip/response_with_limit_adapter' require_relative '../lib/terrapin/multi_pipe_extensions' require_relative '../lib/mastodon/snowflake' +require_relative '../lib/mastodon/feature' require_relative '../lib/mastodon/version' require_relative '../lib/mastodon/rack_middleware' require_relative '../lib/public_file_server_middleware' diff --git a/lib/mastodon/feature.rb b/lib/mastodon/feature.rb new file mode 100644 index 0000000000..eca8204a38 --- /dev/null +++ b/lib/mastodon/feature.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +module Mastodon::Feature + class << self + def enabled_features + @enabled_features ||= + (ENV['EXPERIMENTAL_FEATURES'] || '').split(',').map(&:strip) + end + + def method_missing(name) + if respond_to_missing?(name) + feature = name.to_s.sub(/_enabled\?$/, '') + enabled = enabled_features.include?(feature) + define_singleton_method(name) { enabled } + + return enabled + end + + super + end + + def respond_to_missing?(name) + name.to_s.end_with?('_enabled?') + end + end +end diff --git a/spec/lib/mastodon/feature_spec.rb b/spec/lib/mastodon/feature_spec.rb new file mode 100644 index 0000000000..d4290aa85d --- /dev/null +++ b/spec/lib/mastodon/feature_spec.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Mastodon::Feature do + around do |example| + ClimateControl.modify EXPERIMENTAL_FEATURES: 'fasp,fetch_all_replies' do + example.run + end + end + + describe '::fasp_enabled?' do + subject { described_class.fasp_enabled? } + + it { is_expected.to be true } + end + + describe '::fetch_all_replies_enabled?' do + subject { described_class.fetch_all_replies_enabled? } + + it { is_expected.to be true } + end + + describe '::unspecified_feature_enabled?' do + subject { described_class.unspecified_feature_enabled? } + + it { is_expected.to be false } + end +end