From f498f8672c36537643e2fd025e8d310c9465edf7 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 7 Apr 2024 10:56:23 -0400 Subject: [PATCH] Update registrations --- lib/tusker_push.ex | 8 +++ lib/tusker_push/registration.ex | 18 +++++-- .../controllers/registrations_controller.ex | 54 ++++++++++++++++--- lib/tusker_push_web/router.ex | 2 +- 4 files changed, 72 insertions(+), 10 deletions(-) diff --git a/lib/tusker_push.ex b/lib/tusker_push.ex index 2deb3d2..665ec68 100644 --- a/lib/tusker_push.ex +++ b/lib/tusker_push.ex @@ -18,6 +18,14 @@ defmodule TuskerPush do |> Repo.insert() end + @spec update_registration(Registration.t(), Map.t()) :: + {:ok, Registration.t()} | {:error, Ecto.Changeset.t()} + def update_registration(registration, params) do + registration + |> Registration.update_changeset(params) + |> Repo.update() + end + @spec unregister(String.t()) :: :ok | {:error, :no_registration | Ecto.Changeset.t()} def unregister(id) when is_binary(id) do with registration when not is_nil(registration) <- Repo.get(Registration, id), diff --git a/lib/tusker_push/registration.ex b/lib/tusker_push/registration.ex index e720c80..f1b6db8 100644 --- a/lib/tusker_push/registration.ex +++ b/lib/tusker_push/registration.ex @@ -27,7 +27,7 @@ defmodule TuskerPush.Registration do timestamps() end - @required_fields [ + @create_fields [ :storekit_original_transaction_id, :apns_environment, :apns_device_token, @@ -36,7 +36,19 @@ defmodule TuskerPush.Registration do def create_changeset(registration \\ %__MODULE__{}, params) do registration - |> cast(params, @required_fields) - |> validate_required(@required_fields) + |> cast(params, @create_fields) + |> validate_required(@create_fields) + end + + @update_fields [ + :apns_environment, + :apns_device_token, + :push_version + ] + + def update_changeset(registration, params) do + registration + |> cast(params, @update_fields) + |> validate_required(@update_fields) end end diff --git a/lib/tusker_push_web/controllers/registrations_controller.ex b/lib/tusker_push_web/controllers/registrations_controller.ex index 6c813d1..40f92c3 100644 --- a/lib/tusker_push_web/controllers/registrations_controller.ex +++ b/lib/tusker_push_web/controllers/registrations_controller.ex @@ -24,10 +24,52 @@ defmodule TuskerPushWeb.RegistrationsController do endpoint: url(~p"/mastodon/push/#{id}") }) else - {:env, nil} -> + {:error, %Changeset{valid?: false} = changeset} -> + errors = + changeset.errors + |> Enum.map(fn {k, {reason, _}} -> %{key: k, reason: reason} end) + conn |> put_status(400) - |> json(%{error: "invalid apns environment"}) + |> json(%{error: "validation failed", fields: errors}) + + {:error, reason} -> + Logger.error("Failed creating registration: #{inspect(reason)}") + + conn + |> put_status(500) + |> json(%{error: "unknown error", fields: []}) + end + end + + def create(conn, _) do + conn + |> put_status(400) + |> json(%{error: "missing required parameters"}) + end + + def update(conn, %{ + "id" => id, + "environment" => env, + "device_token" => token, + "push_version" => version + }) do + params = %{ + apns_environment: env, + apns_device_token: token, + push_version: version + } + + with {:registration, registration} when not is_nil(registration) <- + {:registration, TuskerPush.get_registration(id)}, + {:ok, _} <- TuskerPush.update_registration(registration, params) do + conn + |> json(%{id: id, endpoint: url(~p"/mastodon/push/#{id}")}) + else + {:registration, nil} -> + conn + |> put_status(404) + |> json(%{error: "not found", fields: []}) {:error, %Changeset{valid?: false} = changeset} -> errors = @@ -36,18 +78,18 @@ defmodule TuskerPushWeb.RegistrationsController do conn |> put_status(400) - |> json(%{errors: errors}) + |> json(%{error: "validation failed", fields: errors}) {:error, reason} -> - Logger.error("Failed creating registration: #{inspect(reason)}") + Logger.error("Failed updating registration: #{inspect(reason)}") conn |> put_status(500) - |> json(%{error: "unknown error"}) + |> json(%{error: "unknown error", fields: []}) end end - def create(conn, _) do + def update(conn, _) do conn |> put_status(400) |> json(%{error: "missing required parameters"}) diff --git a/lib/tusker_push_web/router.ex b/lib/tusker_push_web/router.ex index bd76991..4f4c68b 100644 --- a/lib/tusker_push_web/router.ex +++ b/lib/tusker_push_web/router.ex @@ -8,7 +8,7 @@ defmodule TuskerPushWeb.Router do scope "/app", TuskerPushWeb do pipe_through :api - resources "/registrations", RegistrationsController, only: [:create, :delete] + resources "/registrations", RegistrationsController, only: [:create, :update, :delete] end scope "/mastodon", TuskerPushWeb do