2015-09-07 21:53:43 +00:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2014-11-13 22:35:37 +00:00
|
|
|
*/
|
2014-05-12 00:13:09 +00:00
|
|
|
(function () {
|
2014-11-13 22:35:37 +00:00
|
|
|
'use strict';
|
|
|
|
window.Whisper = window.Whisper || {};
|
2014-05-12 00:13:09 +00:00
|
|
|
|
2015-05-20 22:52:25 +00:00
|
|
|
var Message = window.Whisper.Message = Backbone.Model.extend({
|
2014-12-12 03:41:40 +00:00
|
|
|
database : Whisper.Database,
|
|
|
|
storeName : 'messages',
|
2015-09-14 02:42:49 +00:00
|
|
|
initialize: function() {
|
|
|
|
this.on('change:attachments', this.updateImageUrl);
|
|
|
|
this.on('destroy', this.revokeImageUrl);
|
2016-09-21 00:19:51 +00:00
|
|
|
this.on('change:expirationStartTimestamp', this.setToExpire);
|
|
|
|
this.on('change:expireTimer', this.setToExpire);
|
|
|
|
this.setToExpire();
|
2015-09-14 02:42:49 +00:00
|
|
|
},
|
2014-12-12 03:41:40 +00:00
|
|
|
defaults : function() {
|
2014-11-20 23:43:51 +00:00
|
|
|
return {
|
|
|
|
timestamp: new Date().getTime(),
|
|
|
|
attachments: []
|
|
|
|
};
|
|
|
|
},
|
2014-11-13 22:35:37 +00:00
|
|
|
validate: function(attributes, options) {
|
2014-12-12 03:41:40 +00:00
|
|
|
var required = ['conversationId', 'received_at', 'sent_at'];
|
2014-11-13 22:35:37 +00:00
|
|
|
var missing = _.filter(required, function(attr) { return !attributes[attr]; });
|
|
|
|
if (missing.length) {
|
|
|
|
console.log("Message missing attributes: " + missing);
|
|
|
|
}
|
2015-02-17 19:54:15 +00:00
|
|
|
},
|
|
|
|
isEndSession: function() {
|
2015-06-01 21:08:21 +00:00
|
|
|
var flag = textsecure.protobuf.DataMessage.Flags.END_SESSION;
|
2015-02-17 19:54:15 +00:00
|
|
|
return !!(this.get('flags') & flag);
|
|
|
|
},
|
2016-09-27 00:26:00 +00:00
|
|
|
isExpirationTimerUpdate: function() {
|
|
|
|
var flag = textsecure.protobuf.DataMessage.Flags.EXPIRATION_TIMER_UPDATE;
|
|
|
|
return !!(this.get('flags') & flag);
|
|
|
|
},
|
2015-02-17 19:54:15 +00:00
|
|
|
isGroupUpdate: function() {
|
|
|
|
return !!(this.get('group_update'));
|
2015-02-25 00:02:33 +00:00
|
|
|
},
|
|
|
|
isIncoming: function() {
|
|
|
|
return this.get('type') === 'incoming';
|
2015-03-12 00:49:01 +00:00
|
|
|
},
|
2016-04-04 23:14:15 +00:00
|
|
|
isUnread: function() {
|
|
|
|
return !!this.get('unread');
|
|
|
|
},
|
2017-05-24 22:13:39 +00:00
|
|
|
// overriding this to allow for this.unset('unread'), save to db, then fetch()
|
|
|
|
// to propagate. We don't want the unset key in the db so our unread index stays
|
|
|
|
// small.
|
2017-05-24 22:19:53 +00:00
|
|
|
// jscs:disable
|
2017-05-24 22:13:39 +00:00
|
|
|
fetch: function(options) {
|
|
|
|
options = options ? _.clone(options) : {};
|
|
|
|
if (options.parse === void 0) options.parse = true;
|
|
|
|
var model = this;
|
|
|
|
var success = options.success;
|
|
|
|
options.success = function(resp) {
|
|
|
|
model.attributes = {}; // this is the only changed line
|
|
|
|
if (!model.set(model.parse(resp, options), options)) return false;
|
|
|
|
if (success) success(model, resp, options);
|
|
|
|
model.trigger('sync', model, resp, options);
|
|
|
|
};
|
|
|
|
var error = options.error;
|
|
|
|
options.error = function(resp) {
|
|
|
|
if (error) error(model, resp, options);
|
|
|
|
model.trigger('error', model, resp, options);
|
|
|
|
};
|
|
|
|
return this.sync('read', this, options);
|
|
|
|
},
|
2017-05-24 22:19:53 +00:00
|
|
|
// jscs:enable
|
2015-03-19 23:17:26 +00:00
|
|
|
getDescription: function() {
|
|
|
|
if (this.isGroupUpdate()) {
|
2015-03-23 22:44:47 +00: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-19 23:17:26 +00:00
|
|
|
}
|
|
|
|
if (this.isEndSession()) {
|
2016-04-23 17:25:59 +00:00
|
|
|
return i18n('sessionEnded');
|
2015-03-19 23:17:26 +00:00
|
|
|
}
|
2015-09-30 21:27:18 +00:00
|
|
|
if (this.isIncoming() && this.hasErrors()) {
|
2016-04-23 18:18:22 +00:00
|
|
|
return i18n('incomingError');
|
2015-09-30 21:27:18 +00:00
|
|
|
}
|
2015-03-19 23:17:26 +00:00
|
|
|
return this.get('body');
|
|
|
|
},
|
2017-01-23 04:20:53 +00:00
|
|
|
isKeyChange: function() {
|
|
|
|
return this.get('type') === 'keychange';
|
|
|
|
},
|
2015-09-14 03:25:04 +00:00
|
|
|
getNotificationText: function() {
|
|
|
|
var description = this.getDescription();
|
|
|
|
if (description) {
|
|
|
|
return description;
|
|
|
|
}
|
|
|
|
if (this.get('attachments').length > 0) {
|
2016-04-16 03:07:35 +00:00
|
|
|
return i18n('mediaMessage');
|
2015-09-14 03:25:04 +00:00
|
|
|
}
|
2016-09-29 22:23:32 +00:00
|
|
|
if (this.isExpirationTimerUpdate()) {
|
|
|
|
return i18n('timerSetTo',
|
|
|
|
Whisper.ExpirationTimerOptions.getAbbreviated(
|
|
|
|
this.get('expirationTimerUpdate').expireTimer
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2017-01-23 04:20:53 +00:00
|
|
|
if (this.isKeyChange()) {
|
|
|
|
var conversation = this.getModelForKeyChange();
|
|
|
|
return i18n('keychanged', conversation.getTitle());
|
|
|
|
}
|
2015-09-14 03:25:04 +00:00
|
|
|
|
|
|
|
return '';
|
|
|
|
},
|
2015-09-14 02:42:49 +00:00
|
|
|
updateImageUrl: function() {
|
|
|
|
this.revokeImageUrl();
|
2015-09-14 03:25:04 +00:00
|
|
|
var attachment = this.get('attachments')[0];
|
2015-09-14 02:42:49 +00: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;
|
|
|
|
},
|
2016-09-21 23:26:42 +00:00
|
|
|
getConversation: function() {
|
|
|
|
return ConversationController.add({
|
|
|
|
id: this.get('conversationId')
|
|
|
|
});
|
|
|
|
},
|
2016-10-05 13:31:27 +00:00
|
|
|
getExpirationTimerUpdateSource: function() {
|
|
|
|
if (this.isExpirationTimerUpdate()) {
|
|
|
|
var conversationId = this.get('expirationTimerUpdate').source;
|
|
|
|
var c = ConversationController.get(conversationId);
|
|
|
|
if (!c) {
|
|
|
|
c = ConversationController.create({id: conversationId, type: 'private'});
|
|
|
|
c.fetch();
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
},
|
2015-03-12 00:49:01 +00:00
|
|
|
getContact: function() {
|
2015-09-17 05:06:20 +00:00
|
|
|
var conversationId = this.get('source');
|
|
|
|
if (!this.isIncoming()) {
|
|
|
|
conversationId = textsecure.storage.user.getNumber();
|
2015-03-12 03:41:15 +00:00
|
|
|
}
|
2015-09-17 05:06:20 +00:00
|
|
|
var c = ConversationController.get(conversationId);
|
|
|
|
if (!c) {
|
2015-11-06 00:30:36 +00:00
|
|
|
c = ConversationController.create({id: conversationId, type: 'private'});
|
2015-09-17 05:06:20 +00:00
|
|
|
c.fetch();
|
|
|
|
}
|
|
|
|
return c;
|
2015-02-24 00:23:22 +00:00
|
|
|
},
|
2016-09-18 06:55:05 +00:00
|
|
|
getModelForKeyChange: function() {
|
|
|
|
var id = this.get('key_changed');
|
2017-01-23 04:20:53 +00:00
|
|
|
if (!this.modelForKeyChange) {
|
|
|
|
var c = ConversationController.get(id);
|
|
|
|
if (!c) {
|
|
|
|
c = ConversationController.create({ id: id, type: 'private' });
|
|
|
|
c.fetch();
|
|
|
|
}
|
|
|
|
this.modelForKeyChange = c;
|
2016-09-18 06:55:05 +00:00
|
|
|
}
|
2017-01-23 04:20:53 +00:00
|
|
|
return this.modelForKeyChange;
|
2016-09-18 06:55:05 +00:00
|
|
|
},
|
2017-06-15 19:27:41 +00:00
|
|
|
getModelForVerifiedChange: function() {
|
|
|
|
var id = this.get('verifiedChanged');
|
|
|
|
if (!this.modelForVerifiedChange) {
|
|
|
|
var c = ConversationController.get(id);
|
|
|
|
if (!c) {
|
|
|
|
c = ConversationController.create({ id: id, type: 'private' });
|
|
|
|
c.fetch();
|
|
|
|
}
|
|
|
|
this.modelForVerifiedChange = c;
|
|
|
|
}
|
|
|
|
return this.modelForVerifiedChange;
|
|
|
|
},
|
2015-02-24 00:23:22 +00:00
|
|
|
isOutgoing: function() {
|
|
|
|
return this.get('type') === 'outgoing';
|
|
|
|
},
|
2015-09-30 21:27:18 +00:00
|
|
|
hasErrors: function() {
|
|
|
|
return _.size(this.get('errors')) > 0;
|
|
|
|
},
|
2015-09-28 20:33:26 +00:00
|
|
|
|
|
|
|
send: function(promise) {
|
2015-10-28 20:57:32 +00:00
|
|
|
this.trigger('pending');
|
2015-11-20 00:57:48 +00:00
|
|
|
return promise.then(function(result) {
|
2016-09-29 00:37:57 +00:00
|
|
|
var now = Date.now();
|
2015-10-28 20:57:32 +00:00
|
|
|
this.trigger('done');
|
2015-11-20 00:57:48 +00:00
|
|
|
if (result.dataMessage) {
|
|
|
|
this.set({dataMessage: result.dataMessage});
|
|
|
|
}
|
2016-09-29 00:37:57 +00:00
|
|
|
this.save({sent: true, expirationStartTimestamp: now});
|
2015-11-20 00:57:48 +00:00
|
|
|
this.sendSyncMessage();
|
|
|
|
}.bind(this)).catch(function(result) {
|
2016-09-29 00:37:57 +00:00
|
|
|
var now = Date.now();
|
2015-10-28 20:57:32 +00:00
|
|
|
this.trigger('done');
|
2015-11-20 00:57:48 +00:00
|
|
|
if (result.dataMessage) {
|
|
|
|
this.set({dataMessage: result.dataMessage});
|
|
|
|
}
|
2015-12-04 20:02:19 +00:00
|
|
|
|
2017-06-22 21:03:05 +00:00
|
|
|
var promises = [];
|
|
|
|
|
2015-12-04 20:02:19 +00:00
|
|
|
if (result instanceof Error) {
|
2017-02-16 22:59:19 +00:00
|
|
|
this.saveErrors(result);
|
2017-02-16 02:27:06 +00:00
|
|
|
if (result.name === 'SignedPreKeyRotationError') {
|
2017-06-23 16:34:28 +00:00
|
|
|
promises.push(getAccountManager().rotateSignedPreKey());
|
2017-06-22 21:03:05 +00:00
|
|
|
}
|
2017-06-29 03:30:35 +00:00
|
|
|
else if (result.name === 'OutgoingIdentityKeyError') {
|
|
|
|
var c = ConversationController.get(result.number);
|
|
|
|
promises.push(c.getProfiles());
|
2017-02-16 02:27:06 +00:00
|
|
|
}
|
2015-12-04 20:02:19 +00:00
|
|
|
} else {
|
2017-02-16 22:59:19 +00:00
|
|
|
this.saveErrors(result.errors);
|
2015-12-04 20:02:19 +00:00
|
|
|
if (result.successfulNumbers.length > 0) {
|
2016-09-29 00:37:57 +00:00
|
|
|
this.set({sent: true, expirationStartTimestamp: now});
|
2017-06-22 21:03:05 +00:00
|
|
|
promises.push(this.sendSyncMessage());
|
2015-12-04 20:02:19 +00:00
|
|
|
}
|
2017-06-22 21:03:05 +00:00
|
|
|
promises = promises.concat(_.map(result.errors, function(error) {
|
2017-06-29 03:30:35 +00:00
|
|
|
if (error.name === 'OutgoingIdentityKeyError') {
|
|
|
|
var c = ConversationController.get(error.number);
|
|
|
|
promises.push(c.getProfiles());
|
2017-06-22 21:03:05 +00:00
|
|
|
}
|
|
|
|
}));
|
2015-11-20 00:57:48 +00:00
|
|
|
}
|
2015-12-04 20:02:19 +00:00
|
|
|
|
2017-06-22 21:03:05 +00:00
|
|
|
return Promise.all(promises).then(function() {
|
|
|
|
this.trigger('send-error', this.get('errors'));
|
|
|
|
}.bind(this));
|
2015-11-20 00:57:48 +00:00
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
sendSyncMessage: function() {
|
2016-02-17 01:46:20 +00: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(
|
2016-09-29 00:37:57 +00:00
|
|
|
dataMessage, this.get('sent_at'), this.get('destination'), this.get('expirationStartTimestamp')
|
2016-02-17 01:46:20 +00:00
|
|
|
).then(function() {
|
|
|
|
this.save({synced: true, dataMessage: null});
|
|
|
|
}.bind(this));
|
2015-09-28 20:33:26 +00:00
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
2015-10-02 01:21:20 +00: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 19:14:34 +00:00
|
|
|
errors = errors.map(function(e) {
|
2015-11-17 19:59:21 +00:00
|
|
|
if (e.constructor === Error ||
|
|
|
|
e.constructor === TypeError ||
|
|
|
|
e.constructor === ReferenceError) {
|
2015-10-02 19:14:34 +00:00
|
|
|
return _.pick(e, 'name', 'message', 'code', 'number', 'reason');
|
|
|
|
}
|
|
|
|
return e;
|
|
|
|
});
|
2015-10-02 19:28:29 +00:00
|
|
|
errors = errors.concat(this.get('errors') || []);
|
|
|
|
|
|
|
|
return this.save({errors : errors});
|
2015-10-02 19:14:34 +00:00
|
|
|
},
|
|
|
|
|
2016-03-22 21:47:17 +00:00
|
|
|
hasNetworkError: function(number) {
|
|
|
|
var error = _.find(this.get('errors'), function(e) {
|
|
|
|
return (e.name === 'MessageError' ||
|
|
|
|
e.name === 'OutgoingMessageError' ||
|
2017-02-16 02:27:06 +00:00
|
|
|
e.name === 'SendMessageNetworkError' ||
|
|
|
|
e.name === 'SignedPreKeyRotationError');
|
2016-03-22 21:47:17 +00:00
|
|
|
});
|
|
|
|
return !!error;
|
|
|
|
},
|
2015-10-03 01:31:07 +00:00
|
|
|
removeOutgoingErrors: function(number) {
|
|
|
|
var errors = _.partition(this.get('errors'), function(e) {
|
|
|
|
return e.number === number &&
|
2016-02-06 00:42:53 +00:00
|
|
|
(e.name === 'MessageError' ||
|
|
|
|
e.name === 'OutgoingMessageError' ||
|
2017-02-16 02:27:06 +00:00
|
|
|
e.name === 'SendMessageNetworkError' ||
|
2017-06-15 01:02:03 +00:00
|
|
|
e.name === 'SignedPreKeyRotationError' ||
|
|
|
|
e.name === 'OutgoingIdentityKeyError');
|
2015-10-03 01:31:07 +00:00
|
|
|
});
|
|
|
|
this.set({errors: errors[1]});
|
|
|
|
return errors[0][0];
|
|
|
|
},
|
2017-02-16 02:27:06 +00:00
|
|
|
isReplayableError: function(e) {
|
|
|
|
return (e.name === 'MessageError' ||
|
|
|
|
e.name === 'OutgoingMessageError' ||
|
|
|
|
e.name === 'SendMessageNetworkError' ||
|
2017-06-15 01:02:03 +00:00
|
|
|
e.name === 'SignedPreKeyRotationError' ||
|
|
|
|
e.name === 'OutgoingIdentityKeyError');
|
2017-02-16 02:27:06 +00:00
|
|
|
},
|
2015-09-22 22:52:42 +00:00
|
|
|
resend: function(number) {
|
2015-10-03 01:31:07 +00:00
|
|
|
var error = this.removeOutgoingErrors(number);
|
2015-09-22 22:52:42 +00:00
|
|
|
if (error) {
|
|
|
|
var promise = new textsecure.ReplayableError(error).replay();
|
|
|
|
this.send(promise);
|
|
|
|
}
|
|
|
|
},
|
2015-06-17 00:46:53 +00:00
|
|
|
handleDataMessage: function(dataMessage) {
|
2015-03-18 23:26:55 +00: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 18:33:01 +00:00
|
|
|
var type = message.get('type');
|
2015-03-18 23:26:55 +00:00
|
|
|
var timestamp = message.get('sent_at');
|
2015-06-01 21:08:21 +00:00
|
|
|
var conversationId = message.get('conversationId');
|
2015-06-17 00:46:53 +00:00
|
|
|
if (dataMessage.group) {
|
|
|
|
conversationId = dataMessage.group.id;
|
2015-06-01 21:08:21 +00:00
|
|
|
}
|
2015-08-27 19:38:51 +00:00
|
|
|
var conversation = ConversationController.create({id: conversationId});
|
2016-03-15 20:09:06 +00:00
|
|
|
conversation.queueJob(function() {
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
conversation.fetch().always(function() {
|
|
|
|
var now = new Date().getTime();
|
|
|
|
var attributes = { type: 'private' };
|
|
|
|
if (dataMessage.group) {
|
|
|
|
var group_update = null;
|
|
|
|
attributes = {
|
|
|
|
type: 'group',
|
|
|
|
groupId: dataMessage.group.id,
|
|
|
|
};
|
|
|
|
if (dataMessage.group.type === textsecure.protobuf.GroupContext.Type.UPDATE) {
|
|
|
|
attributes = {
|
|
|
|
type : 'group',
|
|
|
|
groupId : dataMessage.group.id,
|
|
|
|
name : dataMessage.group.name,
|
|
|
|
avatar : dataMessage.group.avatar,
|
2017-02-07 03:20:36 +00:00
|
|
|
members : _.union(dataMessage.group.members, conversation.get('members')),
|
2016-03-15 20:09:06 +00:00
|
|
|
};
|
|
|
|
group_update = conversation.changedAttributes(_.pick(dataMessage.group, 'name', 'avatar')) || {};
|
2017-02-07 03:20:36 +00:00
|
|
|
var difference = _.difference(attributes.members, conversation.get('members'));
|
2016-03-15 20:09:06 +00:00
|
|
|
if (difference.length > 0) {
|
|
|
|
group_update.joined = difference;
|
|
|
|
}
|
2017-05-30 04:36:08 +00:00
|
|
|
if (conversation.get('left')) {
|
|
|
|
console.log('re-added to a left group');
|
|
|
|
attributes.left = false;
|
|
|
|
}
|
2016-03-15 20:09:06 +00:00
|
|
|
}
|
|
|
|
else if (dataMessage.group.type === textsecure.protobuf.GroupContext.Type.QUIT) {
|
|
|
|
if (source == textsecure.storage.user.getNumber()) {
|
2017-05-12 21:56:16 +00:00
|
|
|
attributes.left = true;
|
2016-03-15 20:09:06 +00:00
|
|
|
group_update = { left: "You" };
|
|
|
|
} else {
|
|
|
|
group_update = { left: source };
|
|
|
|
}
|
|
|
|
attributes.members = _.without(conversation.get('members'), source);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (group_update !== null) {
|
|
|
|
message.set({group_update: group_update});
|
|
|
|
}
|
2015-03-18 23:26:55 +00:00
|
|
|
}
|
2016-09-29 02:28:23 +00:00
|
|
|
message.set({
|
|
|
|
body : dataMessage.body,
|
|
|
|
conversationId : conversation.id,
|
|
|
|
attachments : dataMessage.attachments,
|
|
|
|
decrypted_at : now,
|
|
|
|
flags : dataMessage.flags,
|
|
|
|
errors : []
|
|
|
|
});
|
2016-03-15 20:09:06 +00:00
|
|
|
if (type === 'outgoing') {
|
2016-04-11 22:06:06 +00:00
|
|
|
var receipts = Whisper.DeliveryReceipts.forMessage(conversation, message);
|
|
|
|
receipts.forEach(function(receipt) {
|
2016-03-15 20:09:06 +00:00
|
|
|
message.set({
|
|
|
|
delivered: (message.get('delivered') || 0) + 1
|
|
|
|
});
|
|
|
|
});
|
2015-12-04 02:48:04 +00:00
|
|
|
}
|
2016-03-15 20:09:06 +00:00
|
|
|
attributes.active_at = now;
|
|
|
|
conversation.set(attributes);
|
2015-05-18 21:23:09 +00:00
|
|
|
|
2016-09-28 23:47:57 +00:00
|
|
|
if (message.isExpirationTimerUpdate()) {
|
|
|
|
message.set({
|
|
|
|
expirationTimerUpdate: {
|
|
|
|
source : source,
|
|
|
|
expireTimer : dataMessage.expireTimer
|
|
|
|
}
|
|
|
|
});
|
|
|
|
conversation.set({expireTimer: dataMessage.expireTimer});
|
|
|
|
} else if (dataMessage.expireTimer) {
|
2016-09-21 00:19:51 +00:00
|
|
|
message.set({expireTimer: dataMessage.expireTimer});
|
2016-09-29 01:33:17 +00:00
|
|
|
}
|
2016-10-05 13:32:40 +00:00
|
|
|
|
2017-01-04 10:43:05 +00:00
|
|
|
if (!message.isEndSession() && !message.isGroupUpdate()) {
|
2016-10-05 13:32:40 +00:00
|
|
|
if (dataMessage.expireTimer) {
|
|
|
|
if (dataMessage.expireTimer !== conversation.get('expireTimer')) {
|
2017-01-03 12:52:29 +00:00
|
|
|
conversation.updateExpirationTimer(
|
2016-10-05 13:32:40 +00:00
|
|
|
dataMessage.expireTimer, source,
|
|
|
|
message.get('received_at'));
|
|
|
|
}
|
|
|
|
} else if (conversation.get('expireTimer')) {
|
2017-02-21 03:12:53 +00:00
|
|
|
conversation.updateExpirationTimer(null, source,
|
2016-10-05 13:32:40 +00:00
|
|
|
message.get('received_at'));
|
2016-09-29 01:33:17 +00:00
|
|
|
}
|
2016-09-21 00:19:51 +00:00
|
|
|
}
|
2017-05-10 01:37:20 +00:00
|
|
|
if (type === 'incoming') {
|
|
|
|
var readReceipt = Whisper.ReadReceipts.forMessage(message);
|
|
|
|
if (readReceipt) {
|
|
|
|
if (message.get('expireTimer') && !message.get('expirationStartTimestamp')) {
|
|
|
|
message.set('expirationStartTimestamp', readReceipt.get('read_at'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (readReceipt || message.isExpirationTimerUpdate()) {
|
|
|
|
message.unset('unread');
|
2017-06-07 21:34:05 +00:00
|
|
|
// This is primarily to allow the conversation to mark all older messages as
|
|
|
|
// read, as is done when we receive a read receipt for a message we already
|
|
|
|
// know about.
|
|
|
|
Whisper.ReadReceipts.notifyConversation(message);
|
2017-05-10 01:37:20 +00:00
|
|
|
} else {
|
|
|
|
conversation.set('unreadCount', conversation.get('unreadCount') + 1);
|
|
|
|
}
|
|
|
|
}
|
2016-09-21 00:19:51 +00:00
|
|
|
|
2016-03-15 20:09:06 +00:00
|
|
|
var conversation_timestamp = conversation.get('timestamp');
|
|
|
|
if (!conversation_timestamp || message.get('sent_at') > conversation_timestamp) {
|
|
|
|
conversation.set({
|
2017-02-23 00:15:27 +00:00
|
|
|
lastMessage : message.getNotificationText(),
|
2016-04-15 00:32:30 +00:00
|
|
|
timestamp: message.get('sent_at')
|
2016-03-15 20:09:06 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
message.save().then(function() {
|
|
|
|
conversation.save().then(function() {
|
|
|
|
conversation.trigger('newmessage', message);
|
2017-05-26 22:33:50 +00:00
|
|
|
// We fetch() here because, between the message.save() above and the previous
|
|
|
|
// line's trigger() call, we might have marked all messages unread in the
|
|
|
|
// database. This message might already be read!
|
|
|
|
var previousUnread = message.get('unread');
|
|
|
|
message.fetch().then(function() {
|
|
|
|
if (previousUnread !== message.get('unread')) {
|
|
|
|
console.log('Caught race condition on new message read state! ' +
|
|
|
|
'Manually starting timers.');
|
|
|
|
// We call markRead() even though the message is already marked read
|
|
|
|
// because we need to start expiration timers, etc.
|
|
|
|
message.markRead();
|
|
|
|
}
|
|
|
|
if (message.get('unread')) {
|
|
|
|
conversation.notify(message);
|
|
|
|
}
|
|
|
|
resolve();
|
|
|
|
});
|
2016-03-15 20:09:06 +00:00
|
|
|
});
|
|
|
|
});
|
2015-03-18 23:26:55 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-02-22 05:37:47 +00:00
|
|
|
},
|
2016-09-21 22:06:31 +00:00
|
|
|
markRead: function(read_at) {
|
2016-02-22 05:37:47 +00:00
|
|
|
this.unset('unread');
|
2016-09-21 00:19:51 +00:00
|
|
|
if (this.get('expireTimer') && !this.get('expirationStartTimestamp')) {
|
2016-09-21 22:06:31 +00:00
|
|
|
this.set('expirationStartTimestamp', read_at || Date.now());
|
2016-09-21 00:19:51 +00:00
|
|
|
}
|
2016-03-08 20:02:47 +00:00
|
|
|
Whisper.Notifications.remove(Whisper.Notifications.where({
|
|
|
|
messageId: this.id
|
|
|
|
}));
|
2016-02-22 05:37:47 +00:00
|
|
|
return this.save();
|
2016-09-21 00:19:51 +00:00
|
|
|
},
|
2016-09-22 21:12:38 +00:00
|
|
|
isExpiring: function() {
|
|
|
|
return this.get('expireTimer') && this.get('expirationStartTimestamp');
|
|
|
|
},
|
2017-06-06 22:16:45 +00:00
|
|
|
isExpired: function() {
|
|
|
|
return this.msTilExpire() <= 0;
|
|
|
|
},
|
2016-09-22 21:12:38 +00:00
|
|
|
msTilExpire: function() {
|
|
|
|
if (!this.isExpiring()) {
|
|
|
|
return Infinity;
|
|
|
|
}
|
|
|
|
var now = Date.now();
|
|
|
|
var start = this.get('expirationStartTimestamp');
|
|
|
|
var delta = this.get('expireTimer') * 1000;
|
|
|
|
var ms_from_now = start + delta - now;
|
|
|
|
if (ms_from_now < 0) {
|
|
|
|
ms_from_now = 0;
|
|
|
|
}
|
|
|
|
return ms_from_now;
|
|
|
|
},
|
2016-09-21 00:19:51 +00:00
|
|
|
setToExpire: function() {
|
2017-02-21 23:32:40 +00:00
|
|
|
if (this.isExpiring() && !this.get('expires_at')) {
|
|
|
|
var start = this.get('expirationStartTimestamp');
|
|
|
|
var delta = this.get('expireTimer') * 1000;
|
|
|
|
var expires_at = start + delta;
|
|
|
|
this.save('expires_at', expires_at);
|
2017-04-12 20:31:50 +00:00
|
|
|
Whisper.ExpiringMessagesListener.update();
|
2017-02-21 23:32:40 +00:00
|
|
|
console.log('message', this.get('sent_at'), 'expires at', expires_at);
|
2016-09-21 00:19:51 +00:00
|
|
|
}
|
2014-11-13 22:35:37 +00:00
|
|
|
}
|
2015-03-18 23:26:55 +00:00
|
|
|
|
2014-11-13 22:35:37 +00:00
|
|
|
});
|
2014-05-18 21:26:55 +00:00
|
|
|
|
2014-11-13 22:35:37 +00:00
|
|
|
Whisper.MessageCollection = Backbone.Collection.extend({
|
2014-12-12 03:41:40 +00:00
|
|
|
model : Message,
|
|
|
|
database : Whisper.Database,
|
|
|
|
storeName : 'messages',
|
|
|
|
comparator : 'received_at',
|
2015-03-12 00:49:01 +00:00
|
|
|
initialize : function(models, options) {
|
|
|
|
if (options) {
|
|
|
|
this.conversation = options.conversation;
|
|
|
|
}
|
|
|
|
},
|
2014-12-12 03:41:40 +00:00
|
|
|
destroyAll : function () {
|
2014-11-13 22:35:37 +00:00
|
|
|
return Promise.all(this.models.map(function(m) {
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
m.destroy().then(resolve).fail(reject);
|
|
|
|
});
|
|
|
|
}));
|
2014-12-20 01:15:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
fetchSentAt: function(timestamp) {
|
2016-04-11 22:06:06 +00:00
|
|
|
return new Promise(function(resolve) {
|
|
|
|
return this.fetch({
|
|
|
|
index: {
|
|
|
|
// 'receipt' index on sent_at
|
|
|
|
name: 'receipt',
|
|
|
|
only: timestamp
|
|
|
|
}
|
|
|
|
}).always(resolve);
|
|
|
|
}.bind(this));
|
2014-12-19 20:55:29 +00:00
|
|
|
},
|
|
|
|
|
2017-05-19 21:06:56 +00:00
|
|
|
getLoadedUnreadCount: function() {
|
|
|
|
return this.models.reduce(function(total, model) {
|
2017-07-05 20:52:27 +00:00
|
|
|
var unread = model.get('unread') && model.isIncoming();
|
|
|
|
return total + (unread ? 1 : 0);
|
2017-05-19 21:06:56 +00:00
|
|
|
}, 0);
|
|
|
|
},
|
|
|
|
|
|
|
|
fetchConversation: function(conversationId, limit, unreadCount) {
|
2016-02-19 01:27:57 +00:00
|
|
|
if (typeof limit !== 'number') {
|
|
|
|
limit = 100;
|
|
|
|
}
|
2017-05-19 21:06:56 +00:00
|
|
|
if (typeof unreadCount !== 'number') {
|
|
|
|
unreadCount = 0;
|
|
|
|
}
|
2017-05-25 20:25:08 +00:00
|
|
|
|
|
|
|
var startingLoadedUnread = 0;
|
|
|
|
if (unreadCount > 0) {
|
|
|
|
startingLoadedUnread = this.getLoadedUnreadCount();
|
|
|
|
}
|
2015-11-11 00:03:19 +00: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 01:08:50 +00:00
|
|
|
} else {
|
2015-11-11 00:03:19 +00:00
|
|
|
// not our first rodeo, fetch older messages.
|
|
|
|
upper = this.at(0).get('received_at');
|
|
|
|
}
|
2016-02-19 01:27:57 +00:00
|
|
|
var options = {remove: false, limit: limit};
|
2015-11-11 00:03:19 +00: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);
|
2017-05-19 21:06:56 +00:00
|
|
|
}.bind(this)).then(function() {
|
2017-05-25 20:25:08 +00:00
|
|
|
if (unreadCount > 0) {
|
|
|
|
if (unreadCount <= startingLoadedUnread) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var loadedUnread = this.getLoadedUnreadCount();
|
|
|
|
if (startingLoadedUnread === loadedUnread) {
|
|
|
|
// that fetch didn't get us any more unread. stop fetching more.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-19 21:06:56 +00:00
|
|
|
return this.fetchConversation(conversationId, limit, unreadCount);
|
|
|
|
}
|
2015-11-11 00:03:19 +00:00
|
|
|
}.bind(this));
|
2015-02-18 02:03:05 +00:00
|
|
|
},
|
|
|
|
|
2017-02-21 23:32:40 +00:00
|
|
|
fetchNextExpiring: function() {
|
|
|
|
this.fetch({ index: { name: 'expires_at' }, limit: 1 });
|
|
|
|
},
|
|
|
|
|
|
|
|
fetchExpired: function() {
|
|
|
|
console.log('loading expired messages');
|
|
|
|
this.fetch({
|
|
|
|
conditions: { expires_at: { $lte: Date.now() } },
|
|
|
|
addIndividually: true
|
|
|
|
});
|
2014-11-13 22:35:37 +00:00
|
|
|
}
|
|
|
|
});
|
2015-02-19 08:20:22 +00:00
|
|
|
})();
|