diff --git a/app/models/activity.rb b/app/models/activity.rb index 3a5f0c5be..0a66fe3cf 100644 --- a/app/models/activity.rb +++ b/app/models/activity.rb @@ -74,7 +74,8 @@ class Activity < ApplicationRecord breadcrumbs: {} ) - after_create ->(activity) { Activities::DispatchWebhooksJob.perform_later(activity) } + after_create ->(activity) { Activities::DispatchWebhooksJob.perform_later(activity) }, + if: -> { Rails.application.config.x.webhooks_enabled } def self.activity_types_list activity_list = type_ofs.map do |key, value| diff --git a/app/models/webhook.rb b/app/models/webhook.rb index 2388f0ebc..4fc61105f 100644 --- a/app/models/webhook.rb +++ b/app/models/webhook.rb @@ -6,12 +6,19 @@ class Webhook < ApplicationRecord belongs_to :activity_filter validates :http_method, presence: true validates :url, presence: true + validate :enabled? validate :valid_url scope :active, -> { where(active: true) } private + def enabled? + unless Rails.application.config.x.webhooks_enabled + errors.add(:configuration, I18n.t('activerecord.errors.models.webhook.attributes.configuration.disabled')) + end + end + def valid_url unless /\A#{URI::DEFAULT_PARSER.make_regexp(%w(http https))}\z/.match?(url) errors.add(:url, I18n.t('activerecord.errors.models.webhook.attributes.url.not_valid')) diff --git a/app/permissions/organization.rb b/app/permissions/organization.rb index e702e0347..7a11acb37 100644 --- a/app/permissions/organization.rb +++ b/app/permissions/organization.rb @@ -14,7 +14,7 @@ module Organization end can :create_acitivity_filters do - true + Rails.application.config.x.webhooks_enabled end end end diff --git a/config/application.rb b/config/application.rb index 2451162ff..eccd405fa 100644 --- a/config/application.rb +++ b/config/application.rb @@ -40,6 +40,8 @@ module Scinote # Max uploaded file size in MB config.x.file_max_size_mb = (ENV['FILE_MAX_SIZE_MB'] || 50).to_i + config.x.webhooks_enabled = ENV['ENABLE_WEBHOOKS'] == 'true' + # Logging config.log_formatter = proc do |severity, datetime, progname, msg| "[#{datetime}] #{severity}: #{msg}\n" diff --git a/config/locales/en.yml b/config/locales/en.yml index ddceb65ff..51b91ee5f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -148,6 +148,8 @@ en: per_column_limit: "Too many items in the column" webhook: attributes: + configuration: + disabled: 'Webhooks are disabled' url: not_valid: 'Not valid URL'