Change user.discoverable field to user.is_discoverable
[akkoma] / lib / pleroma / web / rich_media / parsers / oembed_parser.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.RichMedia.Parsers.OEmbed do
6 def parse(html, _data) do
7 with elements = [_ | _] <- get_discovery_data(html),
8 oembed_url when is_binary(oembed_url) <- get_oembed_url(elements),
9 {:ok, oembed_data} <- get_oembed_data(oembed_url) do
10 oembed_data
11 else
12 _e -> %{}
13 end
14 end
15
16 defp get_discovery_data(html) do
17 html |> Floki.find("link[type='application/json+oembed']")
18 end
19
20 defp get_oembed_url([{"link", attributes, _children} | _]) do
21 Enum.find_value(attributes, fn {k, v} -> if k == "href", do: v end)
22 end
23
24 defp get_oembed_data(url) do
25 with {:ok, %Tesla.Env{body: json}} <- Pleroma.Web.RichMedia.Helpers.rich_media_get(url) do
26 Jason.decode(json)
27 end
28 end
29 end