From 8cf27d0fbbc6ad74c7adde45b6194e5960d3ffae Mon Sep 17 00:00:00 2001 From: David Roetzel Date: Wed, 5 Mar 2025 16:41:54 +0100 Subject: [PATCH] Add simple feature flag system (#34038) Co-authored-by: Claire --- config/application.rb | 1 + config/mastodon.yml | 1 + lib/mastodon/feature.rb | 26 ++++++++++++++++++++++++++ spec/lib/mastodon/feature_spec.rb | 30 ++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+) create mode 100644 lib/mastodon/feature.rb create mode 100644 spec/lib/mastodon/feature_spec.rb 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/config/mastodon.yml b/config/mastodon.yml index a4442e873c..c1c1740368 100644 --- a/config/mastodon.yml +++ b/config/mastodon.yml @@ -1,5 +1,6 @@ --- shared: + experimental_features: <%= ENV.fetch('EXPERIMENTAL_FEATURES', nil) %> self_destruct_value: <%= ENV.fetch('SELF_DESTRUCT', nil) %> software_update_url: <%= ENV.fetch('UPDATE_CHECK_URL', 'https://api.joinmastodon.org/update-check') %> source: diff --git a/lib/mastodon/feature.rb b/lib/mastodon/feature.rb new file mode 100644 index 0000000000..18e6dc9639 --- /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 ||= + (Rails.configuration.x.mastodon.experimental_features || '').split(',').map(&:strip) + end + + def method_missing(name) + if respond_to_missing?(name) + feature = name.to_s.delete_suffix('_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..a7fe4fe90b --- /dev/null +++ b/spec/lib/mastodon/feature_spec.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Mastodon::Feature do + around do |example| + original_value = Rails.configuration.x.mastodon.experimental_features + Rails.configuration.x.mastodon.experimental_features = 'fasp,fetch_all_replies' + example.run + Rails.configuration.x.mastodon.experimental_features = original_value + 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