Skip to content

Commit

Permalink
Refactor: インスタンスで利用可能な機能を検出する処理
Browse files Browse the repository at this point in the history
  • Loading branch information
kmycode committed Apr 18, 2024
1 parent 957caea commit 80823f6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
15 changes: 9 additions & 6 deletions app/models/instance_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,18 @@ def local_features

def feature_available?(info, softwares, feature_name)
return false if info.nil?
return true if softwares.include?(info['software'])

return false unless info.data.is_a?(Hash)
return false unless info.data['metadata'].is_a?(Hash)
softwares.include?(software_name(info)) || metadata_features(info)&.include?(feature_name) || false
end

def metadata_features(info)
return nil unless info.data.is_a?(Hash) && info.data['metadata'].is_a?(Hash) && info.data['metadata']['features'].is_a?(Array)

features = info.data.dig('metadata', 'features')
return false unless features.is_a?(Array)
info.data['metadata']['features']
end

features.include?(feature_name)
def software_name(info)
info.software
end
end

Expand Down
16 changes: 15 additions & 1 deletion spec/models/instance_info_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'rails_helper'

RSpec.describe InstanceInfo do
describe '.available_features' do
describe '.available_features#emoji_reaction' do
subject { described_class.available_features('example.com')[:emoji_reaction] }

it 'availables if local account' do
Expand Down Expand Up @@ -45,4 +45,18 @@
expect(subject).to be false
end
end

describe '.available_features#circle' do
subject { described_class.available_features('example.com')[:circle] }

it 'does not available if misskey server' do
Fabricate(:instance_info, domain: 'example.com', software: 'misskey')
expect(subject).to be false
end

it 'availables if misskey server with features' do
Fabricate(:instance_info, domain: 'example.com', software: 'misskey', data: { metadata: { features: ['circle'] } })
expect(subject).to be true
end
end
end

0 comments on commit 80823f6

Please sign in to comment.