[WIP] Test transmogrifier

Introduce MobilizonWeb.API namespace

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Format

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

WIP

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

remove unneeded code

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Fix tests

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Fix warnings

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2018-12-14 17:41:55 +01:00
parent e3a8343112
commit c1e6612405
41 changed files with 2961 additions and 246 deletions

View File

@@ -167,6 +167,7 @@ defmodule Mobilizon.Actors.Actor do
])
|> build_urls(:Group)
|> put_change(:domain, nil)
|> put_change(:keys, Actors.create_keys())
|> put_change(:type, :Group)
|> validate_required([:url, :outbox_url, :inbox_url, :type, :preferred_username])
|> unique_constraint(:preferred_username, name: :actors_preferred_username_domain_type_index)
@@ -292,7 +293,7 @@ defmodule Mobilizon.Actors.Actor do
{:already_following, false} <- {:already_following, following?(follower, followed)} do
do_follow(follower, followed, approved)
else
{:already_following, _} ->
{:already_following, %Follower{}} ->
{:error,
"Could not follow actor: you are already following #{followed.preferred_username}"}
@@ -301,6 +302,17 @@ defmodule Mobilizon.Actors.Actor do
end
end
@spec unfollow(struct(), struct()) :: {:ok, Follower.t()} | {:error, Ecto.Changeset.t()}
def unfollow(%Actor{} = followed, %Actor{} = follower) do
with {:already_following, %Follower{} = follow} <-
{:already_following, following?(follower, followed)} do
Actors.delete_follower(follow)
else
{:already_following, false} ->
{:error, "Could not unfollow actor: you are not following #{followed.preferred_username}"}
end
end
defp do_follow(%Actor{} = follower, %Actor{} = followed, approved) do
Actors.create_follower(%{
"actor_id" => follower.id,
@@ -311,12 +323,13 @@ defmodule Mobilizon.Actors.Actor do
@spec following?(struct(), struct()) :: boolean()
def following?(
%Actor{id: follower_actor_id} = _follower_actor,
%Actor{followers: followers} = _followed
%Actor{} = follower_actor,
%Actor{} = followed_actor
) do
followers
|> Enum.map(& &1.actor_id)
|> Enum.member?(follower_actor_id)
case Actors.get_follower(followed_actor, follower_actor) do
nil -> false
%Follower{} = follow -> follow
end
end
@spec actor_acct_from_actor(struct()) :: String.t()

View File

@@ -162,16 +162,41 @@ defmodule Mobilizon.Actors do
)
end
def get_group_by_name(name) do
case String.split(name, "@") do
[name] ->
Repo.get_by(Actor, preferred_username: name, type: :Group)
@doc """
Get a group by it's title
"""
@spec get_group_by_title(String.t()) :: Actor.t() | nil
def get_group_by_title(title) do
case String.split(title, "@") do
[title] ->
get_local_group_by_title(title)
[name, domain] ->
Repo.get_by(Actor, preferred_username: name, domain: domain, type: :Group)
[title, domain] ->
Repo.one(
from(a in Actor,
where: a.preferred_username == ^title and a.type == "Group" and a.domain == ^domain
)
)
end
end
@doc """
Get a local group by it's title
"""
@spec get_local_group_by_title(String.t()) :: Actor.t() | nil
def get_local_group_by_title(title) do
title
|> do_get_local_group_by_title
|> Repo.one()
end
@spec do_get_local_group_by_title(String.t()) :: Ecto.Query.t()
defp do_get_local_group_by_title(title) do
from(a in Actor,
where: a.preferred_username == ^title and a.type == "Group" and is_nil(a.domain)
)
end
@doc """
Creates a group.
@@ -185,8 +210,6 @@ defmodule Mobilizon.Actors do
"""
def create_group(attrs \\ %{}) do
attrs = Map.put(attrs, :keys, create_keys())
%Actor{}
|> Actor.group_creation(attrs)
|> Repo.insert()
@@ -218,10 +241,11 @@ defmodule Mobilizon.Actors do
keys: data.keys,
avatar_url: data.avatar_url,
banner_url: data.banner_url,
name: data.name
name: data.name,
summary: data.summary
]
],
conflict_target: [:preferred_username, :domain, :type]
conflict_target: [:url]
)
if preload, do: {:ok, Repo.preload(actor, [:followers])}, else: {:ok, actor}
@@ -516,9 +540,11 @@ defmodule Mobilizon.Actors do
end
end
# Create a new RSA key
@doc """
Create a new RSA key
"""
@spec create_keys() :: String.t()
defp create_keys() do
def create_keys() do
key = :public_key.generate_key({:rsa, 2048, 65_537})
entry = :public_key.pem_entry_encode(:RSAPrivateKey, key)
[entry] |> :public_key.pem_encode() |> String.trim_trailing()
@@ -958,6 +984,13 @@ defmodule Mobilizon.Actors do
|> Repo.preload([:actor, :target_actor])
end
@spec get_follower(Actor.t(), Actor.t()) :: Follower.t()
def get_follower(%Actor{id: followed_id}, %Actor{id: follower_id}) do
Repo.one(
from(f in Follower, where: f.target_actor_id == ^followed_id and f.actor_id == ^follower_id)
)
end
@doc """
Creates a follower.
@@ -1013,6 +1046,24 @@ defmodule Mobilizon.Actors do
Repo.delete(follower)
end
@doc """
Delete a follower by followed and follower actors
## Examples
iex> delete_follower(%Actor{}, %Actor{})
{:ok, %Mobilizon.Actors.Follower{}}
iex> delete_follower(%Actor{}, %Actor{})
{:error, %Ecto.Changeset{}}
"""
@spec delete_follower(Actor.t(), Actor.t()) ::
{:ok, Follower.t()} | {:error, Ecto.Changeset.t()}
def delete_follower(%Actor{} = followed, %Actor{} = follower) do
get_follower(followed, follower) |> Repo.delete()
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking follower changes.

View File

@@ -21,4 +21,8 @@ defmodule Mobilizon.Actors.Follower do
|> validate_required([:score, :approved, :target_actor_id, :actor_id])
|> unique_constraint(:target_actor_id, name: :followers_actor_target_actor_unique_index)
end
def url(%Follower{id: id}) do
"#{MobilizonWeb.Endpoint.url()}/follow/#{id}/activity"
end
end

View File

@@ -116,7 +116,7 @@ defmodule Mobilizon.Addresses do
rescue
e in ArgumentError ->
Logger.error("#{type_input} is not an existing atom : #{inspect(e)}")
nil
:invalid_type
end
else
type_input
@@ -128,7 +128,7 @@ defmodule Mobilizon.Addresses do
process_point(data["latitude"], data["longitude"])
end
else
{:error, nil}
{:error, :invalid_type}
end
end

View File

@@ -117,11 +117,30 @@ defmodule Mobilizon.Events do
Repo.get_by!(Event, url: url)
end
# @doc """
# Gets an event by it's UUID
# """
# @depreciated "Use get_event_full_by_uuid/3 instead"
# def get_event_by_uuid(uuid) do
# Repo.get_by(Event, uuid: uuid)
# end
@doc """
Gets an event by it's UUID
Gets a full event by it's UUID
"""
def get_event_by_uuid(uuid) do
Repo.get_by(Event, uuid: uuid)
@spec get_event_full_by_uuid(String.t()) :: Event.t()
def get_event_full_by_uuid(uuid) do
event = Repo.get_by(Event, uuid: uuid)
Repo.preload(event, [
:organizer_actor,
:category,
:sessions,
:tracks,
:tags,
:participants,
:physical_address
])
end
@doc """
@@ -144,25 +163,31 @@ defmodule Mobilizon.Events do
@doc """
Gets an event by it's URL
"""
def get_event_full_by_url!(url) do
event = Repo.get_by!(Event, url: url)
Repo.preload(event, [
:organizer_actor,
:category,
:sessions,
:tracks,
:tags,
:participants,
:physical_address
])
def get_event_full_by_url(url) do
case Repo.one(
from(e in Event,
where: e.url == ^url,
preload: [
:organizer_actor,
:category,
:sessions,
:tracks,
:tags,
:participants,
:physical_address
]
)
) do
nil -> {:error, :event_not_found}
event -> {:ok, event}
end
end
@doc """
Gets a full event by it's UUID
Gets an event by it's URL
"""
def get_event_full_by_uuid(uuid) do
event = Repo.get_by(Event, uuid: uuid)
def get_event_full_by_url!(url) do
event = Repo.get_by!(Event, url: url)
Repo.preload(event, [
:organizer_actor,
@@ -233,7 +258,7 @@ defmodule Mobilizon.Events do
{:ok, %Participant{} = _participant} <-
%Participant{}
|> Participant.changeset(%{
actor_id: attrs.organizer_actor_id,
actor_id: event.organizer_actor_id,
role: 4,
event_id: event.id
})
@@ -609,8 +634,12 @@ defmodule Mobilizon.Events do
Participant.changeset(participant, %{})
end
def list_requests_for_actor(%Actor{} = actor) do
Repo.all(from(p in Participant, where: p.actor_id == ^actor.id and p.approved == false))
@doc """
List event participation requests for an actor
"""
@spec list_requests_for_actor(Actor.t()) :: list(Participant.t())
def list_requests_for_actor(%Actor{id: actor_id}) do
Repo.all(from(p in Participant, where: p.actor_id == ^actor_id and p.approved == false))
end
alias Mobilizon.Events.Session
@@ -631,24 +660,18 @@ defmodule Mobilizon.Events do
@doc """
Returns the list of sessions for an event
"""
def list_sessions_for_event(event_uuid) do
@spec list_sessions_for_event(Event.t()) :: list(Session.t())
def list_sessions_for_event(%Event{id: event_id}) do
Repo.all(
from(
s in Session,
join: e in Event,
on: s.event_id == e.id,
where: e.uuid == ^event_uuid
where: e.id == ^event_id
)
)
end
@doc """
Returns the list of sessions for a track
"""
def list_sessions_for_track(track_id) do
Repo.all(from(s in Session, where: s.track_id == ^track_id))
end
@doc """
Gets a single session.
@@ -745,6 +768,14 @@ defmodule Mobilizon.Events do
Repo.all(Track)
end
@doc """
Returns the list of sessions for a track
"""
@spec list_sessions_for_track(Track.t()) :: list(Session.t())
def list_sessions_for_track(%Track{id: track_id}) do
Repo.all(from(s in Session, where: s.track_id == ^track_id))
end
@doc """
Gets a single track.
@@ -880,9 +911,29 @@ defmodule Mobilizon.Events do
"""
def get_comment!(id), do: Repo.get!(Comment, id)
def get_comment_from_uuid(uuid), do: Repo.get_by(Comment, uuid: uuid)
# @doc """
# Gets a single comment from it's UUID
def get_comment_from_uuid!(uuid), do: Repo.get_by!(Comment, uuid: uuid)
# """
# @spec get_comment_from_uuid(String.t) :: {:ok, Comment.t} | {:error, nil}
# def get_comment_from_uuid(uuid), do: Repo.get_by(Comment, uuid: uuid)
# @doc """
# Gets a single comment by it's UUID.
# Raises `Ecto.NoResultsError` if the Comment does not exist.
# ## Examples
# iex> get_comment_from_uuid!("123AFV13")
# %Comment{}
# iex> get_comment_from_uuid!("20R9HKDJHF")
# ** (Ecto.NoResultsError)
# """
# @spec get_comment_from_uuid(String.t) :: Comment.t
# def get_comment_from_uuid!(uuid), do: Repo.get_by!(Comment, uuid: uuid)
def get_comment_full_from_uuid(uuid) do
with %Comment{} = comment <- Repo.get_by!(Comment, uuid: uuid) do
@@ -894,9 +945,18 @@ defmodule Mobilizon.Events do
def get_comment_from_url!(url), do: Repo.get_by!(Comment, url: url)
def get_comment_full_from_url(url) do
case Repo.one(
from(c in Comment, where: c.url == ^url, preload: [:actor, :in_reply_to_comment])
) do
nil -> {:error, :comment_not_found}
comment -> {:ok, comment}
end
end
def get_comment_full_from_url!(url) do
with %Comment{} = comment <- Repo.get_by!(Comment, url: url) do
Repo.preload(comment, :actor)
Repo.preload(comment, [:actor, :in_reply_to_comment])
end
end