Skip to content

Commit

Permalink
Support handlers for sent stanzas
Browse files Browse the repository at this point in the history
This can be used e.g. for updating metrics for sent messages.

To use it, put a list of handlers in client properties:

  {sent_stanza_handlers, [Handler1, Handler2, ...]}
  • Loading branch information
chrzaszcz committed Jan 8, 2019
1 parent 1f66c41 commit afadb70
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/escalus_connection.erl
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@

-type stanza_msg() :: {stanza, pid(), exml:element(), map()}.

-type metadata() :: #{recv_timestamp := integer()}.
-type metadata() :: #{recv_timestamp => integer()}.

%%%===================================================================
%%% Public API
Expand Down Expand Up @@ -181,10 +181,12 @@ maybe_set_jid(Client = #client{props = Props}) ->
end.

-spec send(escalus:client(), exml_stream:element()) -> ok.
send(#client{module = Mod, event_client = EventClient, rcv_pid = Pid, jid = Jid}, Elem) ->
send(#client{module = Mod, event_client = EventClient, rcv_pid = Pid, jid = Jid} = Client, Elem) ->
escalus_event:outgoing_stanza(EventClient, Elem),
escalus_ct:log_stanza(Jid, out, Elem),
Mod:send(Pid, Elem).
Mod:send(Pid, Elem),
handle_stanza(Client, Elem, #{}, sent_stanza_handlers(Client)),
ok.

-spec get_stanza(client(), any()) -> exml_stream:element().
get_stanza(Client, Name) ->
Expand Down Expand Up @@ -271,6 +273,9 @@ apply_handler(Handler, Client, Stanza, _Metadata) when is_function(Handler, 2) -
received_stanza_handlers(#client{props = Props}) ->
proplists:get_value(received_stanza_handlers, Props, []).

sent_stanza_handlers(#client{props = Props}) ->
proplists:get_value(sent_stanza_handlers, Props, []).

get_stream_end(#client{rcv_pid = Pid, jid = Jid}, Timeout) ->
receive
{stanza, Pid, Stanza = #xmlel{name = <<"close">>}, _} ->
Expand Down

0 comments on commit afadb70

Please sign in to comment.