admin_settings.rb 2.6 KB

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