Add simple feature flag system (#34038)

Co-authored-by: Claire <claire.github-309c@sitedethib.com>
pull/2986/head
David Roetzel 2025-03-05 16:41:54 +01:00 committed by GitHub
parent cadda2f957
commit 8cf27d0fbb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 58 additions and 0 deletions

View File

@ -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'

View File

@ -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:

26
lib/mastodon/feature.rb Normal file
View File

@ -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

View File

@ -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