diff --git a/lib/eventos/actors/actor.ex b/lib/eventos/actors/actor.ex index fdfc8409..411cafb3 100644 --- a/lib/eventos/actors/actor.ex +++ b/lib/eventos/actors/actor.ex @@ -76,7 +76,8 @@ defmodule Eventos.Actors.Actor do @doc false def changeset(%Actor{} = actor, attrs) do actor - |> Ecto.Changeset.cast(attrs, [:url, :outbox_url, :inbox_url, :shared_inbox_url, :following_url, :followers_url, :type, :name, :domain, :summary, :preferred_username, :keys, :manually_approves_followers, :suspended, :avatar_url, :banner_url]) + |> Ecto.Changeset.cast(attrs, [:url, :outbox_url, :inbox_url, :shared_inbox_url, :following_url, :followers_url, :type, :name, :domain, :summary, :preferred_username, :keys, :manually_approves_followers, :suspended, :avatar_url, :banner_url, :user_id]) + |> put_change(:url, "#{EventosWeb.Endpoint.url()}/@#{attrs["prefered_username"]}") |> validate_required([:preferred_username, :keys, :suspended, :url]) |> unique_constraint(:prefered_username, name: :actors_preferred_username_domain_index) end diff --git a/lib/eventos_web/controllers/actor_controller.ex b/lib/eventos_web/controllers/actor_controller.ex index 67aded7a..03dc14c3 100644 --- a/lib/eventos_web/controllers/actor_controller.ex +++ b/lib/eventos_web/controllers/actor_controller.ex @@ -5,7 +5,7 @@ defmodule EventosWeb.ActorController do use EventosWeb, :controller alias Eventos.Actors - alias Eventos.Actors.Actor + alias Eventos.Actors.{Actor, User} alias Eventos.Service.ActivityPub action_fallback EventosWeb.FallbackController @@ -15,6 +15,17 @@ defmodule EventosWeb.ActorController do render(conn, "index.json", actors: actors) end + def create(conn, %{"actor" => actor_params}) do + with %User{} = user <- Guardian.Plug.current_resource(conn), + actor_params <- Map.put(actor_params, "user_id", user.id), + {:ok, %Actor{} = actor} <- Actors.create_actor(actor_params) do + conn + |> put_status(:created) + |> put_resp_header("location", actor_path(conn, :show, actor.preferred_username)) + |> render("show_basic.json", actor: actor) + end + end + def show(conn, %{"name" => name}) do actor = Actors.get_actor_by_name_with_everything(name) render(conn, "show.json", actor: actor) diff --git a/lib/eventos_web/router.ex b/lib/eventos_web/router.ex index 31d96937..b396ed31 100644 --- a/lib/eventos_web/router.ex +++ b/lib/eventos_web/router.ex @@ -76,6 +76,7 @@ defmodule EventosWeb.Router do get "/user", UserController, :show_current_actor post "/sign-out", UserSessionController, :sign_out resources "/users", UserController, except: [:new, :edit, :show] + post "/actors", ActorController, :create patch "/actors/:name", ActorController, :update post "/events", EventController, :create patch "/events/:uuid", EventController, :update diff --git a/test/eventos/actors/actors_test.exs b/test/eventos/actors/actors_test.exs index 3ce9bf3b..55042181 100644 --- a/test/eventos/actors/actors_test.exs +++ b/test/eventos/actors/actors_test.exs @@ -37,7 +37,6 @@ defmodule Eventos.ActorsTest do assert actor.domain == "some domain" assert actor.keys == "some keypair" assert actor.suspended - assert actor.url == "some url" assert actor.preferred_username == "some username" end @@ -54,7 +53,6 @@ defmodule Eventos.ActorsTest do assert actor.domain == "some updated domain" assert actor.keys == "some updated keys" refute actor.suspended - assert actor.url == "some updated url" assert actor.preferred_username == "some updated username" end diff --git a/test/eventos_web/controllers/actor_controller_test.exs b/test/eventos_web/controllers/actor_controller_test.exs index a38443e6..f7e775d9 100644 --- a/test/eventos_web/controllers/actor_controller_test.exs +++ b/test/eventos_web/controllers/actor_controller_test.exs @@ -11,6 +11,12 @@ defmodule EventosWeb.ActorControllerTest do {:ok, conn: conn, user: user, actor: actor} end + key = :public_key.generate_key({:rsa, 2048, 65_537}) + entry = :public_key.pem_entry_encode(:RSAPrivateKey, key) + pem = [entry] |> :public_key.pem_encode() |> String.trim_trailing() + + @create_attrs %{preferred_username: "otheridentity", summary: "This is my other identity", domain: nil, keys: pem, user: nil} + describe "index" do test "lists all actors", %{conn: conn, user: user, actor: actor} do conn = get conn, actor_path(conn, :index) @@ -18,6 +24,14 @@ defmodule EventosWeb.ActorControllerTest do end end + describe "create actor" do + test "from an existing user", %{conn: conn, user: user} do + conn = auth_conn(conn, user) + conn = post conn, actor_path(conn, :create), actor: @create_attrs + assert json_response(conn, 201)["data"]["username"] == @create_attrs.preferred_username + end + end + ### # Not possible atm ###