Merge branch 'ssl_trusted_cert' into 'develop'
[akkoma] / lib / pleroma / web / rel_me.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.RelMe do
6 @hackney_options [
7 pool: :media,
8 recv_timeout: 2_000,
9 max_body: 2_000_000,
10 with_body: true
11 ]
12
13 if Mix.env() == :test do
14 def parse(url) when is_binary(url), do: parse_url(url)
15 else
16 def parse(url) when is_binary(url) do
17 Cachex.fetch!(:rel_me_cache, url, fn _ ->
18 {:commit, parse_url(url)}
19 end)
20 rescue
21 e -> {:error, "Cachex error: #{inspect(e)}"}
22 end
23 end
24
25 def parse(_), do: {:error, "No URL provided"}
26
27 defp parse_url(url) do
28 {:ok, %Tesla.Env{body: html}} = Pleroma.HTTP.get(url, [], adapter: @hackney_options)
29
30 data =
31 Floki.attribute(html, "link[rel~=me]", "href") ++
32 Floki.attribute(html, "a[rel~=me]", "href")
33
34 {:ok, data}
35 rescue
36 e -> {:error, "Parsing error: #{inspect(e)}"}
37 end
38
39 def maybe_put_rel_me("http" <> _ = target_page, profile_urls) when is_list(profile_urls) do
40 {:ok, rel_me_hrefs} = parse(target_page)
41
42 true = Enum.any?(rel_me_hrefs, fn x -> x in profile_urls end)
43
44 "me"
45 rescue
46 _ -> nil
47 end
48
49 def maybe_put_rel_me(_, _) do
50 nil
51 end
52 end