2015-09-07 23:53:43 +02:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2014-11-13 23:35:37 +01:00
|
|
|
*/
|
2014-05-12 02:13:09 +02:00
|
|
|
(function () {
|
2014-11-13 23:35:37 +01:00
|
|
|
'use strict';
|
|
|
|
window.Whisper = window.Whisper || {};
|
2014-05-12 02:13:09 +02:00
|
|
|
|
2015-05-21 00:52:25 +02:00
|
|
|
var Message = window.Whisper.Message = Backbone.Model.extend({
|
2014-12-12 04:41:40 +01:00
|
|
|
database : Whisper.Database,
|
|
|
|
storeName : 'messages',
|
2015-09-14 04:42:49 +02:00
|
|
|
initialize: function() {
|
|
|
|
this.on('change:attachments', this.updateImageUrl);
|
|
|
|
this.on('destroy', this.revokeImageUrl);
|
|
|
|
},
|
2014-12-12 04:41:40 +01:00
|
|
|
defaults : function() {
|
2014-11-21 00:43:51 +01:00
|
|
|
return {
|
|
|
|
timestamp: new Date().getTime(),
|
|
|
|
attachments: []
|
|
|
|
};
|
|
|
|
},
|
2014-11-13 23:35:37 +01:00
|
|
|
validate: function(attributes, options) {
|
2014-12-12 04:41:40 +01:00
|
|
|
var required = ['conversationId', 'received_at', 'sent_at'];
|
2014-11-13 23:35:37 +01:00
|
|
|
var missing = _.filter(required, function(attr) { return !attributes[attr]; });
|
|
|
|
if (missing.length) {
|
|
|
|
console.log("Message missing attributes: " + missing);
|
|
|
|
}
|
2015-02-17 20:54:15 +01:00
|
|
|
},
|
|
|
|
isEndSession: function() {
|
2015-06-01 23:08:21 +02:00
|
|
|
var flag = textsecure.protobuf.DataMessage.Flags.END_SESSION;
|
2015-02-17 20:54:15 +01:00
|
|
|
return !!(this.get('flags') & flag);
|
|
|
|
},
|
|
|
|
isGroupUpdate: function() {
|
|
|
|
return !!(this.get('group_update'));
|
2015-02-25 01:02:33 +01:00
|
|
|
},
|
|
|
|
isIncoming: function() {
|
|
|
|
return this.get('type') === 'incoming';
|
2015-03-12 01:49:01 +01:00
|
|
|
},
|
2015-03-20 00:17:26 +01:00
|
|
|
getDescription: function() {
|
|
|
|
if (this.isGroupUpdate()) {
|
2015-03-23 23:44:47 +01:00
|
|
|
var group_update = this.get('group_update');
|
|
|
|
if (group_update.left) {
|
|
|
|
return group_update.left + ' left the group.';
|
|
|
|
}
|
|
|
|
|
|
|
|
var messages = ['Updated the group.'];
|
|
|
|
if (group_update.name) {
|
|
|
|
messages.push("Title is now '" + group_update.name + "'.");
|
|
|
|
}
|
|
|
|
if (group_update.joined) {
|
|
|
|
messages.push(group_update.joined.join(', ') + ' joined the group.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return messages.join(' ');
|
2015-03-20 00:17:26 +01:00
|
|
|
}
|
|
|
|
if (this.isEndSession()) {
|
2015-03-23 23:44:47 +01:00
|
|
|
return 'Secure session ended.';
|
2015-03-20 00:17:26 +01:00
|
|
|
}
|
2015-03-20 18:47:58 +01:00
|
|
|
if (this.isIncoming() && this.hasKeyConflicts()) {
|
|
|
|
return 'Received message with unknown identity key.';
|
|
|
|
}
|
2015-09-30 23:27:18 +02:00
|
|
|
if (this.isIncoming() && this.hasErrors()) {
|
2015-10-01 00:22:59 +02:00
|
|
|
return 'Error handling incoming message.';
|
2015-09-30 23:27:18 +02:00
|
|
|
}
|
2015-03-20 00:17:26 +01:00
|
|
|
|
|
|
|
return this.get('body');
|
|
|
|
},
|
2015-09-14 05:25:04 +02:00
|
|
|
getNotificationText: function() {
|
|
|
|
var description = this.getDescription();
|
|
|
|
if (description) {
|
|
|
|
return description;
|
|
|
|
}
|
|
|
|
if (this.get('attachments').length > 0) {
|
|
|
|
return 'Media message';
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
},
|
2015-09-14 04:42:49 +02:00
|
|
|
updateImageUrl: function() {
|
|
|
|
this.revokeImageUrl();
|
2015-09-14 05:25:04 +02:00
|
|
|
var attachment = this.get('attachments')[0];
|
2015-09-14 04:42:49 +02:00
|
|
|
if (attachment) {
|
|
|
|
var blob = new Blob([attachment.data], {
|
|
|
|
type: attachment.contentType
|
|
|
|
});
|
|
|
|
this.imageUrl = URL.createObjectURL(blob);
|
|
|
|
} else {
|
|
|
|
this.imageUrl = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
revokeImageUrl: function() {
|
|
|
|
if (this.imageUrl) {
|
|
|
|
URL.revokeObjectURL(this.imageUrl);
|
|
|
|
this.imageUrl = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
getImageUrl: function() {
|
|
|
|
if (this.imageUrl === undefined) {
|
|
|
|
this.updateImageUrl();
|
|
|
|
}
|
|
|
|
return this.imageUrl;
|
|
|
|
},
|
2015-03-12 01:49:01 +01:00
|
|
|
getContact: function() {
|
2015-09-17 07:06:20 +02:00
|
|
|
var conversationId = this.get('source');
|
|
|
|
if (!this.isIncoming()) {
|
|
|
|
conversationId = textsecure.storage.user.getNumber();
|
2015-03-12 04:41:15 +01:00
|
|
|
}
|
2015-09-17 07:06:20 +02:00
|
|
|
var c = ConversationController.get(conversationId);
|
|
|
|
if (!c) {
|
2015-11-06 01:30:36 +01:00
|
|
|
c = ConversationController.create({id: conversationId, type: 'private'});
|
2015-09-17 07:06:20 +02:00
|
|
|
c.fetch();
|
|
|
|
}
|
|
|
|
return c;
|
2015-02-24 01:23:22 +01:00
|
|
|
},
|
|
|
|
isOutgoing: function() {
|
|
|
|
return this.get('type') === 'outgoing';
|
|
|
|
},
|
2015-09-30 23:27:18 +02:00
|
|
|
hasErrors: function() {
|
|
|
|
return _.size(this.get('errors')) > 0;
|
|
|
|
},
|
2015-02-18 03:03:05 +01:00
|
|
|
hasKeyConflicts: function() {
|
|
|
|
return _.any(this.get('errors'), function(e) {
|
|
|
|
return (e.name === 'IncomingIdentityKeyError' ||
|
|
|
|
e.name === 'OutgoingIdentityKeyError');
|
|
|
|
});
|
|
|
|
},
|
|
|
|
hasKeyConflict: function(number) {
|
|
|
|
return _.any(this.get('errors'), function(e) {
|
|
|
|
return (e.name === 'IncomingIdentityKeyError' ||
|
|
|
|
e.name === 'OutgoingIdentityKeyError') &&
|
|
|
|
e.number === number;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
getKeyConflict: function(number) {
|
2015-02-24 01:23:22 +01:00
|
|
|
return _.find(this.get('errors'), function(e) {
|
2015-02-18 03:03:05 +01:00
|
|
|
return (e.name === 'IncomingIdentityKeyError' ||
|
|
|
|
e.name === 'OutgoingIdentityKeyError') &&
|
|
|
|
e.number === number;
|
2015-02-24 01:23:22 +01:00
|
|
|
});
|
2015-02-18 03:03:05 +01:00
|
|
|
},
|
2015-09-28 22:33:26 +02:00
|
|
|
|
|
|
|
send: function(promise) {
|
2015-10-28 21:57:32 +01:00
|
|
|
this.trigger('pending');
|
2015-11-20 01:57:48 +01:00
|
|
|
return promise.then(function(result) {
|
2015-10-28 21:57:32 +01:00
|
|
|
this.trigger('done');
|
2015-11-20 01:57:48 +01:00
|
|
|
if (result.dataMessage) {
|
|
|
|
this.set({dataMessage: result.dataMessage});
|
|
|
|
}
|
2015-09-28 22:33:26 +02:00
|
|
|
this.save({sent: true});
|
2015-11-20 01:57:48 +01:00
|
|
|
this.sendSyncMessage();
|
|
|
|
}.bind(this)).catch(function(result) {
|
2015-10-28 21:57:32 +01:00
|
|
|
this.trigger('done');
|
2015-11-20 01:57:48 +01:00
|
|
|
if (result.dataMessage) {
|
|
|
|
this.set({dataMessage: result.dataMessage});
|
|
|
|
}
|
2015-12-04 21:02:19 +01:00
|
|
|
|
|
|
|
if (result instanceof Error) {
|
|
|
|
this.saveErrors(result);
|
|
|
|
} else {
|
|
|
|
this.saveErrors(result.errors);
|
|
|
|
if (result.successfulNumbers.length > 0) {
|
2015-12-11 01:13:14 +01:00
|
|
|
this.set({sent: true});
|
2015-12-04 21:02:19 +01:00
|
|
|
this.sendSyncMessage();
|
|
|
|
}
|
2015-11-20 01:57:48 +01:00
|
|
|
}
|
2015-12-04 21:02:19 +01:00
|
|
|
|
2015-11-20 01:57:48 +01:00
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
sendSyncMessage: function() {
|
2016-02-17 02:46:20 +01:00
|
|
|
this.syncPromise = this.syncPromise || Promise.resolve();
|
|
|
|
this.syncPromise = this.syncPromise.then(function() {
|
|
|
|
var dataMessage = this.get('dataMessage');
|
|
|
|
if (this.get('synced') || !dataMessage) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return textsecure.messaging.sendSyncMessage(
|
|
|
|
dataMessage, this.get('sent_at'), this.get('destination')
|
|
|
|
).then(function() {
|
|
|
|
this.save({synced: true, dataMessage: null});
|
|
|
|
}.bind(this));
|
2015-09-28 22:33:26 +02:00
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
2015-10-02 03:21:20 +02:00
|
|
|
saveErrors: function(errors) {
|
|
|
|
if (!(errors instanceof Array)) {
|
|
|
|
errors = [errors];
|
|
|
|
}
|
|
|
|
errors.forEach(function(e) {
|
|
|
|
console.log(e);
|
|
|
|
console.log(e.reason, e.stack);
|
|
|
|
});
|
2015-10-02 21:14:34 +02:00
|
|
|
errors = errors.map(function(e) {
|
2015-11-17 20:59:21 +01:00
|
|
|
if (e.constructor === Error ||
|
|
|
|
e.constructor === TypeError ||
|
|
|
|
e.constructor === ReferenceError) {
|
2015-10-02 21:14:34 +02:00
|
|
|
return _.pick(e, 'name', 'message', 'code', 'number', 'reason');
|
|
|
|
}
|
|
|
|
return e;
|
|
|
|
});
|
2015-10-02 21:28:29 +02:00
|
|
|
errors = errors.concat(this.get('errors') || []);
|
|
|
|
|
|
|
|
return this.save({errors : errors});
|
2015-10-02 21:14:34 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
removeConflictFor: function(number) {
|
|
|
|
var errors = _.reject(this.get('errors'), function(e) {
|
|
|
|
return e.number === number &&
|
|
|
|
(e.name === 'IncomingIdentityKeyError' ||
|
|
|
|
e.name === 'OutgoingIdentityKeyError');
|
2015-10-02 03:21:20 +02:00
|
|
|
});
|
2015-10-02 21:14:34 +02:00
|
|
|
this.set({errors: errors});
|
2015-10-02 03:21:20 +02:00
|
|
|
},
|
|
|
|
|
2015-10-03 03:31:07 +02:00
|
|
|
removeOutgoingErrors: function(number) {
|
|
|
|
var errors = _.partition(this.get('errors'), function(e) {
|
|
|
|
return e.number === number &&
|
2016-02-06 01:42:53 +01:00
|
|
|
(e.name === 'MessageError' ||
|
|
|
|
e.name === 'OutgoingMessageError' ||
|
2015-10-03 03:31:07 +02:00
|
|
|
e.name === 'SendMessageNetworkError');
|
|
|
|
});
|
|
|
|
this.set({errors: errors[1]});
|
|
|
|
return errors[0][0];
|
|
|
|
},
|
|
|
|
|
2015-09-23 00:52:42 +02:00
|
|
|
resend: function(number) {
|
2015-10-03 03:31:07 +02:00
|
|
|
var error = this.removeOutgoingErrors(number);
|
2015-09-23 00:52:42 +02:00
|
|
|
if (error) {
|
|
|
|
var promise = new textsecure.ReplayableError(error).replay();
|
|
|
|
this.send(promise);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-02-18 03:03:05 +01:00
|
|
|
resolveConflict: function(number) {
|
|
|
|
var error = this.getKeyConflict(number);
|
|
|
|
if (error) {
|
|
|
|
var promise = new textsecure.ReplayableError(error).replay();
|
|
|
|
if (this.isIncoming()) {
|
2015-10-02 03:21:20 +02:00
|
|
|
promise = promise.then(function(dataMessage) {
|
2015-11-07 02:37:12 +01:00
|
|
|
this.removeConflictFor(number);
|
2015-06-17 02:46:53 +02:00
|
|
|
this.handleDataMessage(dataMessage);
|
2015-02-18 03:03:05 +01:00
|
|
|
}.bind(this));
|
2015-10-02 21:14:34 +02:00
|
|
|
} else {
|
2016-02-18 21:24:46 +01:00
|
|
|
promise = this.send(promise).then(function() {
|
2015-11-06 03:40:10 +01:00
|
|
|
this.removeConflictFor(number);
|
2015-10-02 21:14:34 +02:00
|
|
|
this.save();
|
|
|
|
}.bind(this));
|
2015-02-18 03:03:05 +01:00
|
|
|
}
|
2015-10-02 21:14:34 +02:00
|
|
|
promise.catch(function(e) {
|
2016-02-11 23:15:28 +01:00
|
|
|
this.removeConflictFor(number);
|
2015-10-02 03:21:20 +02:00
|
|
|
this.saveErrors(e);
|
|
|
|
}.bind(this));
|
2015-10-02 21:14:34 +02:00
|
|
|
|
2015-08-01 02:20:33 +02:00
|
|
|
return promise;
|
2015-02-18 03:03:05 +01:00
|
|
|
}
|
2015-03-19 00:26:55 +01:00
|
|
|
},
|
2015-06-17 02:46:53 +02:00
|
|
|
handleDataMessage: function(dataMessage) {
|
2015-03-19 00:26:55 +01:00
|
|
|
// This function can be called from the background script on an
|
|
|
|
// incoming message or from the frontend after the user accepts an
|
|
|
|
// identity key change.
|
|
|
|
var message = this;
|
|
|
|
var source = message.get('source');
|
2015-06-17 20:33:01 +02:00
|
|
|
var type = message.get('type');
|
2015-03-19 00:26:55 +01:00
|
|
|
var timestamp = message.get('sent_at');
|
2015-06-01 23:08:21 +02:00
|
|
|
var conversationId = message.get('conversationId');
|
2015-06-17 02:46:53 +02:00
|
|
|
if (dataMessage.group) {
|
|
|
|
conversationId = dataMessage.group.id;
|
2015-06-01 23:08:21 +02:00
|
|
|
}
|
2015-08-27 21:38:51 +02:00
|
|
|
var conversation = ConversationController.create({id: conversationId});
|
2015-06-01 23:08:21 +02:00
|
|
|
conversation.fetch().always(function() {
|
|
|
|
var now = new Date().getTime();
|
|
|
|
var attributes = { type: 'private' };
|
2015-06-17 02:46:53 +02:00
|
|
|
if (dataMessage.group) {
|
2016-01-12 19:41:24 +01:00
|
|
|
var group_update = null;
|
2015-06-01 23:08:21 +02:00
|
|
|
attributes = {
|
|
|
|
type: 'group',
|
2015-06-17 02:46:53 +02:00
|
|
|
groupId: dataMessage.group.id,
|
2015-06-01 23:08:21 +02:00
|
|
|
};
|
2015-06-17 02:46:53 +02:00
|
|
|
if (dataMessage.group.type === textsecure.protobuf.GroupContext.Type.UPDATE) {
|
2015-03-19 21:49:09 +01:00
|
|
|
attributes = {
|
2015-06-01 23:08:21 +02:00
|
|
|
type : 'group',
|
2015-06-17 02:46:53 +02:00
|
|
|
groupId : dataMessage.group.id,
|
|
|
|
name : dataMessage.group.name,
|
|
|
|
avatar : dataMessage.group.avatar,
|
|
|
|
members : dataMessage.group.members,
|
2015-03-19 21:49:09 +01:00
|
|
|
};
|
2015-08-02 19:10:43 +02:00
|
|
|
group_update = conversation.changedAttributes(_.pick(dataMessage.group, 'name', 'avatar')) || {};
|
2015-06-17 02:46:53 +02:00
|
|
|
var difference = _.difference(dataMessage.group.members, conversation.get('members'));
|
2015-06-01 23:08:21 +02:00
|
|
|
if (difference.length > 0) {
|
|
|
|
group_update.joined = difference;
|
2015-03-19 00:26:55 +01:00
|
|
|
}
|
|
|
|
}
|
2015-06-17 02:46:53 +02:00
|
|
|
else if (dataMessage.group.type === textsecure.protobuf.GroupContext.Type.QUIT) {
|
2015-12-04 03:48:04 +01:00
|
|
|
if (source == textsecure.storage.user.getNumber()) {
|
|
|
|
group_update = { left: "You" };
|
|
|
|
} else {
|
|
|
|
group_update = { left: source };
|
|
|
|
}
|
2015-06-01 23:08:21 +02:00
|
|
|
attributes.members = _.without(conversation.get('members'), source);
|
|
|
|
}
|
2015-05-18 23:23:09 +02:00
|
|
|
|
2016-01-12 19:41:24 +01:00
|
|
|
if (group_update !== null) {
|
2015-06-01 23:08:21 +02:00
|
|
|
message.set({group_update: group_update});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (type === 'outgoing') {
|
|
|
|
// lazy hack - check for receipts that arrived early.
|
2015-06-17 02:46:53 +02:00
|
|
|
if (dataMessage.group && dataMessage.group.id) { // group sync
|
2015-06-01 23:08:21 +02:00
|
|
|
var members = conversation.get('members') || [];
|
|
|
|
var receipts = window.receipts.where({ timestamp: timestamp });
|
|
|
|
for (var i in receipts) {
|
|
|
|
if (members.indexOf(receipts[i].get('source')) > -1) {
|
|
|
|
window.receipts.remove(receipts[i]);
|
2015-05-18 23:23:09 +02:00
|
|
|
message.set({
|
|
|
|
delivered: (message.get('delivered') || 0) + 1
|
|
|
|
});
|
|
|
|
}
|
2015-06-01 23:08:21 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var receipt = window.receipts.findWhere({
|
|
|
|
timestamp: timestamp,
|
|
|
|
source: conversationId
|
|
|
|
});
|
|
|
|
if (receipt) {
|
|
|
|
window.receipts.remove(receipt);
|
|
|
|
message.set({
|
|
|
|
delivered: (message.get('delivered') || 0) + 1
|
|
|
|
});
|
2015-05-18 23:23:09 +02:00
|
|
|
}
|
|
|
|
}
|
2015-06-01 23:08:21 +02:00
|
|
|
}
|
|
|
|
attributes.active_at = now;
|
|
|
|
if (type === 'incoming') {
|
|
|
|
attributes.unreadCount = conversation.get('unreadCount') + 1;
|
|
|
|
}
|
|
|
|
conversation.set(attributes);
|
2015-03-19 00:26:55 +01:00
|
|
|
|
2015-06-01 23:08:21 +02:00
|
|
|
message.set({
|
2015-06-17 02:46:53 +02:00
|
|
|
body : dataMessage.body,
|
2015-06-01 23:08:21 +02:00
|
|
|
conversationId : conversation.id,
|
2015-06-17 02:46:53 +02:00
|
|
|
attachments : dataMessage.attachments,
|
2015-06-01 23:08:21 +02:00
|
|
|
decrypted_at : now,
|
2015-06-17 02:46:53 +02:00
|
|
|
flags : dataMessage.flags,
|
2015-06-01 23:08:21 +02:00
|
|
|
errors : []
|
|
|
|
});
|
2015-03-19 00:26:55 +01:00
|
|
|
|
2015-07-23 00:24:03 +02:00
|
|
|
var conversation_timestamp = conversation.get('timestamp');
|
|
|
|
if (!conversation_timestamp || message.get('sent_at') > conversation_timestamp) {
|
2015-06-01 23:08:21 +02:00
|
|
|
conversation.set({
|
|
|
|
timestamp: message.get('sent_at'),
|
2015-11-03 01:46:52 +01:00
|
|
|
lastMessage: message.getNotificationText()
|
2015-06-01 23:08:21 +02:00
|
|
|
});
|
|
|
|
}
|
2015-08-28 03:02:39 +02:00
|
|
|
else if (!conversation.get('lastMessage')) {
|
|
|
|
conversation.set({
|
2015-11-03 01:46:52 +01:00
|
|
|
lastMessage: message.getNotificationText()
|
2015-08-28 03:02:39 +02:00
|
|
|
});
|
|
|
|
}
|
2015-03-19 00:26:55 +01:00
|
|
|
|
2015-12-10 01:48:29 +01:00
|
|
|
message.save().then(function() {
|
|
|
|
conversation.save().then(function() {
|
2015-11-07 23:11:13 +01:00
|
|
|
conversation.trigger('newmessage', message);
|
|
|
|
conversation.notify(message);
|
2015-03-19 00:26:55 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-11-13 23:35:37 +01:00
|
|
|
}
|
2015-03-19 00:26:55 +01:00
|
|
|
|
2014-11-13 23:35:37 +01:00
|
|
|
});
|
2014-05-18 23:26:55 +02:00
|
|
|
|
2014-11-13 23:35:37 +01:00
|
|
|
Whisper.MessageCollection = Backbone.Collection.extend({
|
2014-12-12 04:41:40 +01:00
|
|
|
model : Message,
|
|
|
|
database : Whisper.Database,
|
|
|
|
storeName : 'messages',
|
|
|
|
comparator : 'received_at',
|
2015-03-12 01:49:01 +01:00
|
|
|
initialize : function(models, options) {
|
|
|
|
if (options) {
|
|
|
|
this.conversation = options.conversation;
|
|
|
|
}
|
|
|
|
},
|
2014-12-12 04:41:40 +01:00
|
|
|
destroyAll : function () {
|
2014-11-13 23:35:37 +01:00
|
|
|
return Promise.all(this.models.map(function(m) {
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
m.destroy().then(resolve).fail(reject);
|
|
|
|
});
|
|
|
|
}));
|
2014-12-20 02:15:57 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
fetchSentAt: function(timestamp) {
|
|
|
|
return this.fetch({
|
|
|
|
index: {
|
|
|
|
// 'receipt' index on sent_at
|
|
|
|
name: 'receipt',
|
|
|
|
only: timestamp
|
|
|
|
}
|
|
|
|
});
|
2014-12-19 21:55:29 +01:00
|
|
|
},
|
|
|
|
|
2016-02-19 02:27:57 +01:00
|
|
|
fetchConversation: function(conversationId, limit) {
|
|
|
|
if (typeof limit !== 'number') {
|
|
|
|
limit = 100;
|
|
|
|
}
|
2015-11-11 01:03:19 +01:00
|
|
|
return new Promise(function(resolve) {
|
|
|
|
var upper;
|
|
|
|
if (this.length === 0) {
|
|
|
|
// fetch the most recent messages first
|
|
|
|
upper = Number.MAX_VALUE;
|
2016-02-18 02:08:50 +01:00
|
|
|
} else {
|
2015-11-11 01:03:19 +01:00
|
|
|
// not our first rodeo, fetch older messages.
|
|
|
|
upper = this.at(0).get('received_at');
|
|
|
|
}
|
2016-02-19 02:27:57 +01:00
|
|
|
var options = {remove: false, limit: limit};
|
2015-11-11 01:03:19 +01:00
|
|
|
options.index = {
|
|
|
|
// 'conversation' index on [conversationId, received_at]
|
|
|
|
name : 'conversation',
|
|
|
|
lower : [conversationId],
|
|
|
|
upper : [conversationId, upper],
|
|
|
|
order : 'desc'
|
|
|
|
// SELECT messages WHERE conversationId = this.id ORDER
|
|
|
|
// received_at DESC
|
|
|
|
};
|
|
|
|
this.fetch(options).then(resolve);
|
|
|
|
}.bind(this));
|
2015-02-18 03:03:05 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
hasKeyConflicts: function() {
|
|
|
|
return this.any(function(m) { return m.hasKeyConflicts(); });
|
2014-11-13 23:35:37 +01:00
|
|
|
}
|
|
|
|
});
|
2015-02-19 09:20:22 +01:00
|
|
|
})();
|