@@ -47,7 +47,7 @@ defmodule Mobilizon.Service.Export.Feed do
|
||||
defp fetch_actor_event_feed(name) do
|
||||
with %Actor{} = actor <- Actors.get_local_actor_by_name(name),
|
||||
{:visibility, true} <- {:visibility, Actor.is_public_visibility(actor)},
|
||||
{:ok, events, _count} <- Events.list_public_events_for_actor(actor) do
|
||||
%Page{elements: events} <- Events.list_public_events_for_actor(actor) do
|
||||
{:ok, build_actor_feed(actor, events)}
|
||||
else
|
||||
err ->
|
||||
|
||||
@@ -49,7 +49,8 @@ defmodule Mobilizon.Service.Export.ICalendar do
|
||||
@spec export_public_actor(Actor.t()) :: String.t()
|
||||
def export_public_actor(%Actor{} = actor) do
|
||||
with true <- Actor.is_public_visibility(actor),
|
||||
{:ok, events, _} <- Events.list_public_events_for_actor(actor) do
|
||||
%Page{elements: events} <-
|
||||
Events.list_public_events_for_actor(actor) do
|
||||
{:ok, %ICalendar{events: events |> Enum.map(&do_export_event/1)} |> ICalendar.to_ics()}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,8 +4,8 @@ defmodule Mobilizon.Service.Geospatial.Addok do
|
||||
"""
|
||||
|
||||
alias Mobilizon.Addresses.Address
|
||||
alias Mobilizon.Config
|
||||
alias Mobilizon.Service.Geospatial.Provider
|
||||
alias Mobilizon.Service.HTTP.BaseClient
|
||||
|
||||
require Logger
|
||||
|
||||
@@ -15,26 +15,18 @@ defmodule Mobilizon.Service.Geospatial.Addok do
|
||||
@default_country Application.get_env(:mobilizon, __MODULE__) |> get_in([:default_country]) ||
|
||||
"France"
|
||||
|
||||
@http_options [
|
||||
follow_redirect: true,
|
||||
ssl: [{:versions, [:"tlsv1.2"]}]
|
||||
]
|
||||
|
||||
@impl Provider
|
||||
@doc """
|
||||
Addok implementation for `c:Mobilizon.Service.Geospatial.Provider.geocode/3`.
|
||||
"""
|
||||
@spec geocode(String.t(), keyword()) :: list(Address.t())
|
||||
def geocode(lon, lat, options \\ []) do
|
||||
user_agent = Keyword.get(options, :user_agent, Config.instance_user_agent())
|
||||
headers = [{"User-Agent", user_agent}]
|
||||
url = build_url(:geocode, %{lon: lon, lat: lat}, options)
|
||||
|
||||
Logger.debug("Asking addok for addresses with #{url}")
|
||||
|
||||
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
|
||||
HTTPoison.get(url, headers, @http_options),
|
||||
{:ok, %{"features" => features}} <- Poison.decode(body) do
|
||||
with {:ok, %{status: 200, body: body}} <- BaseClient.get(url),
|
||||
%{"features" => features} <- body do
|
||||
process_data(features)
|
||||
else
|
||||
_ -> []
|
||||
@@ -47,14 +39,11 @@ defmodule Mobilizon.Service.Geospatial.Addok do
|
||||
"""
|
||||
@spec search(String.t(), keyword()) :: list(Address.t())
|
||||
def search(q, options \\ []) do
|
||||
user_agent = Keyword.get(options, :user_agent, Config.instance_user_agent())
|
||||
headers = [{"User-Agent", user_agent}]
|
||||
url = build_url(:search, %{q: q}, options)
|
||||
Logger.debug("Asking addok for addresses with #{url}")
|
||||
|
||||
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
|
||||
HTTPoison.get(url, headers, @http_options),
|
||||
{:ok, %{"features" => features}} <- Poison.decode(body) do
|
||||
with {:ok, %{status: 200, body: body}} <- BaseClient.get(url),
|
||||
%{"features" => features} <- body do
|
||||
process_data(features)
|
||||
else
|
||||
_ -> []
|
||||
|
||||
@@ -7,6 +7,7 @@ defmodule Mobilizon.Service.Geospatial.GoogleMaps do
|
||||
|
||||
alias Mobilizon.Addresses.Address
|
||||
alias Mobilizon.Service.Geospatial.Provider
|
||||
alias Mobilizon.Service.HTTP.BaseClient
|
||||
|
||||
require Logger
|
||||
|
||||
@@ -28,11 +29,6 @@ defmodule Mobilizon.Service.Geospatial.GoogleMaps do
|
||||
|
||||
@api_key_missing_message "API Key required to use Google Maps"
|
||||
|
||||
@http_options [
|
||||
follow_redirect: true,
|
||||
ssl: [{:versions, [:"tlsv1.2"]}]
|
||||
]
|
||||
|
||||
@impl Provider
|
||||
@doc """
|
||||
Google Maps implementation for `c:Mobilizon.Service.Geospatial.Provider.geocode/3`.
|
||||
@@ -43,12 +39,11 @@ defmodule Mobilizon.Service.Geospatial.GoogleMaps do
|
||||
|
||||
Logger.debug("Asking Google Maps for reverse geocode with #{url}")
|
||||
|
||||
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
|
||||
HTTPoison.get(url, [], @http_options),
|
||||
{:ok, %{"results" => results, "status" => "OK"}} <- Poison.decode(body) do
|
||||
with {:ok, %{status: 200, body: body}} <- BaseClient.get(url),
|
||||
%{"results" => results, "status" => "OK"} <- body do
|
||||
Enum.map(results, fn entry -> process_data(entry, options) end)
|
||||
else
|
||||
{:ok, %{"status" => "REQUEST_DENIED", "error_message" => error_message}} ->
|
||||
%{"status" => "REQUEST_DENIED", "error_message" => error_message} ->
|
||||
raise ArgumentError, message: to_string(error_message)
|
||||
end
|
||||
end
|
||||
@@ -63,15 +58,14 @@ defmodule Mobilizon.Service.Geospatial.GoogleMaps do
|
||||
|
||||
Logger.debug("Asking Google Maps for addresses with #{url}")
|
||||
|
||||
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
|
||||
HTTPoison.get(url, [], @http_options),
|
||||
{:ok, %{"results" => results, "status" => "OK"}} <- Poison.decode(body) do
|
||||
with {:ok, %{status: 200, body: body}} <- BaseClient.get(url),
|
||||
%{"results" => results, "status" => "OK"} <- body do
|
||||
results |> Enum.map(fn entry -> process_data(entry, options) end)
|
||||
else
|
||||
{:ok, %{"status" => "REQUEST_DENIED", "error_message" => error_message}} ->
|
||||
%{"status" => "REQUEST_DENIED", "error_message" => error_message} ->
|
||||
raise ArgumentError, message: to_string(error_message)
|
||||
|
||||
{:ok, %{"results" => [], "status" => "ZERO_RESULTS"}} ->
|
||||
%{"results" => [], "status" => "ZERO_RESULTS"} ->
|
||||
[]
|
||||
end
|
||||
end
|
||||
@@ -165,18 +159,17 @@ defmodule Mobilizon.Service.Geospatial.GoogleMaps do
|
||||
|
||||
Logger.debug("Asking Google Maps for details with #{url}")
|
||||
|
||||
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
|
||||
HTTPoison.get(url, [], @http_options),
|
||||
{:ok, %{"result" => %{"name" => name}, "status" => "OK"}} <- Poison.decode(body) do
|
||||
with {:ok, %{status: 200, body: body}} <- BaseClient.get(url),
|
||||
%{"result" => %{"name" => name}, "status" => "OK"} <- body do
|
||||
name
|
||||
else
|
||||
{:ok, %{"status" => "REQUEST_DENIED", "error_message" => error_message}} ->
|
||||
%{"status" => "REQUEST_DENIED", "error_message" => error_message} ->
|
||||
raise ArgumentError, message: to_string(error_message)
|
||||
|
||||
{:ok, %{"status" => "INVALID_REQUEST"}} ->
|
||||
%{"status" => "INVALID_REQUEST"} ->
|
||||
raise ArgumentError, message: "Invalid Request"
|
||||
|
||||
{:ok, %{"results" => [], "status" => "ZERO_RESULTS"}} ->
|
||||
%{"results" => [], "status" => "ZERO_RESULTS"} ->
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
@@ -10,8 +10,8 @@ defmodule Mobilizon.Service.Geospatial.MapQuest do
|
||||
"""
|
||||
|
||||
alias Mobilizon.Addresses.Address
|
||||
alias Mobilizon.Config
|
||||
alias Mobilizon.Service.Geospatial.Provider
|
||||
alias Mobilizon.Service.HTTP.BaseClient
|
||||
|
||||
require Logger
|
||||
|
||||
@@ -21,11 +21,6 @@ defmodule Mobilizon.Service.Geospatial.MapQuest do
|
||||
|
||||
@api_key_missing_message "API Key required to use MapQuest"
|
||||
|
||||
@http_options [
|
||||
follow_redirect: true,
|
||||
ssl: [{:versions, [:"tlsv1.2"]}]
|
||||
]
|
||||
|
||||
@impl Provider
|
||||
@doc """
|
||||
MapQuest implementation for `c:Mobilizon.Service.Geospatial.Provider.geocode/3`.
|
||||
@@ -35,25 +30,21 @@ defmodule Mobilizon.Service.Geospatial.MapQuest do
|
||||
api_key = Keyword.get(options, :api_key, @api_key)
|
||||
limit = Keyword.get(options, :limit, 10)
|
||||
open_data = Keyword.get(options, :open_data, true)
|
||||
user_agent = Keyword.get(options, :user_agent, Config.instance_user_agent())
|
||||
headers = [{"User-Agent", user_agent}]
|
||||
|
||||
prefix = if open_data, do: "open", else: "www"
|
||||
|
||||
if is_nil(api_key), do: raise(ArgumentError, message: @api_key_missing_message)
|
||||
|
||||
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
|
||||
HTTPoison.get(
|
||||
with {:ok, %{status: 200, body: body}} <-
|
||||
BaseClient.get(
|
||||
"https://#{prefix}.mapquestapi.com/geocoding/v1/reverse?key=#{api_key}&location=#{
|
||||
lat
|
||||
},#{lon}&maxResults=#{limit}",
|
||||
headers,
|
||||
@http_options
|
||||
},#{lon}&maxResults=#{limit}"
|
||||
),
|
||||
{:ok, %{"results" => results, "info" => %{"statuscode" => 0}}} <- Poison.decode(body) do
|
||||
%{"results" => results, "info" => %{"statuscode" => 0}} <- body do
|
||||
results |> Enum.map(&process_data/1)
|
||||
else
|
||||
{:ok, %HTTPoison.Response{status_code: 403, body: err}} ->
|
||||
{:ok, %{status: 403, body: err}} ->
|
||||
raise(ArgumentError, message: err)
|
||||
end
|
||||
end
|
||||
@@ -64,8 +55,6 @@ defmodule Mobilizon.Service.Geospatial.MapQuest do
|
||||
"""
|
||||
@spec search(String.t(), keyword()) :: list(Address.t())
|
||||
def search(q, options \\ []) do
|
||||
user_agent = Keyword.get(options, :user_agent, Config.instance_user_agent())
|
||||
headers = [{"User-Agent", user_agent}]
|
||||
limit = Keyword.get(options, :limit, 10)
|
||||
api_key = Keyword.get(options, :api_key, @api_key)
|
||||
|
||||
@@ -82,12 +71,11 @@ defmodule Mobilizon.Service.Geospatial.MapQuest do
|
||||
|
||||
Logger.debug("Asking MapQuest for addresses with #{url}")
|
||||
|
||||
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
|
||||
HTTPoison.get(url, headers, @http_options),
|
||||
{:ok, %{"results" => results, "info" => %{"statuscode" => 0}}} <- Poison.decode(body) do
|
||||
with {:ok, %{status: 200, body: body}} <- BaseClient.get(url),
|
||||
%{"results" => results, "info" => %{"statuscode" => 0}} <- body do
|
||||
results |> Enum.map(&process_data/1)
|
||||
else
|
||||
{:ok, %HTTPoison.Response{status_code: 403, body: err}} ->
|
||||
{:ok, %{status: 403, body: err}} ->
|
||||
raise(ArgumentError, message: err)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -8,8 +8,8 @@ defmodule Mobilizon.Service.Geospatial.Mimirsbrunn do
|
||||
"""
|
||||
|
||||
alias Mobilizon.Addresses.Address
|
||||
alias Mobilizon.Config
|
||||
alias Mobilizon.Service.Geospatial.Provider
|
||||
alias Mobilizon.Service.HTTP.BaseClient
|
||||
|
||||
require Logger
|
||||
|
||||
@@ -17,25 +17,17 @@ defmodule Mobilizon.Service.Geospatial.Mimirsbrunn do
|
||||
|
||||
@endpoint Application.get_env(:mobilizon, __MODULE__) |> get_in([:endpoint])
|
||||
|
||||
@http_options [
|
||||
follow_redirect: true,
|
||||
ssl: [{:versions, [:"tlsv1.2"]}]
|
||||
]
|
||||
|
||||
@impl Provider
|
||||
@doc """
|
||||
Mimirsbrunn implementation for `c:Mobilizon.Service.Geospatial.Provider.geocode/3`.
|
||||
"""
|
||||
@spec geocode(number(), number(), keyword()) :: list(Address.t())
|
||||
def geocode(lon, lat, options \\ []) do
|
||||
user_agent = Keyword.get(options, :user_agent, Config.instance_user_agent())
|
||||
headers = [{"User-Agent", user_agent}]
|
||||
url = build_url(:geocode, %{lon: lon, lat: lat}, options)
|
||||
Logger.debug("Asking Mimirsbrunn for reverse geocoding with #{url}")
|
||||
|
||||
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
|
||||
HTTPoison.get(url, headers, @http_options),
|
||||
{:ok, %{"features" => features}} <- Poison.decode(body) do
|
||||
with {:ok, %{status: 200, body: body}} <- BaseClient.get(url),
|
||||
{:ok, %{"features" => features}} <- Jason.decode(body) do
|
||||
process_data(features)
|
||||
else
|
||||
_ -> []
|
||||
@@ -48,14 +40,11 @@ defmodule Mobilizon.Service.Geospatial.Mimirsbrunn do
|
||||
"""
|
||||
@spec search(String.t(), keyword()) :: list(Address.t())
|
||||
def search(q, options \\ []) do
|
||||
user_agent = Keyword.get(options, :user_agent, Config.instance_user_agent())
|
||||
headers = [{"User-Agent", user_agent}]
|
||||
url = build_url(:search, %{q: q}, options)
|
||||
Logger.debug("Asking Mimirsbrunn for addresses with #{url}")
|
||||
|
||||
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
|
||||
HTTPoison.get(url, headers, @http_options),
|
||||
{:ok, %{"features" => features}} <- Poison.decode(body) do
|
||||
with {:ok, %{status: 200, body: body}} <- BaseClient.get(url),
|
||||
{:ok, %{"features" => features}} <- Jason.decode(body) do
|
||||
process_data(features)
|
||||
else
|
||||
_ -> []
|
||||
|
||||
@@ -4,8 +4,8 @@ defmodule Mobilizon.Service.Geospatial.Nominatim do
|
||||
"""
|
||||
|
||||
alias Mobilizon.Addresses.Address
|
||||
alias Mobilizon.Config
|
||||
alias Mobilizon.Service.Geospatial.Provider
|
||||
alias Mobilizon.Service.HTTP.BaseClient
|
||||
|
||||
require Logger
|
||||
|
||||
@@ -14,25 +14,17 @@ defmodule Mobilizon.Service.Geospatial.Nominatim do
|
||||
@endpoint Application.get_env(:mobilizon, __MODULE__) |> get_in([:endpoint])
|
||||
@api_key Application.get_env(:mobilizon, __MODULE__) |> get_in([:api_key])
|
||||
|
||||
@http_options [
|
||||
follow_redirect: true,
|
||||
ssl: [{:versions, [:"tlsv1.2"]}]
|
||||
]
|
||||
|
||||
@impl Provider
|
||||
@doc """
|
||||
Nominatim implementation for `c:Mobilizon.Service.Geospatial.Provider.geocode/3`.
|
||||
"""
|
||||
@spec geocode(String.t(), keyword()) :: list(Address.t())
|
||||
def geocode(lon, lat, options \\ []) do
|
||||
user_agent = Keyword.get(options, :user_agent, Config.instance_user_agent())
|
||||
headers = [{"User-Agent", user_agent}]
|
||||
url = build_url(:geocode, %{lon: lon, lat: lat}, options)
|
||||
Logger.debug("Asking Nominatim for geocode with #{url}")
|
||||
|
||||
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
|
||||
HTTPoison.get(url, headers, @http_options),
|
||||
{:ok, %{"features" => features}} <- Poison.decode(body) do
|
||||
with {:ok, %{status: 200, body: body}} <- BaseClient.get(url),
|
||||
%{"features" => features} <- body do
|
||||
features |> process_data() |> Enum.filter(& &1)
|
||||
else
|
||||
_ -> []
|
||||
@@ -45,14 +37,11 @@ defmodule Mobilizon.Service.Geospatial.Nominatim do
|
||||
"""
|
||||
@spec search(String.t(), keyword()) :: list(Address.t())
|
||||
def search(q, options \\ []) do
|
||||
user_agent = Keyword.get(options, :user_agent, Config.instance_user_agent())
|
||||
headers = [{"User-Agent", user_agent}]
|
||||
url = build_url(:search, %{q: q}, options)
|
||||
Logger.debug("Asking Nominatim for addresses with #{url}")
|
||||
|
||||
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
|
||||
HTTPoison.get(url, headers, @http_options),
|
||||
{:ok, %{"features" => features}} <- Poison.decode(body) do
|
||||
with {:ok, %{status: 200, body: body}} <- BaseClient.get(url),
|
||||
%{"features" => features} <- body do
|
||||
features |> process_data() |> Enum.filter(& &1)
|
||||
else
|
||||
_ -> []
|
||||
|
||||
@@ -6,8 +6,8 @@ defmodule Mobilizon.Service.Geospatial.Pelias do
|
||||
"""
|
||||
|
||||
alias Mobilizon.Addresses.Address
|
||||
alias Mobilizon.Config
|
||||
alias Mobilizon.Service.Geospatial.Provider
|
||||
alias Mobilizon.Service.HTTP.BaseClient
|
||||
|
||||
require Logger
|
||||
|
||||
@@ -15,25 +15,17 @@ defmodule Mobilizon.Service.Geospatial.Pelias do
|
||||
|
||||
@endpoint Application.get_env(:mobilizon, __MODULE__) |> get_in([:endpoint])
|
||||
|
||||
@http_options [
|
||||
follow_redirect: true,
|
||||
ssl: [{:versions, [:"tlsv1.2"]}]
|
||||
]
|
||||
|
||||
@impl Provider
|
||||
@doc """
|
||||
Pelias implementation for `c:Mobilizon.Service.Geospatial.Provider.geocode/3`.
|
||||
"""
|
||||
@spec geocode(number(), number(), keyword()) :: list(Address.t())
|
||||
def geocode(lon, lat, options \\ []) do
|
||||
user_agent = Keyword.get(options, :user_agent, Config.instance_user_agent())
|
||||
headers = [{"User-Agent", user_agent}]
|
||||
url = build_url(:geocode, %{lon: lon, lat: lat}, options)
|
||||
Logger.debug("Asking Pelias for reverse geocoding with #{url}")
|
||||
|
||||
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
|
||||
HTTPoison.get(url, headers, @http_options),
|
||||
{:ok, %{"features" => features}} <- Poison.decode(body) do
|
||||
with {:ok, %{status: 200, body: body}} <- BaseClient.get(url),
|
||||
{:ok, %{"features" => features}} <- Jason.decode(body) do
|
||||
process_data(features)
|
||||
else
|
||||
_ -> []
|
||||
@@ -46,14 +38,11 @@ defmodule Mobilizon.Service.Geospatial.Pelias do
|
||||
"""
|
||||
@spec search(String.t(), keyword()) :: list(Address.t())
|
||||
def search(q, options \\ []) do
|
||||
user_agent = Keyword.get(options, :user_agent, Config.instance_user_agent())
|
||||
headers = [{"User-Agent", user_agent}]
|
||||
url = build_url(:search, %{q: q}, options)
|
||||
Logger.debug("Asking Pelias for addresses with #{url}")
|
||||
|
||||
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
|
||||
HTTPoison.get(url, headers, @http_options),
|
||||
{:ok, %{"features" => features}} <- Poison.decode(body) do
|
||||
with {:ok, %{status: 200, body: body}} <- BaseClient.get(url),
|
||||
{:ok, %{"features" => features}} <- Jason.decode(body) do
|
||||
process_data(features)
|
||||
else
|
||||
_ -> []
|
||||
|
||||
@@ -4,8 +4,8 @@ defmodule Mobilizon.Service.Geospatial.Photon do
|
||||
"""
|
||||
|
||||
alias Mobilizon.Addresses.Address
|
||||
alias Mobilizon.Config
|
||||
alias Mobilizon.Service.Geospatial.Provider
|
||||
alias Mobilizon.Service.HTTP.BaseClient
|
||||
|
||||
require Logger
|
||||
|
||||
@@ -13,11 +13,6 @@ defmodule Mobilizon.Service.Geospatial.Photon do
|
||||
|
||||
@endpoint Application.get_env(:mobilizon, __MODULE__) |> get_in([:endpoint])
|
||||
|
||||
@http_options [
|
||||
follow_redirect: true,
|
||||
ssl: [{:versions, [:"tlsv1.2"]}]
|
||||
]
|
||||
|
||||
@impl Provider
|
||||
@doc """
|
||||
Photon implementation for `c:Mobilizon.Service.Geospatial.Provider.geocode/3`.
|
||||
@@ -26,14 +21,11 @@ defmodule Mobilizon.Service.Geospatial.Photon do
|
||||
"""
|
||||
@spec geocode(number(), number(), keyword()) :: list(Address.t())
|
||||
def geocode(lon, lat, options \\ []) do
|
||||
user_agent = Keyword.get(options, :user_agent, Config.instance_user_agent())
|
||||
headers = [{"User-Agent", user_agent}]
|
||||
url = build_url(:geocode, %{lon: lon, lat: lat}, options)
|
||||
Logger.debug("Asking photon for reverse geocoding with #{url}")
|
||||
|
||||
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
|
||||
HTTPoison.get(url, headers, @http_options),
|
||||
{:ok, %{"features" => features}} <- Poison.decode(body) do
|
||||
with {:ok, %{status: 200, body: body}} <- BaseClient.get(url),
|
||||
%{"features" => features} <- body do
|
||||
process_data(features)
|
||||
else
|
||||
_ -> []
|
||||
@@ -46,14 +38,11 @@ defmodule Mobilizon.Service.Geospatial.Photon do
|
||||
"""
|
||||
@spec search(String.t(), keyword()) :: list(Address.t())
|
||||
def search(q, options \\ []) do
|
||||
user_agent = Keyword.get(options, :user_agent, Config.instance_user_agent())
|
||||
headers = [{"User-Agent", user_agent}]
|
||||
url = build_url(:search, %{q: q}, options)
|
||||
Logger.debug("Asking photon for addresses with #{url}")
|
||||
|
||||
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
|
||||
HTTPoison.get(url, headers, @http_options),
|
||||
{:ok, %{"features" => features}} <- Poison.decode(body) do
|
||||
with {:ok, %{status: 200, body: body}} <- BaseClient.get(url),
|
||||
%{"features" => features} <- body do
|
||||
process_data(features)
|
||||
else
|
||||
_ -> []
|
||||
|
||||
@@ -15,7 +15,6 @@ defmodule Mobilizon.Service.Geospatial.Provider do
|
||||
|
||||
## Shared options
|
||||
|
||||
* `:user_agent` User-Agent string to send to the backend. Defaults to `"Mobilizon"` or `Mobilizon.Config.instance_user_agent/0`
|
||||
* `:lang` Lang in which to prefer results. Used as a request parameter or
|
||||
through an `Accept-Language` HTTP header. Defaults to `"en"`.
|
||||
* `:country_code` An ISO 3166 country code. String or `nil`
|
||||
|
||||
38
lib/service/http/activity_pub.ex
Normal file
38
lib/service/http/activity_pub.ex
Normal file
@@ -0,0 +1,38 @@
|
||||
defmodule Mobilizon.Service.HTTP.ActivityPub do
|
||||
@moduledoc """
|
||||
Tesla HTTP Client that is preconfigured to get and post ActivityPub content
|
||||
"""
|
||||
|
||||
alias Mobilizon.Config
|
||||
|
||||
@adapter Application.get_env(:tesla, __MODULE__, [])[:adapter] || Tesla.Adapter.Hackney
|
||||
@default_opts [
|
||||
recv_timeout: 20_000
|
||||
]
|
||||
@user_agent Config.instance_user_agent()
|
||||
|
||||
def client(options \\ []) do
|
||||
headers = Keyword.get(options, :headers, [])
|
||||
opts = Keyword.merge(@default_opts, Keyword.get(options, :opts, []))
|
||||
|
||||
middleware = [
|
||||
{Tesla.Middleware.Headers,
|
||||
[{"User-Agent", @user_agent}, {"Accept", "application/activity+json"}] ++ headers},
|
||||
Tesla.Middleware.FollowRedirects,
|
||||
{Tesla.Middleware.Timeout, timeout: 10_000},
|
||||
{Tesla.Middleware.JSON, decode_content_types: "application/activity+json"}
|
||||
]
|
||||
|
||||
adapter = {@adapter, opts}
|
||||
|
||||
Tesla.client(middleware, adapter)
|
||||
end
|
||||
|
||||
def get(client, url) do
|
||||
Tesla.get(client, url)
|
||||
end
|
||||
|
||||
def post(client, url, data) do
|
||||
Tesla.post(client, url, data)
|
||||
end
|
||||
end
|
||||
30
lib/service/http/base_client.ex
Normal file
30
lib/service/http/base_client.ex
Normal file
@@ -0,0 +1,30 @@
|
||||
defmodule Mobilizon.Service.HTTP.BaseClient do
|
||||
@moduledoc """
|
||||
Tesla HTTP Basic Client
|
||||
"""
|
||||
|
||||
use Tesla
|
||||
alias Mobilizon.Config
|
||||
|
||||
@default_opts [
|
||||
recv_timeout: 20_000
|
||||
]
|
||||
|
||||
adapter(Tesla.Adapter.Hackney, @default_opts)
|
||||
|
||||
@user_agent Config.instance_user_agent()
|
||||
|
||||
plug(Tesla.Middleware.FollowRedirects)
|
||||
|
||||
plug(Tesla.Middleware.Timeout, timeout: 10_000)
|
||||
|
||||
plug(Tesla.Middleware.Headers, [{"User-Agent", @user_agent}])
|
||||
|
||||
def get(url) do
|
||||
get(url)
|
||||
end
|
||||
|
||||
def post(url, data) do
|
||||
post(url, data)
|
||||
end
|
||||
end
|
||||
@@ -1,6 +1,6 @@
|
||||
defimpl Mobilizon.Service.Metadata, for: Mobilizon.Conversations.Comment do
|
||||
defimpl Mobilizon.Service.Metadata, for: Mobilizon.Discussions.Comment do
|
||||
alias Phoenix.HTML.Tag
|
||||
alias Mobilizon.Conversations.Comment
|
||||
alias Mobilizon.Discussions.Comment
|
||||
|
||||
def build_tags(%Comment{} = comment, _locale \\ "en") do
|
||||
[
|
||||
|
||||
@@ -2,10 +2,9 @@ defimpl Mobilizon.Service.Metadata, for: Mobilizon.Events.Event do
|
||||
alias Phoenix.HTML
|
||||
alias Phoenix.HTML.Tag
|
||||
alias Mobilizon.Events.Event
|
||||
alias Mobilizon.Service.Formatter.HTML, as: HTMLFormatter
|
||||
alias Mobilizon.Web.JsonLD.ObjectView
|
||||
alias Mobilizon.Web.MediaProxy
|
||||
import Mobilizon.Web.Gettext
|
||||
import Mobilizon.Service.Metadata.Utils, only: [process_description: 2, strip_tags: 1]
|
||||
|
||||
def build_tags(%Event{} = event, locale \\ "en") do
|
||||
event = Map.put(event, :description, process_description(event.description, locale))
|
||||
@@ -41,24 +40,10 @@ defimpl Mobilizon.Service.Metadata, for: Mobilizon.Events.Event do
|
||||
]
|
||||
end
|
||||
|
||||
defp process_description(nil, locale), do: process_description("", locale)
|
||||
|
||||
defp process_description("", locale) do
|
||||
Gettext.put_locale(locale)
|
||||
gettext("The event organizer didn't add any description.")
|
||||
end
|
||||
|
||||
defp process_description(description, _locale) do
|
||||
description
|
||||
|> HTMLFormatter.strip_tags()
|
||||
|> String.slice(0..200)
|
||||
|> (&"#{&1}…").()
|
||||
end
|
||||
|
||||
# Insert JSON-LD schema by hand because Tag.content_tag wants to escape it
|
||||
defp json(%Event{title: title} = event) do
|
||||
"event.json"
|
||||
|> ObjectView.render(%{event: %{event | title: HTMLFormatter.strip_tags(title)}})
|
||||
|> ObjectView.render(%{event: %{event | title: strip_tags(title)}})
|
||||
|> Jason.encode!()
|
||||
end
|
||||
end
|
||||
|
||||
34
lib/service/metadata/post.ex
Normal file
34
lib/service/metadata/post.ex
Normal file
@@ -0,0 +1,34 @@
|
||||
defimpl Mobilizon.Service.Metadata, for: Mobilizon.Posts.Post do
|
||||
alias Phoenix.HTML
|
||||
alias Phoenix.HTML.Tag
|
||||
alias Mobilizon.Posts.Post
|
||||
alias Mobilizon.Web.JsonLD.ObjectView
|
||||
import Mobilizon.Service.Metadata.Utils, only: [process_description: 2, strip_tags: 1]
|
||||
|
||||
def build_tags(%Post{} = post, locale \\ "en") do
|
||||
post = Map.put(post, :body, process_description(post.body, locale))
|
||||
|
||||
tags = [
|
||||
Tag.tag(:meta, property: "og:title", content: post.title),
|
||||
Tag.tag(:meta, property: "og:url", content: post.url),
|
||||
Tag.tag(:meta, property: "og:description", content: post.body),
|
||||
Tag.tag(:meta, property: "og:type", content: "article"),
|
||||
Tag.tag(:meta, property: "twitter:card", content: "summary"),
|
||||
# Tell Search Engines what's the origin
|
||||
Tag.tag(:link, rel: "canonical", href: post.url)
|
||||
]
|
||||
|
||||
tags ++
|
||||
[
|
||||
Tag.tag(:meta, property: "twitter:card", content: "summary_large_image"),
|
||||
~s{<script type="application/ld+json">#{json(post)}</script>} |> HTML.raw()
|
||||
]
|
||||
end
|
||||
|
||||
# Insert JSON-LD schema by hand because Tag.content_tag wants to escape it
|
||||
defp json(%Post{title: title} = post) do
|
||||
"post.json"
|
||||
|> ObjectView.render(%{post: %{post | title: strip_tags(title)}})
|
||||
|> Jason.encode!()
|
||||
end
|
||||
end
|
||||
@@ -3,10 +3,34 @@ defmodule Mobilizon.Service.Metadata.Utils do
|
||||
Tools to convert tags to string.
|
||||
"""
|
||||
|
||||
alias Mobilizon.Service.Formatter.HTML, as: HTMLFormatter
|
||||
alias Phoenix.HTML
|
||||
import Mobilizon.Web.Gettext
|
||||
|
||||
@slice_limit 200
|
||||
|
||||
@spec stringify_tags(Enum.t()) :: String.t()
|
||||
def stringify_tags(tags), do: Enum.reduce(tags, "", &stringify_tag/2)
|
||||
|
||||
defp stringify_tag(tag, acc) when is_tuple(tag), do: acc <> HTML.safe_to_string(tag)
|
||||
defp stringify_tag(tag, acc) when is_binary(tag), do: acc <> tag
|
||||
|
||||
@spec strip_tags(String.t()) :: String.t()
|
||||
def strip_tags(text), do: HTMLFormatter.strip_tags(text)
|
||||
|
||||
@spec process_description(String.t(), String.t(), integer()) :: String.t()
|
||||
def process_description(description, locale \\ "en", limit \\ @slice_limit)
|
||||
def process_description(nil, locale, limit), do: process_description("", locale, limit)
|
||||
|
||||
def process_description("", locale, _limit) do
|
||||
Gettext.put_locale(locale)
|
||||
gettext("The event organizer didn't add any description.")
|
||||
end
|
||||
|
||||
def process_description(description, _locale, limit) do
|
||||
description
|
||||
|> HTMLFormatter.strip_tags()
|
||||
|> String.slice(0..limit)
|
||||
|> (&"#{&1}…").()
|
||||
end
|
||||
end
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -3,7 +3,7 @@ defmodule Mobilizon.Service.Statistics do
|
||||
A module that provides cached statistics
|
||||
"""
|
||||
|
||||
alias Mobilizon.{Conversations, Events, Users}
|
||||
alias Mobilizon.{Discussions, Events, Users}
|
||||
|
||||
def get_cached_value(key) do
|
||||
case Cachex.fetch(:statistics, key, fn key ->
|
||||
@@ -26,6 +26,6 @@ defmodule Mobilizon.Service.Statistics do
|
||||
end
|
||||
|
||||
defp create_cache(:local_comments) do
|
||||
Conversations.count_local_comments()
|
||||
Discussions.count_local_comments()
|
||||
end
|
||||
end
|
||||
|
||||
@@ -98,7 +98,7 @@ defmodule Mobilizon.Service.Workers.Notification do
|
||||
else
|
||||
err ->
|
||||
require Logger
|
||||
Logger.error(inspect(err))
|
||||
Logger.debug(inspect(err))
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user