闭社主体 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.

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