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

Feature/meta infos for urls #591

Draft
wants to merge 3 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
30 changes: 30 additions & 0 deletions lib/radiator/accounts/raindrop_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,36 @@ defmodule Radiator.Accounts.RaindropClient do
end
end

@doc """
Returns a string list of URLs in a collection
"""
def list_urls_in_collection(user_id, collection_id) do
service =
user_id
|> Accounts.get_raindrop_tokens()
|> refresh_token_if()

if is_nil(service) do
{:error, :unauthorized}
else
{:ok, %Req.Response{} = response} =
[
method: :get,
url: "https://api.raindrop.io/rest/v1/raindrops/#{collection_id}",
headers: [
{"Authorization", "Bearer #{service.data.access_token}"}
]
]
|> Req.request()

response.body
|> Map.get("items")
|> Enum.map(fn item ->
Map.get(item, "link")
end)
end
end

@doc """
first time fetching access token and storing it as webservice entry
"""
Expand Down
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 episode_id(%{episode_id: episode_id}), do: episode_id
end
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
4 changes: 3 additions & 1 deletion lib/radiator/outline/node_repository.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ defmodule Radiator.Outline.NodeRepository do

@doc """
Returns the list of nodes for an episode.
Called by the liveview to fetch all nodes for an episode.

## Examples

Expand All @@ -69,7 +70,8 @@ defmodule Radiator.Outline.NodeRepository do

def list_nodes_by_episode(episode_id) do
Node
|> where([p], p.episode_id == ^episode_id)
|> where([n], n.episode_id == ^episode_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
20 changes: 15 additions & 5 deletions lib/radiator/resources/node_changed_worker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
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: 17 additions & 3 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 @@ -52,12 +53,25 @@ defmodule Radiator.Outline.NodeRepositoryTest do
end

describe "list_nodes_by_episode/1" do
test "list_nodes/1 returns only nodes of this episode" do
test "returns only nodes of this episode" do
node1 = node_fixture()
node2 = node_fixture()

assert NodeRepository.list_nodes_by_episode(node1.episode_id) == [node1]
assert NodeRepository.list_nodes_by_episode(node2.episode_id) == [node2]
assert NodeRepository.list_nodes_by_episode(node1.episode_id) |> Enum.map(& &1.uuid) == [
node1.uuid
]

assert NodeRepository.list_nodes_by_episode(node2.episode_id) |> Enum.map(& &1.uuid) == [
node2.uuid
]
end

test "preloads optional associated URLs" do
node = node_fixture()
url = ResourcesFixtures.url_fixture(node_id: node.uuid)
[loaded_node] = NodeRepository.list_nodes_by_episode(node.episode_id)
assert loaded_node.uuid == node.uuid
assert loaded_node.urls == [url]
end
end

Expand Down