Skip to content

Commit

Permalink
Fix push bug
Browse files Browse the repository at this point in the history
  • Loading branch information
chrzaszcz committed May 30, 2023
1 parent c9624c1 commit 814af89
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 26 deletions.
4 changes: 2 additions & 2 deletions big_tests/tests/push_integration_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -954,8 +954,8 @@ add_user_server_to_whitelist(User, {NodeAddr, NodeName}) ->
assert_push_notification_in_session(User, NodeName, Service, DeviceToken) ->
Info = mongoose_helper:get_session_info(?RPC_SPEC, User),
{_JID, NodeName, Details} = maps:get(?SESSION_KEY, Info),
?assertMatch({<<"service">>, Service}, lists:keyfind(<<"service">>, 1, Details)),
?assertMatch({<<"device_id">>, DeviceToken}, lists:keyfind(<<"device_id">>, 1, Details)).
?assertMatch(#{<<"service">> := Service}, Details),
?assertMatch(#{<<"device_id">> := DeviceToken}, Details).

wait_for_push_request(DeviceToken) ->
mongoose_push_mock:wait_for_push_request(DeviceToken, 10000).
Expand Down
19 changes: 9 additions & 10 deletions src/event_pusher/mod_event_pusher_push.erl
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@
%% Types
-type publish_service() :: {PubSub :: jid:jid(), Node :: pubsub_node(), Form :: form()}.
-type pubsub_node() :: binary().
-type form_field() :: {Name :: binary(), Value :: binary()}.
-type form() :: [form_field()].
-type form() :: #{binary() => binary()}.

-export_type([pubsub_node/0, form_field/0, form/0]).
-export_type([pubsub_node/0, form/0]).
-export_type([publish_service/0]).

%%--------------------------------------------------------------------
Expand Down Expand Up @@ -238,18 +237,18 @@ parse_form(Form) ->
parse_form_fields(Form) ->
case mongoose_data_forms:parse_form_fields(Form) of
#{type := <<"submit">>, ns := ?NS_PUBSUB_PUB_OPTIONS, kvs := KVs} ->
ParsedKVs = lists:map(fun parse_kv/1, maps:to_list(KVs)),
case lists:member(invalid_field, ParsedKVs) of
false -> ParsedKVs;
true -> invalid_form
case maps:filtermap(fun(_, [V]) -> {true, V};
(_, _) -> false
end, KVs) of
ParsedKVs when map_size(ParsedKVs) < map_size(KVs) ->
invalid_form;
ParsedKVs ->
ParsedKVs
end;
_ ->
invalid_form
end.

parse_kv({K, [V]}) -> {K, V};
parse_kv(_Other) -> invalid_field.

-spec enable_node(mongooseim:host_type(), jid:jid(), jid:jid(), pubsub_node(), form()) ->
ok | {error, Reason :: term()}.
enable_node(HostType, From, BarePubSubJID, Node, FormFields) ->
Expand Down
2 changes: 1 addition & 1 deletion src/event_pusher/mod_event_pusher_push_plugin.erl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

-optional_callbacks([should_publish/3, prepare_notification/2, publish_notification/4]).

-type push_payload() :: mod_event_pusher_push:form().
-type push_payload() :: [{binary(), binary()}].
-export_type([push_payload/0]).
%%--------------------------------------------------------------------
%% API
Expand Down
9 changes: 4 additions & 5 deletions src/event_pusher/mod_event_pusher_push_plugin_defaults.erl
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,10 @@ push_content_fields(SenderId, BodyCData, MessageCount) ->
PushPayload :: mod_event_pusher_push_plugin:push_payload()) ->
any().
publish_via_hook(Acc0, HostType, To, {PubsubJID, Node, Form}, PushPayload) ->
OptionMap = maps:from_list(Form),
%% Acc is ignored by mod_push_service_mongoosepush, added here only for
%% traceability purposes and push_SUITE code unification
Acc = mongoose_acc:set(push_notifications, pubsub_jid, PubsubJID, Acc0),
case mongoose_hooks:push_notifications(HostType, Acc, [maps:from_list(PushPayload)], OptionMap) of
case mongoose_hooks:push_notifications(HostType, Acc, [maps:from_list(PushPayload)], Form) of
{error, device_not_registered} ->
%% We disable the push node in case the error type is device_not_registered
mod_event_pusher_push:disable_node(HostType, To, PubsubJID, Node);
Expand Down Expand Up @@ -191,15 +190,15 @@ push_notification_iq(Node, Form, PushPayload) ->
children = [make_form(?PUSH_FORM_TYPE, PushPayload)]}
]}
]}
] ++ maybe_publish_options(Form)}
] ++ maybe_publish_options(maps:to_list(Form))}
]}.

-spec make_form(binary(), mod_event_pusher_push:form()) -> exml:element().
-spec make_form(binary(), mod_event_pusher_push_plugin:push_payload()) -> exml:element().
make_form(FormType, FieldKVs) ->
Fields = [#{var => Name, values => [Value]} || {Name, Value} <- FieldKVs],
mongoose_data_forms:form(#{ns => FormType, type => <<"submit">>, fields => Fields}).

-spec maybe_publish_options(mod_event_pusher_push:form()) -> [exml:element()].
-spec maybe_publish_options([{binary(), binary()}]) -> [exml:element()].
maybe_publish_options([]) ->
[];
maybe_publish_options(FormFields) ->
Expand Down
11 changes: 5 additions & 6 deletions src/event_pusher/mod_event_pusher_push_rdbms.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ init(_HostType, _Opts) ->
Node :: mod_event_pusher_push:pubsub_node(),
Form :: mod_event_pusher_push:form(),
Result :: ok | {error, term()}.
enable(HostType, User, PubSub, Node, Forms) ->
enable(HostType, User, PubSub, Node, Form) ->
ExtUser = jid:to_bare_binary(User),
ExtPubSub = jid:to_binary(PubSub),
ExtForms = encode_form(Forms),
ExtForms = encode_form(Form),
execute_delete(HostType, ExtUser, Node, ExtPubSub),
CreatedAt = os:system_time(microsecond),
case execute_insert(HostType, ExtUser, Node, ExtPubSub, ExtForms, CreatedAt) of
Expand Down Expand Up @@ -81,12 +81,11 @@ decode_row({NodeID, PubSubBin, FormJSON}) ->
NodeID,
decode_form(FormJSON)}.

encode_form(Forms) ->
jiffy:encode({Forms}).
encode_form(Form) ->
jiffy:encode(Form).

decode_form(FormJSON) ->
{Items} = jiffy:decode(FormJSON),
Items.
jiffy:decode(FormJSON, [return_maps]).

%% Prepared queries

Expand Down
4 changes: 2 additions & 2 deletions src/hooks/mongoose_hooks.erl
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,8 @@ presence_probe_hook(HostType, Acc, From, To, Pid) ->
-spec push_notifications(HostType, Acc, NotificationForms, Options) -> Result when
HostType :: mongooseim:host_type(),
Acc :: ok | mongoose_acc:t(),
NotificationForms :: [#{atom() => binary()}],
Options :: #{atom() => binary()},
NotificationForms :: [#{binary() => binary()}],
Options :: #{binary() => binary()},
Result :: ok | {error, any()}.
push_notifications(HostType, Acc, NotificationForms, Options) ->
Params = #{options => Options, notification_forms => NotificationForms},
Expand Down

0 comments on commit 814af89

Please sign in to comment.