Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, :outline_node_container_id, event_id: Ecto.UUID.generate()]
end
1 change: 1 addition & 0 deletions lib/radiator/outline/node_repository.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ defmodule Radiator.Outline.NodeRepository do
def list_nodes_by_node_container(outline_node_container_id) do
Node
|> where([p], p.outline_node_container_id == ^outline_node_container_id)
|> preload(:urls)
|> Repo.all()
|> Enum.group_by(& &1.parent_id)
|> Enum.map(fn {_parent_id, children} -> Radiator.Outline.order_sibling_nodes(children) end)
Expand Down
17 changes: 15 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 @@ -30,7 +32,18 @@ defmodule Radiator.Resources.NodeChangedWorker do
|> Map.put(:episode_id, episode_id)
end)

_created_urls = Resources.rebuild_node_urls(node_id, url_attributes)
url_resources = Resources.rebuild_node_urls(node_id, url_attributes)

if url_resources != [] do
%UrlsAnalyzedEvent{
node_id: node_id,
urls: url_resources,
outline_node_container_id: node.outline_node_container_id
}
|> EventStore.persist_event()
|> Dispatch.broadcast()
end

: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
20 changes: 14 additions & 6 deletions test/radiator/outline/node_repository_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Radiator.Outline.NodeRepositoryTest do
alias Radiator.Outline.Node
alias Radiator.Outline.NodeRepository
alias Radiator.PodcastFixtures
alias Radiator.ResourcesFixtures

import Radiator.OutlineFixtures
import Ecto.Query, warn: false
Expand Down Expand Up @@ -70,13 +71,20 @@ defmodule Radiator.Outline.NodeRepositoryTest do
node1 = node_fixture()
node2 = node_fixture()

assert NodeRepository.list_nodes_by_node_container(node1.outline_node_container_id) == [
node1
]
[result1] = NodeRepository.list_nodes_by_node_container(node1.outline_node_container_id)
[result2] = NodeRepository.list_nodes_by_node_container(node2.outline_node_container_id)

assert NodeRepository.list_nodes_by_node_container(node2.outline_node_container_id) == [
node2
]
assert result1.uuid == node1.uuid
assert result2.uuid == node2.uuid
end

test "preloads optional associated URLs" do
node = node_fixture()
node_id = node.uuid
url = ResourcesFixtures.url_fixture(node_id: node.uuid)

[%Node{uuid: ^node_id, urls: [^url]}] =
NodeRepository.list_nodes_by_node_container(node.outline_node_container_id)
end
end

Expand Down
Loading