Merge branch 'feature/rich-media' into 'develop'
URL previews in posts Closes #402 See merge request pleroma/pleroma!617
This commit is contained in:
commit
4258dd8633
9 changed files with 179 additions and 1 deletions
|
|
@ -0,0 +1,17 @@
|
|||
defmodule Pleroma.Web.RichMedia.RichMediaController do
|
||||
use Pleroma.Web, :controller
|
||||
|
||||
import Pleroma.Web.ControllerHelper, only: [json_response: 3]
|
||||
|
||||
def parse(conn, %{"url" => url}) do
|
||||
case Pleroma.Web.RichMedia.Parser.parse(url) do
|
||||
{:ok, data} ->
|
||||
conn
|
||||
|> json_response(200, data)
|
||||
|
||||
{:error, msg} ->
|
||||
conn
|
||||
|> json_response(404, msg)
|
||||
end
|
||||
end
|
||||
end
|
||||
26
lib/pleroma/web/rich_media/parser.ex
Normal file
26
lib/pleroma/web/rich_media/parser.ex
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
defmodule Pleroma.Web.RichMedia.Parser do
|
||||
@parsers [Pleroma.Web.RichMedia.Parsers.OGP]
|
||||
|
||||
def parse(url) do
|
||||
{:ok, %Tesla.Env{body: html}} = Pleroma.HTTP.get(url)
|
||||
|
||||
html |> maybe_parse() |> get_parsed_data()
|
||||
end
|
||||
|
||||
defp maybe_parse(html) do
|
||||
Enum.reduce_while(@parsers, %{}, fn parser, acc ->
|
||||
case parser.parse(html, acc) do
|
||||
{:ok, data} -> {:halt, data}
|
||||
{:error, _msg} -> {:cont, acc}
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
defp get_parsed_data(data) when data == %{} do
|
||||
{:error, "No metadata found"}
|
||||
end
|
||||
|
||||
defp get_parsed_data(data) do
|
||||
{:ok, data}
|
||||
end
|
||||
end
|
||||
30
lib/pleroma/web/rich_media/parsers/ogp.ex
Normal file
30
lib/pleroma/web/rich_media/parsers/ogp.ex
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
defmodule Pleroma.Web.RichMedia.Parsers.OGP do
|
||||
def parse(html, data) do
|
||||
with elements = [_ | _] <- get_elements(html),
|
||||
ogp_data =
|
||||
Enum.reduce(elements, data, fn el, acc ->
|
||||
attributes = normalize_attributes(el)
|
||||
|
||||
Map.merge(acc, attributes)
|
||||
end) do
|
||||
{:ok, ogp_data}
|
||||
else
|
||||
_e -> {:error, "No OGP metadata found"}
|
||||
end
|
||||
end
|
||||
|
||||
defp get_elements(html) do
|
||||
html |> Floki.find("meta[property^='og:']")
|
||||
end
|
||||
|
||||
defp normalize_attributes(html_node) do
|
||||
{_tag, attributes, _children} = html_node
|
||||
|
||||
data =
|
||||
Enum.into(attributes, %{}, fn {name, value} ->
|
||||
{name, String.trim_leading(value, "og:")}
|
||||
end)
|
||||
|
||||
%{String.to_atom(data["property"]) => data["content"]}
|
||||
end
|
||||
end
|
||||
|
|
@ -232,6 +232,12 @@ defmodule Pleroma.Web.Router do
|
|||
put("/settings", MastodonAPIController, :put_settings)
|
||||
end
|
||||
|
||||
scope "/api", Pleroma.Web.RichMedia do
|
||||
pipe_through(:authenticated_api)
|
||||
|
||||
get("/rich_media/parse", RichMediaController, :parse)
|
||||
end
|
||||
|
||||
scope "/api/v1", Pleroma.Web.MastodonAPI do
|
||||
pipe_through(:api)
|
||||
get("/instance", MastodonAPIController, :masto_instance)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue