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

View File

@ -4,7 +4,7 @@ defmodule TuskerPushWeb.PushController do
require Logger
def push(conn, %{"id" => id}) do
def push(conn, %{"id" => id} = params) do
with {:registration, registration} when not is_nil(registration) <-
{:registration, TuskerPush.get_registration(id)},
:ok <- TuskerPush.check_registration_expired(registration),
@ -12,7 +12,8 @@ defmodule TuskerPushWeb.PushController do
{:body, {:ok, body, conn}} <- {:body, read_body(conn)},
{:salt, salt} when not is_nil(salt) <- get_salt(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")
else
{:registration, nil} ->