summaryrefslogtreecommitdiff
path: root/lib/plugins/link.ex
diff options
context:
space:
mode:
authorJordan Bracco <href@random.sh>2025-06-26 16:10:16 +0200
committerJordan Bracco <href@random.sh>2025-06-26 16:10:16 +0200
commita94c1cc0ac767db64d92420620202838da52545b (patch)
tree1bc180f1a0b0c26f6e7acff7cb903a2ef821d1c7 /lib/plugins/link.ex
parenttxt: do not list, link only (diff)
link: cache and inhibit
Diffstat (limited to 'lib/plugins/link.ex')
-rw-r--r--lib/plugins/link.ex126
1 files changed, 75 insertions, 51 deletions
diff --git a/lib/plugins/link.ex b/lib/plugins/link.ex
index 89fe944..bdc0fe9 100644
--- a/lib/plugins/link.ex
+++ b/lib/plugins/link.ex
@@ -61,7 +61,6 @@ defmodule Nola.Plugins.Link do
defstruct [:client]
def init([]) do
- Store.setup()
{:ok, _} = Registry.register(Nola.PubSub, "messages", plugin: __MODULE__)
# {:ok, _} = Registry.register(Nola.PubSub, "messages:telegram", [plugin: __MODULE__])
Logger.info("Link handler started")
@@ -75,39 +74,11 @@ defmodule Nola.Plugins.Link do
uri = URI.parse(word)
if uri.scheme && uri.host do
- spawn(fn ->
- :timer.kill_after(:timer.seconds(30))
-
- case expand_link([uri]) do
- {:ok, uris, text} ->
- text =
- case uris do
- [uri] ->
- text
-
- [luri | _] ->
- if luri.host == uri.host && luri.path == uri.path do
- text
- else
- ["-> #{URI.to_string(luri)}", text]
- end
- end
-
- case text do
- lines when is_list(lines) ->
- for text <- lines, do: message.replyfun.(text)
-
- text when is_binary(text) ->
- message.replyfun.(text)
-
- nil ->
- nil
- end
-
- _ ->
- nil
- end
- end)
+ if Store.inhibit_link?(word, {message.network, message.channel}) do
+ Logger.debug("link inhibited #{word}")
+ else
+ handle_link(word, uri, message)
+ end
end
end
end)
@@ -123,6 +94,48 @@ defmodule Nola.Plugins.Link do
:ok
end
+ def handle_link(url, uri, message) do
+ spawn(fn ->
+ :timer.kill_after(:timer.seconds(30))
+
+ store = Store.get_link(url)
+
+ case store || expand_link([uri]) do
+ {:ok, uris, text} = save ->
+ text =
+ case uris do
+ [uri] ->
+ text
+
+ [luri | _] ->
+ if luri.host == uri.host && luri.path == uri.path do
+ text
+ else
+ ["-> #{URI.to_string(luri)}", text]
+ end
+ end
+
+ case text do
+ lines when is_list(lines) ->
+ for text <- lines, do: message.replyfun.(text)
+ if !store, do: Store.insert_link(url, save)
+ Store.witness_link(url, {message.network, message.channel})
+
+ text when is_binary(text) ->
+ message.replyfun.(text)
+ if !store, do: Store.insert_link(url, save)
+ Store.witness_link(url, {message.network, message.channel})
+
+ nil ->
+ nil
+ end
+
+ _ ->
+ nil
+ end
+ end)
+ end
+
# 1. Match the first valid handler
# 2. Try to run the handler
# 3. If :error or crash, default link.
@@ -140,12 +153,15 @@ defmodule Nola.Plugins.Link do
handler =
Enum.reduce_while(handlers, nil, fn {module, opts}, acc ->
- Logger.debug("link: attempt expanding: #{inspect(module)} for #{inspect(uri)}")
module = Module.concat([module])
case module.match(uri, opts) do
- {true, params} -> {:halt, {module, params, opts}}
- false -> {:cont, acc}
+ {true, params} ->
+ Logger.debug("link: will expand with #{inspect(module)} for #{inspect(uri)}")
+ {:halt, {module, params, opts}}
+
+ false ->
+ {:cont, acc}
end
end)
@@ -157,12 +173,17 @@ defmodule Nola.Plugins.Link do
end
def run_expand(acc = [uri | _], {module, params, opts}) do
- Logger.debug("link: expanding #{inspect(uri)} with #{inspect(module)}")
-
case module.expand(uri, params, opts) do
- {:ok, data} -> {:ok, acc, data}
- :error -> expand_default(acc)
- :skip -> nil
+ {:ok, data} ->
+ Logger.debug("link: expanded #{inspect(uri)} with #{inspect(module)}")
+ {:ok, acc, data}
+
+ :error ->
+ Logger.error("Error expanding URL #{uri} with #{inspect(module)}")
+ expand_default(acc)
+
+ :skip ->
+ nil
end
rescue
e ->
@@ -245,7 +266,8 @@ defmodule Nola.Plugins.Link do
{:redirect, location}
end
- defp get_req(_, {:ok, status, headers, client}) do
+ defp get_req(url, {:ok, status, headers, client}) do
+ Logger.error("Error fetching URL #{url} = #{status}")
:hackney.close(client)
{:error, status, headers}
end
@@ -268,6 +290,7 @@ defmodule Nola.Plugins.Link do
tmpfile
end
+ Logger.debug("expanding body with #{inspect(handler)}: #{inspect(body)}")
handler.post_expand(url, body, params, opts)
{:error, reason} ->
@@ -277,7 +300,6 @@ defmodule Nola.Plugins.Link do
defp get_body(_, len, client, h, _acc) do
:hackney.close(client)
- IO.inspect(h)
{:ok, "Error: file over 30"}
end
@@ -299,24 +321,26 @@ defmodule Nola.Plugins.Link do
{:redirect, link} ->
new_uri = URI.parse(link)
-
- # new_uri = %URI{new_uri | scheme: scheme, authority: uri.authority, host: uri.host, port: uri.port}
expand_link([new_uri | acc])
- {:error, status, _headers} ->
- # text = Plug.Conn.Status.reason_phrase(status)
- # {:ok, acc, "Error: HTTP #{text} (#{status})"}
+ {:error, status, _headers} when status in [400, 403] ->
+ Logger.warning("Was denied to fetch URL, using scraper #{url} = #{status}")
retry_expand_with_scraper(acc, url)
+ {:error, status, _headers} ->
+ Logger.error("Error fetching URL #{url} = #{status}")
+ {:ok, acc, nil}
+
{:error, {:tls_alert, {:handshake_failure, err}}} ->
- # "TLS Error: #{to_string(err)}"}
+ Logger.error("Error fetching URL #{url} = TLS Error: #{to_string(err)}")
{:ok, acc, nil}
{:error, :timeout} ->
+ Logger.error("Error fetching URL #{url} = timeout")
retry_expand_with_scraper(acc, url)
{:error, reason} ->
- # "Error: #{to_string(reason)}"}
+ Logger.error("Error fetching URL #{url} = #{to_string(reason)}")
{:ok, acc, nil}
end
end