logo

pleroma

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

o_embed.ex (973B)


  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.RichMedia.Parsers.OEmbed do
  5. def parse(html, _data) do
  6. with elements = [_ | _] <- get_discovery_data(html),
  7. oembed_url when is_binary(oembed_url) <- get_oembed_url(elements),
  8. {:ok, oembed_data = %{"html" => html}} <- get_oembed_data(oembed_url) do
  9. %{oembed_data | "html" => Pleroma.HTML.filter_tags(html)}
  10. else
  11. _e -> %{}
  12. end
  13. end
  14. defp get_discovery_data(html) do
  15. html |> Floki.find("link[type='application/json+oembed']")
  16. end
  17. defp get_oembed_url([{"link", attributes, _children} | _]) do
  18. Enum.find_value(attributes, fn {k, v} -> if k == "href", do: v end)
  19. end
  20. defp get_oembed_data(url) do
  21. with {:ok, %Tesla.Env{body: json}} <- Pleroma.Web.RichMedia.Helpers.rich_media_get(url) do
  22. Jason.decode(json)
  23. end
  24. end
  25. end