Skip to content

Commit

Permalink
extract urls in async task
Browse files Browse the repository at this point in the history
  • Loading branch information
electronicbites committed Sep 2, 2024
1 parent dc76726 commit 9601fc3
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions lib/radiator/outline/node_change_listener.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,18 @@ defmodule Radiator.Outline.NodeChangeListener do
It is an eventconsumer that listens to changes in the outline and starts workers
"""
use GenServer

alias Radiator.Outline.Event.{
NodeContentChangedEvent,
NodeDeletedEvent,
NodeInsertedEvent,
NodeMovedEvent
}

alias Radiator.Outline.Dispatch
alias Radiator.UrlExtractor

require Logger

def start_link(_) do
GenServer.start_link(__MODULE__, :ok, [])
Expand All @@ -15,19 +26,37 @@ defmodule Radiator.Outline.NodeChangeListener do
{:ok, []}
end

def handle_info(%Radiator.Outline.Event.NodeContentChangedEvent{} = _event, state) do
def handle_info(%NodeContentChangedEvent{node_id: node_id, content: content}, state) do
Task.async(fn ->
scan_content_for_urls(node_id, content)
end)

{:noreply, state}
end

def handle_info(%NodeInsertedEvent{} = _event, state) do
{:noreply, state}
end

def handle_info(%Radiator.Outline.Event.NodeInsertedEvent{} = _event, state) do
def handle_info(%NodeMovedEvent{} = _event, state) do
{:noreply, state}
end

def handle_info(%Radiator.Outline.Event.NodeMovedEvent{} = _event, state) do
def handle_info(%NodeDeletedEvent{} = _event, state) do
{:noreply, state}
end

def handle_info(%Radiator.Outline.Event.NodeDeletedEvent{} = _event, state) do
def handle_info(_reference, state) do
Logger.warning("Unknown event type")
{:noreply, state}
end

def scan_content_for_urls(_node_id, content) do
result = UrlExtractor.extract_urls(content)
Logger.debug("Extracted #{Enum.count(result)} Urls!!")

Enum.each(result, fn info ->
Logger.debug("URL: #{info.url}")
end)
end
end

0 comments on commit 9601fc3

Please sign in to comment.