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.

96 lines
2.2 KiB

  1. # frozen_string_literal: true
  2. module Admin
  3. class SettingsController < BaseController
  4. ADMIN_SETTINGS = %w(
  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. open_registrations
  13. closed_registrations_message
  14. open_deletion
  15. timeline_preview
  16. show_staff_badge
  17. bootstrap_timeline_accounts
  18. flavour
  19. skin
  20. flavour_and_skin
  21. thumbnail
  22. hero
  23. mascot
  24. min_invite_role
  25. activity_api_enabled
  26. peers_api_enabled
  27. show_known_fediverse_at_about_page
  28. preview_sensitive_media
  29. custom_css
  30. profile_directory
  31. hide_followers_count
  32. ).freeze
  33. BOOLEAN_SETTINGS = %w(
  34. open_registrations
  35. open_deletion
  36. timeline_preview
  37. show_staff_badge
  38. activity_api_enabled
  39. peers_api_enabled
  40. show_known_fediverse_at_about_page
  41. preview_sensitive_media
  42. profile_directory
  43. hide_followers_count
  44. ).freeze
  45. UPLOAD_SETTINGS = %w(
  46. thumbnail
  47. hero
  48. mascot
  49. ).freeze
  50. def edit
  51. authorize :settings, :show?
  52. @admin_settings = Form::AdminSettings.new
  53. end
  54. def update
  55. authorize :settings, :update?
  56. settings = settings_params
  57. flavours_and_skin = settings.delete('flavour_and_skin')
  58. if flavours_and_skin
  59. settings['flavour'], settings['skin'] = flavours_and_skin.split('/', 2)
  60. end
  61. settings.each do |key, value|
  62. if UPLOAD_SETTINGS.include?(key)
  63. upload = SiteUpload.where(var: key).first_or_initialize(var: key)
  64. upload.update(file: value)
  65. else
  66. setting = Setting.where(var: key).first_or_initialize(var: key)
  67. setting.update(value: value_for_update(key, value))
  68. end
  69. end
  70. flash[:notice] = I18n.t('generic.changes_saved_msg')
  71. redirect_to edit_admin_settings_path
  72. end
  73. private
  74. def settings_params
  75. params.require(:form_admin_settings).permit(ADMIN_SETTINGS)
  76. end
  77. def value_for_update(key, value)
  78. if BOOLEAN_SETTINGS.include?(key)
  79. value == '1'
  80. else
  81. value
  82. end
  83. end
  84. end
  85. end