Improve add_message_type/2 function

Let add_message_type/2 accept the type as an atom, and let the function
handle the 'normal' message type.  This doesn't change the behavior, but
avoids some code duplication.
This commit is contained in:
Holger Weiss 2014-12-21 18:44:53 +01:00
parent cc958f7787
commit e5428c5500
2 changed files with 26 additions and 18 deletions

View File

@ -4485,10 +4485,7 @@ broadcast_stanza(Host, _Node, _NodeId, _Type, NodeOptions, SubsByDepth, NotifyTy
NotificationType = get_option(NodeOptions, notification_type, headline),
BroadcastAll = get_option(NodeOptions, broadcast_all_resources), %% XXX this is not standard, but usefull
From = service_jid(Host),
Stanza = case NotificationType of
normal -> BaseStanza;
MsgType -> add_message_type(BaseStanza, iolist_to_binary(atom_to_list(MsgType)))
end,
Stanza = add_message_type(BaseStanza, NotificationType),
%% Handles explicit subscriptions
SubIDsByJID = subscribed_nodes_by_jid(NotifyType, SubsByDepth),
lists:foreach(fun ({LJID, NodeName, SubIDs}) ->
@ -4520,10 +4517,8 @@ broadcast_stanza({LUser, LServer, LResource}, Publisher, Node, NodeId, Type, Nod
SenderResource = user_resource(LUser, LServer, LResource),
case ejabberd_sm:get_session_pid(LUser, LServer, SenderResource) of
C2SPid when is_pid(C2SPid) ->
Stanza = case get_option(NodeOptions, notification_type, headline) of
normal -> BaseStanza;
MsgType -> add_message_type(BaseStanza, iolist_to_binary(atom_to_list(MsgType)))
end,
NotificationType = get_option(NodeOptions, notification_type, headline),
Stanza = add_message_type(BaseStanza, NotificationType),
%% set the from address on the notification to the bare JID of the account owner
%% Also, add "replyto" if entity has presence subscription to the account owner
%% See XEP-0163 1.1 section 4.3.1
@ -5301,10 +5296,19 @@ itemsEls(Items) ->
#xmlel{name = <<"item">>, attrs = itemAttr(ItemId), children = Payload}
end, Items).
-spec(add_message_type/2 ::
(
Message :: xmlel(),
Type :: atom())
-> xmlel()
).
add_message_type(Message, normal) -> Message;
add_message_type(#xmlel{name = <<"message">>, attrs = Attrs, children = Els},
Type) ->
#xmlel{name = <<"message">>,
attrs = [{<<"type">>, Type} | Attrs], children = Els};
attrs = [{<<"type">>, jlib:atom_to_binary(Type)} | Attrs],
children = Els};
add_message_type(XmlEl, _Type) -> XmlEl.
%% Place of <headers/> changed at the bottom of the stanza

View File

@ -4091,10 +4091,7 @@ broadcast_stanza(Host, _Node, _NodeId, _Type, NodeOptions, SubsByDepth, NotifyTy
NotificationType = get_option(NodeOptions, notification_type, headline),
BroadcastAll = get_option(NodeOptions, broadcast_all_resources), %% XXX this is not standard, but usefull
From = service_jid(Host),
Stanza = case NotificationType of
normal -> BaseStanza;
MsgType -> add_message_type(BaseStanza, iolist_to_binary(atom_to_list(MsgType)))
end,
Stanza = add_message_type(BaseStanza, NotificationType),
%% Handles explicit subscriptions
SubIDsByJID = subscribed_nodes_by_jid(NotifyType, SubsByDepth),
lists:foreach(fun ({LJID, NodeName, SubIDs}) ->
@ -4126,10 +4123,8 @@ broadcast_stanza({LUser, LServer, LResource}, Publisher, Node, NodeId, Type, Nod
SenderResource = user_resource(LUser, LServer, LResource),
case ejabberd_sm:get_session_pid(LUser, LServer, SenderResource) of
C2SPid when is_pid(C2SPid) ->
Stanza = case get_option(NodeOptions, notification_type, headline) of
normal -> BaseStanza;
MsgType -> add_message_type(BaseStanza, iolist_to_binary(atom_to_list(MsgType)))
end,
NotificationType = get_option(NodeOptions, notification_type, headline),
Stanza = add_message_type(BaseStanza, NotificationType),
%% set the from address on the notification to the bare JID of the account owner
%% Also, add "replyto" if entity has presence subscription to the account owner
%% See XEP-0163 1.1 section 4.3.1
@ -4966,10 +4961,19 @@ itemsEls(Items) ->
#xmlel{name = <<"item">>, attrs = itemAttr(ItemId), children = Payload}
end, Items).
-spec(add_message_type/2 ::
(
Message :: xmlel(),
Type :: atom())
-> xmlel()
).
add_message_type(Message, normal) -> Message;
add_message_type(#xmlel{name = <<"message">>, attrs = Attrs, children = Els},
Type) ->
#xmlel{name = <<"message">>,
attrs = [{<<"type">>, Type} | Attrs], children = Els};
attrs = [{<<"type">>, jlib:atom_to_binary(Type)} | Attrs],
children = Els};
add_message_type(XmlEl, _Type) -> XmlEl.
%% Place of <headers/> changed at the bottom of the stanza