Handle pushes from Mastodon

This commit is contained in:
Shadowfacts 2024-04-05 18:26:10 -04:00
parent fae112fb67
commit f7a64e742a
6 changed files with 67 additions and 2 deletions

1
.gitignore vendored
View File

@ -25,3 +25,4 @@ erl_crash.dump
# Ignore package tarball (built via "mix hex.build"). # Ignore package tarball (built via "mix hex.build").
tusker_push-*.tar tusker_push-*.tar
*.secret.exs

View File

@ -61,3 +61,5 @@ config :phoenix, :stacktrace_depth, 20
# Initialize plugs at runtime for faster development compilation # Initialize plugs at runtime for faster development compilation
config :phoenix, :plug_init_mode, :runtime config :phoenix, :plug_init_mode, :runtime
import_config "dev.secret.exs"

View File

@ -19,7 +19,7 @@ defmodule TuskerPush do
end end
@spec unregister(String.t()) :: :ok | {:error, :no_registration | Ecto.Changeset.t()} @spec unregister(String.t()) :: :ok | {:error, :no_registration | Ecto.Changeset.t()}
def unregister(id) do def unregister(id) when is_binary(id) do
with registration when not is_nil(registration) <- Repo.get(Registration, id), with registration when not is_nil(registration) <- Repo.get(Registration, id),
{:ok, _} <- Repo.delete(registration) do {:ok, _} <- Repo.delete(registration) do
:ok :ok
@ -31,4 +31,23 @@ defmodule TuskerPush do
{:error, reason} {:error, reason}
end end
end end
@spec unregister(Registration.t()) :: :ok | {:error, Ecto.Changeset.t()}
def unregister(%Registration{} = registration) do
case Repo.delete(registration) do
{:ok, _} -> :ok
{:error, reason} -> {:error, reason}
end
end
@spec get_registration(String.t()) :: Registration.t() | nil
def get_registration(id) do
Repo.get(Registration, id)
end
@spec check_registration_expired(Registration.t()) :: :ok | {:expired, Registration.t()}
def check_registration_expired(_registration) do
# TODO: expiration & grace period
:ok
end
end end

View File

@ -0,0 +1,33 @@
defmodule TuskerPushWeb.PushController do
use TuskerPushWeb, :controller
require Logger
def push(conn, %{"id" => id}) do
with {:registration, registration} when not is_nil(registration) <-
{:registration, TuskerPush.get_registration(id)},
:ok <- TuskerPush.check_registration_expired(registration),
{:ok, body, conn} <- read_body(conn) do
IO.inspect(body |> byte_size())
send_resp(conn, 200, "ok")
else
{:registration, nil} ->
send_resp(conn, 400, "unregistered")
{:expired, registration} ->
TuskerPush.unregister(registration)
send_resp(conn, 400, "unregistered")
{:more, _, conn} ->
Logger.error("Didn't finish reading")
send_resp(conn, 500, "failed to read body")
{:error, reason} ->
Logger.error("Reading body: #{inspect(reason)}")
send_resp(conn, 500, "failed to read body")
end
end
end

View File

@ -16,7 +16,11 @@ defmodule TuskerPushWeb.RegistrationsController do
apns_environment: env, apns_environment: env,
apns_device_token: token apns_device_token: token
}) do }) do
json(conn, %{id: id}) conn
|> json(%{
id: id,
endpoint: url(~p"/mastodon/push/#{id}")
})
else else
{:env, nil} -> {:env, nil} ->
conn conn

View File

@ -10,4 +10,10 @@ defmodule TuskerPushWeb.Router do
resources "/registrations", RegistrationsController, only: [:create, :delete] resources "/registrations", RegistrationsController, only: [:create, :delete]
end end
scope "/mastodon", TuskerPushWeb do
pipe_through :api
post "/push/:id", PushController, :push
end
end end