Browse Source

feat: send webmentions from bookmark worker

master
Inhji Y. 10 months ago
parent
commit
6e227586af
  1. 33
      apps/bookmarks/lib/bookmarks/worker.ex
  2. 4
      apps/indie/mix.exs
  3. 5
      apps/tomie_web/lib/tomie_web/handlers/micropub_handler.ex
  4. 7
      apps/tomie_web/lib/tomie_web/live/bookmark_live/new.ex
  5. 1
      apps/tomie_web/mix.exs
  6. 2
      config/config.exs
  7. 2
      mix.lock

33
apps/bookmarks/lib/bookmarks/worker.ex

@ -3,11 +3,22 @@ defmodule Bookmarks.Worker do
queue: :bookmarks,
max_attempts: 5
alias Bookmarks.Bookmark
@impl Oban.Worker
def perform(%{"id" => id}, _job) do
with bookmark <- Bookmarks.get_bookmark!(id),
source <- Map.get(bookmark, :source),
{:ok, html} <- Scraper.Html.get_html(source),
def perform(%{"id" => id, "url" => url}, _job) do
bookmark = Bookmarks.get_bookmark!(id)
with :ok <- scrape_bookmark_url(bookmark),
:ok <- send_webmention(bookmark, url) do
:ok
else
error -> error
end
end
def scrape_bookmark_url(%Bookmark{source: source} = bookmark) do
with {:ok, html} <- Scraper.Html.get_html(source),
{:ok, result} <- Scraper.Html.parse(html),
{:ok, updated_bookmark} <- Bookmarks.update_bookmark(bookmark, %{title: result.title}) do
{:ok, updated_bookmark} =
@ -20,9 +31,21 @@ defmodule Bookmarks.Worker do
bookmark: updated_bookmark
})
:ok
else
_ -> {:error, "scrape error"}
end
end
def send_webmention(%Bookmark{source: destination}, source_url) do
with {:ok, endpoint} <- Webmentions.discover_endpoint(destination),
:ok <- Webmentions.send_webmention(endpoint, source_url, destination) do
:ok
else
_ -> :error
{:ok, nil} -> {:error, "No endpoint found"}
{:ok, ""} -> {:error, "No endpoint found"}
{:error, status} when is_number(status) -> {:error, "Failed with status #{status}"}
{:error, message} -> {:error, message}
end
end
end

4
apps/indie/mix.exs

@ -27,7 +27,9 @@ defmodule Indie.MixProject do
[
{:bookmarks, in_umbrella: true},
{:http, in_umbrella: true},
{:plug_micropub, "~> 0.1.0"}
{:oban, "~> 1.2"},
{:plug_micropub, "~> 0.1.0"},
{:webmentions, "~> 0.4.2"}
]
end
end

5
apps/tomie_web/lib/tomie_web/handlers/micropub_handler.ex

@ -33,7 +33,10 @@ defmodule TomieWeb.MicropubHandler do
case Bookmarks.create_bookmark(attrs) do
{:ok, bookmark} ->
Bookmarks.Worker.new(%{id: bookmark.id})
Bookmarks.Worker.new(%{
id: bookmark.id,
url: Routes.home_url(TomieWeb.Endpoint, :bookmark, bookmark)
})
|> Oban.insert()
{:ok, :created, Routes.home_url(TomieWeb.Endpoint, :bookmark, bookmark)}

7
apps/tomie_web/lib/tomie_web/live/bookmark_live/new.ex

@ -35,9 +35,12 @@ defmodule TomieWeb.BookmarkLive.New do
{:ok, bookmark} ->
Bookmarks.set_tags(tags, bookmark)
Bookmarks.Worker.new(%{id: bookmark.id})
Bookmarks.Worker.new(%{
id: bookmark.id,
url: Routes.home_url(TomieWeb.Endpoint, :bookmark, bookmark)
})
|> Oban.insert()
{:noreply,
push_redirect(socket, to: Routes.live_path(socket, BookmarkLive.Show, bookmark))}

1
apps/tomie_web/mix.exs

@ -44,6 +44,7 @@ defmodule TomieWeb.MixProject do
{:excoveralls, "~> 0.13.0", only: :test},
{:gettext, "~> 0.11"},
{:indie, in_umbrella: true},
{:indie, in_umbrella: true},
{:jason, "~> 1.0"},
{:listens, in_umbrella: true},
{:phoenix, "~> 1.5.0"},

2
config/config.exs

@ -46,7 +46,7 @@ config :indie,
config :tomie, Oban,
repo: Db.Repo,
prune: {:maxlen, 10_000},
queues: [bookmarks: 10, listens: 10, scraper: 10],
queues: [bookmarks: 10, listens: 10, scraper: 10, indie: 10],
crontab: [
{"* * * * *", Listens.Workers.Listenbrainz, args: %{user: "inhji"}},
{"* * * * *", Listens.Workers.DiscogsArtist, args: %{}},

2
mix.lock

@ -67,6 +67,7 @@
"telemetry": {:hex, :telemetry, "0.4.2", "2808c992455e08d6177322f14d3bdb6b625fbcfd233a73505870d8738a2f4599", [:rebar3], [], "hexpm", "2d1419bd9dda6a206d7b5852179511722e2b18812310d304620c7bd92a13fcef"},
"telemetry_metrics": {:hex, :telemetry_metrics, "0.5.0", "1b796e74add83abf844e808564275dfb342bcc930b04c7577ab780e262b0d998", [:mix], [{:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "31225e6ce7a37a421a0a96ec55244386aec1c190b22578bd245188a4a33298fd"},
"telemetry_poller": {:hex, :telemetry_poller, "0.5.1", "21071cc2e536810bac5628b935521ff3e28f0303e770951158c73eaaa01e962a", [:rebar3], [{:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "4cab72069210bc6e7a080cec9afffad1b33370149ed5d379b81c7c5f0c663fd4"},
"tesla": {:hex, :tesla, "1.3.3", "26ae98627af5c406584aa6755ab5fc96315d70d69a24dd7f8369cfcb75094a45", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: true]}, {:exjsx, ">= 3.0.0", [hex: :exjsx, repo: "hexpm", optional: true]}, {:fuse, "~> 2.4", [hex: :fuse, repo: "hexpm", optional: true]}, {:gun, "~> 1.3", [hex: :gun, repo: "hexpm", optional: true]}, {:hackney, "~> 1.6", [hex: :hackney, repo: "hexpm", optional: true]}, {:ibrowse, "~> 4.4.0", [hex: :ibrowse, repo: "hexpm", optional: true]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: true]}, {:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.0", [hex: :mint, repo: "hexpm", optional: true]}, {:poison, ">= 1.0.0", [hex: :poison, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "2648f1c276102f9250299e0b7b57f3071c67827349d9173f34c281756a1b124c"},
"timex": {:hex, :timex, "3.6.2", "845cdeb6119e2fef10751c0b247b6c59d86d78554c83f78db612e3290f819bc2", [:mix], [{:combine, "~> 0.10", [hex: :combine, repo: "hexpm", optional: false]}, {:gettext, "~> 0.10", [hex: :gettext, repo: "hexpm", optional: false]}, {:tzdata, "~> 0.1.8 or ~> 0.5 or ~> 1.0.0", [hex: :tzdata, repo: "hexpm", optional: false]}], "hexpm", "26030b46199d02a590be61c2394b37ea25a3664c02fafbeca0b24c972025d47a"},
"tzdata": {:hex, :tzdata, "1.0.3", "73470ad29dde46e350c60a66e6b360d3b99d2d18b74c4c349dbebbc27a09a3eb", [:mix], [{:hackney, "~> 1.0", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "a6e1ee7003c4d04ecbd21dd3ec690d4c6662db5d3bbdd7262d53cdf5e7c746c1"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.5.0", "8516502659002cec19e244ebd90d312183064be95025a319a6c7e89f4bccd65b", [:rebar3], [], "hexpm", "d48d002e15f5cc105a696cf2f1bbb3fc72b4b770a184d8420c8db20da2674b38"},
@ -74,4 +75,5 @@
"uuid": {:hex, :uuid, "1.1.8", "e22fc04499de0de3ed1116b770c7737779f226ceefa0badb3592e64d5cfb4eb9", [:mix], [], "hexpm", "c790593b4c3b601f5dc2378baae7efaf5b3d73c4c6456ba85759905be792f2ac"},
"waffle": {:hex, :waffle, "1.1.0", "7a7b8620d1d43cdea5221910508710504ad718f681f463e0b75974c7d15608b7", [:mix], [{:ex_aws, "~> 2.1.2", [hex: :ex_aws, repo: "hexpm", optional: true]}, {:ex_aws_s3, "~> 2.0", [hex: :ex_aws_s3, repo: "hexpm", optional: true]}, {:hackney, "~> 1.9", [hex: :hackney, repo: "hexpm", optional: false]}, {:sweet_xml, "~> 0.6", [hex: :sweet_xml, repo: "hexpm", optional: true]}], "hexpm", "f0488aeea4987d044c74f8ba7c6829154b778c498463a9db68492c7073ebee9e"},
"waffle_ecto": {:hex, :waffle_ecto, "0.0.9", "d61b0b6a1547f42a7a0fa6efff8389ac4219dbbe45fcf7a2add4c923f416cd85", [:mix], [{:ecto, ">= 2.1.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:waffle, "~> 1.0", [hex: :waffle, repo: "hexpm", optional: false]}], "hexpm", "994cd5a08134b3d19ba5930b7b56c329557909b0d41c7bc3ab0aef6c5b07aa32"},
"webmentions": {:hex, :webmentions, "0.4.2", "1e9ead66d64ae5a49c2b774872b8e0aee4785ca8de0ad0efd34a8ed31cf883ba", [:mix], [{:floki, "~> 0.23", [hex: :floki, repo: "hexpm", optional: false]}, {:tesla, "~> 1.3.0", [hex: :tesla, repo: "hexpm", optional: false]}], "hexpm", "ebe829d86a83f9a6062bfe592e38369fb21d2653aa137012ebbdc42abba3f925"},
}
Loading…
Cancel
Save