DB/Index Redux
This change removes the timestamp field from messages and conversations in favor of multiple semantically named timestamp fields: sent_at, received_at on messages; active_at on conversations. This requires/lets us rethink and improve our indexing scheme thusly: The inbox index on conversations will order entries by the conversation.active_at property, which should only appear on conversations destined for the inbox. The receipt index will use the message.sent_at property, for effecient lookup of outgoing messages by timestamp, for use in processing delivery receipts. The group index on conversation.members is multi-entry, meaning that looking up any phone number in this index will efficiently yield all groups the number belongs to. The conversation index lets us scan messages in a single conversation, in the order they were received (or the reverse order). It is a compound index on [conversationId, received_at].
This commit is contained in:
parent
9c736df7d0
commit
006653ed8e
5 changed files with 58 additions and 35 deletions
|
@ -22,12 +22,13 @@
|
|||
database: Whisper.Database,
|
||||
storeName: 'conversations',
|
||||
defaults: function() {
|
||||
var timestamp = new Date().getTime();
|
||||
return {
|
||||
name: 'New Conversation',
|
||||
image: '/images/default.png',
|
||||
unreadCount: 0,
|
||||
timestamp: new Date().getTime(),
|
||||
active: true
|
||||
name : 'New Conversation',
|
||||
image : '/images/default.png',
|
||||
unreadCount : 0,
|
||||
timestamp : timestamp,
|
||||
active_at : timestamp
|
||||
};
|
||||
},
|
||||
|
||||
|
@ -42,18 +43,20 @@
|
|||
},
|
||||
|
||||
sendMessage: function(message, attachments) {
|
||||
var timestamp = Date.now();
|
||||
var now = Date.now();
|
||||
this.messageCollection.add({
|
||||
body : message,
|
||||
timestamp : timestamp,
|
||||
conversationId : this.id,
|
||||
type : 'outgoing',
|
||||
attachments : attachments,
|
||||
body : message,
|
||||
conversationId : this.id,
|
||||
type : 'outgoing',
|
||||
attachments : attachments,
|
||||
sent_at : now,
|
||||
received_at : now
|
||||
}).save();
|
||||
|
||||
this.save({ timestamp: timestamp,
|
||||
unreadCount: 0,
|
||||
active: true});
|
||||
this.save({
|
||||
unreadCount : 0,
|
||||
active_at : now
|
||||
});
|
||||
|
||||
if (this.get('type') == 'private') {
|
||||
return textsecure.messaging.sendMessageToNumber(this.get('id'), message, attachments);
|
||||
|
@ -85,15 +88,29 @@
|
|||
|
||||
fetchMessages: function(options) {
|
||||
options = options || {};
|
||||
options.conditions = {conversationId: this.id };
|
||||
options.index = {
|
||||
// 'conversation' index on conversationId
|
||||
// WHERE conversationId = this.id ORDER received_at DESC
|
||||
name : 'conversation',
|
||||
lower : [this.id],
|
||||
upper : [this.id, Number.MAX_VALUE],
|
||||
order : 'desc'
|
||||
};
|
||||
return this.messageCollection.fetch(options);
|
||||
// TODO pagination/infinite scroll
|
||||
// limit: 10, offset: page*10,
|
||||
},
|
||||
|
||||
archive: function() {
|
||||
this.unset('active_at');
|
||||
},
|
||||
|
||||
destroyMessages: function() {
|
||||
var models = this.messageCollection.models;
|
||||
this.messageCollection.reset([]);
|
||||
_.each(models, function(message) { message.destroy(); });
|
||||
return this.save({active: false});
|
||||
this.archive();
|
||||
return this.save();
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -110,7 +127,7 @@
|
|||
var attributes = {};
|
||||
attributes = {
|
||||
name : name,
|
||||
numbers : recipients,
|
||||
members : recipients,
|
||||
type : 'group',
|
||||
};
|
||||
var conversation = this.add(attributes, {merge: true});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue