From 3f5c040d7cf6b6b5446de66316d06de88ea3905e Mon Sep 17 00:00:00 2001 From: David Roetzel Date: Mon, 16 Dec 2024 15:50:59 +0100 Subject: [PATCH] Handle full content lifecycle for subscribed fasps --- app/models/concerns/status/fasp_concern.rb | 12 +++++++- ...announce_content_lifecycle_event_worker.rb | 28 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 app/workers/fasp/announce_content_lifecycle_event_worker.rb diff --git a/app/models/concerns/status/fasp_concern.rb b/app/models/concerns/status/fasp_concern.rb index 748f22fb0f..84b95dcc5b 100644 --- a/app/models/concerns/status/fasp_concern.rb +++ b/app/models/concerns/status/fasp_concern.rb @@ -5,12 +5,22 @@ module Status::FaspConcern included do after_commit :announce_new_content_to_subscribed_fasp, on: :create + after_commit :announce_updated_content_to_subscribed_fasp, on: :update + after_commit :announce_deleted_content_to_subscribed_fasp, on: :destroy end private def announce_new_content_to_subscribed_fasp store_uri unless uri # TODO: solve this more elegantly - Fasp::AnnounceNewContentWorker.perform_async(uri) + Fasp::AnnounceContentLifecycleEventWorker.perform_async(uri, 'new') + end + + def announce_updated_content_to_subscribed_fasp + Fasp::AnnounceContentLifecycleEventWorker.perform_async(uri, 'update') + end + + def announce_deleted_content_to_subscribed_fasp + Fasp::AnnounceContentLifecycleEventWorker.perform_async(uri, 'delete') end end diff --git a/app/workers/fasp/announce_content_lifecycle_event_worker.rb b/app/workers/fasp/announce_content_lifecycle_event_worker.rb new file mode 100644 index 0000000000..f18b45fae0 --- /dev/null +++ b/app/workers/fasp/announce_content_lifecycle_event_worker.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +class Fasp::AnnounceContentLifecycleEventWorker + include Sidekiq::Worker + + sidekiq_options queue: 'fasp', retry: 5 + + def perform(uri, event_type) + Fasp::Subscription.includes(:fasp_provider).content.lifecycle.each do |subscription| + announce(subscription, uri, event_type) + end + end + + private + + def announce(subscription, uri, event_type) + Fasp::Request.new(subscription.fasp_provider).post('/data_sharing/v0/announcements', body: { + source: { + subscription: { + id: subscription.id.to_s, + }, + }, + category: 'content', + eventType: event_type, + objectUris: [uri], + }) + end +end