Introduce group posts

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2020-07-09 17:24:28 +02:00
parent bec1c69d4b
commit 9c9f1385fb
249 changed files with 11886 additions and 5023 deletions

View File

@@ -16,24 +16,24 @@ defmodule Mobilizon.Service.RichMedia.Favicon do
ssl: [{:versions, [:"tlsv1.2"]}]
]
@spec fetch(String.t(), List.t()) :: {:ok, String.t()} | {:error, any()}
@spec fetch(String.t(), Enum.t()) :: {:ok, String.t()} | {:error, any()}
def fetch(url, options \\ []) do
user_agent = Keyword.get(options, :user_agent, Config.instance_user_agent())
headers = [{"User-Agent", user_agent}]
case HTTPoison.get(url, headers, @options) do
{:ok, %HTTPoison.Response{status_code: code, body: body}} when code in 200..299 ->
case Tesla.get(url, headers: headers, opts: @options) do
{:ok, %{status: code, body: body}} when code in 200..299 ->
find_favicon_url(url, body, headers)
{:ok, %HTTPoison.Response{}} ->
{:ok, %{}} ->
{:error, "Error while fetching the page"}
{:error, %HTTPoison.Error{reason: reason}} ->
{:error, reason}
{:error, err} ->
{:error, err}
end
end
@spec find_favicon_url(String.t(), String.t(), List.t()) :: {:ok, String.t()} | {:error, any()}
@spec find_favicon_url(String.t(), String.t(), Enum.t()) :: {:ok, String.t()} | {:error, any()}
defp find_favicon_url(url, body, headers) do
Logger.debug("finding favicon URL for #{url}")
@@ -85,20 +85,20 @@ defmodule Mobilizon.Service.RichMedia.Favicon do
end
end
@spec find_favicon_in_root(String.t(), List.t()) :: {:ok, String.t()} | {:error, any()}
@spec find_favicon_in_root(String.t(), Enum.t()) :: {:ok, String.t()} | {:error, any()}
defp find_favicon_in_root(url, headers) do
uri = URI.parse(url)
favicon_url = "#{uri.scheme}://#{uri.host}/favicon.ico"
case HTTPoison.head(favicon_url, headers, @options) do
{:ok, %HTTPoison.Response{status_code: code}} when code in 200..299 ->
case Tesla.head(favicon_url, headers: headers, opts: @options) do
{:ok, %{status: code}} when code in 200..299 ->
{:ok, favicon_url}
{:ok, %HTTPoison.Response{}} ->
{:ok, %{}} ->
{:error, "Error while doing a HEAD request on the favicon"}
{:error, %HTTPoison.Error{reason: reason}} ->
{:error, reason}
{:error, err} ->
{:error, err}
end
end
end

View File

@@ -12,7 +12,7 @@ defmodule Mobilizon.Service.RichMedia.Parser do
timeout: 10_000,
recv_timeout: 20_000,
follow_redirect: true,
# TODO: Remove me once Hackney/HTTPoison fixes their shit with TLS1.3 and OTP 23
# TODO: Remove me once Hackney/HTTPoison fixes their issue with TLS1.3 and OTP 23
ssl: [{:versions, [:"tlsv1.2"]}]
]
@@ -46,7 +46,7 @@ defmodule Mobilizon.Service.RichMedia.Parser do
{:error, "Cachex error: #{inspect(e)}"}
end
@spec parse_url(String.t(), List.t()) :: {:ok, map()} | {:error, any()}
@spec parse_url(String.t(), Enum.t()) :: {:ok, map()} | {:error, any()}
defp parse_url(url, options \\ []) do
user_agent = Keyword.get(options, :user_agent, Config.instance_user_agent())
headers = [{"User-Agent", user_agent}]
@@ -54,12 +54,12 @@ defmodule Mobilizon.Service.RichMedia.Parser do
try do
with {:ok, _} <- prevent_local_address(url),
{:ok, %HTTPoison.Response{body: body, status_code: code, headers: response_headers}}
{:ok, %{body: body, status: code, headers: response_headers}}
when code in 200..299 <-
HTTPoison.get(
Tesla.get(
url,
headers,
@options
headers: headers,
opts: @options
),
{:is_html, _response_headers, true} <-
{:is_html, response_headers, is_html(response_headers)} do
@@ -87,7 +87,7 @@ defmodule Mobilizon.Service.RichMedia.Parser do
end
end
@spec get_data_for_media(List.t(), String.t()) :: map()
@spec get_data_for_media(Enum.t(), String.t()) :: map()
defp get_data_for_media(response_headers, url) do
data = %{title: get_filename_from_headers(response_headers) || get_filename_from_url(url)}
@@ -98,21 +98,21 @@ defmodule Mobilizon.Service.RichMedia.Parser do
end
end
@spec is_html(List.t()) :: boolean
defp is_html(headers) do
@spec is_html(Enum.t()) :: boolean
def is_html(headers) do
headers
|> get_header("Content-Type")
|> content_type_header_matches(["text/html", "application/xhtml"])
end
@spec is_image(List.t()) :: boolean
@spec is_image(Enum.t()) :: boolean
defp is_image(headers) do
headers
|> get_header("Content-Type")
|> content_type_header_matches(["image/"])
end
@spec content_type_header_matches(String.t() | nil, List.t()) :: boolean
@spec content_type_header_matches(String.t() | nil, Enum.t()) :: boolean
defp content_type_header_matches(header, content_types)
defp content_type_header_matches(nil, _content_types), do: false
@@ -120,15 +120,17 @@ defmodule Mobilizon.Service.RichMedia.Parser do
Enum.any?(content_types, fn content_type -> String.starts_with?(header, content_type) end)
end
@spec get_header(List.t(), String.t()) :: String.t() | nil
@spec get_header(Enum.t(), String.t()) :: String.t() | nil
defp get_header(headers, key) do
key = String.downcase(key)
case List.keyfind(headers, key, 0) do
{^key, value} -> String.downcase(value)
nil -> nil
end
end
@spec get_filename_from_headers(List.t()) :: String.t() | nil
@spec get_filename_from_headers(Enum.t()) :: String.t() | nil
defp get_filename_from_headers(headers) do
case get_header(headers, "Content-Disposition") do
nil -> nil
@@ -138,12 +140,16 @@ defmodule Mobilizon.Service.RichMedia.Parser do
@spec get_filename_from_url(String.t()) :: String.t()
defp get_filename_from_url(url) do
%URI{path: path} = URI.parse(url)
case URI.parse(url) do
%URI{path: nil} ->
nil
path
|> String.split("/", trim: true)
|> Enum.at(-1)
|> URI.decode()
%URI{path: path} ->
path
|> String.split("/", trim: true)
|> Enum.at(-1)
|> URI.decode()
end
end
# The following is taken from https://github.com/elixir-plug/plug/blob/65986ad32f9aaae3be50dc80cbdd19b326578da7/lib/plug/parsers/multipart.ex#L207

View File

@@ -42,7 +42,7 @@ defmodule Mobilizon.Service.RichMedia.Parsers.OEmbed do
end
defp get_oembed_data(url) do
with {:ok, %HTTPoison.Response{body: json}} <- HTTPoison.get(url, [], @http_options),
with {:ok, %{body: json}} <- Tesla.get(url, opts: @http_options),
{:ok, data} <- Jason.decode(json),
data <- data |> Map.new(fn {k, v} -> {String.to_atom(k), v} end) do
{:ok, data}

View File

@@ -34,7 +34,7 @@ defmodule Mobilizon.Service.RichMedia.Parsers.OGP do
|> Map.put(:height, get_integer_value(data, :"image:height"))
end
@spec get_integer_value(Map.t(), atom()) :: integer() | nil
@spec get_integer_value(map(), atom()) :: integer() | nil
defp get_integer_value(data, key) do
with value when not is_nil(value) <- Map.get(data, key),
{value, ""} <- Integer.parse(value) do