2018-05-19 08:19:21 +00:00
|
|
|
defmodule EventosWeb.ActorController do
|
|
|
|
@moduledoc """
|
|
|
|
Controller for Actors
|
|
|
|
"""
|
|
|
|
use EventosWeb, :controller
|
|
|
|
|
|
|
|
alias Eventos.Actors
|
2018-07-10 12:56:10 +00:00
|
|
|
alias Eventos.Actors.{Actor, User}
|
2018-05-19 08:19:21 +00:00
|
|
|
alias Eventos.Service.ActivityPub
|
|
|
|
|
2018-07-27 08:45:35 +00:00
|
|
|
action_fallback(EventosWeb.FallbackController)
|
2018-05-19 08:19:21 +00:00
|
|
|
|
|
|
|
def index(conn, _params) do
|
|
|
|
actors = Actors.list_actors()
|
|
|
|
render(conn, "index.json", actors: actors)
|
|
|
|
end
|
|
|
|
|
2018-07-10 12:56:10 +00:00
|
|
|
def create(conn, %{"actor" => actor_params}) do
|
|
|
|
with %User{} = user <- Guardian.Plug.current_resource(conn),
|
2018-07-27 08:45:35 +00:00
|
|
|
actor_params <- Map.put(actor_params, "user_id", user.id),
|
|
|
|
actor_params <- Map.put(actor_params, "keys", keys_for_account()),
|
|
|
|
{:ok, %Actor{} = actor} <- Actors.create_actor(actor_params) do
|
2018-07-10 12:56:10 +00:00
|
|
|
conn
|
|
|
|
|> put_status(:created)
|
|
|
|
|> put_resp_header("location", actor_path(conn, :show, actor.preferred_username))
|
|
|
|
|> render("show_basic.json", actor: actor)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-11 11:46:19 +00:00
|
|
|
defp keys_for_account() do
|
|
|
|
key = :public_key.generate_key({:rsa, 2048, 65_537})
|
|
|
|
entry = :public_key.pem_entry_encode(:RSAPrivateKey, key)
|
2018-07-27 08:45:35 +00:00
|
|
|
|
2018-07-11 11:46:19 +00:00
|
|
|
[entry]
|
|
|
|
|> :public_key.pem_encode()
|
|
|
|
|> String.trim_trailing()
|
|
|
|
end
|
|
|
|
|
2018-05-19 08:19:21 +00:00
|
|
|
def show(conn, %{"name" => name}) do
|
|
|
|
actor = Actors.get_actor_by_name_with_everything(name)
|
|
|
|
render(conn, "show.json", actor: actor)
|
|
|
|
end
|
|
|
|
|
2018-05-30 12:27:21 +00:00
|
|
|
@email_regex ~r/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
|
2018-05-19 08:19:21 +00:00
|
|
|
def search(conn, %{"name" => name}) do
|
2018-07-27 08:45:35 +00:00
|
|
|
# find already saved accounts
|
|
|
|
case Actors.search(name) do
|
2018-05-30 12:27:21 +00:00
|
|
|
{:ok, actors} ->
|
|
|
|
render(conn, "index.json", actors: actors)
|
2018-07-27 08:45:35 +00:00
|
|
|
|
|
|
|
{:error, err} ->
|
|
|
|
json(conn, err)
|
2018-05-19 08:19:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update(conn, %{"name" => name, "actor" => actor_params}) do
|
|
|
|
actor = Actors.get_local_actor_by_name(name)
|
|
|
|
|
|
|
|
with {:ok, %Actor{} = actor} <- Actors.update_actor(actor, actor_params) do
|
|
|
|
render(conn, "show.json", actor: actor)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-27 08:45:35 +00:00
|
|
|
# def delete(conn, %{"id" => id_str}) do
|
|
|
|
# {id, _} = Integer.parse(id_str)
|
|
|
|
# if Guardian.Plug.current_resource(conn).actor.id == id do
|
|
|
|
# actor = Actors.get_actor!(id)
|
|
|
|
# with {:ok, %Actor{}} <- Actors.delete_actor(actor) do
|
|
|
|
# send_resp(conn, :no_content, "")
|
|
|
|
# end
|
|
|
|
# else
|
|
|
|
# send_resp(conn, 401, "")
|
|
|
|
# end
|
|
|
|
# end
|
2018-05-19 08:19:21 +00:00
|
|
|
end
|