diff --git a/app/controllers/admin/settings/mascots_controller.rb b/app/controllers/admin/settings/mascots_controller.rb new file mode 100644 index 000000000..cd4e66f0a --- /dev/null +++ b/app/controllers/admin/settings/mascots_controller.rb @@ -0,0 +1,34 @@ +module Admin + module Settings + class MascotsController < Admin::ApplicationController + def create + errors = upsert_config(settings_params) + + if errors.none? + Audit::Logger.log(:internal, current_user, params.dup) + redirect_to admin_config_path, notice: "Site configuration was successfully updated." + else + redirect_to admin_config_path, alert: "😠#{errors.to_sentence}" + end + end + + def settings_params + params + .require(:settings_mascot) + .permit(*::Settings::Mascot.keys) + end + + def upsert_config(configs) + errors = [] + configs.each do |key, value| + ::Settings::Mascot.public_send("#{key}=", value) if value.present? + rescue ActiveRecord::RecordInvalid => e + errors << e.message + next + end + + errors + end + end + end +end diff --git a/app/controllers/chat_channels_controller.rb b/app/controllers/chat_channels_controller.rb index 9b75d8438..ecc56f786 100644 --- a/app/controllers/chat_channels_controller.rb +++ b/app/controllers/chat_channels_controller.rb @@ -57,10 +57,10 @@ class ChatChannelsController < ApplicationController flash[:error] = chat_channel.errors.full_messages.to_sentence else if chat_channel_params[:discoverable].to_i.zero? - ChatChannelMembership.create(user_id: SiteConfig.mascot_user_id, chat_channel_id: chat_channel.id, + ChatChannelMembership.create(user_id: Settings::Mascot.mascot_user_id, chat_channel_id: chat_channel.id, role: "member", status: "active") else - ChatChannelMembership.find_by(user_id: SiteConfig.mascot_user_id)&.destroy + ChatChannelMembership.find_by(user_id: Settings::Mascot.mascot_user_id)&.destroy end flash[:settings_notice] = "Channel settings updated." end @@ -76,10 +76,10 @@ class ChatChannelsController < ApplicationController message: "Channel settings updation failed. Try again later." }, success: :bad_request else if chat_channel_params[:discoverable] - ChatChannelMembership.create(user_id: SiteConfig.mascot_user_id, chat_channel_id: @chat_channel.id, + ChatChannelMembership.create(user_id: Settings::Mascot.mascot_user_id, chat_channel_id: @chat_channel.id, role: "member", status: "active") else - ChatChannelMembership.find_by(user_id: SiteConfig.mascot_user_id)&.destroy + ChatChannelMembership.find_by(user_id: Settings::Mascot.mascot_user_id)&.destroy end render json: { success: true, message: "Channel settings updated.", data: {} }, success: :ok end diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 154626e34..4c009ddfb 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -110,7 +110,7 @@ class CommentsController < ApplicationController response_template = ResponseTemplate.find(params[:response_template][:id]) authorize response_template, :moderator_create? - moderator = User.find(SiteConfig.mascot_user_id) + moderator = User.find(Settings::Mascot.mascot_user_id) @comment = Comment.new(permitted_attributes(Comment)) @comment.user_id = moderator.id @comment.body_markdown = response_template.content diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index 501fabc54..e43dc590b 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -73,7 +73,7 @@ class SearchController < ApplicationController def chat_channels user_ids = if chat_channel_params[:user_id].present? - [current_user.id, SiteConfig.mascot_user_id, chat_channel_params[:user_id]].reject(&:blank?) + [current_user.id, Settings::Mascot.mascot_user_id, chat_channel_params[:user_id]].reject(&:blank?) else [current_user.id] end diff --git a/app/lib/constants/settings/mascot.rb b/app/lib/constants/settings/mascot.rb new file mode 100644 index 000000000..4c325a70d --- /dev/null +++ b/app/lib/constants/settings/mascot.rb @@ -0,0 +1,32 @@ +module Constants + module Settings + module Mascot + DETAILS = { + footer_image_url: { + description: "Special cute mascot image used in the footer.", + placeholder: ::Constants::SiteConfig::IMAGE_PLACEHOLDER + }, + footer_image_width: { + description: "The footer mascot width will resized to this value, defaults to 52", + placeholder: "" + }, + footer_image_height: { + description: "The footer mascot height will be resized to this value, defaults to 120", + placeholder: "" + }, + image_description: { + description: "Used as the alt text for the mascot image", + placeholder: "" + }, + image_url: { + description: "Used as the mascot image.", + placeholder: ::Constants::SiteConfig::IMAGE_PLACEHOLDER + }, + mascot_user_id: { + description: "User ID of the Mascot account", + placeholder: "1" + } + }.freeze + end + end +end diff --git a/app/lib/constants/site_config.rb b/app/lib/constants/site_config.rb index a9ff8df5e..b2bdd4c4a 100644 --- a/app/lib/constants/site_config.rb +++ b/app/lib/constants/site_config.rb @@ -118,30 +118,6 @@ module Constants description: "Community Moderators Newsletter ID", placeholder: "" }, - mascot_footer_image_url: { - description: "Special cute mascot image used in the footer.", - placeholder: IMAGE_PLACEHOLDER - }, - mascot_footer_image_width: { - description: "The footer mascot width will resized to this value, defaults to 52", - placeholder: "" - }, - mascot_footer_image_height: { - description: "The footer mascot height will be resized to this value, defaults to 120", - placeholder: "" - }, - mascot_image_description: { - description: "Used as the alt text for the mascot image", - placeholder: "" - }, - mascot_image_url: { - description: "Used as the mascot image.", - placeholder: IMAGE_PLACEHOLDER - }, - mascot_user_id: { - description: "User ID of the Mascot account", - placeholder: "1" - }, meta_keywords: { description: "", placeholder: "List of valid keywords: comma separated, letters only e.g. engineering, development" diff --git a/app/models/article.rb b/app/models/article.rb index 565772369..e1059c896 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -181,7 +181,7 @@ class Article < ApplicationRecord published .where(user_id: User.with_role(:super_admin) .union(User.with_role(:admin)) - .union(id: [SiteConfig.staff_user_id, SiteConfig.mascot_user_id].compact) + .union(id: [SiteConfig.staff_user_id, Settings::Mascot.mascot_user_id].compact) .select(:id)).order(published_at: :desc).tagged_with(tag_name) } @@ -792,7 +792,7 @@ class Article < ApplicationRecord return unless SiteConfig.spam_trigger_terms.any? { |term| Regexp.new(term.downcase).match?(title.downcase) } Reaction.create( - user_id: SiteConfig.mascot_user_id, + user_id: Settings::Mascot.mascot_user_id, reactable_id: id, reactable_type: "Article", category: "vomit", @@ -802,7 +802,7 @@ class Article < ApplicationRecord user.add_role(:suspended) Note.create( - author_id: SiteConfig.mascot_user_id, + author_id: Settings::Mascot.mascot_user_id, noteable_id: user_id, noteable_type: "User", reason: "automatic_suspend", diff --git a/app/models/comment.rb b/app/models/comment.rb index 49e28857c..4d5c0fda6 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -287,7 +287,7 @@ class Comment < ApplicationRecord user.registered_at > 5.days.ago Reaction.create( - user_id: SiteConfig.mascot_user_id, + user_id: Settings::Mascot.mascot_user_id, reactable_id: id, reactable_type: "Comment", category: "vomit", @@ -297,7 +297,7 @@ class Comment < ApplicationRecord user.add_role(:suspended) Note.create( - author_id: SiteConfig.mascot_user_id, + author_id: Settings::Mascot.mascot_user_id, noteable_id: user_id, noteable_type: "User", reason: "automatic_suspend", diff --git a/app/models/reaction.rb b/app/models/reaction.rb index cbba6b6eb..62a95af05 100644 --- a/app/models/reaction.rb +++ b/app/models/reaction.rb @@ -130,7 +130,7 @@ class Reaction < ApplicationRecord end def negative_reaction_from_untrusted_user? - return if user&.any_admin? || user&.id == SiteConfig.mascot_user_id + return if user&.any_admin? || user&.id == Settings::Mascot.mascot_user_id negative? && !user.trusted end diff --git a/app/models/settings/mascot.rb b/app/models/settings/mascot.rb new file mode 100644 index 000000000..42d297915 --- /dev/null +++ b/app/models/settings/mascot.rb @@ -0,0 +1,25 @@ +module Settings + class Mascot < RailsSettings::Base + self.table_name = :settings_mascots + + # The configuration is cached, change this if you want to force update + # the cache, or call Settings::Mascot.clear_cache + cache_prefix { "v1" } + + field :footer_image_height, type: :integer, default: 120 + field :footer_image_url, type: :string, validates: { url: true } + field :footer_image_width, type: :integer, default: 52 + field :image_description, type: :string, default: "The community mascot" + field :image_url, + type: :string, + default: proc { URL.local_image("mascot.png") }, + validates: { url: true } + field :mascot_user_id, type: :integer, default: nil + + # NOTE: @citizen428 - This is duplicated for now, I will refactor once + # all settings models have been extracted. + def self.get_default(field) + get_field(field)[:default] + end + end +end diff --git a/app/models/user.rb b/app/models/user.rb index c7dd4945d..365a56cda 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -312,7 +312,7 @@ class User < ApplicationRecord end def self.mascot_account - find_by(id: SiteConfig.mascot_user_id) + find_by(id: Settings::Mascot.mascot_user_id) end def tag_line diff --git a/app/services/authentication/providers/apple.rb b/app/services/authentication/providers/apple.rb index 58812685b..5e5893481 100644 --- a/app/services/authentication/providers/apple.rb +++ b/app/services/authentication/providers/apple.rb @@ -20,7 +20,7 @@ module Authentication } if Rails.env.test? - user_data[:profile_image] = SiteConfig.mascot_image_url + user_data[:profile_image] = Settings::Mascot.image_url else user_data[:remote_profile_image_url] = Users::ProfileImageGenerator.call end diff --git a/app/services/users/create_mascot_account.rb b/app/services/users/create_mascot_account.rb index db1c8f701..4be3bd84f 100644 --- a/app/services/users/create_mascot_account.rb +++ b/app/services/users/create_mascot_account.rb @@ -4,7 +4,7 @@ module Users email: "mascot@forem.com", username: "mascot", name: "Mascot", - profile_image: SiteConfig.mascot_image_url, + profile_image: Settings::Mascot.image_url, confirmed_at: Time.current, registered_at: Time.current, password: SecureRandom.hex @@ -15,10 +15,10 @@ module Users end def call - raise "Mascot already set" if SiteConfig.mascot_user_id + raise "Mascot already set" if Settings::Mascot.mascot_user_id mascot = User.create!(mascot_params) - SiteConfig.mascot_user_id = mascot.id + Settings::Mascot.mascot_user_id = mascot.id end def mascot_params diff --git a/app/views/admin/configs/show.html.erb b/app/views/admin/configs/show.html.erb index b2c7d70d9..b44ca5049 100644 --- a/app/views/admin/configs/show.html.erb +++ b/app/views/admin/configs/show.html.erb @@ -768,7 +768,7 @@ <% end %> - <%= form_for(SiteConfig.new, url: admin_config_path) do |f| %> + <%= form_for(Settings::Mascot.new, url: admin_settings_mascots_path, html: { data: { testid: "mascotSectionForm" } }) do |f| %>