Skip to content

Commit 594a0ac

Browse files
committed
Add Pushy (pushy.me) support
1 parent b9f5213 commit 594a0ac

File tree

10 files changed

+69
-10
lines changed

10 files changed

+69
-10
lines changed

config/prod.exs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ config :maru, MongoosePush.Router,
1717

1818
config :mongoose_push, loglevel:
1919
{:system, :atom, "PUSH_LOGLEVEL", :info}
20+
21+
config :mongoose_push, pushy_enabled:
22+
{:system, :boolean, "PUSH_PUSHY_ENABLED", false}
23+
2024
config :mongoose_push, fcm_enabled:
21-
{:system, :boolean, "PUSH_FCM_ENABLED", true}
25+
{:system, :boolean, "PUSH_FCM_ENABLED", false}
2226

2327
config :mongoose_push, apns_enabled:
24-
{:system, :boolean, "PUSH_APNS_ENABLED", true}
28+
{:system, :boolean, "PUSH_APNS_ENABLED", false}
2529

2630
config :mongoose_push, fcm: [
2731
default: [
@@ -32,6 +36,15 @@ config :mongoose_push, fcm: [
3236
]
3337
]
3438

39+
config :mongoose_push, pushy: [
40+
default: [
41+
endpoint: {:system, :string, "PUSH_PUSHY_ENDPOINT", nil},
42+
key: {:system, :string, "PUSH_PUSHY_APP_KEY", "fake_app_key"},
43+
pool_size: {:system, :integer, "PUSH_PUSHY_POOL_SIZE", 100},
44+
mode: :prod,
45+
]
46+
]
47+
3548
config :mongoose_push, apns: [
3649
dev: [
3750
endpoint: {:system, :string, "PUSH_APNS_DEV_ENDPOINT", nil},

lib/mongoose_push/api.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ defmodule MongoosePush.API do
1616
{500, %{:details => reason}}
1717
end
1818
end
19+
def to_status({:error, reason}) when is_binary(reason) do
20+
{400, reason}
21+
end
1922
def to_status({:error, _reason}) do
2023
{500, nil}
2124
end

lib/mongoose_push/api/v1.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ defmodule MongoosePush.API.V1 do
1010
parsers: [:urlencoded, :json, :multipart]
1111

1212
params do
13-
requires :service, type: Atom, values: [:fcm, :apns]
13+
requires :service, type: Atom, values: [:fcm, :apns, :pushy]
1414
requires :body, type: String
1515
requires :title, type: String
1616
optional :badge, type: Integer

lib/mongoose_push/api/v2.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ defmodule MongoosePush.API.V2 do
1010
parsers: [:urlencoded, :json, :multipart]
1111

1212
params do
13-
requires :service, type: Atom, values: [:fcm, :apns]
13+
requires :service, type: Atom, values: [:fcm, :apns, :pushy]
1414
optional :mode, type: Atom, values: [:prod, :dev]
1515
optional :priority, type: Atom, values: [:normal, :high]
1616
optional :time_to_live, type: Integer

lib/mongoose_push/application.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ defmodule MongoosePush.Application do
4343
def services do
4444
[
4545
fcm: MongoosePush.Service.FCM,
46-
apns: MongoosePush.Service.APNS
46+
apns: MongoosePush.Service.APNS,
47+
pushy: MongoosePush.Service.Pushy
4748
]
4849
end
4950

@@ -68,7 +69,7 @@ defmodule MongoosePush.Application do
6869
case service do
6970
:apns ->
7071
[:cert, :key]
71-
:fcm ->
72+
_ ->
7273
[]
7374
end
7475
config

lib/mongoose_push/pools.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ defmodule MongoosePush.Pools do
2424
Enum.map(config, &(elem(&1, 0)))
2525
end
2626

27+
@spec pools_by_mode(MongoosePush.service, MongoosePush.mode) :: list(atom)
28+
def pools_by_mode(:pushy = service, _mode) do
29+
config = pools_config(service)
30+
Enum.map(config, &(elem(&1, 0)))
31+
end
32+
2733
def pools_by_mode(:apns = service, mode) do
2834
config = pools_config(service)
2935

lib/mongoose_push/service/fcm.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ defmodule MongoosePush.Service.FCM do
2626
|> Enum.reduce(%{}, fn(field, map) ->
2727
Map.put(map, field, alert[field])
2828
end)
29+
|> Enum.filter(fn({_, value}) -> value != nil end)
30+
|> Map.new()
2931

3032
Notification.new(device_id, msg, request[:data])
3133
|> Notification.put_priority(@priority_mapping[request[:priority]])

lib/mongoose_push/service/pushy.ex

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
defmodule MongoosePush.Service.Pushy do
2+
@moduledoc """
3+
Pushy service provider implementation.
4+
"""
5+
6+
@behaviour MongoosePush.Service
7+
alias MongoosePush.Pools
8+
require Logger
9+
10+
@spec prepare_notification(String.t(), MongoosePush.request) ::
11+
Service.notification
12+
def prepare_notification(device_id, request) do
13+
MongoosePush.Service.FCM.prepare_notification(device_id, request)
14+
end
15+
16+
@spec push(Service.notification(), String.t(), atom(), Service.options()) ::
17+
:ok | {:error, term}
18+
def push(notification, device_id, worker, opts \\ []) do
19+
MongoosePush.Service.FCM.push(notification, device_id, worker, opts)
20+
end
21+
22+
@spec workers({atom, Keyword.t()} | nil) :: list(Supervisor.Spec.spec())
23+
def workers(nil), do: []
24+
def workers({pool_name, pool_config}) do
25+
Logger.info ~s"Starting Pushy pool with API key #{pool_config[:key]}"
26+
pool_size = pool_config[:pool_size]
27+
Enum.map(1..pool_size, fn(id) ->
28+
worker_name = Pools.worker_name(:pushy, pool_name, id)
29+
Supervisor.Spec.worker(Pigeon.PushyWorker,
30+
[worker_name, pool_config], [id: worker_name])
31+
end)
32+
end
33+
34+
end

mix.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ defmodule MongoosePush.Mixfile do
2525

2626
defp deps do
2727
[
28-
{:pigeon, github: "rslota/pigeon", ref: "2860eee35b58e2d8674f805f1151f57b9faeca21"},
28+
{:pigeon, github: "rslota/pigeon", ref: "fa7fa5e"},
2929
{:chatterbox, github: "joedevivo/chatterbox", ref: "ff0c2e0", override: true},
3030

3131
{:maru, github: "rslota/maru", ref: "54fc038", override: true},
@@ -51,7 +51,7 @@ defmodule MongoosePush.Mixfile do
5151
# Until eproxus/meck #fcc551e3 is in a release, we need to use master version
5252
# to include this commit (fixes mocking in Erlang 20.x + Elixir 1.5.x)
5353
{:meck, github: "eproxus/meck", override: true},
54-
{:httpoison, "~> 0.13"},
54+
{:httpoison, "~> 1.4"},
5555
{:excoveralls, "~> 0.7", only: :test},
5656
{:dialyxir, "~> 0.4", only: [:dev, :test], runtime: false},
5757
{:credo, "~> 0.5", only: [:dev, :test]},

mix.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"goldrush": {:hex, :goldrush, "0.1.9", "f06e5d5f1277da5c413e84d5a2924174182fb108dabb39d5ec548b27424cd106", [:rebar3], [], "hexpm"},
2222
"hackney": {:hex, :hackney, "1.11.0", "4951ee019df102492dabba66a09e305f61919a8a183a7860236c0fde586134b6", [:rebar3], [{:certifi, "2.0.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "5.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"},
2323
"hpack": {:git, "https://github.com/joedevivo/hpack.git", "6b58b6231e9b6ab83096715120578976f72f4f7c", [tag: "0.2.3"]},
24-
"httpoison": {:hex, :httpoison, "0.13.0", "bfaf44d9f133a6599886720f3937a7699466d23bb0cd7a88b6ba011f53c6f562", [:mix], [{:hackney, "~> 1.8", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"},
24+
"httpoison": {:hex, :httpoison, "1.5.1", "0f55b5b673b03c5c327dac7015a67cb571b99b631acc0bc1b0b98dcd6b9f2104", [:mix], [{:hackney, "~> 1.8", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"},
2525
"hut": {:hex, :hut, "1.2.0", "0089df0faa2827c605bbada88153f24fff5ea7a4be32ecf0250a7fdc2719cafb", [:"erlang.mk", :rebar, :rebar3], [], "hexpm"},
2626
"idna": {:hex, :idna, "5.1.0", "d72b4effeb324ad5da3cab1767cb16b17939004e789d8c0ad5b70f3cea20c89a", [:rebar3], [{:unicode_util_compat, "0.3.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"},
2727
"jason": {:hex, :jason, "1.0.0", "0f7cfa9bdb23fed721ec05419bcee2b2c21a77e926bce0deda029b5adc716fe2", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"},
@@ -37,7 +37,7 @@
3737
"mix_docker": {:hex, :mix_docker, "0.5.0", "c7ad34008c43d4a949d69721f39c4d2a2afc509c179926a683117ea8dff8af59", [:mix], [{:distillery, "~> 1.2", [hex: :distillery, repo: "hexpm", optional: false]}], "hexpm"},
3838
"mock": {:hex, :mock, "0.3.1", "994f00150f79a0ea50dc9d86134cd9ebd0d177ad60bd04d1e46336cdfdb98ff9", [:mix], [{:meck, "~> 0.8.8", [hex: :meck, repo: "hexpm", optional: false]}], "hexpm"},
3939
"parse_trans": {:hex, :parse_trans, "3.1.0", "1bad3b959941cc53ffd6f4769a5d2666f9cdf179b2d62826636497d3fbad9ec0", [:rebar3], [], "hexpm"},
40-
"pigeon": {:git, "https://github.com/rslota/pigeon.git", "2860eee35b58e2d8674f805f1151f57b9faeca21", [ref: "2860eee35b58e2d8674f805f1151f57b9faeca21"]},
40+
"pigeon": {:git, "https://github.com/rslota/pigeon.git", "fa7fa5e8686477c1ce19a7592bf4f13d994bc0f2", [ref: "fa7fa5e"]},
4141
"plug": {:hex, :plug, "1.5.0", "224b25b4039bedc1eac149fb52ed456770b9678bbf0349cdd810460e1e09195b", [:mix], [{:cowboy, "~> 1.0.1 or ~> 1.1 or ~> 2.1", [hex: :cowboy, repo: "hexpm", optional: true]}, {:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}], "hexpm"},
4242
"pobox": {:hex, :pobox, "1.0.4", "e695bc3b2b547dd6c8e5a19cb743396e6670e306ad3ff498844738f9418bd686", [:rebar3], [], "hexpm"},
4343
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"},

0 commit comments

Comments
 (0)