@@ -6,8 +6,8 @@ defmodule Mobilizon.Service.Activity do
|
||||
alias Mobilizon.Activities.Activity
|
||||
alias Mobilizon.Service.Activity.{Comment, Discussion, Event, Group, Member, Post, Resource}
|
||||
|
||||
@callback insert_activity(entity :: struct(), options :: map()) ::
|
||||
{:ok, Oban.Job.t()} | {:ok, any()}
|
||||
@callback insert_activity(entity :: struct(), options :: Keyword.t()) ::
|
||||
{:ok, Oban.Job.t()} | {:ok, any()} | {:error, Ecto.Changeset.t()}
|
||||
|
||||
@callback get_object(object_id :: String.t() | integer()) :: struct()
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ defmodule Mobilizon.Service.Activity.Member do
|
||||
Actors.get_member(member_id)
|
||||
end
|
||||
|
||||
@spec get_author(Member.t(), Member.t() | nil) :: integer()
|
||||
@spec get_author(Member.t(), Keyword.t()) :: integer()
|
||||
defp get_author(%Member{actor_id: actor_id}, options) do
|
||||
moderator = Keyword.get(options, :moderator)
|
||||
|
||||
|
||||
@@ -191,14 +191,10 @@ defmodule Mobilizon.Service.Auth.LDAPAuthenticator do
|
||||
])
|
||||
end
|
||||
|
||||
@spec register_user(String.t()) :: User.t() | any()
|
||||
@spec register_user(String.t()) :: User.t() | {:error, Ecto.Changeset.t()}
|
||||
defp register_user(email) do
|
||||
case Users.create_external(email, "ldap") do
|
||||
{:ok, %User{} = user} ->
|
||||
user
|
||||
|
||||
error ->
|
||||
error
|
||||
with {:ok, %User{} = user} <- Users.create_external(email, "ldap") do
|
||||
user
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ defmodule Mobilizon.Service.CleanOrphanMedia do
|
||||
* `grace_period` how old in hours can the media be before it's taken into account for deletion
|
||||
* `dry_run` just return the media that would have been deleted, don't actually delete it
|
||||
"""
|
||||
@spec clean(Keyword.t()) :: {:ok, list(Media.t())} | {:error, String.t()}
|
||||
@spec clean(Keyword.t()) :: {:ok, list(Media.t())}
|
||||
def clean(opts \\ []) do
|
||||
medias = find_media(opts)
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ defmodule Mobilizon.Service.CleanUnconfirmedUsers do
|
||||
|
||||
Remove media that is not attached to an entity, such as media uploads that were never used in entities.
|
||||
"""
|
||||
@spec clean(Keyword.t()) :: {:ok, list(Media.t())} | {:error, String.t()}
|
||||
@spec clean(Keyword.t()) :: {:ok, list(Media.t())}
|
||||
def clean(opts \\ []) do
|
||||
users_to_delete = find_unconfirmed_users_to_clean(opts)
|
||||
|
||||
|
||||
@@ -21,49 +21,41 @@ defmodule Mobilizon.Service.Export.Feed do
|
||||
|
||||
@item_limit 500
|
||||
|
||||
def version, do: Config.instance_version()
|
||||
@spec version :: String.t()
|
||||
defp version, do: Config.instance_version()
|
||||
|
||||
@spec create_cache(String.t()) :: {:commit, String.t()} | {:ignore, any()}
|
||||
@spec create_cache(String.t()) ::
|
||||
{:commit, String.t()}
|
||||
| {:ignore, :actor_not_found | :actor_not_public | :bad_token | :token_not_found}
|
||||
def create_cache("actor_" <> name) do
|
||||
case fetch_actor_event_feed(name) do
|
||||
{:ok, res} ->
|
||||
{:commit, res}
|
||||
|
||||
err ->
|
||||
{:error, err} ->
|
||||
{:ignore, err}
|
||||
end
|
||||
end
|
||||
|
||||
@spec create_cache(String.t()) :: {:commit, String.t()} | {:ignore, any()}
|
||||
def create_cache("token_" <> token) do
|
||||
case fetch_events_from_token(token) do
|
||||
{:ok, res} ->
|
||||
{:commit, res}
|
||||
|
||||
err ->
|
||||
{:error, err} ->
|
||||
{:ignore, err}
|
||||
end
|
||||
end
|
||||
|
||||
def create_cache("instance") do
|
||||
case fetch_instance_feed() do
|
||||
{:ok, res} ->
|
||||
{:commit, res}
|
||||
|
||||
err ->
|
||||
{:ignore, err}
|
||||
end
|
||||
{:ok, res} = fetch_instance_feed()
|
||||
{:commit, res}
|
||||
end
|
||||
|
||||
@spec fetch_instance_feed :: {:ok, String.t()}
|
||||
defp fetch_instance_feed do
|
||||
case Common.fetch_instance_public_content(@item_limit) do
|
||||
{:ok, events, posts} ->
|
||||
{:ok, build_instance_feed(events, posts)}
|
||||
|
||||
err ->
|
||||
{:error, err}
|
||||
end
|
||||
{:ok, events, posts} = Common.fetch_instance_public_content(@item_limit)
|
||||
{:ok, build_instance_feed(events, posts)}
|
||||
end
|
||||
|
||||
# Build an atom feed from the whole instance and its public events and posts
|
||||
@@ -90,7 +82,8 @@ defmodule Mobilizon.Service.Export.Feed do
|
||||
|> Atomex.generate_document()
|
||||
end
|
||||
|
||||
@spec fetch_actor_event_feed(String.t(), integer()) :: String.t()
|
||||
@spec fetch_actor_event_feed(String.t(), integer()) ::
|
||||
{:ok, String.t()} | {:error, :actor_not_found | :actor_not_public}
|
||||
defp fetch_actor_event_feed(name, limit \\ @item_limit) do
|
||||
case Common.fetch_actor_event_feed(name, limit) do
|
||||
{:ok, actor, events, posts} ->
|
||||
@@ -199,7 +192,8 @@ defmodule Mobilizon.Service.Export.Feed do
|
||||
end
|
||||
|
||||
# Only events, not posts
|
||||
@spec fetch_events_from_token(String.t(), integer()) :: {:ok, String.t()} | {:error, atom()}
|
||||
@spec fetch_events_from_token(String.t(), integer()) ::
|
||||
{:ok, String.t()} | {:error, :bad_token | :token_not_found}
|
||||
defp fetch_events_from_token(token, limit \\ @item_limit) do
|
||||
case Common.fetch_events_from_token(token, limit) do
|
||||
%{events: events, token: token, user: user, actor: actor, type: type} ->
|
||||
|
||||
@@ -93,11 +93,7 @@ defmodule Mobilizon.Service.Geospatial.Addok do
|
||||
if is_nil(value), do: url, else: do_add_parameter(url, key, value)
|
||||
end
|
||||
|
||||
@spec do_add_parameter(String.t(), :coords | :type, %{lat: float, lon: float} | :administrative) ::
|
||||
String.t()
|
||||
defp do_add_parameter(url, :coords, coords),
|
||||
do: "#{url}&lat=#{coords.lat}&lon=#{coords.lon}"
|
||||
|
||||
@spec do_add_parameter(String.t(), :type, :administrative | atom()) :: String.t()
|
||||
defp do_add_parameter(url, :type, :administrative),
|
||||
do: "#{url}&type=municipality"
|
||||
|
||||
|
||||
@@ -31,16 +31,18 @@ defmodule Mobilizon.Service.Geospatial.GoogleMaps do
|
||||
@doc """
|
||||
Google Maps implementation for `c:Mobilizon.Service.Geospatial.Provider.geocode/3`.
|
||||
"""
|
||||
@spec geocode(String.t(), keyword()) :: list(Address.t()) | no_return
|
||||
@spec geocode(float(), float(), keyword()) :: list(Address.t())
|
||||
def geocode(lon, lat, options \\ []) do
|
||||
url = build_url(:geocode, %{lon: lon, lat: lat}, options)
|
||||
|
||||
Logger.debug("Asking Google Maps for reverse geocode with #{url}")
|
||||
|
||||
with {:ok, %{status: 200, body: body}} <- GeospatialClient.get(url),
|
||||
%{"results" => results, "status" => "OK"} <- body do
|
||||
Enum.map(results, fn entry -> process_data(entry, options) end)
|
||||
else
|
||||
%Tesla.Env{status: 200, body: body} = GeospatialClient.get!(url)
|
||||
|
||||
case body do
|
||||
%{"results" => results, "status" => "OK"} ->
|
||||
Enum.map(results, &process_data(&1, options))
|
||||
|
||||
%{"status" => "REQUEST_DENIED", "error_message" => error_message} ->
|
||||
raise ArgumentError, message: to_string(error_message)
|
||||
end
|
||||
@@ -50,16 +52,18 @@ defmodule Mobilizon.Service.Geospatial.GoogleMaps do
|
||||
@doc """
|
||||
Google Maps implementation for `c:Mobilizon.Service.Geospatial.Provider.search/2`.
|
||||
"""
|
||||
@spec search(String.t(), keyword()) :: list(Address.t()) | no_return
|
||||
@spec search(String.t(), keyword()) :: list(Address.t())
|
||||
def search(q, options \\ []) do
|
||||
url = build_url(:search, %{q: q}, options)
|
||||
|
||||
Logger.debug("Asking Google Maps for addresses with #{url}")
|
||||
|
||||
with {:ok, %{status: 200, body: body}} <- GeospatialClient.get(url),
|
||||
%{"results" => results, "status" => "OK"} <- body do
|
||||
results |> Enum.map(fn entry -> process_data(entry, options) end)
|
||||
else
|
||||
%Tesla.Env{status: 200, body: body} = GeospatialClient.get!(url)
|
||||
|
||||
case body do
|
||||
%{"results" => results, "status" => "OK"} ->
|
||||
results |> Enum.map(fn entry -> process_data(entry, options) end)
|
||||
|
||||
%{"status" => "REQUEST_DENIED", "error_message" => error_message} ->
|
||||
raise ArgumentError, message: to_string(error_message)
|
||||
|
||||
@@ -68,7 +72,7 @@ defmodule Mobilizon.Service.Geospatial.GoogleMaps do
|
||||
end
|
||||
end
|
||||
|
||||
@spec build_url(atom(), map(), list()) :: String.t() | no_return
|
||||
@spec build_url(:search | :geocode, map(), list()) :: String.t() | no_return
|
||||
defp build_url(method, args, options) do
|
||||
limit = Keyword.get(options, :limit, 10)
|
||||
lang = Keyword.get(options, :lang, "en")
|
||||
@@ -97,6 +101,7 @@ defmodule Mobilizon.Service.Geospatial.GoogleMaps do
|
||||
URI.encode(uri)
|
||||
end
|
||||
|
||||
@spec process_data(map(), Keyword.t()) :: Address.t()
|
||||
defp process_data(
|
||||
%{
|
||||
"formatted_address" => description,
|
||||
@@ -148,16 +153,18 @@ defmodule Mobilizon.Service.Geospatial.GoogleMaps do
|
||||
end
|
||||
end
|
||||
|
||||
@spec do_fetch_place_details(String.t() | nil, Keyword.t()) :: String.t() | no_return
|
||||
@spec do_fetch_place_details(String.t() | nil, Keyword.t()) :: String.t() | nil
|
||||
defp do_fetch_place_details(place_id, options) do
|
||||
url = build_url(:place_details, %{place_id: place_id}, options)
|
||||
|
||||
Logger.debug("Asking Google Maps for details with #{url}")
|
||||
|
||||
with {:ok, %{status: 200, body: body}} <- GeospatialClient.get(url),
|
||||
%{"result" => %{"name" => name}, "status" => "OK"} <- body do
|
||||
name
|
||||
else
|
||||
%Tesla.Env{status: 200, body: body} = GeospatialClient.get!(url)
|
||||
|
||||
case body do
|
||||
%{"result" => %{"name" => name}, "status" => "OK"} ->
|
||||
name
|
||||
|
||||
%{"status" => "REQUEST_DENIED", "error_message" => error_message} ->
|
||||
raise ArgumentError, message: to_string(error_message)
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ defmodule Mobilizon.Service.Geospatial.Provider do
|
||||
%Address{}
|
||||
"""
|
||||
@callback geocode(longitude :: number, latitude :: number, options :: keyword) ::
|
||||
[Address.t()] | {:error, atom()}
|
||||
[Address.t()]
|
||||
|
||||
@doc """
|
||||
Search for an address
|
||||
@@ -63,23 +63,21 @@ defmodule Mobilizon.Service.Geospatial.Provider do
|
||||
iex> search("10 rue Jangot")
|
||||
%Address{}
|
||||
"""
|
||||
@callback search(address :: String.t(), options :: keyword) :: [Address.t()] | {:error, atom()}
|
||||
@callback search(address :: String.t(), options :: keyword) :: [Address.t()]
|
||||
|
||||
@doc """
|
||||
Returns a `Geo.Point` for given coordinates
|
||||
"""
|
||||
@spec coordinates([number | String.t()], number) :: Geo.Point.t() | nil
|
||||
def coordinates(coords, srid \\ 4326)
|
||||
|
||||
def coordinates([x, y], srid) when is_number(x) and is_number(y) do
|
||||
%Geo.Point{coordinates: {x, y}, srid: srid}
|
||||
@spec coordinates([number | String.t()]) :: Geo.Point.t() | nil
|
||||
def coordinates([x, y]) when is_number(x) and is_number(y) do
|
||||
%Geo.Point{coordinates: {x, y}, srid: 4326}
|
||||
end
|
||||
|
||||
def coordinates([x, y], srid) when is_binary(x) and is_binary(y) do
|
||||
%Geo.Point{coordinates: {String.to_float(x), String.to_float(y)}, srid: srid}
|
||||
def coordinates([x, y]) when is_binary(x) and is_binary(y) do
|
||||
%Geo.Point{coordinates: {String.to_float(x), String.to_float(y)}, srid: 4326}
|
||||
end
|
||||
|
||||
def coordinates(_, _), do: nil
|
||||
def coordinates(_), do: nil
|
||||
|
||||
@spec endpoint(atom()) :: String.t()
|
||||
def endpoint(provider) do
|
||||
|
||||
Reference in New Issue
Block a user