Skip to content

Commit

Permalink
mod_presence
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonVides committed Sep 3, 2022
1 parent e80ecce commit bdc1f33
Show file tree
Hide file tree
Showing 5 changed files with 647 additions and 54 deletions.
41 changes: 20 additions & 21 deletions big_tests/tests/presence_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,26 @@ all() ->
{group, subscribe_group}].

groups() ->
G = [{presence, [parallel], [available,
available_direct,
available_direct_then_unavailable,
available_direct_then_disconnect,
additions,
invisible_presence]},
{presence_priority, [parallel], [negative_priority_presence]},
{roster, [parallel], [get_roster,
fail_to_get_another_users_roster,
add_contact,
fail_to_add_contact_for_another_user,
remove_contact]},
{roster_versioning, [], [versioning,
versioning_no_store]},
{subscribe_group, [parallel], [subscribe,
subscribe_decline,
subscribe_relog,
subscribe_preserves_extra_info,
unsubscribe,
remove_unsubscribe]}],
ct_helper:repeat_all_until_all_ok(G).
[{presence, [parallel], [available,
available_direct,
available_direct_then_unavailable,
available_direct_then_disconnect,
additions,
invisible_presence]},
{presence_priority, [parallel], [negative_priority_presence]},
{roster, [parallel], [get_roster,
fail_to_get_another_users_roster,
add_contact,
fail_to_add_contact_for_another_user,
remove_contact]},
{roster_versioning, [], [versioning,
versioning_no_store]},
{subscribe_group, [parallel], [subscribe,
subscribe_decline,
subscribe_relog,
subscribe_preserves_extra_info,
unsubscribe,
remove_unsubscribe]}].

suite() ->
require_rpc_nodes([mim]) ++ escalus:suite().
Expand Down
28 changes: 6 additions & 22 deletions src/c2s/mongoose_c2s.erl
Original file line number Diff line number Diff line change
Expand Up @@ -786,31 +786,15 @@ patch_acc_for_reroute(Acc, Sid) ->
close_parser(#c2s_data{parser = undefined}) -> ok;
close_parser(#c2s_data{parser = Parser}) -> exml_stream:free_parser(Parser).

-spec close_session(c2s_data(), c2s_state(), mongoose_acc:t(), term()) -> mongoose_acc:t().
close_session(StateData, session_established, Acc, Reason) ->
Status = close_session_status(Reason),
PresenceUnavailable = mongoose_c2s_stanzas:presence_unavailable(Status),
Acc1 = mongoose_acc:update_stanza(#{from_jid => StateData#c2s_data.jid,
to_jid => jid:to_bare(StateData#c2s_data.jid),
element => PresenceUnavailable}, Acc),
ejabberd_sm:close_session_unset_presence(
Acc1, StateData#c2s_data.sid, StateData#c2s_data.jid, Status, sm_unset_reason(Reason));
-spec close_session(c2s_data(), c2s_state(), mongoose_acc:t(), term()) -> ok.
close_session(#c2s_data{jid = Jid, sid = Sid}, session_established, Acc, Reason) ->
ejabberd_sm:close_session(Acc, Sid, Jid, sm_unset_reason(Reason)),
ok;
close_session(_, _, Acc, _) ->
Acc.

close_session_status({shutdown, retries}) ->
<<"Too many attempts">>;
close_session_status({shutdown, replaced}) ->
<<"Replaced by new connection">>;
close_session_status(normal) ->
<<>>;
close_session_status(_) ->
<<"Unknown condition">>.

sm_unset_reason({shutdown, retries}) ->
retries;
sm_unset_reason({shutdown, replaced}) ->
replaced;
sm_unset_reason({shutdown, Reason}) ->
Reason;
sm_unset_reason(normal) ->
normal;
sm_unset_reason(_) ->
Expand Down
11 changes: 0 additions & 11 deletions src/c2s/mongoose_c2s_stanzas.erl
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,3 @@ successful_resource_binding(IQ, Jid) ->
attrs = [{<<"xmlns">>, ?NS_BIND}],
children = [JIDEl]}]},
jlib:iq_to_xml(Res).

-spec presence_unavailable(binary()) -> exml:element().
presence_unavailable(<<>>) ->
#xmlel{name = <<"presence">>,
attrs = [{<<"type">>, <<"unavailable">>}]};
presence_unavailable(Status) ->
StatusEl = #xmlel{name = <<"status">>,
children = [#xmlcdata{content = Status}]},
#xmlel{name = <<"presence">>,
attrs = [{<<"type">>, <<"unavailable">>}],
children = [StatusEl]}.
15 changes: 15 additions & 0 deletions src/jlib.erl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
stanza_errort/5,
stream_error/1,
stream_errort/3,
maybe_append_delay/4,
remove_delay_tags/1]).

-include_lib("exml/include/exml.hrl").
Expand Down Expand Up @@ -560,6 +561,20 @@ stream_errort(Condition, Lang, Text) ->
]
}.

-spec maybe_append_delay(Packet :: exml:element(),
From :: jid:jid(),
TS :: integer(),
Desc :: undefined | iodata()) -> exml:element().
maybe_append_delay(Packet = #xmlel{children = Children}, From, TS, Desc) ->
case exml_query:path(Packet, [{element, <<"delay">>}]) of
undefined ->
TsString = calendar:system_time_to_rfc3339(TS, [{offset, "Z"}, {unit, microsecond}]),
DelayTag = jlib:timestamp_to_xml(TsString, From, Desc),
Packet#xmlel{children = [DelayTag | Children]};
_ ->
Packet
end.

remove_delay_tags(#xmlel{children = Els} = Packet) ->
NEl = lists:foldl(
fun(#xmlel{name= <<"delay">>, attrs = Attrs} = R, El)->
Expand Down
Loading

0 comments on commit bdc1f33

Please sign in to comment.