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.

120 lines
2.8 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. flavour
  19. skin
  20. min_invite_role
  21. activity_api_enabled
  22. peers_api_enabled
  23. show_known_fediverse_at_about_page
  24. preview_sensitive_media
  25. custom_css
  26. profile_directory
  27. hide_followers_count
  28. enable_keybase
  29. flavour_and_skin
  30. thumbnail
  31. hero
  32. mascot
  33. show_reblogs_in_public_timelines
  34. show_replies_in_public_timelines
  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. hide_followers_count
  46. enable_keybase
  47. show_reblogs_in_public_timelines
  48. show_replies_in_public_timelines
  49. ).freeze
  50. UPLOAD_KEYS = %i(
  51. thumbnail
  52. hero
  53. mascot
  54. ).freeze
  55. PSEUDO_KEYS = %i(
  56. flavour_and_skin
  57. ).freeze
  58. attr_accessor(*KEYS)
  59. validates :site_short_description, :site_description, html: { wrap_with: :p }
  60. validates :site_extended_description, :site_terms, :closed_registrations_message, html: true
  61. validates :registrations_mode, inclusion: { in: %w(open approved none) }
  62. validates :min_invite_role, inclusion: { in: %w(disabled user moderator admin) }
  63. validates :site_contact_email, :site_contact_username, presence: true
  64. validates :site_contact_username, existing_username: true
  65. validates :bootstrap_timeline_accounts, existing_username: { multiple: true }
  66. def initialize(_attributes = {})
  67. super
  68. initialize_attributes
  69. end
  70. def save
  71. return false unless valid?
  72. KEYS.each do |key|
  73. next if PSEUDO_KEYS.include?(key)
  74. value = instance_variable_get("@#{key}")
  75. if UPLOAD_KEYS.include?(key) && !value.nil?
  76. upload = SiteUpload.where(var: key).first_or_initialize(var: key)
  77. upload.update(file: value)
  78. else
  79. setting = Setting.where(var: key).first_or_initialize(var: key)
  80. setting.update(value: typecast_value(key, value))
  81. end
  82. end
  83. end
  84. def flavour_and_skin
  85. "#{Setting.flavour}/#{Setting.skin}"
  86. end
  87. def flavour_and_skin=(value)
  88. @flavour, @skin = value.split('/', 2)
  89. end
  90. private
  91. def initialize_attributes
  92. KEYS.each do |key|
  93. next if PSEUDO_KEYS.include?(key)
  94. instance_variable_set("@#{key}", Setting.public_send(key)) if instance_variable_get("@#{key}").nil?
  95. end
  96. end
  97. def typecast_value(key, value)
  98. if BOOLEAN_KEYS.include?(key)
  99. value == '1'
  100. else
  101. value
  102. end
  103. end
  104. end