Various typespec and compilation improvements
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
@@ -1466,7 +1466,7 @@ defmodule Mobilizon.Actors do
|
||||
|
||||
@spec actors_for_location(Ecto.Query.t(), String.t(), integer()) :: Ecto.Query.t()
|
||||
defp actors_for_location(query, location, radius)
|
||||
when is_valid_string?(location) and not is_nil(radius) do
|
||||
when is_valid_string(location) and not is_nil(radius) do
|
||||
with {lon, lat} <- Geohax.decode(location),
|
||||
point <- Geo.WKT.decode!("SRID=4326;POINT(#{lon} #{lat})") do
|
||||
query
|
||||
|
||||
@@ -4,7 +4,6 @@ defmodule Mobilizon.Config do
|
||||
"""
|
||||
|
||||
alias Mobilizon.Actors
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Service.GitStatus
|
||||
|
||||
@spec instance_config :: keyword
|
||||
@@ -317,14 +316,14 @@ defmodule Mobilizon.Config do
|
||||
|
||||
@spec create_cache(atom()) :: integer()
|
||||
defp create_cache(:anonymous_actor_id) do
|
||||
with {:ok, %Actor{id: actor_id}} <- Actors.get_or_create_internal_actor("anonymous") do
|
||||
with {:ok, %{id: actor_id}} <- Actors.get_or_create_internal_actor("anonymous") do
|
||||
actor_id
|
||||
end
|
||||
end
|
||||
|
||||
@spec create_cache(atom()) :: integer()
|
||||
defp create_cache(:relay_actor_id) do
|
||||
with {:ok, %Actor{id: actor_id}} <- Actors.get_or_create_internal_actor("relay") do
|
||||
with {:ok, %{id: actor_id}} <- Actors.get_or_create_internal_actor("relay") do
|
||||
actor_id
|
||||
end
|
||||
end
|
||||
|
||||
@@ -249,8 +249,7 @@ defmodule Mobilizon.Discussions do
|
||||
{:ok, comment}
|
||||
end
|
||||
else
|
||||
comment
|
||||
|> Repo.delete()
|
||||
Repo.delete(comment)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -206,6 +206,7 @@ defmodule Mobilizon.Events.Event do
|
||||
|
||||
defp put_tags(%Changeset{} = changeset, _), do: changeset
|
||||
|
||||
@spec process_tag(map() | Tag.t()) :: Tag.t() | Ecto.Changeset.t()
|
||||
# We need a changeset instead of a raw struct because of slug which is generated in changeset
|
||||
defp process_tag(%{id: id} = _tag) do
|
||||
Events.get_tag(id)
|
||||
@@ -248,13 +249,8 @@ defmodule Mobilizon.Events.Event do
|
||||
# In case the provided picture is an existing one
|
||||
@spec put_picture(Changeset.t(), map) :: Changeset.t()
|
||||
defp put_picture(%Changeset{} = changeset, %{picture: %{media_id: id} = _picture}) do
|
||||
case Medias.get_media!(id) do
|
||||
%Media{} = picture ->
|
||||
put_assoc(changeset, :picture, picture)
|
||||
|
||||
_ ->
|
||||
changeset
|
||||
end
|
||||
%Media{} = picture = Medias.get_media!(id)
|
||||
put_assoc(changeset, :picture, picture)
|
||||
end
|
||||
|
||||
# In case it's a new picture
|
||||
|
||||
@@ -631,9 +631,10 @@ defmodule Mobilizon.Events do
|
||||
@doc """
|
||||
Returns the list of tags.
|
||||
"""
|
||||
@spec list_tags(integer | nil, integer | nil) :: [Tag.t()]
|
||||
def list_tags(page \\ nil, limit \\ nil) do
|
||||
@spec list_tags(String.t() | nil, integer | nil, integer | nil) :: [Tag.t()]
|
||||
def list_tags(filter \\ nil, page \\ nil, limit \\ nil) do
|
||||
Tag
|
||||
|> tag_filter(filter)
|
||||
|> Page.paginate(page, limit)
|
||||
|> Repo.all()
|
||||
end
|
||||
@@ -1396,7 +1397,7 @@ defmodule Mobilizon.Events do
|
||||
end
|
||||
|
||||
@spec events_for_tags(Ecto.Query.t(), map()) :: Ecto.Query.t()
|
||||
defp events_for_tags(query, %{tags: tags}) when is_valid_string?(tags) do
|
||||
defp events_for_tags(query, %{tags: tags}) when is_valid_string(tags) do
|
||||
query
|
||||
|> join(:inner, [q], te in "events_tags", on: q.id == te.event_id)
|
||||
|> join(:inner, [q, ..., te], t in Tag, on: te.tag_id == t.id)
|
||||
@@ -1410,7 +1411,7 @@ defmodule Mobilizon.Events do
|
||||
do: query
|
||||
|
||||
defp events_for_location(query, %{location: location, radius: radius})
|
||||
when is_valid_string?(location) and not is_nil(radius) do
|
||||
when is_valid_string(location) and not is_nil(radius) do
|
||||
with {lon, lat} <- Geohax.decode(location),
|
||||
point <- Geo.WKT.decode!("SRID=4326;POINT(#{lon} #{lat})") do
|
||||
query
|
||||
@@ -1471,6 +1472,16 @@ defmodule Mobilizon.Events do
|
||||
from(t in Tag, where: t.title == ^title, limit: 1)
|
||||
end
|
||||
|
||||
@spec tag_filter(Ecto.Query.t(), String.t() | nil) :: Ecto.Query.t()
|
||||
defp tag_filter(query, nil), do: query
|
||||
defp tag_filter(query, ""), do: query
|
||||
|
||||
defp tag_filter(query, filter) when is_binary(filter) do
|
||||
query
|
||||
|> where([q], ilike(q.slug, ^"%#{filter}%"))
|
||||
|> or_where([q], ilike(q.title, ^"%#{filter}%"))
|
||||
end
|
||||
|
||||
@spec tags_for_event_query(integer) :: Ecto.Query.t()
|
||||
defp tags_for_event_query(event_id) do
|
||||
from(
|
||||
|
||||
@@ -10,7 +10,7 @@ defmodule Mobilizon.Events.Participant do
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Events
|
||||
alias Mobilizon.Events.{Event, ParticipantRole}
|
||||
alias Mobilizon.Web.Email.Checker
|
||||
alias Mobilizon.Events.Participant.Metadata
|
||||
|
||||
alias Mobilizon.Web.Endpoint
|
||||
|
||||
@@ -24,7 +24,6 @@ defmodule Mobilizon.Events.Participant do
|
||||
|
||||
@required_attrs [:url, :role, :event_id, :actor_id]
|
||||
@attrs @required_attrs
|
||||
@metadata_attrs [:email, :confirmation_token, :cancellation_token, :message, :locale]
|
||||
|
||||
@timestamps_opts [type: :utc_datetime]
|
||||
|
||||
@@ -33,13 +32,7 @@ defmodule Mobilizon.Events.Participant do
|
||||
field(:role, ParticipantRole, default: :participant)
|
||||
field(:url, :string)
|
||||
|
||||
embeds_one :metadata, Metadata, on_replace: :delete do
|
||||
field(:email, :string)
|
||||
field(:confirmation_token, :string)
|
||||
field(:cancellation_token, :string)
|
||||
field(:message, :string)
|
||||
field(:locale, :string)
|
||||
end
|
||||
embeds_one(:metadata, Metadata, on_replace: :delete)
|
||||
|
||||
belongs_to(:event, Event, primary_key: true)
|
||||
belongs_to(:actor, Actor, primary_key: true)
|
||||
@@ -68,18 +61,12 @@ defmodule Mobilizon.Events.Participant do
|
||||
def changeset(%__MODULE__{} = participant, attrs) do
|
||||
participant
|
||||
|> cast(attrs, @attrs)
|
||||
|> cast_embed(:metadata, with: &metadata_changeset/2)
|
||||
|> cast_embed(:metadata)
|
||||
|> ensure_url()
|
||||
|> validate_required(@required_attrs)
|
||||
|> unique_constraint(:actor_id, name: :participants_event_id_actor_id_index)
|
||||
end
|
||||
|
||||
defp metadata_changeset(schema, params) do
|
||||
schema
|
||||
|> cast(params, @metadata_attrs)
|
||||
|> Checker.validate_changeset()
|
||||
end
|
||||
|
||||
# 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: %__MODULE__{url: nil}} = changeset) do
|
||||
|
||||
36
lib/mobilizon/events/participant_metadata.ex
Normal file
36
lib/mobilizon/events/participant_metadata.ex
Normal file
@@ -0,0 +1,36 @@
|
||||
defmodule Mobilizon.Events.Participant.Metadata do
|
||||
@moduledoc """
|
||||
Participation stats on event
|
||||
"""
|
||||
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
alias Mobilizon.Web.Email.Checker
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
email: String.t(),
|
||||
confirmation_token: String.t(),
|
||||
cancellation_token: String.t(),
|
||||
message: String.t(),
|
||||
locale: String.t()
|
||||
}
|
||||
|
||||
@attrs [:email, :confirmation_token, :cancellation_token, :message, :locale]
|
||||
|
||||
@derive Jason.Encoder
|
||||
embedded_schema do
|
||||
field(:email, :string)
|
||||
field(:confirmation_token, :string)
|
||||
field(:cancellation_token, :string)
|
||||
field(:message, :string)
|
||||
field(:locale, :string)
|
||||
end
|
||||
|
||||
@doc false
|
||||
@spec changeset(t, map) :: Ecto.Changeset.t()
|
||||
def changeset(schema, params) do
|
||||
schema
|
||||
|> cast(params, @attrs)
|
||||
|> Checker.validate_changeset()
|
||||
end
|
||||
end
|
||||
@@ -51,7 +51,7 @@ defmodule Mobilizon.Medias.Media do
|
||||
end
|
||||
|
||||
@doc false
|
||||
@spec changeset(struct(), map) :: Ecto.Changeset.t()
|
||||
@spec metadata_changeset(Metadata.t(), map) :: Ecto.Changeset.t()
|
||||
def metadata_changeset(metadata, attrs) do
|
||||
metadata
|
||||
|> cast(attrs, @metadata_attrs)
|
||||
|
||||
Reference in New Issue
Block a user