1
0

import_validator.rb 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # frozen_string_literal: true
  2. class ImportValidator < ActiveModel::Validator
  3. KNOWN_HEADERS = [
  4. 'Account address',
  5. '#domain',
  6. '#uri',
  7. ].freeze
  8. def validate(import)
  9. return if import.type.blank? || import.data.blank?
  10. # We parse because newlines could be part of individual rows. This
  11. # runs on create so we should be reading the local file here before
  12. # it is uploaded to object storage or moved anywhere...
  13. csv_data = CSV.parse(import.data.queued_for_write[:original].read)
  14. row_count = csv_data.size
  15. row_count -= 1 if KNOWN_HEADERS.include?(csv_data.first&.first)
  16. import.errors.add(:data, I18n.t('imports.errors.over_rows_processing_limit', count: ImportService::ROWS_PROCESSING_LIMIT)) if row_count > ImportService::ROWS_PROCESSING_LIMIT
  17. case import.type
  18. when 'following'
  19. validate_following_import(import, row_count)
  20. end
  21. end
  22. private
  23. def validate_following_import(import, row_count)
  24. base_limit = FollowLimitValidator.limit_for_account(import.account)
  25. limit = begin
  26. if import.overwrite?
  27. base_limit
  28. else
  29. base_limit - import.account.following_count
  30. end
  31. end
  32. import.errors.add(:data, I18n.t('users.follow_limit_reached', limit: base_limit)) if row_count > limit
  33. end
  34. end