-
-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved FCM result parsing into it's own module for readability. Also cleaned up various linting warnings.
- Loading branch information
Showing
5 changed files
with
136 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
defmodule Pigeon.FCM.ResultParser do | ||
@moduledoc false | ||
|
||
alias Pigeon.FCM.NotificationResponse | ||
|
||
def parse([], [], on_response, result) do | ||
on_response.({:ok, result}) | ||
end | ||
|
||
def parse(regid, results, on_response, result) when is_binary(regid) do | ||
parse([regid], results, on_response, result) | ||
end | ||
|
||
# Handle RegID updates | ||
def parse([regid | reg_res], | ||
[%{"message_id" => id, "registration_id" => new_regid} | rest_results], | ||
on_response, | ||
%NotificationResponse{ update: update} = resp) do | ||
|
||
new_updates = [{regid, new_regid} | update] | ||
parse(reg_res, rest_results, on_response, %{resp | message_id: id, update: new_updates}) | ||
end | ||
|
||
# Handle successful RegIDs, also parse `message_id` | ||
def parse([regid | reg_res], | ||
[%{"message_id" => id} | rest_results], | ||
on_response, | ||
%NotificationResponse{ok: ok} = resp) do | ||
|
||
parse(reg_res, rest_results, on_response, %{resp | message_id: id, ok: [regid | ok]}) | ||
end | ||
|
||
# Retry `Unavailable` RegIDs | ||
def parse([regid | reg_res], | ||
[%{"error" => "Unavailable"} | rest_results], | ||
on_response, | ||
%NotificationResponse{retry: retry} = resp) do | ||
|
||
parse(reg_res, rest_results, on_response, %{resp | retry: [regid | retry]}) | ||
end | ||
|
||
# Remove `NotRegistered` or `InvalidRegistration` RegIDs | ||
def parse([regid | reg_res], | ||
[%{"error" => invalid} | rest_results], | ||
on_response, | ||
%NotificationResponse{remove: remove} = resp) when invalid == "NotRegistered" | ||
or invalid == "InvalidRegistration" do | ||
|
||
parse(reg_res, rest_results, on_response, %{resp | remove: [regid | remove]}) | ||
end | ||
|
||
# Handle all other error keys | ||
def parse([regid | reg_res] = regs, | ||
[%{"error" => error} | rest_results] = results, | ||
on_response, | ||
%NotificationResponse{error: regs_in_error} = resp) do | ||
|
||
if Map.has_key?(regs_in_error, error) do | ||
parse(reg_res, rest_results, on_response, %{resp | error: %{regs_in_error | error => regid}}) | ||
else | ||
# create map key if required. | ||
parse(regs, results, on_response, %{resp | error: Map.merge(%{error => []}, regs_in_error)}) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
defmodule Pigeon.FCM.ResultParserTest do | ||
use ExUnit.Case | ||
|
||
alias Pigeon.FCM.{NotificationResponse, ResultParser} | ||
|
||
test "parse_result with success" do | ||
{:ok, response} = | ||
ResultParser.parse( | ||
["regid"], | ||
[%{ "message_id" => "1:0408" }], | ||
&(&1), %NotificationResponse{} | ||
) | ||
assert response.ok == ["regid"] | ||
end | ||
|
||
test "parse_result with success and new registration_id" do | ||
{:ok, response} = | ||
ResultParser.parse( | ||
["regid"], | ||
[%{ "message_id" => "1:2342", "registration_id" => "32" }], | ||
&(&1), %NotificationResponse{} | ||
) | ||
|
||
assert response.update == [{"regid", "32"}] | ||
assert response.message_id == "1:2342" | ||
end | ||
|
||
test "parse_result with error unavailable" do | ||
{:ok, response} = | ||
ResultParser.parse( | ||
["regid"], | ||
[%{ "error" => "Unavailable" }], | ||
&(&1), | ||
%NotificationResponse{} | ||
) | ||
assert response.retry == ["regid"] | ||
end | ||
|
||
test "parse_result with custom error" do | ||
{:ok, response} = | ||
ResultParser.parse( | ||
["regid"], | ||
[%{ "error" => "CustomError" }], | ||
&(&1), | ||
%NotificationResponse{} | ||
) | ||
assert response.error == %{"CustomError" => "regid"} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,20 @@ | ||
defmodule Pigeon.FCM.WorkerTest do | ||
use ExUnit.Case | ||
alias Pigeon.FCM | ||
alias Pigeon.FCM.{NotificationResponse} | ||
|
||
@data %{"message" => "Test push"} | ||
@payload %{"data" => @data} | ||
|
||
defp valid_fcm_reg_id, do: Application.get_env(:pigeon, :test)[:valid_fcm_reg_id] | ||
|
||
test "parse_result with success" do | ||
{:ok, response} = | ||
FCM.Worker.parse_result1( | ||
["regid"], | ||
[%{ "message_id" => "1:0408" }], | ||
&(&1), %NotificationResponse{} | ||
) | ||
assert response.ok == ["regid"] | ||
end | ||
|
||
test "parse_result with success and new registration_id" do | ||
{:ok, response} = | ||
FCM.Worker.parse_result1( | ||
["regid"], | ||
[%{ "message_id" => "1:2342", "registration_id" => "32" }], | ||
&(&1), %NotificationResponse{} | ||
) | ||
|
||
assert response.update == [{"regid", "32"}] | ||
assert response.message_id == "1:2342" | ||
end | ||
|
||
test "parse_result with error unavailable" do | ||
{:ok, response} = | ||
FCM.Worker.parse_result1( | ||
["regid"], | ||
[%{ "error" => "Unavailable" }], | ||
&(&1), | ||
%NotificationResponse{} | ||
) | ||
assert response.retry == ["regid"] | ||
end | ||
|
||
test "parse_result with custom error" do | ||
{:ok, response} = | ||
FCM.Worker.parse_result1( | ||
["regid"], | ||
[%{ "error" => "CustomError" }], | ||
&(&1), | ||
%NotificationResponse{} | ||
) | ||
assert response.error == %{"CustomError" => "regid"} | ||
end | ||
alias Pigeon.FCM | ||
|
||
test "send malformed JSON" do | ||
#{:ok, pid} = FCM.Worker.start_link(:gonecrashing, key: Application.get_env(:pigeon, :fcm)[:key]) | ||
opts = [name: :gonecrashing, key: Application.get_env(:pigeon, :test)[:fcm_key]] | ||
opts = [ | ||
name: :gonecrashing, | ||
key: Application.get_env(:pigeon, :test)[:fcm_key] | ||
] | ||
{:ok, pid} = FCM.start_connection(opts) | ||
|
||
me = self() | ||
:gen_server.cast(pid, {:push, :fcm, {"toto", "this is not json"}, &(send me, &1), %{}}) | ||
bad = {"toto", "this is not json"} | ||
:gen_server.cast(pid, {:push, :fcm, bad, &(send me, &1), %{}}) | ||
assert_receive {:error, :malformed_json}, 5000 | ||
|
||
:gen_server.cast(pid, :stop) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters