Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.4KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2021 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. if Pleroma.Config.get(:env) == :test do
  11. def parse(url) when is_binary(url), do: parse_url(url)
  12. else
  13. @cachex Pleroma.Config.get([:cachex, :provider], Cachex)
  14. def parse(url) when is_binary(url) do
  15. @cachex.fetch!(:rel_me_cache, url, fn _ ->
  16. {:commit, parse_url(url)}
  17. end)
  18. rescue
  19. e -> {:error, "Cachex error: #{inspect(e)}"}
  20. end
  21. end
  22. def parse(_), do: {:error, "No URL provided"}
  23. defp parse_url(url) do
  24. with {:ok, %Tesla.Env{body: html, status: status}} when status in 200..299 <-
  25. Pleroma.HTTP.get(url, [], @options),
  26. {:ok, html_tree} <- Floki.parse_document(html),
  27. data <-
  28. Floki.attribute(html_tree, "link[rel~=me]", "href") ++
  29. Floki.attribute(html_tree, "a[rel~=me]", "href") do
  30. {:ok, data}
  31. end
  32. rescue
  33. e -> {:error, "Parsing error: #{inspect(e)}"}
  34. end
  35. def maybe_put_rel_me("http" <> _ = target_page, profile_urls) when is_list(profile_urls) do
  36. {:ok, rel_me_hrefs} = parse(target_page)
  37. true = Enum.any?(rel_me_hrefs, fn x -> x in profile_urls end)
  38. "me"
  39. rescue
  40. _ -> nil
  41. end
  42. def maybe_put_rel_me(_, _) do
  43. nil
  44. end
  45. end