Typespec fixes and refactoring

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2022-05-03 12:23:09 +02:00
parent f6a17d8b3a
commit 999a33c7c3
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773
10 changed files with 28 additions and 30 deletions

View file

@ -191,8 +191,8 @@ defmodule Mobilizon.Federation.ActivityPub.Refresher do
defp process_collection(_, _), do: :error defp process_collection(_, _), do: :error
# If we're handling an activity # If we're handling an activity
@spec handling_element(map()) :: {:ok, any, struct} | :error @spec handling_element(map() | String.t()) ::
@spec handling_element(String.t()) :: {:ok, struct} | {:ok, atom, struct} | {:error, any()} {:ok, any, struct} | {:ok, struct} | {:ok, atom, struct} | {:error, any()} | :error
defp handling_element(%{"type" => activity_type} = data) defp handling_element(%{"type" => activity_type} = data)
when activity_type in ["Create", "Update", "Delete"] do when activity_type in ["Create", "Update", "Delete"] do
object = get_in(data, ["object"]) object = get_in(data, ["object"])

View file

@ -192,5 +192,5 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Actor do
Map.put(res, "location", AddressConverter.model_to_as(physical_address)) Map.put(res, "location", AddressConverter.model_to_as(physical_address))
end end
defp maybe_add_physical_address(res, %ActorModel{physical_address: _}), do: res defp maybe_add_physical_address(res, _), do: res
end end

View file

@ -48,12 +48,8 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Discussion do
@impl Converter @impl Converter
@spec as_to_model_data(map) :: map() | {:error, atom()} @spec as_to_model_data(map) :: map() | {:error, atom()}
def as_to_model_data(%{"type" => "Note", "name" => name} = object) when is_valid_string(name) do def as_to_model_data(%{"type" => "Note", "name" => name} = object) when is_valid_string(name) do
case extract_actors(object) do with %{actor_id: actor_id, creator_id: creator_id} <- extract_actors(object) do
%{actor_id: actor_id, creator_id: creator_id} -> %{actor_id: actor_id, creator_id: creator_id, title: name, url: object["id"]}
%{actor_id: actor_id, creator_id: creator_id, title: name, url: object["id"]}
{:error, error} ->
{:error, error}
end end
end end

View file

@ -46,22 +46,18 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Media do
actor_id actor_id
) )
when is_binary(media_url) do when is_binary(media_url) do
case upload_media(media_url, name) do with {:ok, %{url: url} = uploaded} <- upload_media(media_url, name) do
{:error, err} -> case Medias.get_media_by_url(url) do
{:error, err} %MediaModel{file: _file} = media ->
{:ok, media}
{:ok, %{url: url} = uploaded} -> nil ->
case Medias.get_media_by_url(url) do Medias.create_media(%{
%MediaModel{file: _file} = media -> file: Map.take(uploaded, [:url, :name, :content_type, :size]),
{:ok, media} metadata: Map.take(uploaded, [:width, :height, :blurhash]),
actor_id: actor_id
nil -> })
Medias.create_media(%{ end
file: Map.take(uploaded, [:url, :name, :content_type, :size]),
metadata: Map.take(uploaded, [:width, :height, :blurhash]),
actor_id: actor_id
})
end
end end
end end

View file

@ -848,7 +848,13 @@ defmodule Mobilizon.Actors do
@doc """ @doc """
Returns a paginated list of members for a group. Returns a paginated list of members for a group.
""" """
@spec list_members_for_group(Actor.t(), list(atom()), integer | nil, integer | nil) :: @spec list_members_for_group(
Actor.t(),
String.t() | nil,
list(atom()),
integer | nil,
integer | nil
) ::
Page.t(Member.t()) Page.t(Member.t())
def list_members_for_group( def list_members_for_group(
%Actor{id: group_id, type: :Group}, %Actor{id: group_id, type: :Group},

View file

@ -29,7 +29,7 @@ defmodule Mobilizon.Service.Activity.Renderer do
@type common_render :: %{body: String.t(), url: String.t()} @type common_render :: %{body: String.t(), url: String.t()}
@callback render(entity :: Activity.t(), Keyword.t()) :: common_render() @callback render(activity :: Activity.t(), Keyword.t()) :: common_render()
@spec render(Activity.t()) :: render() @spec render(Activity.t()) :: render()
def render(%Activity{} = activity, options \\ []) do def render(%Activity{} = activity, options \\ []) do

View file

@ -72,7 +72,7 @@ defmodule Mobilizon.Service.Geospatial.GoogleMaps do
end end
end end
@spec build_url(:search | :geocode, map(), list()) :: String.t() | no_return @spec build_url(:search | :geocode | :place_details, map(), list()) :: String.t() | no_return
defp build_url(method, args, options) do defp build_url(method, args, options) do
limit = Keyword.get(options, :limit, 10) limit = Keyword.get(options, :limit, 10)
lang = Keyword.get(options, :lang, "en") lang = Keyword.get(options, :lang, "en")

View file

@ -289,7 +289,7 @@ defmodule Mobilizon.Service.Notifications.Scheduler do
{:ok, nil} {:ok, nil}
# Sending to calculated time # Sending to calculated time
true -> match?(%DateTime{}, send_at) ->
Notification.enqueue(:pending_membership_notification, params, scheduled_at: send_at) Notification.enqueue(:pending_membership_notification, params, scheduled_at: send_at)
end end
end end

View file

@ -73,7 +73,7 @@ defmodule Mobilizon.Service.Notifier.Push do
Map.get(@default_behavior, activity_setting, false) Map.get(@default_behavior, activity_setting, false)
end end
@spec send_subscription(Activity.t(), any, Keyword.t()) :: no_return @spec send_subscription(Activity.t(), any, Keyword.t()) :: any()
defp send_subscription(activity, subscription, options) do defp send_subscription(activity, subscription, options) do
activity activity
|> payload(options) |> payload(options)

View file

@ -41,7 +41,7 @@ defmodule Mobilizon.Service.Workers.Helper do
alias Oban.Job alias Oban.Job
@spec enqueue(String.t() | :atom, map(), Keyword.t()) :: @spec enqueue(String.t() | atom(), map(), Keyword.t()) ::
{:ok, Job.t()} | {:error, Ecto.Changeset.t()} {:ok, Job.t()} | {:error, Ecto.Changeset.t()}
def enqueue(operation, params, worker_args \\ []) do def enqueue(operation, params, worker_args \\ []) do
params = Map.merge(%{"op" => operation}, params) params = Map.merge(%{"op" => operation}, params)