Extract filename and csv helper methods from `Form::Import` (#26129)

pull/2434/head
Matt Jankowski 2023-09-28 10:22:05 -04:00 committed by GitHub
parent 937dc42f10
commit 3060bfa4bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 8 deletions

View File

@ -43,14 +43,14 @@ class Form::Import
validate :validate_data
def guessed_type
return :muting if csv_data.headers.include?('Hide notifications')
return :following if csv_data.headers.include?('Show boosts') || csv_data.headers.include?('Notify on new posts') || csv_data.headers.include?('Languages')
return :following if data.original_filename&.start_with?('follows') || data.original_filename&.start_with?('following_accounts')
return :blocking if data.original_filename&.start_with?('blocks') || data.original_filename&.start_with?('blocked_accounts')
return :muting if data.original_filename&.start_with?('mutes') || data.original_filename&.start_with?('muted_accounts')
return :domain_blocking if data.original_filename&.start_with?('domain_blocks') || data.original_filename&.start_with?('blocked_domains')
return :bookmarks if data.original_filename&.start_with?('bookmarks')
return :lists if data.original_filename&.start_with?('lists')
return :muting if csv_headers_match?('Hide notifications')
return :following if csv_headers_match?('Show boosts') || csv_headers_match?('Notify on new posts') || csv_headers_match?('Languages')
return :following if file_name_matches?('follows') || file_name_matches?('following_accounts')
return :blocking if file_name_matches?('blocks') || file_name_matches?('blocked_accounts')
return :muting if file_name_matches?('mutes') || file_name_matches?('muted_accounts')
return :domain_blocking if file_name_matches?('domain_blocks') || file_name_matches?('blocked_domains')
return :bookmarks if file_name_matches?('bookmarks')
return :lists if file_name_matches?('lists')
end
# Whether the uploaded CSV file seems to correspond to a different import type than the one selected
@ -79,6 +79,14 @@ class Form::Import
private
def file_name_matches?(string)
data.original_filename&.start_with?(string)
end
def csv_headers_match?(string)
csv_data.headers.include?(string)
end
def default_csv_headers
case type.to_sym
when :following, :blocking, :muting