]> granicus.if.org Git - ejabberd/commitdiff
fix EJAB-1048, add timestamp to last published items
authorChristophe Romain <christophe.romain@process-one.net>
Wed, 23 Sep 2009 09:27:12 +0000 (09:27 +0000)
committerChristophe Romain <christophe.romain@process-one.net>
Wed, 23 Sep 2009 09:27:12 +0000 (09:27 +0000)
SVN Revision: 2611

src/mod_pubsub/mod_pubsub.erl
src/mod_pubsub/mod_pubsub_odbc.erl
src/mod_pubsub/pubsub_odbc.patch

index d8475f749feabcbb74cabaab2533a91932e85572..c2b1b7583ad7e2d1b61fe4900c0f1b3d52050ff0 100644 (file)
@@ -2265,9 +2265,10 @@ send_items(Host, Node, NodeId, Type, LJID, last) ->
        undefined ->
            send_items(Host, Node, NodeId, Type, LJID, 1);
        LastItem ->
-           Stanza = event_stanza(
+           {ModifNow, ModifLjid} = LastItem#pubsub_item.modification,
+           Stanza = event_stanza_with_delay(
                [{xmlelement, "items", nodeAttr(Node),
-               itemsEls([LastItem])}]),
+                 itemsEls([LastItem])}], ModifNow, ModifLjid),
            ejabberd_router ! {route, service_jid(Host), jlib:make_jid(LJID), Stanza}
     end;
 send_items(Host, Node, NodeId, Type, LJID, Number) ->
@@ -2282,9 +2283,17 @@ send_items(Host, Node, NodeId, Type, LJID, Number) ->
        _ ->
            []
     end,
-    Stanza = event_stanza(
-       [{xmlelement, "items", nodeAttr(Node),
-         itemsEls(ToSend)}]),
+    Stanza = case ToSend of
+       [LastItem] ->
+           {ModifNow, ModifLjid} = LastItem#pubsub_item.modification,
+           event_stanza_with_delay(
+               [{xmlelement, "items", nodeAttr(Node),
+                 itemsEls(ToSend)}], ModifNow, ModifLjid);
+       _ ->
+           event_stanza(
+               [{xmlelement, "items", nodeAttr(Node),
+                 itemsEls(ToSend)}])
+    end,
     ejabberd_router ! {route, service_jid(Host), jlib:make_jid(LJID), Stanza}.
 
 %% @spec (Host, JID, Plugins) -> {error, Reason} | {result, Response}
@@ -2837,8 +2846,16 @@ payload_xmlelements([_|Tail], Count) -> payload_xmlelements(Tail, Count).
 %%     Els = [xmlelement()]
 %% @doc <p>Build pubsub event stanza</p>
 event_stanza(Els) ->
+    event_stanza_withmoreels(Els, []).
+
+event_stanza_with_delay(Els, ModifNow, ModifLjid) ->
+    DateTime = calendar:now_to_datetime(ModifNow),
+    MoreEls = [jlib:timestamp_to_xml(DateTime, utc, ModifLjid, "")],
+    event_stanza_withmoreels(Els, MoreEls).
+
+event_stanza_withmoreels(Els, MoreEls) ->
     {xmlelement, "message", [],
-     [{xmlelement, "event", [{"xmlns", ?NS_PUBSUB_EVENT}], Els}]}.
+     [{xmlelement, "event", [{"xmlns", ?NS_PUBSUB_EVENT}], Els} | MoreEls]}.
 
 %%%%%% broadcast functions
 
index b6b2541720f176aea5f9f892c0ec622d462b0b93..3c4021e3af11e4e1622eebeefff24e0d42451411 100644 (file)
@@ -2100,17 +2100,22 @@ send_items(Host, Node, NodeId, Type, LJID, last) ->
     Stanza = case get_cached_item(Host, NodeId) of
        undefined ->
            % special ODBC optimization, works only with node_hometree_odbc, node_flat_odbc and node_pep_odbc
-           ToSend = case node_action(Host, Type, get_last_items, [NodeId, LJID, 1]) of
-               {result, []} -> [];
-               {result, Items} -> Items
-           end,
-           event_stanza(
-               [{xmlelement, "items", nodeAttr(Node),
-               itemsEls(ToSend)}]);
+           case node_action(Host, Type, get_last_items, [NodeId, LJID, 1]) of
+               {result, [LastItem]} ->
+                   {ModifNow, ModifLjid} = LastItem#pubsub_item.modification,
+                   event_stanza_with_delay(
+                       [{xmlelement, "items", nodeAttr(Node),
+                         itemsEls([LastItem])}], ModifNow, ModifLjid);
+               _ ->
+                   event_stanza(
+                       [{xmlelement, "items", nodeAttr(Node),
+                         itemsEls([])}])
+           end;
        LastItem ->
-           event_stanza(
+           {ModifNow, ModifLjid} = LastItem#pubsub_item.modification,
+           event_stanza_with_delay(
                [{xmlelement, "items", nodeAttr(Node),
-               itemsEls([LastItem])}])
+                 itemsEls([LastItem])}], ModifNow, ModifLjid)
     end,
     ejabberd_router ! {route, service_jid(Host), jlib:make_jid(LJID), Stanza};
 send_items(Host, Node, NodeId, Type, LJID, Number) ->
@@ -2125,9 +2130,17 @@ send_items(Host, Node, NodeId, Type, LJID, Number) ->
        _ ->
            []
     end,
-    Stanza = event_stanza(
-       [{xmlelement, "items", nodeAttr(Node),
-         itemsEls(ToSend)}]),
+    Stanza = case ToSend of
+       [LastItem] ->
+           {ModifNow, ModifLjid} = LastItem#pubsub_item.modification,
+           event_stanza_with_delay(
+               [{xmlelement, "items", nodeAttr(Node),
+                 itemsEls(ToSend)}], ModifNow, ModifLjid);
+       _ ->
+           event_stanza(
+               [{xmlelement, "items", nodeAttr(Node),
+                 itemsEls(ToSend)}])
+    end,
     ejabberd_router ! {route, service_jid(Host), jlib:make_jid(LJID), Stanza}.
 
 %% @spec (Host, JID, Plugins) -> {error, Reason} | {result, Response}
@@ -2663,8 +2676,16 @@ payload_xmlelements([_|Tail], Count) -> payload_xmlelements(Tail, Count).
 %%     Els = [xmlelement()]
 %% @doc <p>Build pubsub event stanza</p>
 event_stanza(Els) ->
+    event_stanza_withmoreels(Els, []).
+
+event_stanza_with_delay(Els, ModifNow, ModifLjid) ->
+    DateTime = calendar:now_to_datetime(ModifNow),
+    MoreEls = [jlib:timestamp_to_xml(DateTime, utc, ModifLjid, "")],
+    event_stanza_withmoreels(Els, MoreEls).
+
+event_stanza_withmoreels(Els, MoreEls) ->
     {xmlelement, "message", [],
-     [{xmlelement, "event", [{"xmlns", ?NS_PUBSUB_EVENT}], Els}]}.
+     [{xmlelement, "event", [{"xmlns", ?NS_PUBSUB_EVENT}], Els} | MoreEls]}.
 
 %%%%%% broadcast functions
 
index 36adf1b7d0eb694ee98d11057a456b2ba76e59bb..c4da735ba9d3c5793f889f706cb48f881526b86b 100644 (file)
@@ -1,5 +1,5 @@
---- mod_pubsub.erl     2009-09-09 21:00:22.000000000 +0200
-+++ mod_pubsub_odbc.erl        2009-09-09 23:14:58.000000000 +0200
+--- mod_pubsub.erl     2009-09-23 11:13:35.000000000 +0200
++++ mod_pubsub_odbc.erl        2009-09-23 11:25:11.000000000 +0200
 @@ -45,7 +45,7 @@
  %%% TODO
  %%% plugin: generate Reply (do not use broadcast atom anymore)
                Error ->
                    Error
            end
-@@ -2261,15 +2097,22 @@
+@@ -2261,16 +2097,27 @@
  %% @doc <p>Resend the items of a node to the user.</p>
  %% @todo use cache-last-item feature
  send_items(Host, Node, NodeId, Type, LJID, last) ->
        undefined ->
 -          send_items(Host, Node, NodeId, Type, LJID, 1);
 +          % special ODBC optimization, works only with node_hometree_odbc, node_flat_odbc and node_pep_odbc
-+          ToSend = case node_action(Host, Type, get_last_items, [NodeId, LJID, 1]) of
-+              {result, []} -> [];
-+              {result, Items} -> Items
-+          end,
-+          event_stanza(
-+              [{xmlelement, "items", nodeAttr(Node),
-+              itemsEls(ToSend)}]);
++          case node_action(Host, Type, get_last_items, [NodeId, LJID, 1]) of
++              {result, [LastItem]} ->
++                  {ModifNow, ModifLjid} = LastItem#pubsub_item.modification,
++                  event_stanza_with_delay(
++                      [{xmlelement, "items", nodeAttr(Node),
++                        itemsEls([LastItem])}], ModifNow, ModifLjid);
++              _ ->
++                  event_stanza(
++                      [{xmlelement, "items", nodeAttr(Node),
++                        itemsEls([])}])
++          end;
        LastItem ->
--          Stanza = event_stanza(
-+          event_stanza(
+           {ModifNow, ModifLjid} = LastItem#pubsub_item.modification,
+-          Stanza = event_stanza_with_delay(
++          event_stanza_with_delay(
                [{xmlelement, "items", nodeAttr(Node),
--              itemsEls([LastItem])}]),
+-                itemsEls([LastItem])}], ModifNow, ModifLjid),
 -          ejabberd_router ! {route, service_jid(Host), jlib:make_jid(LJID), Stanza}
 -    end;
-+              itemsEls([LastItem])}])
++                itemsEls([LastItem])}], ModifNow, ModifLjid)
 +    end,
 +    ejabberd_router ! {route, service_jid(Host), jlib:make_jid(LJID), Stanza};
  send_items(Host, Node, NodeId, Type, LJID, Number) ->
      ToSend = case node_action(Host, Type, get_items, [NodeId, LJID]) of
        {result, []} -> 
-@@ -2387,29 +2230,12 @@
+@@ -2396,29 +2243,12 @@
        error ->
            {error, ?ERR_BAD_REQUEST};
        _ ->
                                    end, Entities),
                                {result, []};
                            _ ->
-@@ -2462,11 +2288,11 @@
+@@ -2471,11 +2301,11 @@
      end.
  
  read_sub(Subscriber, Node, NodeID, SubID, Lang) ->
            OptionsEl = {xmlelement, "options", [{"node", node_to_string(Node)},
                                                 {"jid", jlib:jid_to_string(Subscriber)},
                                                 {"subid", SubID}],
-@@ -2497,7 +2323,7 @@
+@@ -2506,7 +2336,7 @@
                     error -> {"", "", ""};
                     J -> jlib:jid_tolower(J)
                 end,
      {result, Subs} = node_call(Type, get_subscriptions,
                               [NodeID, Subscriber]),
      SubIDs = lists:foldl(fun({subscribed, SID}, Acc) ->
-@@ -2517,7 +2343,7 @@
+@@ -2526,7 +2356,7 @@
      end.
  
  write_sub(Subscriber, NodeID, SubID, Options) ->
        {error, notfound} ->
            {error, ?ERR_EXTENDED(?ERR_NOT_ACCEPTABLE, "invalid-subid")};
        {result, _} ->
-@@ -2685,8 +2511,8 @@
+@@ -2694,8 +2524,8 @@
                                     {"subscription", subscription_to_string(Sub)} | nodeAttr(Node)], []}]}]},
                ejabberd_router ! {route, service_jid(Host), jlib:make_jid(JID), Stanza}
            end,
                                true ->
                                    Result = lists:foldl(fun({JID, Subscription, SubId}, Acc) ->
  
-@@ -2968,7 +2794,7 @@
+@@ -2985,7 +2815,7 @@
                        {Depth, [{N, get_node_subs(N)} || N <- Nodes]}
            end, tree_call(Host, get_parentnodes_tree, [Host, Node, service_jid(Host)]))}
        end,
        {result, CollSubs} -> CollSubs;
        _ -> []
      end.
-@@ -2982,9 +2808,9 @@
+@@ -2999,9 +2829,9 @@
  
  get_options_for_subs(NodeID, Subs) ->
      lists:foldl(fun({JID, subscribed, SubID}, Acc) ->
                            _ -> Acc
                        end;
                    (_, Acc) ->
-@@ -3178,6 +3004,30 @@
+@@ -3195,6 +3025,30 @@
            Result
      end.
  
  %% @spec (Host, Options) -> MaxItems
  %%     Host = host()
  %%     Options = [Option]
-@@ -3551,7 +3401,13 @@
+@@ -3568,7 +3422,13 @@
  tree_action(Host, Function, Args) ->
      ?DEBUG("tree_action ~p ~p ~p",[Host,Function,Args]),
      Fun = fun() -> tree_call(Host, Function, Args) end,
  
  %% @doc <p>node plugin call.</p>
  node_call(Type, Function, Args) ->
-@@ -3571,13 +3427,13 @@
+@@ -3588,13 +3448,13 @@
  
  node_action(Host, Type, Function, Args) ->
      ?DEBUG("node_action ~p ~p ~p ~p",[Host,Type,Function,Args]),
                        case tree_call(Host, get_node, [Host, Node]) of
                            N when is_record(N, pubsub_node) ->
                                case Action(N) of
-@@ -3590,8 +3446,14 @@
+@@ -3607,8 +3467,14 @@
                        end
                end, Trans).
  
        {result, Result} -> {result, Result};
        {error, Error} -> {error, Error};
        {atomic, {result, Result}} -> {result, Result};
-@@ -3599,6 +3461,15 @@
+@@ -3616,6 +3482,15 @@
        {aborted, Reason} ->
            ?ERROR_MSG("transaction return internal error: ~p~n", [{aborted, Reason}]),
            {error, ?ERR_INTERNAL_SERVER_ERROR};
        {'EXIT', Reason} ->
            ?ERROR_MSG("transaction return internal error: ~p~n", [{'EXIT', Reason}]),
            {error, ?ERR_INTERNAL_SERVER_ERROR};
-@@ -3607,6 +3478,17 @@
+@@ -3624,6 +3499,17 @@
            {error, ?ERR_INTERNAL_SERVER_ERROR}
      end.