Skip to content

Commit

Permalink
Add: #374 拡張ドメインブロックのインポート・エクスポートについてkmyblue独自の項目のサポート (#520)
Browse files Browse the repository at this point in the history
* Fix: #374 拡張ドメインブロックのインポート・エクスポートについてkmyblue独自の項目が反映されない問題

* Fix test
  • Loading branch information
kmycode authored Feb 5, 2024
1 parent 64a149b commit 4e1e675
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 10 deletions.
53 changes: 50 additions & 3 deletions app/controllers/admin/export_domain_blocks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,18 @@ def import
reject_reports: row.fetch('#reject_reports', false),
private_comment: @global_private_comment,
public_comment: row['#public_comment'],
obfuscate: row.fetch('#obfuscate', false))
obfuscate: row.fetch('#obfuscate', false),
reject_favourite: row.fetch('#reject_favourite', false),
reject_reply: row.fetch('#reject_reply', false),
reject_send_sensitive: row.fetch('#reject_send_sensitive', false),
reject_hashtag: row.fetch('#reject_hashtag', false),
reject_straight_follow: row.fetch('#reject_straight_follow', false),
reject_new_follow: row.fetch('#reject_new_follow', false),
hidden: row.fetch('#hidden', false),
detect_invalid_subscription: row.fetch('#detect_invalid_subscription', false),
reject_reply_exclude_followers: row.fetch('#reject_reply_exclude_followers', false),
reject_friend: row.fetch('#reject_friend', false),
block_trends: row.fetch('#block_trends', false))

if domain_block.invalid?
flash.now[:alert] = I18n.t('admin.export_domain_blocks.invalid_domain_block', error: domain_block.errors.full_messages.join(', '))
Expand Down Expand Up @@ -67,13 +78,49 @@ def export_filename
end

def export_headers
%w(#domain #severity #reject_media #reject_reports #public_comment #obfuscate)
%w(
#domain
#severity
#reject_media
#reject_reports
#public_comment
#obfuscate
#reject_favourite
#reject_reply
#reject_send_sensitive
#reject_hashtag
#reject_straight_follow
#reject_new_follow
#hidden
#detect_invalid_subscription
#reject_reply_exclude_followers
#reject_friend
#block_trends
)
end

def export_data
CSV.generate(headers: export_headers, write_headers: true) do |content|
DomainBlock.with_limitations.order(id: :asc).each do |instance|
content << [instance.domain, instance.severity, instance.reject_media, instance.reject_reports, instance.public_comment, instance.obfuscate]
content << [
instance.domain,
instance.severity,
instance.reject_media,
instance.reject_reports,
instance.public_comment,
instance.obfuscate,
instance.reject_favourite,
instance.reject_reply,
instance.reject_send_sensitive,
instance.reject_hashtag,
instance.reject_straight_follow,
instance.reject_new_follow,
instance.hidden,
instance.detect_invalid_subscription,
instance.reject_reply_exclude_followers,
instance.reject_friend,
instance.block_trends,
]
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion app/models/admin/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ def csv_data
field&.strip
when '#severity'
field&.strip&.to_sym
when '#reject_media', '#reject_favourite', '#reject_reply', '#reject_reports', '#obfuscate'
when '#reject_media', '#reject_reports', '#obfuscate', '#reject_favourite', '#reject_reply', '#reject_send_sensitive',
'#reject_hashtag', '#reject_straight_follow', '#reject_new_follow', '#hidden', '#detect_invalid_subscription',
'#reject_reply_exclude_followers', '#reject_friend', '#block_trends'
ActiveModel::Type::Boolean.new.cast(field)
else
field
Expand Down
12 changes: 11 additions & 1 deletion spec/controllers/admin/export_domain_blocks_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
Fabricate(:domain_block, domain: 'bad.domain', severity: 'silence', public_comment: 'bad server')
Fabricate(:domain_block, domain: 'worse.domain', severity: 'suspend', reject_media: true, reject_reports: true, public_comment: 'worse server', obfuscate: true)
Fabricate(:domain_block, domain: 'reject.media', severity: 'noop', reject_media: true, public_comment: 'reject media and test unicode characters ♥')
Fabricate(:domain_block, domain: 'little.spam', severity: 'noop', public_comment: 'has some spams', reject_favourite: true, reject_reply: true, reject_straight_follow: true)
Fabricate(:domain_block, domain: 'no.op', severity: 'noop', public_comment: 'noop')

get :export, params: { format: :csv }
Expand All @@ -43,7 +44,16 @@ def domain_blocks_csv_file
end

it 'renders page with expected domain blocks' do
expect(assigns(:domain_blocks).map { |block| [block.domain, block.severity.to_sym] }).to contain_exactly(['bad.domain', :silence], ['worse.domain', :suspend], ['reject.media', :noop])
expect(assigns(:domain_blocks).map { |block| [block.domain, block.severity.to_sym] }).to contain_exactly(['bad.domain', :silence], ['worse.domain', :suspend], ['reject.media', :noop], ['little.spam', :noop])
end

it 'renders page with extended domain blocks' do
expect(assigns(:domain_blocks).map { |block| [block.domain, block.reject_favourite, block.reject_reply, block.reject_friend] }).to contain_exactly(
['bad.domain', false, false, false],
['worse.domain', false, false, false],
['reject.media', false, false, false],
['little.spam', true, true, false]
)
end

it 'returns http success' do
Expand Down
9 changes: 5 additions & 4 deletions spec/fixtures/files/domain_blocks.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#domain,#severity,#reject_media,#reject_reports,#public_comment,#obfuscate
bad.domain,silence,false,false,bad server,false
worse.domain,suspend,true,true,worse server,true
reject.media,noop,true,false,reject media and test unicode characters ♥,false
#domain,#severity,#reject_media,#reject_reports,#public_comment,#obfuscate,#reject_favourite,#reject_reply,#reject_send_sensitive,#reject_hashtag,#reject_straight_follow,#reject_new_follow,#hidden,#detect_invalid_subscription,#reject_reply_exclude_followers,#reject_friend,#block_trends
bad.domain,silence,false,false,bad server,false,false,false,false,false,false,false,false,false,false,false,false
worse.domain,suspend,true,true,worse server,true,false,false,false,false,false,false,false,false,false,false,false
reject.media,noop,true,false,reject media and test unicode characters ♥,false,false,false,false,false,false,false,false,false,false,false,false
little.spam,noop,false,false,has some spams,false,true,true,false,false,true,false,false,false,false,false,false
2 changes: 1 addition & 1 deletion spec/models/form/import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@
it_behaves_like 'on successful import', 'blocking', 'merge', 'imports.txt', (%w([email protected] [email protected]).map { |acct| { 'acct' => acct } })
it_behaves_like 'on successful import', 'blocking', 'overwrite', 'imports.txt', (%w([email protected] [email protected]).map { |acct| { 'acct' => acct } })
it_behaves_like 'on successful import', 'muting', 'merge', 'imports.txt', (%w([email protected] [email protected]).map { |acct| { 'acct' => acct } })
it_behaves_like 'on successful import', 'domain_blocking', 'merge', 'domain_blocks.csv', (%w(bad.domain worse.domain reject.media).map { |domain| { 'domain' => domain } })
it_behaves_like 'on successful import', 'domain_blocking', 'merge', 'domain_blocks.csv', (%w(bad.domain worse.domain reject.media little.spam).map { |domain| { 'domain' => domain } })
it_behaves_like 'on successful import', 'bookmarks', 'merge', 'bookmark-imports.txt', (%w(https://example.com/statuses/1312 https://local.com/users/foo/statuses/42 https://unknown-remote.com/users/bar/statuses/1 https://example.com/statuses/direct).map { |uri| { 'uri' => uri } })

it_behaves_like 'on successful import', 'following', 'merge', 'following_accounts.csv', [
Expand Down

0 comments on commit 4e1e675

Please sign in to comment.