2024-04-05 19:04:38 +00:00
|
|
|
defmodule TuskerPushWeb.RegistrationsController do
|
|
|
|
alias TuskerPush.Registration
|
|
|
|
alias Ecto.Changeset
|
|
|
|
use TuskerPushWeb, :controller
|
|
|
|
|
|
|
|
require Logger
|
|
|
|
|
|
|
|
def create(conn, %{
|
|
|
|
"transaction_id" => transaction_id,
|
|
|
|
"environment" => env,
|
2024-04-06 19:26:44 +00:00
|
|
|
"device_token" => token,
|
|
|
|
"push_version" => version
|
2024-04-05 19:04:38 +00:00
|
|
|
}) do
|
|
|
|
with {:ok, %Registration{id: id}} <-
|
|
|
|
TuskerPush.register(%{
|
|
|
|
storekit_original_transaction_id: transaction_id,
|
|
|
|
apns_environment: env,
|
2024-04-06 19:26:44 +00:00
|
|
|
apns_device_token: token,
|
|
|
|
push_version: version
|
2024-04-05 19:04:38 +00:00
|
|
|
}) do
|
2024-04-05 22:26:10 +00:00
|
|
|
conn
|
|
|
|
|> json(%{
|
|
|
|
id: id,
|
|
|
|
endpoint: url(~p"/mastodon/push/#{id}")
|
|
|
|
})
|
2024-04-05 19:04:38 +00:00
|
|
|
else
|
|
|
|
{:env, nil} ->
|
|
|
|
conn
|
|
|
|
|> put_status(400)
|
|
|
|
|> json(%{error: "invalid apns environment"})
|
|
|
|
|
|
|
|
{:error, %Changeset{valid?: false} = changeset} ->
|
|
|
|
errors =
|
|
|
|
changeset.errors
|
|
|
|
|> Enum.map(fn {k, {reason, _}} -> %{key: k, reason: reason} end)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_status(400)
|
|
|
|
|> json(%{errors: errors})
|
|
|
|
|
|
|
|
{:error, reason} ->
|
|
|
|
Logger.error("Failed creating registration: #{inspect(reason)}")
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_status(500)
|
|
|
|
|> json(%{error: "unknown error"})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create(conn, _) do
|
|
|
|
conn
|
|
|
|
|> put_status(400)
|
|
|
|
|> json(%{error: "missing required parameters"})
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(conn, %{"id" => id}) do
|
|
|
|
TuskerPush.unregister(id)
|
|
|
|
|
|
|
|
json(conn, %{status: "ok"})
|
|
|
|
end
|
|
|
|
end
|