12274f9ed0
Prefs in user.preferences; push_subscription table; VAPID send on reminder/waitlist. Refs EventHub/EventHubBack#75
113 lines
3.8 KiB
Erlang
113 lines
3.8 KiB
Erlang
%%%-------------------------------------------------------------------
|
|
%%% @doc Web Push VAPID key + subscriptions (Back#75).
|
|
%%% @end
|
|
%%%-------------------------------------------------------------------
|
|
-module(handler_push).
|
|
-behaviour(cowboy_handler).
|
|
|
|
-export([init/2, trails/0]).
|
|
|
|
-include("records.hrl").
|
|
|
|
init(Req, Opts) ->
|
|
Path = cowboy_req:path(Req),
|
|
Method = cowboy_req:method(Req),
|
|
case Path of
|
|
<<"/v1/push/vapid-public-key">> ->
|
|
case logic_web_push:vapid_public_key() of
|
|
{ok, Key} ->
|
|
handler_utils:send_json(Req, 200, #{publicKey => Key});
|
|
{error, missing} ->
|
|
handler_utils:send_error(Req, 503, <<"VAPID not configured">>)
|
|
end;
|
|
<<"/v1/push/subscriptions">> ->
|
|
case handler_utils:auth_user(Req) of
|
|
{ok, UserId, Req1} ->
|
|
case Method of
|
|
<<"POST">> -> post_sub(Req1, UserId);
|
|
<<"DELETE">> -> delete_sub(Req1, UserId);
|
|
_ -> cowboy_req:reply(405, #{}, <<>>, Req1)
|
|
end;
|
|
{error, Code, Message, Req1} ->
|
|
handler_utils:send_error(Req1, Code, Message)
|
|
end;
|
|
_ ->
|
|
cowboy_req:reply(404, #{}, <<>>, Req)
|
|
end,
|
|
{ok, Req, Opts}.
|
|
|
|
trails() ->
|
|
[
|
|
#{
|
|
path => <<"/v1/push/vapid-public-key">>,
|
|
method => <<"GET">>,
|
|
description => <<"VAPID public key for Web Push subscribe">>,
|
|
tags => [<<"Notifications">>],
|
|
responses => #{200 => #{description => <<"OK">>}, 503 => #{description => <<"Not configured">>}}
|
|
},
|
|
#{
|
|
path => <<"/v1/push/subscriptions">>,
|
|
method => <<"POST">>,
|
|
description => <<"Register Web Push subscription">>,
|
|
tags => [<<"Notifications">>],
|
|
responses => #{200 => #{description => <<"OK">>}, 401 => #{description => <<"Unauthorized">>}}
|
|
},
|
|
#{
|
|
path => <<"/v1/push/subscriptions">>,
|
|
method => <<"DELETE">>,
|
|
description => <<"Remove Web Push subscription">>,
|
|
tags => [<<"Notifications">>],
|
|
responses => #{204 => #{description => <<"Deleted">>}, 401 => #{description => <<"Unauthorized">>}}
|
|
}
|
|
].
|
|
|
|
post_sub(Req0, UserId) ->
|
|
{ok, Body, Req1} = cowboy_req:read_body(Req0),
|
|
try jsx:decode(Body, [return_maps]) of
|
|
Map when is_map(Map) ->
|
|
Endpoint = maps:get(<<"endpoint">>, Map, undefined),
|
|
Keys = maps:get(<<"keys">>, Map, #{}),
|
|
P256 = maps:get(<<"p256dh">>, Keys, undefined),
|
|
Auth = maps:get(<<"auth">>, Keys, undefined),
|
|
case {Endpoint, P256, Auth} of
|
|
{E, P, A} when is_binary(E), E =/= <<>>,
|
|
is_binary(P), P =/= <<>>,
|
|
is_binary(A), A =/= <<>> ->
|
|
case core_push_subscription:upsert(UserId, E, P, A) of
|
|
{ok, Rec} ->
|
|
handler_utils:send_json(Req1, 200, #{
|
|
id => Rec#push_subscription.id,
|
|
endpoint => Rec#push_subscription.endpoint
|
|
});
|
|
{error, _} ->
|
|
handler_utils:send_error(Req1, 500, <<"Save failed">>)
|
|
end;
|
|
_ ->
|
|
handler_utils:send_error(Req1, 400, <<"endpoint and keys.p256dh/auth required">>)
|
|
end;
|
|
_ ->
|
|
handler_utils:send_error(Req1, 400, <<"Invalid JSON">>)
|
|
catch
|
|
_:_ ->
|
|
handler_utils:send_error(Req1, 400, <<"Invalid JSON format">>)
|
|
end.
|
|
|
|
delete_sub(Req0, UserId) ->
|
|
{ok, Body, Req1} = cowboy_req:read_body(Req0),
|
|
try jsx:decode(Body, [return_maps]) of
|
|
Map when is_map(Map) ->
|
|
Endpoint = maps:get(<<"endpoint">>, Map, <<>>),
|
|
case Endpoint of
|
|
<<>> ->
|
|
handler_utils:send_error(Req1, 400, <<"endpoint required">>);
|
|
Ep ->
|
|
_ = core_push_subscription:delete_by_endpoint(UserId, Ep),
|
|
cowboy_req:reply(204, #{}, <<>>, Req1)
|
|
end;
|
|
_ ->
|
|
handler_utils:send_error(Req1, 400, <<"Invalid JSON">>)
|
|
catch
|
|
_:_ ->
|
|
handler_utils:send_error(Req1, 400, <<"Invalid JSON format">>)
|
|
end.
|