Bug 865992 - B2G MMS: the read count is wrong after updating the thread containing notification indication. r=vicamo a=leo+
authorGene Lian <clian@mozilla.com>
Fri, 26 Apr 2013 16:02:51 +0800
changeset 130055 1f53f6fc47f03d2ce894e74af5580e7e888d6391
parent 130054 dde0f2ca02d877b8c12fac0d8cb58878a386de84
child 130056 a197aa326c49f013de9a2019b605a74d544bd3ba
push id1552
push userttaubert@mozilla.com
push dateSat, 27 Apr 2013 15:33:29 +0000
treeherderfx-team@40dafc376794 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersvicamo, leo
bugs865992
milestone23.0a1
Bug 865992 - B2G MMS: the read count is wrong after updating the thread containing notification indication. r=vicamo a=leo+
dom/mobilemessage/src/ril/MobileMessageDatabaseService.js
--- a/dom/mobilemessage/src/ril/MobileMessageDatabaseService.js
+++ b/dom/mobilemessage/src/ril/MobileMessageDatabaseService.js
@@ -850,23 +850,20 @@ MobileMessageDatabaseService.prototype =
         }
         aCallback(threadRecord, participantIds);
       };
     });
   },
 
   saveRecord: function saveRecord(aMessageRecord, aAddresses, aCallback) {
     let isOverriding = (aMessageRecord.id !== undefined);
-    let oldThreadId = 0;
     if (!isOverriding) {
       // Assign a new id.
       this.lastMessageId += 1;
       aMessageRecord.id = this.lastMessageId;
-    } else {
-      oldThreadId = aMessageRecord.threadId;
     }
     if (DEBUG) debug("Going to store " + JSON.stringify(aMessageRecord));
 
     let self = this;
     function notifyResult(rv) {
       if (!aCallback) {
         return;
       }
@@ -905,29 +902,38 @@ MobileMessageDatabaseService.prototype =
           // Setup threadId & threadIdIndex.
           aMessageRecord.threadId = threadId;
           aMessageRecord.threadIdIndex = [threadId, timestamp];
           // Setup participantIdsIndex.
           aMessageRecord.participantIdsIndex = [];
           for each (let id in participantIds) {
             aMessageRecord.participantIdsIndex.push([id, timestamp]);
           }
-          // Really add to message store.
-          messageStore.put(aMessageRecord);
+
+          if (!isOverriding) {
+            // Really add to message store.
+            messageStore.put(aMessageRecord);
+            return;
+          }
 
-          // If the overriding message is going to be saved into another
-          // thread which is different from the original one containing the
-          // overrided message, we need to update the original thread info.
-          if (isOverriding && threadId != oldThreadId) {
-            self.updateThreadByMessageChange(messageStore,
-                                             threadStore,
-                                             oldThreadId,
-                                             aMessageRecord.id,
-                                             aMessageRecord.read);
-          }
+          // If we're going to override an old message, we need to update the
+          // info of the original thread containing the overridden message.
+          // To get the original thread ID and read status of the overridden
+          // message record, we need to retrieve it before overriding it.
+          messageStore.get(aMessageRecord.id).onsuccess = function(event) {
+            let oldMessageRecord = event.target.result;
+            messageStore.put(aMessageRecord);
+            if (oldMessageRecord) {
+              self.updateThreadByMessageChange(messageStore,
+                                               threadStore,
+                                               oldMessageRecord.threadId,
+                                               aMessageRecord.id,
+                                               oldMessageRecord.read);
+            }
+          };
         };
 
         let timestamp = aMessageRecord.timestamp;
         if (threadRecord) {
           let needsUpdate = false;
 
           if (threadRecord.lastTimestamp <= timestamp) {
             threadRecord.lastTimestamp = timestamp;