tusker_push/lib/tusker_push_web/controllers/push_controller.ex

34 lines
941 B
Elixir
Raw Normal View History

2024-04-05 22:26:10 +00:00
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