Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove all podcast related infos from outline nodes. #601

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
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
24 changes: 24 additions & 0 deletions lib/radiator/episode_outliner.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule Radiator.EpisodeOutliner do

Check warning on line 1 in lib/radiator/episode_outliner.ex

View workflow job for this annotation

GitHub Actions / Build & Test

Modules should have a @moduledoc tag.
alias Radiator.Outline.NodeRepository
# alias Radiator.Outline
alias Radiator.Podcast
alias Radiator.Podcast.Episode

@doc """
Returns a list of all child nodes.
"""
def list_nodes_by_episode_sorted(episode_id) do
%Episode{outline_node_container: outline_node_container} = Podcast.get_episode!(episode_id)

outline_node_container
|> NodeRepository.list_nodes_by_node_container()
end

def insert_node(%{"episode_id" => episode_id} = attrs) do
%Episode{outline_node_container: outline_node_container} = Podcast.get_episode!(episode_id)

attrs
|> Map.put("outline_node_container", outline_node_container)
|> insert_node()
end
end
63 changes: 20 additions & 43 deletions lib/radiator/outline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
:old_next,
:next,
:children,
:episode_id
:outline_node_container_id
]
end

Expand All @@ -21,8 +21,6 @@
alias Radiator.Outline.NodeRepoResult
alias Radiator.Outline.NodeRepository
alias Radiator.Outline.Validations, as: NodeValidator
alias Radiator.Podcast
alias Radiator.Podcast.Episode
alias Radiator.Repo

require Logger
Expand All @@ -48,17 +46,6 @@
|> order_nodes_by_index(nil, [])
end

@doc """
Returns a list of all child nodes.
"""
def list_nodes_by_episode_sorted(episode_id) do
episode_id
|> NodeRepository.list_nodes_by_episode()
|> Enum.group_by(& &1.parent_id)
|> Enum.map(fn {_parent_id, children} -> order_sibling_nodes(children) end)
|> List.flatten()
end

@doc """
Inserts a node.

Expand All @@ -76,27 +63,26 @@
# if a previous node is given, the new node will be inserted after the previous node
# if no parent is given, the new node will be inserted as a root node
# if no previous node is given, the new node will be inserted as the first child of the parent node
def insert_node(%{"show_id" => _show_id} = attrs) do
def insert_node(%{"outline_node_container_id" => outline_node_container_id} = attrs) do
Repo.transaction(fn ->
prev_id = attrs["prev_id"]
parent_id = attrs["parent_id"]
episode_id = attrs["episode_id"]

prev_node = NodeRepository.get_node_if(prev_id)
parent_node = find_parent_node(prev_node, parent_id)

# find Node which has been previously connected to prev_node
next_node = NodeRepository.get_next_node(episode_id, prev_id, get_node_id(parent_node))
next_node = NodeRepository.get_next_node(outline_node_container_id, prev_id, get_node_id(parent_node))

with true <- parent_and_prev_consistent?(parent_node, prev_node),
true <- episode_valid?(episode_id, parent_node, prev_node),
true <- container_valid?(outline_node_container_id, parent_node, prev_node),
{:ok, node} <-
attrs
|> set_parent_id_if(parent_node)
|> NodeRepository.create_node(),
{:ok, _node_to_move} <-
NodeRepository.move_node_if(next_node, get_node_id(parent_node), node.uuid) do
%NodeRepoResult{node: node, next: get_node_result_info(next_node), episode_id: episode_id}
%NodeRepoResult{node: node, next: get_node_result_info(next_node), outline_node_container_id: outline_node_container_id}

Check warning on line 85 in lib/radiator/outline.ex

View workflow job for this annotation

GitHub Actions / Build & Test

Line is too long (max is 120, was 128).
else
false ->
Repo.rollback("Insert node failed. Parent and prev node are not consistent.")
Expand All @@ -108,14 +94,6 @@
end)
end

def insert_node(%{"episode_id" => episode_id} = attrs) do
%Episode{show_id: show_id} = Podcast.get_episode!(episode_id)

attrs
|> Map.put("show_id", show_id)
|> insert_node()
end

@doc """
Intends a node given by its id (by using the tab key).

Expand Down Expand Up @@ -329,10 +307,9 @@

node_attrs = %{
"content" => new_node_content,
"episode_id" => node.episode_id,
"outline_node_container_id" => node.outline_node_container_id,
"parent_id" => node.parent_id,
"prev_id" => node.uuid,
"outline_node_container_id" => node.outline_node_container_id
"prev_id" => node.uuid
}

{:ok, %NodeRepoResult{node: new_node, next: old_next_node}} =
Expand All @@ -342,7 +319,7 @@
%NodeRepoResult{
node: updated_node,
next: new_node,
episode_id: updated_node.episode_id,
outline_node_container_id: updated_node.outline_node_container_id,
old_next: get_node_result_info(old_next_node)
}}
end
Expand Down Expand Up @@ -391,7 +368,7 @@
node: deleted_node,
next: get_node_result_info(updated_next_node),
children: all_children ++ recursive_deleted_children,
episode_id: node.episode_id
outline_node_container_id: node.outline_node_container_id
}
end

Expand Down Expand Up @@ -455,13 +432,13 @@
def get_node_id(nil), do: nil
def get_node_id(%Node{} = node), do: node.uuid

defp episode_valid?(episode_id, %Node{episode_id: episode_id}, %Node{episode_id: episode_id}),
defp container_valid?(outline_node_container_id, %Node{outline_node_container_id: outline_node_container_id}, %Node{outline_node_container_id: outline_node_container_id}),
do: true

defp episode_valid?(episode_id, %Node{episode_id: episode_id}, nil), do: true
defp episode_valid?(episode_id, nil, %Node{episode_id: episode_id}), do: true
defp episode_valid?(_episode_id, nil, nil), do: true
defp episode_valid?(_episode_id, _parent_node, _prev_node), do: false
defp container_valid?(outline_node_container_id, %Node{outline_node_container_id: outline_node_container_id}, nil), do: true
defp container_valid?(outline_node_container_id, nil, %Node{outline_node_container_id: outline_node_container_id}), do: true
defp container_valid?(_outline_node_container_id, nil, nil), do: true
defp container_valid?(_outline_node_container_id, _parent_node, _prev_node), do: false

defp set_parent_id_if(attrs, nil), do: attrs
defp set_parent_id_if(attrs, %Node{uuid: uuid}), do: Map.put_new(attrs, "parent_id", uuid)
Expand All @@ -478,7 +455,7 @@
defp do_move_node(node, new_prev_id, new_parent_id, prev_node, parent_node) do
node_repo_result = %NodeRepoResult{
node: get_node_result_info(node),
episode_id: node.episode_id
outline_node_container_id: node.outline_node_container_id
}

Repo.transaction(fn ->
Expand Down Expand Up @@ -573,18 +550,18 @@
defp do_move_up(%Node{}, nil), do: {:error, :no_previous_node}

defp do_move_up(
%Node{episode_id: episode_id, parent_id: parent_id} = node,
%Node{outline_node_container_id: outline_node_container_id, parent_id: parent_id} = node,
%Node{} = prev_node
) do
next_node = NodeRepository.get_next_node(episode_id, node.uuid, parent_id)
next_node = NodeRepository.get_next_node(outline_node_container_id, node.uuid, parent_id)

{:ok, updated_node} = NodeRepository.move_node_if(node, parent_id, prev_node.prev_id)
{:ok, updated_prev_node} = NodeRepository.move_node_if(prev_node, parent_id, node.uuid)
{:ok, updated_next_node} = NodeRepository.move_node_if(next_node, parent_id, prev_node.uuid)

%NodeRepoResult{
node: get_node_result_info(updated_node),
episode_id: episode_id,
outline_node_container_id: outline_node_container_id,
old_prev: get_node_result_info(updated_prev_node),
old_next: get_node_result_info(updated_next_node)
}
Expand All @@ -593,7 +570,7 @@
defp do_move_down(%Node{}, nil), do: {:error, :no_next_node}

defp do_move_down(
%Node{episode_id: episode_id, parent_id: parent_id} = node,
%Node{outline_node_container_id: outline_node_container_id, parent_id: parent_id} = node,
%Node{} = next_node
) do
new_next_node = NodeRepository.get_next_node(next_node)
Expand All @@ -606,7 +583,7 @@

%NodeRepoResult{
node: get_node_result_info(updated_node),
episode_id: episode_id,
outline_node_container_id: outline_node_container_id,
old_next: get_node_result_info(updated_next_node),
next: get_node_result_info(updated_new_next_node)
}
Expand Down
14 changes: 7 additions & 7 deletions lib/radiator/outline/command_processor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@

%NodeDeletedEvent{
node: result.node,
episode_id: result.episode_id,
outline_node_container_id: result.outline_node_container_id,
event_id: command.event_id,
user_id: command.user_id,
children: result.children,
Expand All @@ -131,13 +131,13 @@
selection: selection
} = command
) do
{:ok, %NodeRepoResult{node: node, next: next, episode_id: episode_id, old_next: old_next}} =
{:ok, %NodeRepoResult{node: node, next: next, outline_node_container_id: outline_node_container_id, old_next: old_next}} =

Check warning on line 134 in lib/radiator/outline/command_processor.ex

View workflow job for this annotation

GitHub Actions / Build & Test

Line is too long (max is 120, was 126).
node_id
|> Outline.split_node(selection)

# broadcast two events
handle_insert_node_result(
{:ok, %NodeRepoResult{node: next, next: old_next, episode_id: episode_id}},
{:ok, %NodeRepoResult{node: next, next: old_next, outline_node_container_id: outline_node_container_id}},
command
)

Expand All @@ -147,15 +147,15 @@
end

defp handle_insert_node_result(
{:ok, %NodeRepoResult{node: node, next: next, episode_id: episode_id}},
{:ok, %NodeRepoResult{node: node, next: next, outline_node_container_id: outline_node_container_id}},
command
) do
%NodeInsertedEvent{
node: node,
event_id: command.event_id,
user_id: command.user_id,
next: next,
episode_id: episode_id
outline_node_container_id: outline_node_container_id
}
|> EventStore.persist_event()
|> Dispatch.broadcast()
Expand All @@ -181,7 +181,7 @@
event_id: command.event_id,
next: result.next,
children: result.children,
episode_id: result.episode_id
outline_node_container_id: result.outline_node_container_id
}
|> EventStore.persist_event()
|> Dispatch.broadcast()
Expand All @@ -200,7 +200,7 @@
content: node.content,
user_id: command.user_id,
event_id: command.event_id,
episode_id: node.episode_id
outline_node_container_id: node.outline_node_container_id
}
|> EventStore.persist_event()
|> Dispatch.broadcast()
Expand Down
4 changes: 2 additions & 2 deletions lib/radiator/outline/dispatch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ defmodule Radiator.Outline.Dispatch do
if Application.get_env(:radiator, :tree_consistency_validator, false) do
:ok =
event
|> Event.episode_id()
|> Validations.validate_tree_for_episode()
|> Event.outline_node_container_id()
|> Validations.validate_tree_for_outline_node_container()
end

Phoenix.PubSub.broadcast(Radiator.PubSub, "events", event)
Expand Down
4 changes: 2 additions & 2 deletions lib/radiator/outline/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule Radiator.Outline.Event do
def payload(%NodeDeletedEvent{} = event) do
%{
node: event.node,
episode_id: event.episode_id,
outline_node_container_id: event.outline_node_container_id,
children: event.children,
next: event.next
}
Expand All @@ -45,5 +45,5 @@ defmodule Radiator.Outline.Event do
def event_type(%NodeDeletedEvent{} = _event), do: "NodeDeletedEvent"
def event_type(%NodeMovedEvent{} = _event), do: "NodeMovedEvent"

def episode_id(%{episode_id: episode_id}), do: episode_id
def outline_node_container_id(%{outline_node_container_id: outline_node_container_id}), do: outline_node_container_id
end
2 changes: 1 addition & 1 deletion lib/radiator/outline/event/node_content_changed_event.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Radiator.Outline.Event.NodeContentChangedEvent do
@moduledoc false

defstruct [:event_id, :node_id, :content, :user_id, :episode_id]
defstruct [:event_id, :node_id, :content, :user_id, :outline_node_container_id]
end
2 changes: 1 addition & 1 deletion lib/radiator/outline/event/node_deleted_event.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Radiator.Outline.Event.NodeDeletedEvent do
@moduledoc false
defstruct [:event_id, :node, :user_id, :children, :next, :episode_id]
defstruct [:event_id, :node, :user_id, :children, :next, :outline_node_container_id]
end
2 changes: 1 addition & 1 deletion lib/radiator/outline/event/node_inserted_event.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Radiator.Outline.Event.NodeInsertedEvent do
@moduledoc false

defstruct [:event_id, :node, :user_id, :next, :episode_id, :content]
defstruct [:event_id, :node, :user_id, :next, :outline_node_container_id, :content]
end
2 changes: 1 addition & 1 deletion lib/radiator/outline/event/node_moved_event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Radiator.Outline.Event.NodeMovedEvent do
:old_prev,
:old_next,
:next,
:episode_id,
:outline_node_container_id,
:children
]
end
5 changes: 0 additions & 5 deletions lib/radiator/outline/node.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ defmodule Radiator.Outline.Node do
import Ecto.Changeset
alias Radiator.Outline.Node
alias Radiator.Outline.NodeContainer
alias Radiator.Podcast.{Episode, Show}
alias Radiator.Resources.Url

@derive {Jason.Encoder, only: [:uuid, :content, :creator_id, :parent_id, :prev_id]}
Expand All @@ -21,8 +20,6 @@ defmodule Radiator.Outline.Node do

belongs_to :outline_node_container, NodeContainer

belongs_to :episode, Episode
belongs_to :show, Show
belongs_to :parent, Node, references: :uuid, type: Ecto.UUID
belongs_to :prev, Node, references: :uuid, type: Ecto.UUID
has_many :urls, Url, foreign_key: :node_id
Expand All @@ -40,11 +37,9 @@ defmodule Radiator.Outline.Node do
|> cast(attributes, [
:uuid,
:content,
:episode_id,
:creator_id,
:parent_id,
:prev_id,
:show_id,
:outline_node_container_id
])
|> put_uuid()
Expand Down
Loading
Loading