diff --git a/lib/radiator/event_store.ex b/lib/radiator/event_store.ex index 3e002d35..288ff252 100644 --- a/lib/radiator/event_store.ex +++ b/lib/radiator/event_store.ex @@ -13,7 +13,7 @@ defmodule Radiator.EventStore do data: Event.payload(event), event_type: Event.event_type(event), uuid: convert_to_uuid(event.event_id), - user_id: event.user_id + user_id: Event.user_id(event) }) event diff --git a/lib/radiator/event_store/event_data.ex b/lib/radiator/event_store/event_data.ex index 90286a5d..6e460b88 100644 --- a/lib/radiator/event_store/event_data.ex +++ b/lib/radiator/event_store/event_data.ex @@ -20,6 +20,6 @@ defmodule Radiator.EventStore.EventData do def changeset(event, attrs) do event |> cast(attrs, [:uuid, :event_type, :data, :user_id]) - |> validate_required([:uuid, :event_type, :user_id]) + |> validate_required([:uuid, :event_type]) end end diff --git a/lib/radiator/outline/event.ex b/lib/radiator/outline/event.ex index 13b2fab3..a96fc982 100644 --- a/lib/radiator/outline/event.ex +++ b/lib/radiator/outline/event.ex @@ -7,7 +7,8 @@ defmodule Radiator.Outline.Event do NodeContentChangedEvent, NodeDeletedEvent, NodeInsertedEvent, - NodeMovedEvent + NodeMovedEvent, + UrlsAnalyzedEvent } def payload(%NodeInsertedEvent{} = event) do @@ -40,10 +41,21 @@ defmodule Radiator.Outline.Event do } end + def payload(%UrlsAnalyzedEvent{} = event) do + %{ + node_id: event.node_id, + urls: event.urls + } + end + + def user_id(%UrlsAnalyzedEvent{}), do: nil + def user_id(event), do: event.user_id + def event_type(%NodeInsertedEvent{} = _event), do: "NodeInsertedEvent" def event_type(%NodeContentChangedEvent{} = _event), do: "NodeContentChangedEvent" def event_type(%NodeDeletedEvent{} = _event), do: "NodeDeletedEvent" def event_type(%NodeMovedEvent{} = _event), do: "NodeMovedEvent" + def event_type(%UrlsAnalyzedEvent{} = _event), do: "UrlsAnalyzedEvent" def episode_id(%{episode_id: episode_id}), do: episode_id end diff --git a/lib/radiator/outline/event/urls_analyzed_event.ex b/lib/radiator/outline/event/urls_analyzed_event.ex new file mode 100644 index 00000000..0323803d --- /dev/null +++ b/lib/radiator/outline/event/urls_analyzed_event.ex @@ -0,0 +1,4 @@ +defmodule Radiator.Outline.Event.UrlsAnalyzedEvent do + @moduledoc false + defstruct [:node_id, :urls, :episode_id, event_id: Ecto.UUID.generate()] +end diff --git a/lib/radiator/resources/node_changed_worker.ex b/lib/radiator/resources/node_changed_worker.ex index 75d7650b..22780fd7 100644 --- a/lib/radiator/resources/node_changed_worker.ex +++ b/lib/radiator/resources/node_changed_worker.ex @@ -3,7 +3,10 @@ defmodule Radiator.Resources.NodeChangedWorker do job to extract urls from content and persist URLs """ alias __MODULE__ + alias Radiator.EventStore alias Radiator.NodeAnalyzer + alias Radiator.Outline.Dispatch + alias Radiator.Outline.Event.UrlsAnalyzedEvent alias Radiator.Outline.NodeRepository alias Radiator.Resources @@ -16,13 +19,20 @@ defmodule Radiator.Resources.NodeChangedWorker do def perform(node_id) do analyzers = [Radiator.NodeAnalyzer.UrlAnalyzer] + node = NodeRepository.get_node!(node_id) + url_attributes = NodeAnalyzer.do_analyze(node, analyzers) + url_resources = Resources.rebuild_node_urls(node_id, url_attributes) - url_attributes = - node_id - |> NodeRepository.get_node!() - |> NodeAnalyzer.do_analyze(analyzers) + if url_resources != [] do + %UrlsAnalyzedEvent{ + node_id: node_id, + urls: url_resources, + episode_id: node.episode_id + } + |> EventStore.persist_event() + |> Dispatch.broadcast() + end - _created_urls = Resources.rebuild_node_urls(node_id, url_attributes) :ok end end diff --git a/lib/radiator/resources/url.ex b/lib/radiator/resources/url.ex index ccf63523..4b00b917 100644 --- a/lib/radiator/resources/url.ex +++ b/lib/radiator/resources/url.ex @@ -5,6 +5,8 @@ defmodule Radiator.Resources.Url do use Ecto.Schema import Ecto.Changeset + @derive {Jason.Encoder, only: [:id, :url, :start_bytes, :size_bytes, :meta_data]} + defmodule MetaData do @moduledoc """ Meta data for a URL depending on the analyzers.