Bug 839352 - B2G SMS & B2G MMS: clean up the chaotic terminology for |record| and |message| in MobileMessageDatabaseService.js. r=vicamo

This commit is contained in:
Gene Lian 2013-02-08 17:37:51 +08:00
parent 2118fd0622
commit 398b90d22c

View File

@ -54,8 +54,8 @@ XPCOMUtils.defineLazyServiceGetter(this, "gIDBManager",
const GLOBAL_SCOPE = this;
function numberFromMessage(message) {
return message.delivery == DELIVERY_RECEIVED ? message.sender : message.receiver;
function getNumberFromRecord(aRecord) {
return aRecord.delivery == DELIVERY_RECEIVED ? aRecord.sender : aRecord.receiver;
}
/**
@ -293,10 +293,10 @@ MobileMessageDatabaseService.prototype = {
return;
}
let message = cursor.value;
message.messageClass = MESSAGE_CLASS_NORMAL;
message.deliveryStatus = DELIVERY_STATUS_NOT_APPLICABLE;
cursor.update(message);
let record = cursor.value;
record.messageClass = MESSAGE_CLASS_NORMAL;
record.deliveryStatus = DELIVERY_STATUS_NOT_APPLICABLE;
cursor.update(record);
cursor.continue();
};
},
@ -338,26 +338,26 @@ MobileMessageDatabaseService.prototype = {
return;
}
let message = cursor.value;
let contact = message.sender || message.receiver;
let record = cursor.value;
let contact = record.sender || record.receiver;
if (contact in threads) {
let thread = threads[contact];
if (!message.read) {
if (!record.read) {
thread.unreadCount++;
}
if (message.timestamp > thread.timestamp) {
thread.id = message.id;
thread.body = message.body;
thread.timestamp = message.timestamp;
if (record.timestamp > thread.timestamp) {
thread.id = record.id;
thread.body = record.body;
thread.timestamp = record.timestamp;
}
} else {
threads[contact] = {
senderOrReceiver: contact,
id: message.id,
timestamp: message.timestamp,
body: message.body,
unreadCount: message.read ? 0 : 1
id: record.id,
timestamp: record.timestamp,
body: record.body,
unreadCount: record.read ? 0 : 1
};
}
cursor.continue();
@ -400,30 +400,30 @@ MobileMessageDatabaseService.prototype = {
return;
}
let message = cursor.value;
let timestamp = message.timestamp;
message.deliveryIndex = [message.delivery, timestamp];
message.numberIndex = [
[message.sender, timestamp],
[message.receiver, timestamp]
let record = cursor.value;
let timestamp = record.timestamp;
record.deliveryIndex = [record.delivery, timestamp];
record.numberIndex = [
[record.sender, timestamp],
[record.receiver, timestamp]
];
message.readIndex = [message.read, timestamp];
cursor.update(message);
record.readIndex = [record.read, timestamp];
cursor.update(record);
cursor.continue();
};
},
createMessageFromRecord: function createMessageFromRecord(record) {
if (DEBUG) debug("createMessageFromRecord: " + JSON.stringify(record));
return gSmsService.createSmsMessage(record.id,
record.delivery,
record.deliveryStatus,
record.sender,
record.receiver,
record.body,
record.messageClass,
record.timestamp,
record.read);
createMessageFromRecord: function createMessageFromRecord(aRecord) {
if (DEBUG) debug("createMessageFromRecord: " + JSON.stringify(aRecord));
return gSmsService.createSmsMessage(aRecord.id,
aRecord.delivery,
aRecord.deliveryStatus,
aRecord.sender,
aRecord.receiver,
aRecord.body,
aRecord.messageClass,
aRecord.timestamp,
aRecord.read);
},
/**
@ -647,18 +647,18 @@ MobileMessageDatabaseService.prototype = {
return false;
},
saveMessage: function saveMessage(message, callback) {
saveRecord: function saveRecord(aRecord, aCallback) {
this.lastKey += 1;
message.id = this.lastKey;
if (DEBUG) debug("Going to store " + JSON.stringify(message));
aRecord.id = this.lastKey;
if (DEBUG) debug("Going to store " + JSON.stringify(aRecord));
let self = this;
function notifyResult(rv) {
if (!callback) {
if (!aCallback) {
return;
}
let sms = self.createMessageFromRecord(message);
callback.notify(rv, sms);
let sms = self.createMessageFromRecord(aRecord);
aCallback.notify(rv, sms);
}
this.newTxn(READ_WRITE, function(error, txn, stores) {
@ -676,9 +676,9 @@ MobileMessageDatabaseService.prototype = {
};
// First add to main objectStore.
stores[0].put(message);
stores[0].put(aRecord);
let number = numberFromMessage(message);
let number = getNumberFromRecord(aRecord);
// Next update the other objectStore.
stores[1].get(number).onsuccess = function onsuccess(event) {
@ -686,13 +686,13 @@ MobileMessageDatabaseService.prototype = {
if (mostRecentEntry) {
let needsUpdate = false;
if (mostRecentEntry.timestamp <= message.timestamp) {
mostRecentEntry.timestamp = message.timestamp;
mostRecentEntry.body = message.body;
if (mostRecentEntry.timestamp <= aRecord.timestamp) {
mostRecentEntry.timestamp = aRecord.timestamp;
mostRecentEntry.body = aRecord.body;
needsUpdate = true;
}
if (!message.read) {
if (!aRecord.read) {
mostRecentEntry.unreadCount++;
needsUpdate = true;
}
@ -702,15 +702,15 @@ MobileMessageDatabaseService.prototype = {
}
} else {
event.target.source.add({ senderOrReceiver: number,
timestamp: message.timestamp,
body: message.body,
id: message.id,
unreadCount: message.read ? 0 : 1 });
timestamp: aRecord.timestamp,
body: aRecord.body,
id: aRecord.id,
unreadCount: aRecord.read ? 0 : 1 });
}
};
}, [STORE_NAME, MOST_RECENT_STORE_NAME]);
// We return the key that we expect to store in the db
return message.id;
return aRecord.id;
},
@ -744,7 +744,7 @@ MobileMessageDatabaseService.prototype = {
: sender;
}
let message = {
let record = {
deliveryIndex: [DELIVERY_RECEIVED, aDate],
numberIndex: [[sender, aDate], [receiver, aDate]],
readIndex: [FILTER_READ_UNREAD, aDate],
@ -758,7 +758,7 @@ MobileMessageDatabaseService.prototype = {
timestamp: aDate,
read: FILTER_READ_UNREAD
};
return this.saveMessage(message, aCallback);
return this.saveRecord(record, aCallback);
},
saveSendingMessage: function saveSendingMessage(
@ -792,7 +792,7 @@ MobileMessageDatabaseService.prototype = {
}
}
let message = {
let record = {
deliveryIndex: [DELIVERY_SENDING, aDate],
numberIndex: [[sender, aDate], [receiver, aDate]],
readIndex: [FILTER_READ_READ, aDate],
@ -806,7 +806,7 @@ MobileMessageDatabaseService.prototype = {
timestamp: aDate,
read: FILTER_READ_READ
};
return this.saveMessage(message, aCallback);
return this.saveRecord(record, aCallback);
},
setMessageDelivery: function setMessageDelivery(
@ -817,14 +817,14 @@ MobileMessageDatabaseService.prototype = {
}
let self = this;
let message;
let record;
function notifyResult(rv) {
if (!callback) {
return;
}
let sms = null;
if (message) {
sms = self.createMessageFromRecord(message);
if (record) {
sms = self.createMessageFromRecord(record);
}
callback.notify(rv, sms);
}
@ -845,12 +845,12 @@ MobileMessageDatabaseService.prototype = {
let getRequest = store.get(messageId);
getRequest.onsuccess = function onsuccess(event) {
message = event.target.result;
if (!message) {
record = event.target.result;
if (!record) {
if (DEBUG) debug("Message ID " + messageId + " not found");
return;
}
if (message.id != messageId) {
if (record.id != messageId) {
if (DEBUG) {
debug("Retrieve message ID (" + messageId + ") is " +
"different from the one we got");
@ -858,22 +858,22 @@ MobileMessageDatabaseService.prototype = {
return;
}
// Only updates messages that have different delivery or deliveryStatus.
if ((message.delivery == delivery)
&& (message.deliveryStatus == deliveryStatus)) {
if ((record.delivery == delivery)
&& (record.deliveryStatus == deliveryStatus)) {
if (DEBUG) {
debug("The values of attribute delivery and deliveryStatus are the"
+ " the same with given parameters.");
}
return;
}
message.delivery = delivery;
message.deliveryIndex = [delivery, message.timestamp];
message.deliveryStatus = deliveryStatus;
record.delivery = delivery;
record.deliveryIndex = [delivery, record.timestamp];
record.deliveryStatus = deliveryStatus;
if (DEBUG) {
debug("Message.delivery set to: " + delivery
+ ", and Message.deliveryStatus set to: " + deliveryStatus);
}
store.put(message);
store.put(record);
};
});
},
@ -900,13 +900,13 @@ MobileMessageDatabaseService.prototype = {
aRequest.notifyGetMessageFailed(Ci.nsISmsRequest.UNKNOWN_ERROR);
return;
}
let data = request.result[0];
if (!data) {
let record = request.result[0];
if (!record) {
if (DEBUG) debug("Message ID " + messageId + " not found");
aRequest.notifyGetMessageFailed(Ci.nsISmsRequest.NOT_FOUND_ERROR);
return;
}
if (data.id != messageId) {
if (record.id != messageId) {
if (DEBUG) {
debug("Requested message ID (" + messageId + ") is " +
"different from the one we got");
@ -914,7 +914,7 @@ MobileMessageDatabaseService.prototype = {
aRequest.notifyGetMessageFailed(Ci.nsISmsRequest.UNKNOWN_ERROR);
return;
}
let sms = self.createMessageFromRecord(data);
let sms = self.createMessageFromRecord(record);
aRequest.notifyMessageGot(sms);
};
@ -954,8 +954,8 @@ MobileMessageDatabaseService.prototype = {
};
mobileMessageStore.get(messageId).onsuccess = function(event) {
let message = event.target.result;
if (message) {
let record = event.target.result;
if (record) {
if (DEBUG) debug("Deleting message id " + messageId);
// First actually delete the message.
@ -963,13 +963,13 @@ MobileMessageDatabaseService.prototype = {
deleted = true;
// Then update unread count and most recent message.
let number = numberFromMessage(message);
let number = getNumberFromRecord(record);
mruStore.get(number).onsuccess = function(event) {
// This must exist.
let mostRecentEntry = event.target.result;
if (!message.read) {
if (!record.read) {
mostRecentEntry.unreadCount--;
}
@ -998,7 +998,7 @@ MobileMessageDatabaseService.prototype = {
}
mruStore.put(mostRecentEntry);
};
} else if (!message.read) {
} else if (!record.read) {
// Shortcut, just update the unread count.
if (DEBUG) {
debug("Updating unread count for number '" + number + "': " +
@ -1316,18 +1316,18 @@ MobileMessageDatabaseService.prototype = {
this.newTxn(READ_ONLY, function (error, txn, store) {
if (DEBUG) debug("Fetching message " + messageId);
let request = store.get(messageId);
let message;
let record;
request.onsuccess = function onsuccess(event) {
message = request.result;
record = request.result;
};
txn.oncomplete = function oncomplete(event) {
if (DEBUG) debug("Transaction " + txn + " completed.");
if (!message) {
if (!record) {
if (DEBUG) debug("Could not get message id " + messageId);
aRequest.notifyReadMessageListFailed(Ci.nsISmsRequest.NOT_FOUND_ERROR);
}
let sms = self.createMessageFromRecord(message);
let sms = self.createMessageFromRecord(record);
aRequest.notifyNextMessageInListGot(sms);
};
@ -1363,13 +1363,13 @@ MobileMessageDatabaseService.prototype = {
aRequest.notifyMarkMessageReadFailed(Ci.nsISmsRequest.INTERNAL_ERROR);
};
stores[0].get(messageId).onsuccess = function onsuccess(event) {
let message = event.target.result;
if (!message) {
let record = event.target.result;
if (!record) {
if (DEBUG) debug("Message ID " + messageId + " not found");
aRequest.notifyMarkMessageReadFailed(Ci.nsISmsRequest.NOT_FOUND_ERROR);
return;
}
if (message.id != messageId) {
if (record.id != messageId) {
if (DEBUG) {
debug("Retrieve message ID (" + messageId + ") is " +
"different from the one we got");
@ -1379,22 +1379,22 @@ MobileMessageDatabaseService.prototype = {
}
// If the value to be set is the same as the current message `read`
// value, we just notify successfully.
if (message.read == value) {
if (DEBUG) debug("The value of message.read is already " + value);
aRequest.notifyMessageMarkedRead(message.read);
if (record.read == value) {
if (DEBUG) debug("The value of record.read is already " + value);
aRequest.notifyMessageMarkedRead(record.read);
return;
}
message.read = value ? FILTER_READ_READ : FILTER_READ_UNREAD;
message.readIndex = [message.read, message.timestamp];
record.read = value ? FILTER_READ_READ : FILTER_READ_UNREAD;
record.readIndex = [record.read, record.timestamp];
if (DEBUG) debug("Message.read set to: " + value);
event.target.source.put(message).onsuccess = function onsuccess(event) {
event.target.source.put(record).onsuccess = function onsuccess(event) {
if (DEBUG) {
debug("Update successfully completed. Message: " +
JSON.stringify(event.target.result));
}
// Now update the unread count.
let number = numberFromMessage(message);
let number = getNumberFromRecord(record);
stores[1].get(number).onsuccess = function(event) {
let mostRecentEntry = event.target.result;
@ -1407,7 +1407,7 @@ MobileMessageDatabaseService.prototype = {
" -> " + mostRecentEntry.unreadCount);
}
event.target.source.put(mostRecentEntry).onsuccess = function(event) {
aRequest.notifyMessageMarkedRead(message.read);
aRequest.notifyMessageMarkedRead(record.read);
};
};
};