Clean specs

This commit is contained in:
miffigriffy 2019-09-13 01:55:45 +02:00
parent 56efb0acb1
commit 5967b30bb1
21 changed files with 86 additions and 90 deletions

View File

@ -8,7 +8,7 @@ defmodule Mobilizon.Actors.Actor do
import Ecto.Changeset
alias Mobilizon.{Actors, Config, Crypto}
alias Mobilizon.Actors.{Actor, ActorOpenness, ActorType, ActorVisibility, Follower, Member}
alias Mobilizon.Actors.{ActorOpenness, ActorType, ActorVisibility, Follower, Member}
alias Mobilizon.Events.{Event, FeedToken}
alias Mobilizon.Media.File
alias Mobilizon.Reports.{Report, Note}
@ -46,7 +46,7 @@ defmodule Mobilizon.Actors.Actor do
created_reports: [Report.t()],
subject_reports: [Report.t()],
report_notes: [Note.t()],
memberships: [Actor.t()]
memberships: [t]
}
@required_attrs [:preferred_username, :keys, :suspended, :url]
@ -139,7 +139,7 @@ defmodule Mobilizon.Actors.Actor do
has_many(:created_reports, Report, foreign_key: :reporter_id)
has_many(:subject_reports, Report, foreign_key: :reported_id)
has_many(:report_notes, Note, foreign_key: :moderator_id)
many_to_many(:memberships, Actor, join_through: Member)
many_to_many(:memberships, __MODULE__, join_through: Member)
timestamps()
end
@ -147,8 +147,8 @@ defmodule Mobilizon.Actors.Actor do
@doc """
Checks whether actor visibility is public.
"""
@spec is_public_visibility(Actor.t()) :: boolean
def is_public_visibility(%Actor{visibility: visibility}) do
@spec is_public_visibility(t) :: boolean
def is_public_visibility(%__MODULE__{visibility: visibility}) do
visibility in [:public, :unlisted]
end
@ -156,22 +156,22 @@ defmodule Mobilizon.Actors.Actor do
Returns the display name if available, or the preferred username
(with the eventual @domain suffix if it's a distant actor).
"""
@spec display_name(Actor.t()) :: String.t()
def display_name(%Actor{name: name} = actor) when name in [nil, ""] do
@spec display_name(t) :: String.t()
def display_name(%__MODULE__{name: name} = actor) when name in [nil, ""] do
preferred_username_and_domain(actor)
end
def display_name(%Actor{name: name}), do: name
def display_name(%__MODULE__{name: name}), do: name
@doc """
Returns display name and username.
"""
@spec display_name_and_username(Actor.t()) :: String.t()
def display_name_and_username(%Actor{name: name} = actor) when name in [nil, ""] do
@spec display_name_and_username(t) :: String.t()
def display_name_and_username(%__MODULE__{name: name} = actor) when name in [nil, ""] do
preferred_username_and_domain(actor)
end
def display_name_and_username(%Actor{name: name} = actor) do
def display_name_and_username(%__MODULE__{name: name} = actor) do
"#{name} (#{preferred_username_and_domain(actor)})"
end
@ -179,18 +179,18 @@ defmodule Mobilizon.Actors.Actor do
Returns the preferred username with the eventual @domain suffix if it's
a distant actor.
"""
@spec preferred_username_and_domain(Actor.t()) :: String.t()
def preferred_username_and_domain(%Actor{preferred_username: preferred_username, domain: nil}) do
@spec preferred_username_and_domain(t) :: String.t()
def preferred_username_and_domain(%__MODULE__{preferred_username: preferred_username, domain: nil}) do
preferred_username
end
def preferred_username_and_domain(%Actor{preferred_username: preferred_username, domain: domain}) do
def preferred_username_and_domain(%__MODULE__{preferred_username: preferred_username, domain: domain}) do
"#{preferred_username}@#{domain}"
end
@doc false
@spec changeset(t | Ecto.Changeset.t(), map) :: Ecto.Changeset.t()
def changeset(%Actor{} = actor, attrs) do
@spec changeset(t, map) :: Ecto.Changeset.t()
def changeset(%__MODULE__{} = actor, attrs) do
actor
|> cast(attrs, @attrs)
|> build_urls()
@ -205,8 +205,8 @@ defmodule Mobilizon.Actors.Actor do
end
@doc false
@spec update_changeset(t | Ecto.Changeset.t(), map) :: Ecto.Changeset.t()
def update_changeset(%Actor{} = actor, attrs) do
@spec update_changeset(t, map) :: Ecto.Changeset.t()
def update_changeset(%__MODULE__{} = actor, attrs) do
actor
|> cast(attrs, @update_attrs)
|> cast_embed(:avatar)
@ -221,8 +221,8 @@ defmodule Mobilizon.Actors.Actor do
@doc """
Changeset for person registration.
"""
@spec registration_changeset(t | Ecto.Changeset.t(), map) :: Ecto.Changeset.t()
def registration_changeset(%Actor{} = actor, attrs) do
@spec registration_changeset(t, map) :: Ecto.Changeset.t()
def registration_changeset(%__MODULE__{} = actor, attrs) do
actor
|> cast(attrs, @registration_attrs)
|> build_urls()
@ -242,7 +242,7 @@ defmodule Mobilizon.Actors.Actor do
@spec remote_actor_creation_changeset(map) :: Ecto.Changeset.t()
def remote_actor_creation_changeset(attrs) do
changeset =
%Actor{}
%__MODULE__{}
|> cast(attrs, @remote_actor_creation_attrs)
|> validate_required(@remote_actor_creation_required_attrs)
|> cast_embed(:avatar)
@ -267,14 +267,14 @@ defmodule Mobilizon.Actors.Actor do
def relay_creation_changeset(attrs) do
relay_creation_attrs = build_relay_creation_attrs(attrs)
cast(%Actor{}, relay_creation_attrs, @relay_creation_attrs)
cast(%__MODULE__{}, relay_creation_attrs, @relay_creation_attrs)
end
@doc """
Changeset for group creation
"""
@spec group_creation(struct(), map()) :: Ecto.Changeset.t()
def group_creation(%Actor{} = actor, params) do
@spec group_creation_changeset(t, map) :: Ecto.Changeset.t()
def group_creation_changeset(%__MODULE__{} = actor, params) do
actor
|> cast(params, @group_creation_attrs)
|> cast_embed(:avatar)
@ -299,7 +299,7 @@ defmodule Mobilizon.Actors.Actor do
%Ecto.Changeset{changes: %{preferred_username: username} = changes} = changeset
) do
with nil <- Map.get(changes, :domain, nil),
%Actor{preferred_username: _} <- Actors.get_local_actor_by_name(username) do
%__MODULE__{preferred_username: _} <- Actors.get_local_actor_by_name(username) do
add_error(changeset, :preferred_username, "Username is already taken")
else
_ -> changeset
@ -349,8 +349,8 @@ defmodule Mobilizon.Actors.Actor do
Clear multiple caches for an actor
"""
# TODO: move to MobilizonWeb
@spec clear_cache(struct()) :: {:ok, true}
def clear_cache(%Actor{preferred_username: preferred_username, domain: nil}) do
@spec clear_cache(t) :: {:ok, true}
def clear_cache(%__MODULE__{preferred_username: preferred_username, domain: nil}) do
Cachex.del(:activity_pub, "actor_" <> preferred_username)
Cachex.del(:feed, "actor_" <> preferred_username)
Cachex.del(:ics, "actor_" <> preferred_username)

View File

@ -340,7 +340,7 @@ defmodule Mobilizon.Actors do
@spec create_group(map) :: {:ok, Actor.t()} | {:error, Ecto.Changeset.t()}
def create_group(attrs \\ %{}) do
%Actor{}
|> Actor.group_creation(attrs)
|> Actor.group_creation_changeset(attrs)
|> Repo.insert()
end

View File

@ -32,8 +32,8 @@ defmodule Mobilizon.Actors.Bot do
end
@doc false
@spec changeset(t | Ecto.Changeset.t(), map) :: Ecto.Changeset.t()
def changeset(bot, attrs) do
@spec changeset(t, map) :: Ecto.Changeset.t()
def changeset(%__MODULE__{} = bot, attrs) do
bot
|> cast(attrs, @attrs)
|> validate_required(@required_attrs)

View File

@ -29,7 +29,7 @@ defmodule Mobilizon.Actors.Follower do
end
@doc false
@spec changeset(t | Ecto.Changeset.t(), map) :: Ecto.Changeset.t()
@spec changeset(t, map) :: Ecto.Changeset.t()
def changeset(follower, attrs) do
follower
|> cast(attrs, @attrs)

View File

@ -7,7 +7,7 @@ defmodule Mobilizon.Actors.Member do
import Ecto.Changeset
alias Mobilizon.Actors.{Actor, Member, MemberRole}
alias Mobilizon.Actors.{Actor, MemberRole}
@type t :: %__MODULE__{
role: MemberRole.t(),
@ -44,13 +44,13 @@ defmodule Mobilizon.Actors.Member do
@doc """
Checks whether the member is an administrator (admin or creator) of the group.
"""
def is_administrator(%Member{role: :administrator}), do: {:is_admin, true}
def is_administrator(%Member{role: :creator}), do: {:is_admin, true}
def is_administrator(%Member{}), do: {:is_admin, false}
def is_administrator(%__MODULE__{role: :administrator}), do: {:is_admin, true}
def is_administrator(%__MODULE__{role: :creator}), do: {:is_admin, true}
def is_administrator(%__MODULE__{}), do: {:is_admin, false}
@doc false
@spec changeset(t | Ecto.Changeset.t(), map) :: Ecto.Changeset.t()
def changeset(member, attrs) do
@spec changeset(t, map) :: Ecto.Changeset.t()
def changeset(%__MODULE__{} = member, attrs) do
member
|> cast(attrs, @attrs)
|> validate_required(@required_attrs)

View File

@ -7,7 +7,6 @@ defmodule Mobilizon.Addresses.Address do
import Ecto.Changeset
alias Mobilizon.Addresses.Address
alias Mobilizon.Events.Event
@type t :: %__MODULE__{
@ -56,8 +55,8 @@ defmodule Mobilizon.Addresses.Address do
end
@doc false
@spec changeset(t | Ecto.Changeset.t(), map) :: Ecto.Changeset.t()
def changeset(%Address{} = address, attrs) do
@spec changeset(t, map) :: Ecto.Changeset.t()
def changeset(%__MODULE__{} = address, attrs) do
address
|> cast(attrs, @attrs)
|> set_url()

View File

@ -33,8 +33,8 @@ defmodule Mobilizon.Admin.ActionLog do
end
@doc false
@spec changeset(t | Ecto.Changeset.t(), map) :: Ecto.Changeset.t()
def changeset(action_log, attrs) do
@spec changeset(t, map) :: Ecto.Changeset.t()
def changeset(%__MODULE__{} = action_log, attrs) do
action_log
|> cast(attrs, @attrs)
|> validate_required(@required_attrs)

View File

@ -48,13 +48,13 @@ defmodule Mobilizon.Events.Comment do
Returns the id of the first comment in the conversation.
"""
@spec get_thread_id(t) :: integer
def get_thread_id(%Comment{id: id, origin_comment_id: origin_comment_id}) do
def get_thread_id(%__MODULE__{id: id, origin_comment_id: origin_comment_id}) do
origin_comment_id || id
end
@doc false
@spec changeset(t, map) :: Ecto.Changeset.t()
def changeset(%Comment{} = comment, attrs) do
def changeset(%__MODULE__{} = comment, attrs) do
uuid = attrs["uuid"] || Ecto.UUID.generate()
url = attrs["url"] || generate_url(uuid)

View File

@ -11,7 +11,6 @@ defmodule Mobilizon.Events.Event do
alias Mobilizon.Addresses.Address
alias Mobilizon.Events.{
Event,
EventOptions,
EventStatus,
EventVisibility,
@ -115,7 +114,7 @@ defmodule Mobilizon.Events.Event do
@doc false
@spec changeset(t, map) :: Ecto.Changeset.t()
def changeset(%Event{} = event, attrs) do
def changeset(%__MODULE__{} = event, attrs) do
event
|> cast(attrs, @attrs)
|> cast_embed(:options)
@ -124,7 +123,7 @@ defmodule Mobilizon.Events.Event do
@doc false
@spec update_changeset(t, map) :: Ecto.Changeset.t()
def update_changeset(%Event{} = event, attrs) do
def update_changeset(%__MODULE__{} = event, attrs) do
event
|> Ecto.Changeset.cast(attrs, @update_attrs)
|> cast_embed(:options)
@ -136,7 +135,7 @@ defmodule Mobilizon.Events.Event do
Checks whether an event can be managed.
"""
@spec can_be_managed_by(t, integer | String.t()) :: boolean
def can_be_managed_by(%Event{organizer_actor_id: organizer_actor_id}, actor_id)
def can_be_managed_by(%__MODULE__{organizer_actor_id: organizer_actor_id}, actor_id)
when organizer_actor_id == actor_id do
{:event_can_be_managed, true}
end

View File

@ -9,7 +9,6 @@ defmodule Mobilizon.Events.EventOptions do
alias Mobilizon.Events.{
EventOffer,
EventOptions,
EventParticipationCondition,
CommentModeration
}
@ -52,7 +51,7 @@ defmodule Mobilizon.Events.EventOptions do
@doc false
@spec changeset(t, map) :: Ecto.Changeset.t()
def changeset(%EventOptions{} = event_options, attrs) do
def changeset(%__MODULE__{} = event_options, attrs) do
cast(event_options, attrs, @attrs)
end
end

View File

@ -8,7 +8,6 @@ defmodule Mobilizon.Events.FeedToken do
import Ecto.Changeset
alias Mobilizon.Actors.Actor
alias Mobilizon.Events.FeedToken
alias Mobilizon.Users.User
@type t :: %__MODULE__{
@ -33,7 +32,7 @@ defmodule Mobilizon.Events.FeedToken do
@doc false
@spec changeset(t, map) :: Ecto.Changeset.t()
def changeset(%FeedToken{} = feed_token, attrs) do
def changeset(%__MODULE__{} = feed_token, attrs) do
feed_token
|> cast(attrs, @attrs)
|> validate_required(@required_attrs)

View File

@ -10,7 +10,7 @@ defmodule Mobilizon.Events.Participant do
alias Mobilizon.Actors.Actor
alias Mobilizon.Config
alias Mobilizon.Events
alias Mobilizon.Events.{Event, Participant, ParticipantRole}
alias Mobilizon.Events.{Event, ParticipantRole}
@type t :: %__MODULE__{
role: ParticipantRole.t(),
@ -41,7 +41,7 @@ defmodule Mobilizon.Events.Participant do
@spec is_not_only_organizer(integer | String.t(), integer | String.t()) :: boolean
def is_not_only_organizer(event_id, actor_id) do
case Events.list_organizers_participants_for_event(event_id) do
[%Participant{actor: %Actor{id: participant_actor_id}}] ->
[%__MODULE__{actor: %Actor{id: participant_actor_id}}] ->
participant_actor_id == actor_id
_ ->
@ -51,7 +51,7 @@ defmodule Mobilizon.Events.Participant do
@doc false
@spec changeset(t, map) :: Ecto.Changeset.t()
def changeset(%Participant{} = participant, attrs) do
def changeset(%__MODULE__{} = participant, attrs) do
participant
|> cast(attrs, @attrs)
|> ensure_url()
@ -60,7 +60,7 @@ defmodule Mobilizon.Events.Participant do
# If there's a blank URL that's because we're doing the first insert
@spec ensure_url(Ecto.Changeset.t()) :: Ecto.Changeset.t()
defp ensure_url(%Ecto.Changeset{data: %Participant{url: nil}} = changeset) do
defp ensure_url(%Ecto.Changeset{data: %__MODULE__{url: nil}} = changeset) do
case fetch_change(changeset, :url) do
{:ok, _url} ->
changeset

View File

@ -7,7 +7,7 @@ defmodule Mobilizon.Events.Session do
import Ecto.Changeset
alias Mobilizon.Events.{Event, Session, Track}
alias Mobilizon.Events.{Event, Track}
@type t :: %__MODULE__{
audios_urls: String.t(),
@ -57,7 +57,7 @@ defmodule Mobilizon.Events.Session do
@doc false
@spec changeset(t, map) :: Ecto.Changeset.t()
def changeset(%Session{} = session, attrs) do
def changeset(%__MODULE__{} = session, attrs) do
session
|> cast(attrs, @attrs)
|> validate_required(@required_attrs)

View File

@ -7,13 +7,13 @@ defmodule Mobilizon.Events.Tag do
import Ecto.Changeset
alias Mobilizon.Events.{Tag, TagRelation}
alias Mobilizon.Events.TagRelation
alias Mobilizon.Events.Tag.TitleSlug
@type t :: %__MODULE__{
title: String.t(),
slug: TitleSlug.Type.t(),
related_tags: [Tag.t()]
related_tags: [t]
}
@required_attrs [:title, :slug]
@ -23,14 +23,14 @@ defmodule Mobilizon.Events.Tag do
field(:title, :string)
field(:slug, TitleSlug.Type)
many_to_many(:related_tags, Tag, join_through: TagRelation)
many_to_many(:related_tags, __MODULE__, join_through: TagRelation)
timestamps()
end
@doc false
@spec changeset(t, map) :: Ecto.Changeset.t()
def changeset(%Tag{} = tag, attrs) do
def changeset(%__MODULE__{} = tag, attrs) do
tag
|> cast(attrs, @attrs)
|> TitleSlug.maybe_generate_slug()

View File

@ -7,7 +7,7 @@ defmodule Mobilizon.Events.TagRelation do
import Ecto.Changeset
alias Mobilizon.Events.{Tag, TagRelation}
alias Mobilizon.Events.Tag
@type t :: %__MODULE__{
weight: integer,
@ -29,7 +29,7 @@ defmodule Mobilizon.Events.TagRelation do
@doc false
@spec changeset(t, map) :: Ecto.Changeset.t()
def changeset(%TagRelation{} = tag, attrs) do
def changeset(%__MODULE__{} = tag, attrs) do
# Return if tag_id or link_id are not set because it will fail later otherwise
with %Ecto.Changeset{errors: [], changes: changes} = changeset <-
tag

View File

@ -7,7 +7,7 @@ defmodule Mobilizon.Events.Track do
import Ecto.Changeset
alias Mobilizon.Events.{Track, Event, Session}
alias Mobilizon.Events.{Event, Session}
@type t :: %__MODULE__{
color: String.t(),
@ -34,7 +34,7 @@ defmodule Mobilizon.Events.Track do
@doc false
@spec changeset(t, map) :: Ecto.Changeset.t()
def changeset(%Track{} = track, attrs) do
def changeset(%__MODULE__{} = track, attrs) do
track
|> cast(attrs, @attrs)
|> validate_required(@required_attrs)

View File

@ -28,8 +28,8 @@ defmodule Mobilizon.Media.File do
end
@doc false
@spec changeset(t | Ecto.Changeset.t(), map) :: Ecto.Changeset.t()
def changeset(file, attrs) do
@spec changeset(t, map) :: Ecto.Changeset.t()
def changeset(%__MODULE__{} = file, attrs) do
file
|> cast(attrs, @attrs)
|> validate_required(@required_attrs)

View File

@ -23,8 +23,8 @@ defmodule Mobilizon.Media.Picture do
end
@doc false
@spec changeset(t | Ecto.Changeset.t(), map) :: Ecto.Changeset.t()
def changeset(picture, attrs) do
@spec changeset(t, map) :: Ecto.Changeset.t()
def changeset(%__MODULE__{} = picture, attrs) do
picture
|> cast(attrs, [:actor_id])
|> cast_embed(:file)

View File

@ -30,8 +30,8 @@ defmodule Mobilizon.Reports.Note do
end
@doc false
@spec changeset(t | Ecto.Changeset.t(), map) :: Ecto.Changeset.t()
def changeset(note, attrs) do
@spec changeset(t, map) :: Ecto.Changeset.t()
def changeset(%__MODULE__{} = note, attrs) do
note
|> cast(attrs, @attrs)
|> validate_required(@required_attrs)

View File

@ -50,16 +50,16 @@ defmodule Mobilizon.Reports.Report do
end
@doc false
@spec changeset(t | Ecto.Changeset.t(), map) :: Ecto.Changeset.t()
def changeset(report, attrs) do
@spec changeset(t, map) :: Ecto.Changeset.t()
def changeset(%__MODULE__{} = report, attrs) do
report
|> cast(attrs, @attrs)
|> validate_required(@required_attrs)
end
@doc false
@spec creation_changeset(Report.t(), map) :: Ecto.Changeset.t()
def creation_changeset(report, attrs) do
@spec creation_changeset(t, map) :: Ecto.Changeset.t()
def creation_changeset(%__MODULE__{} = report, attrs) do
report
|> changeset(attrs)
|> put_assoc(:comments, attrs["comments"])

View File

@ -11,7 +11,7 @@ defmodule Mobilizon.Users.User do
alias Mobilizon.Crypto
alias Mobilizon.Events.FeedToken
alias Mobilizon.Service.EmailChecker
alias Mobilizon.Users.{User, UserRole}
alias Mobilizon.Users.UserRole
@type t :: %__MODULE__{
email: String.t(),
@ -66,8 +66,8 @@ defmodule Mobilizon.Users.User do
end
@doc false
@spec changeset(t | Ecto.Changeset.t(), map) :: Ecto.Changeset.t()
def changeset(%User{} = user, attrs) do
@spec changeset(t, map) :: Ecto.Changeset.t()
def changeset(%__MODULE__{} = user, attrs) do
changeset =
user
|> cast(attrs, @attrs)
@ -84,8 +84,8 @@ defmodule Mobilizon.Users.User do
end
@doc false
@spec registration_changeset(User.t(), map) :: Ecto.Changeset.t()
def registration_changeset(%User{} = user, attrs) do
@spec registration_changeset(t, map) :: Ecto.Changeset.t()
def registration_changeset(%__MODULE__{} = user, attrs) do
user
|> changeset(attrs)
|> cast_assoc(:default_actor)
@ -99,14 +99,14 @@ defmodule Mobilizon.Users.User do
end
@doc false
@spec send_password_reset_changeset(User.t(), map) :: Ecto.Changeset.t()
def send_password_reset_changeset(%User{} = user, attrs) do
@spec send_password_reset_changeset(t, map) :: Ecto.Changeset.t()
def send_password_reset_changeset(%__MODULE__{} = user, attrs) do
cast(user, attrs, [:reset_password_token, :reset_password_sent_at])
end
@doc false
@spec password_reset_changeset(User.t(), map) :: Ecto.Changeset.t()
def password_reset_changeset(%User{} = user, attrs) do
@spec password_reset_changeset(t, map) :: Ecto.Changeset.t()
def password_reset_changeset(%__MODULE__{} = user, attrs) do
user
|> cast(attrs, @password_reset_required_attrs)
|> validate_length(:password,
@ -120,15 +120,15 @@ defmodule Mobilizon.Users.User do
@doc """
Checks whether an user is confirmed.
"""
@spec is_confirmed(User.t()) :: boolean
def is_confirmed(%User{confirmed_at: nil}), do: false
def is_confirmed(%User{}), do: true
@spec is_confirmed(t) :: boolean
def is_confirmed(%__MODULE__{confirmed_at: nil}), do: false
def is_confirmed(%__MODULE__{}), do: true
@doc """
Returns whether an user owns an actor.
"""
@spec owns_actor(User.t(), integer | String.t()) :: {:is_owned, Actor.t() | nil}
def owns_actor(%User{actors: actors}, actor_id) do
@spec owns_actor(t, integer | String.t()) :: {:is_owned, Actor.t() | nil}
def owns_actor(%__MODULE__{actors: actors}, actor_id) do
user_actor = Enum.find(actors, fn actor -> "#{actor.id}" == "#{actor_id}" end)
{:is_owned, user_actor}