Skip to content

Commit

Permalink
new event that gets emitted when urls have been detected
Browse files Browse the repository at this point in the history
  • Loading branch information
electronicbites committed Jan 8, 2025
1 parent 0f29d0e commit a3ef9e0
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/radiator/event_store.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/radiator/event_store/event_data.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 13 additions & 1 deletion lib/radiator/outline/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ defmodule Radiator.Outline.Event do
NodeContentChangedEvent,
NodeDeletedEvent,
NodeInsertedEvent,
NodeMovedEvent
NodeMovedEvent,
UrlsAnalyzedEvent
}

def payload(%NodeInsertedEvent{} = event) do
Expand Down Expand Up @@ -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 outline_node_container_id(%{outline_node_container_id: outline_node_container_id}),
do: outline_node_container_id
Expand Down
4 changes: 4 additions & 0 deletions lib/radiator/outline/event/urls_analyzed_event.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule Radiator.Outline.Event.UrlsAnalyzedEvent do
@moduledoc false
defstruct [:node_id, :urls, :episode_id, event_id: Ecto.UUID.generate()]
end
16 changes: 14 additions & 2 deletions lib/radiator/resources/node_changed_worker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ defmodule Radiator.Resources.NodeChangedWorker do
"""
alias __MODULE__
alias Radiator.EpisodeOutliner
alias Radiator.EventStore
alias Radiator.NodeAnalyzer
alias Radiator.Outline.Dispatch
alias Radiator.Outline.Event.UrlsAnalyzedEvent
alias Radiator.Outline.NodeRepository
alias Radiator.Resources

Expand All @@ -18,7 +21,6 @@ defmodule Radiator.Resources.NodeChangedWorker do
def perform(node_id) do
analyzers = [Radiator.NodeAnalyzer.UrlAnalyzer]
node = NodeRepository.get_node!(node_id)

episode_id = EpisodeOutliner.episode_id_for_node(node)

url_attributes =
Expand All @@ -29,8 +31,18 @@ defmodule Radiator.Resources.NodeChangedWorker do
|> Map.put(:node_id, node_id)
|> Map.put(:episode_id, episode_id)
end)
url_resources = Resources.rebuild_node_urls(node_id, url_attributes)

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
2 changes: 2 additions & 0 deletions lib/radiator/resources/url.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit a3ef9e0

Please sign in to comment.