You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.5 KiB

  1. # frozen_string_literal: true
  2. class Form::AdminSettings
  3. include ActiveModel::Model
  4. KEYS = %i(
  5. site_contact_username
  6. site_contact_email
  7. site_title
  8. site_short_description
  9. site_description
  10. site_extended_description
  11. site_terms
  12. registrations_mode
  13. closed_registrations_message
  14. open_deletion
  15. timeline_preview
  16. show_staff_badge
  17. bootstrap_timeline_accounts
  18. theme
  19. min_invite_role
  20. activity_api_enabled
  21. peers_api_enabled
  22. show_known_fediverse_at_about_page
  23. preview_sensitive_media
  24. custom_css
  25. profile_directory
  26. thumbnail
  27. hero
  28. mascot
  29. spam_check_enabled
  30. trends
  31. show_domain_blocks
  32. show_domain_blocks_rationale
  33. ).freeze
  34. BOOLEAN_KEYS = %i(
  35. open_deletion
  36. timeline_preview
  37. show_staff_badge
  38. activity_api_enabled
  39. peers_api_enabled
  40. show_known_fediverse_at_about_page
  41. preview_sensitive_media
  42. profile_directory
  43. spam_check_enabled
  44. trends
  45. ).freeze
  46. UPLOAD_KEYS = %i(
  47. thumbnail
  48. hero
  49. mascot
  50. ).freeze
  51. attr_accessor(*KEYS)
  52. validates :site_short_description, :site_description, html: { wrap_with: :p }
  53. validates :site_extended_description, :site_terms, :closed_registrations_message, html: true
  54. validates :registrations_mode, inclusion: { in: %w(open approved none) }
  55. validates :min_invite_role, inclusion: { in: %w(disabled user moderator admin) }
  56. validates :site_contact_email, :site_contact_username, presence: true
  57. validates :site_contact_username, existing_username: true
  58. validates :bootstrap_timeline_accounts, existing_username: { multiple: true }
  59. validates :show_domain_blocks, inclusion: { in: %w(disabled users all) }
  60. validates :show_domain_blocks_rationale, inclusion: { in: %w(disabled users all) }
  61. def initialize(_attributes = {})
  62. super
  63. initialize_attributes
  64. end
  65. def save
  66. return false unless valid?
  67. KEYS.each do |key|
  68. value = instance_variable_get("@#{key}")
  69. if UPLOAD_KEYS.include?(key) && !value.nil?
  70. upload = SiteUpload.where(var: key).first_or_initialize(var: key)
  71. upload.update(file: value)
  72. else
  73. setting = Setting.where(var: key).first_or_initialize(var: key)
  74. setting.update(value: typecast_value(key, value))
  75. end
  76. end
  77. end
  78. private
  79. def initialize_attributes
  80. KEYS.each do |key|
  81. instance_variable_set("@#{key}", Setting.public_send(key)) if instance_variable_get("@#{key}").nil?
  82. end
  83. end
  84. def typecast_value(key, value)
  85. if BOOLEAN_KEYS.include?(key)
  86. value == '1'
  87. else
  88. value
  89. end
  90. end
  91. end