Bug 1289110 - Handle delayed delivery for presence stanzas. r=aleth
authorAbdelrhman Ahmed <ab@abahmed.com>
Tue, 26 Jul 2016 02:21:26 +0200
changeset 25414 2a04b47cf89ae2075bdb0e88b79428e366c24d90
parent 25413 c229bf0c849e1bf474be885cf2288c01340bd879
child 25415 9b3fd23abcf9f4eb33dbb757645e0679887c5804
push id1725
push userclokep@gmail.com
push dateMon, 19 Sep 2016 17:35:08 +0000
treeherdercomm-beta@6ead1abf3817 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaleth
bugs1289110
Bug 1289110 - Handle delayed delivery for presence stanzas. r=aleth
chat/protocols/xmpp/xmpp.jsm
--- a/chat/protocols/xmpp/xmpp.jsm
+++ b/chat/protocols/xmpp/xmpp.jsm
@@ -55,16 +55,20 @@ function parseStatus(aStanza) {
       statusType = Ci.imIStatusInfo.STATUS_AVAILABLE; //FIXME
     else if (show == "dnd")
       statusType = Ci.imIStatusInfo.STATUS_UNAVAILABLE;
     else if (show == "xa")
       statusType = Ci.imIStatusInfo.STATUS_IDLE;
   }
 
   let idleSince = 0;
+  let date = _getDelay(aStanza);
+  if (date)
+    idleSince = date.getTime();
+
   let query = aStanza.getElement(["query"]);
   if (query && query.uri == Stanza.NS.last) {
     let now = Math.floor(Date.now() / 1000);
     idleSince = now - parseInt(query.attributes["seconds"], 10);
     statusType = Ci.imIStatusInfo.STATUS_IDLE;
   }
 
   // Mark official Android clients as mobile.
@@ -75,16 +79,32 @@ function parseStatus(aStanza) {
     statusType = Ci.imIStatusInfo.STATUS_MOBILE;
 
   let status = aStanza.getElement(["status"]);
   status = status ? status.innerText : "";
 
   return {statusType: statusType, statusText: status, idleSince: idleSince};
 };
 
+// Returns a Date object for the delay value (stamp) in aStanza if it exists,
+// otherwise returns undefined.
+function _getDelay(aStanza) {
+  // XEP-0203: Delayed Delivery.
+  let date;
+  let delay = aStanza.getElement(["delay"]);
+  if (delay && delay.uri == Stanza.NS.delay) {
+    if (delay.attributes["stamp"])
+      date = new Date(delay.attributes["stamp"]);
+  }
+  if (date && isNaN(date.getTime()))
+    return undefined;
+
+  return date;
+}
+
 // The timespan after which we consider roomInfo to be stale.
 var kListRefreshInterval = 12 * 60 * 60 * 1000; // 12 hours.
 
 /* This is an ordered list, used to determine chat buddy flags:
  *  index < member    -> noFlags
  *  index = member    -> voiced
  *          moderator -> halfOp
  *          admin     -> op
@@ -1983,24 +2003,17 @@ var XMPPAccountPrototype = {
         // Otherwise, just notify the user.
         let conv = this.createConversation(invitation.from);
         if (conv)
           conv.writeMessage(invitation.from, message, {system: true});
       }
     }
 
     if (body) {
-      let date;
-      let delay = aStanza.getElement(["delay"]);
-      if (delay && delay.uri == Stanza.NS.delay) {
-        if (delay.attributes["stamp"])
-          date = new Date(delay.attributes["stamp"]);
-      }
-      if (date && isNaN(date))
-        date = undefined;
+      let date = _getDelay(aStanza);
       if (type == "groupchat" ||
           (type == "error" && isMuc && !this._conv.has(from))) {
         if (!isMuc) {
           this.WARN("Received a groupchat message for unknown MUC " + norm);
           return;
         }
         let muc = this._mucs.get(norm);
         muc.incomingMessage(body, aStanza, date);