|
18 | 18 |
|
19 | 19 | %% old ones
|
20 | 20 | -export([id/0,
|
21 |
| - message/4, |
| 21 | + message/2, message/4, |
22 | 22 | chat_to/2,
|
23 | 23 | chat/3,
|
24 | 24 | chat_to_short_jid/2,
|
25 | 25 | chat_without_carbon_to/2,
|
| 26 | + chat_to_with_id_and_timestamp/2, |
| 27 | + chat_to_with_id/2, |
26 | 28 | groupchat_to/2,
|
27 | 29 | iq_result/1,
|
28 | 30 | iq_result/2,
|
@@ -304,24 +306,51 @@ message(From, Recipient, Type, Msg) ->
|
304 | 306 | message1(From, Recipient, Type, Msg).
|
305 | 307 |
|
306 | 308 | message1(From, Recipient, Type, Msg) ->
|
307 |
| - FromAttr = case From of |
308 |
| - undefined -> []; |
309 |
| - _ -> [{<<"from">>, From}] |
310 |
| - end, |
311 |
| - #xmlel{name = <<"message">>, |
312 |
| - attrs = FromAttr ++ [{<<"type">>, Type}, |
313 |
| - {<<"to">>, escalus_utils:get_jid(Recipient)}], |
314 |
| - children = [#xmlel{name = <<"body">>, |
315 |
| - children = [#xmlcdata{content = Msg}]}]}. |
| 309 | + AttrsIn = #{to => Recipient, |
| 310 | + type => Type}, |
| 311 | + Attrs = case From of |
| 312 | + undefined -> AttrsIn; |
| 313 | + _ -> AttrsIn#{from => From} |
| 314 | + end, |
| 315 | + message(Msg, Attrs). |
| 316 | + |
| 317 | +-spec message(binary(), Attrs) -> exml:element() when |
| 318 | + Attrs :: #{to => escalus_utils:jid_spec(), |
| 319 | + atom() | binary() => binary()}. |
| 320 | +message(Text, Attrs) -> |
| 321 | + maps:fold(fun (to, V, Stanza) -> setattr(Stanza, <<"to">>, escalus_utils:get_jid(V)); |
| 322 | + (K, V, Stanza) when is_atom(K) -> setattr(Stanza, atom_to_binary(K, utf8), V); |
| 323 | + (K, V, Stanza) when is_binary(K) -> setattr(Stanza, K, V) end, |
| 324 | + #xmlel{name = <<"message">>, |
| 325 | + children = [#xmlel{name = <<"body">>, |
| 326 | + children = [#xmlcdata{content = Text}]}]}, |
| 327 | + Attrs). |
316 | 328 |
|
317 | 329 | chat_to(Recipient, Msg) ->
|
318 |
| - message(undefined, Recipient, <<"chat">>, Msg). |
| 330 | + message(Msg, #{type => <<"chat">>, |
| 331 | + to => Recipient}). |
319 | 332 |
|
320 | 333 | chat(Sender, Recipient, Msg) ->
|
321 |
| - message(Sender, Recipient, <<"chat">>, Msg). |
| 334 | + message(Msg, #{type => <<"chat">>, |
| 335 | + from => Sender, |
| 336 | + to => Recipient}). |
322 | 337 |
|
323 | 338 | chat_to_short_jid(Recipient, Msg) ->
|
324 |
| - chat_to(escalus_utils:get_short_jid(Recipient), Msg). |
| 339 | + message(Msg, #{type => <<"chat">>, |
| 340 | + to => escalus_utils:get_short_jid(Recipient)}). |
| 341 | + |
| 342 | +-spec chat_to_with_id(escalus_utils:jid_spec(), binary()) -> exml:element(). |
| 343 | +chat_to_with_id(Recipient, Msg) -> |
| 344 | + message(Msg, #{type => <<"chat">>, |
| 345 | + to => Recipient, |
| 346 | + id => uuid_v4()}). |
| 347 | + |
| 348 | +-spec chat_to_with_id_and_timestamp(escalus_utils:jid_spec(), binary()) -> exml:element(). |
| 349 | +chat_to_with_id_and_timestamp(Recipient, Msg) -> |
| 350 | + message(Msg, #{type => <<"chat">>, |
| 351 | + to => Recipient, |
| 352 | + id => uuid_v4(), |
| 353 | + timestamp => integer_to_binary(os:system_time(microsecond))}). |
325 | 354 |
|
326 | 355 | chat_without_carbon_to(Recipient, Msg) ->
|
327 | 356 | Stanza = #xmlel{children = Children} = chat_to(Recipient, Msg),
|
@@ -821,7 +850,7 @@ id() ->
|
821 | 850 |
|
822 | 851 | -spec uuid_v4() -> binary().
|
823 | 852 | uuid_v4() ->
|
824 |
| - iolist_to_binary(uuid:uuid_to_string(uuid:get_v4())). |
| 853 | + uuid:uuid_to_string(uuid:get_v4(), binary_standard). |
825 | 854 |
|
826 | 855 | %%--------------------------------------------------------------------
|
827 | 856 | %% Stanzas from inline XML
|
|
0 commit comments