logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

rel_me.ex (1323B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.RelMe do
  5. @options [
  6. pool: :media,
  7. max_body: 2_000_000,
  8. recv_timeout: 2_000
  9. ]
  10. @cachex Pleroma.Config.get([:cachex, :provider], Cachex)
  11. def parse(url) when is_binary(url) do
  12. @cachex.fetch!(:rel_me_cache, url, fn _ ->
  13. {:commit, parse_url(url)}
  14. end)
  15. rescue
  16. e -> {:error, "Cachex error: #{inspect(e)}"}
  17. end
  18. def parse(_), do: {:error, "No URL provided"}
  19. defp parse_url(url) do
  20. with {:ok, %Tesla.Env{body: html, status: status}} when status in 200..299 <-
  21. Pleroma.HTTP.get(url, [], @options),
  22. {:ok, html_tree} <- Floki.parse_document(html),
  23. data <-
  24. Floki.attribute(html_tree, "link[rel~=me]", "href") ++
  25. Floki.attribute(html_tree, "a[rel~=me]", "href") do
  26. {:ok, data}
  27. end
  28. rescue
  29. e -> {:error, "Parsing error: #{inspect(e)}"}
  30. end
  31. def maybe_put_rel_me("http" <> _ = target_page, profile_urls) when is_list(profile_urls) do
  32. {:ok, rel_me_hrefs} = parse(target_page)
  33. true = Enum.any?(rel_me_hrefs, fn x -> x in profile_urls end)
  34. "me"
  35. rescue
  36. _ -> nil
  37. end
  38. def maybe_put_rel_me(_, _) do
  39. nil
  40. end
  41. end