Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fetch all webhook subscribers once (#13044)

Damian Legawiecc2a7da8a0fa9
diff --git a/api/app/models/concerns/spree/webhooks/has_webhooks.rb b/api/app/models/concerns/spree/webhooks/has_webhooks.rb
index 7768ea81a2..438a19af6d 100644
--- a/api/app/models/concerns/spree/webhooks/has_webhooks.rb
+++ b/api/app/models/concerns/spree/webhooks/has_webhooks.rb
@@ -82,7 +82,9 @@ module Spree
end
def no_webhooks_subscribers?(event_name)
- !Spree::Webhooks::Subscriber.active.with_urls_for(event_name).exists?
+ Spree::Current.webhooks_subscribers.none? do |subscriber|
+ subscriber.supports_event?(event_name)
+ end
end
def webhooks_request_options
diff --git a/api/app/models/spree/webhooks/subscriber.rb b/api/app/models/spree/webhooks/subscriber.rb
index c5da0d81a9..7e01dd20c9 100644
--- a/api/app/models/spree/webhooks/subscriber.rb
+++ b/api/app/models/spree/webhooks/subscriber.rb
@@ -28,6 +28,14 @@ module Spree
events.order(:created_at).last&.created_at
end
+ # Returns true if the subscriber supports the given event
+ #
+ # @param event [String] The event to check, e.g. 'product.create'
+ # @return [Boolean]
+ def supports_event?(event)
+ subscriptions.include?(event) || subscriptions.include?('*')
+ end
+
def self.with_urls_for(event)
where(
case ActiveRecord::Base.connection.adapter_name
diff --git a/api/app/services/spree/webhooks/subscribers/queue_requests.rb b/api/app/services/spree/webhooks/subscribers/queue_requests.rb
index 0eed06474a..1dfd27ed17 100644
--- a/api/app/services/spree/webhooks/subscribers/queue_requests.rb
+++ b/api/app/services/spree/webhooks/subscribers/queue_requests.rb
@@ -15,7 +15,11 @@ module Spree
private
def filtered_subscribers(event_name, webhook_payload_body, record, options)
- Spree::Webhooks::Subscriber.active.with_urls_for(event_name)
+ Spree::Current.webhooks_subscribers.map do |subscriber|
+ if subscriber.supports_event?(event_name)
+ subscriber
+ end
+ end.compact
end
end
end
diff --git a/core/app/models/spree/current.rb b/core/app/models/spree/current.rb
index e5378a93ff..6d3cc42180 100644
--- a/core/app/models/spree/current.rb
+++ b/core/app/models/spree/current.rb
@@ -1,9 +1,13 @@
module Spree
class Current < ::ActiveSupport::CurrentAttributes
- attribute :store
+ attribute :store, :webhooks_subscribers
def store
super || Spree::Store.default
end
+
+ def webhooks_subscribers
+ super || store.active_webhooks_subscribers
+ end
end
end
diff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rb
index 198d677d1d..33ed8d5376 100644
--- a/core/app/models/spree/store.rb
+++ b/core/app/models/spree/store.rb
@@ -206,7 +206,11 @@ module Spree
delegate :iso, to: :default_country, prefix: true, allow_nil: true
def self.current(url = nil)
- Spree::Dependencies.current_store_finder.constantize.new(url: url).execute || Spree::Current.store
+ if url.present?
+ Spree::Dependencies.current_store_finder.constantize.new(url: url).execute
+ else
+ Spree::Current.store
+ end
end
# FIXME: we need to drop `or_initialize` in v5
@@ -414,6 +418,13 @@ module Spree
end
end
+ # Returns all active webhooks subscribers for the store
+ #
+ # @return [Array<Spree::Webhooks::Subscriber>]
+ def active_webhooks_subscribers
+ @active_webhooks_subscribers ||= Spree::Webhooks::Subscriber.active
+ end
+
private
def countries_available_for_checkout_cache_key