闭社主体 forked from https://github.com/tootsuite/mastodon
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.

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