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.

106 lines
2.6 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. noindex
  34. ).freeze
  35. BOOLEAN_KEYS = %i(
  36. open_deletion
  37. timeline_preview
  38. show_staff_badge
  39. activity_api_enabled
  40. peers_api_enabled
  41. show_known_fediverse_at_about_page
  42. preview_sensitive_media
  43. profile_directory
  44. spam_check_enabled
  45. trends
  46. noindex
  47. ).freeze
  48. UPLOAD_KEYS = %i(
  49. thumbnail
  50. hero
  51. mascot
  52. ).freeze
  53. attr_accessor(*KEYS)
  54. validates :site_short_description, :site_description, html: { wrap_with: :p }
  55. validates :site_extended_description, :site_terms, :closed_registrations_message, html: true
  56. validates :registrations_mode, inclusion: { in: %w(open approved none) }
  57. validates :min_invite_role, inclusion: { in: %w(disabled user moderator admin) }
  58. validates :site_contact_email, :site_contact_username, presence: true
  59. validates :site_contact_username, existing_username: true
  60. validates :bootstrap_timeline_accounts, existing_username: { multiple: true }
  61. validates :show_domain_blocks, inclusion: { in: %w(disabled users all) }
  62. validates :show_domain_blocks_rationale, inclusion: { in: %w(disabled users all) }
  63. def initialize(_attributes = {})
  64. super
  65. initialize_attributes
  66. end
  67. def save
  68. return false unless valid?
  69. KEYS.each do |key|
  70. value = instance_variable_get("@#{key}")
  71. if UPLOAD_KEYS.include?(key) && !value.nil?
  72. upload = SiteUpload.where(var: key).first_or_initialize(var: key)
  73. upload.update(file: value)
  74. else
  75. setting = Setting.where(var: key).first_or_initialize(var: key)
  76. setting.update(value: typecast_value(key, value))
  77. end
  78. end
  79. end
  80. private
  81. def initialize_attributes
  82. KEYS.each do |key|
  83. instance_variable_set("@#{key}", Setting.public_send(key)) if instance_variable_get("@#{key}").nil?
  84. end
  85. end
  86. def typecast_value(key, value)
  87. if BOOLEAN_KEYS.include?(key)
  88. value == '1'
  89. else
  90. value
  91. end
  92. end
  93. end