Add context param to push route

This commit is contained in:
Shadowfacts 2024-04-08 22:20:03 -04:00
parent 26c0d2565b
commit 4781e6d789
2 changed files with 13 additions and 4 deletions

View File

@ -4,8 +4,9 @@ defmodule TuskerPush.Forwarder do
require Logger require Logger
@spec forward(Registration.t(), binary(), String.t(), String.t()) :: :ok | {:error, term()} @spec forward(Registration.t(), binary(), String.t(), String.t(), String.t() | nil) ::
def forward(%Registration{push_version: 1} = registration, body, salt, key) do :ok | {:error, term()}
def forward(%Registration{push_version: 1} = registration, body, salt, key, context) do
payload = %{ payload = %{
"aps" => %{ "aps" => %{
"alert" => %{ "alert" => %{
@ -20,6 +21,13 @@ defmodule TuskerPush.Forwarder do
"v" => 1 "v" => 1
} }
payload =
unless is_nil(context) do
Map.put(payload, "ctx", context)
else
payload
end
Logger.debug("Sending #{inspect(payload)}") Logger.debug("Sending #{inspect(payload)}")
Apns.send(registration, payload) Apns.send(registration, payload)

View File

@ -4,7 +4,7 @@ defmodule TuskerPushWeb.PushController do
require Logger require Logger
def push(conn, %{"id" => id}) do def push(conn, %{"id" => id} = params) do
with {:registration, registration} when not is_nil(registration) <- with {:registration, registration} when not is_nil(registration) <-
{:registration, TuskerPush.get_registration(id)}, {:registration, TuskerPush.get_registration(id)},
:ok <- TuskerPush.check_registration_expired(registration), :ok <- TuskerPush.check_registration_expired(registration),
@ -12,7 +12,8 @@ defmodule TuskerPushWeb.PushController do
{:body, {:ok, body, conn}} <- {:body, read_body(conn)}, {:body, {:ok, body, conn}} <- {:body, read_body(conn)},
{:salt, salt} when not is_nil(salt) <- get_salt(conn), {:salt, salt} when not is_nil(salt) <- get_salt(conn),
{:key, key} when not is_nil(key) <- get_key(conn), {:key, key} when not is_nil(key) <- get_key(conn),
{:forward, :ok} <- {:forward, Forwarder.forward(registration, body, salt, key)} do context <- Map.get(params, "context"),
{:forward, :ok} <- {:forward, Forwarder.forward(registration, body, salt, key, context)} do
send_resp(conn, 200, "ok") send_resp(conn, 200, "ok")
else else
{:registration, nil} -> {:registration, nil} ->