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');
|
|
|
|
},
|
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-03-20 17:47:58 +00:00
|
|
|
if (this.isIncoming() && this.hasKeyConflicts()) {
|
2016-04-23 18:05:43 +00:00
|
|
|
return i18n('incomingKeyConflict');
|
2015-03-20 17:47:58 +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
|
|
|
},
|
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-02-18 02:03:05 +00: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 00:23:22 +00:00
|
|
|
return _.find(this.get('errors'), function(e) {
|
2015-02-18 02:03:05 +00:00
|
|
|
return (e.name === 'IncomingIdentityKeyError' ||
|
|
|
|
e.name === 'OutgoingIdentityKeyError') &&
|
|
|
|
e.number === number;
|
2015-02-24 00:23:22 +00:00
|
|
|
});
|
2015-02-18 02:03:05 +00:00
|
|
|
},
|
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
|
|
|
|
|
|
|
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') {
|
|
|
|
getAccountManager().rotateSignedPreKey();
|
|
|
|
}
|
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});
|
2015-12-04 20:02:19 +00:00
|
|
|
this.sendSyncMessage();
|
|
|
|
}
|
2015-11-20 00:57:48 +00:00
|
|
|
}
|
2015-12-04 20:02:19 +00:00
|
|
|
|
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
|
|
|
},
|
|
|
|
|
|
|
|
removeConflictFor: function(number) {
|
|
|
|
var errors = _.reject(this.get('errors'), function(e) {
|
|
|
|
return e.number === number &&
|
|
|
|
(e.name === 'IncomingIdentityKeyError' ||
|
|
|
|
e.name === 'OutgoingIdentityKeyError');
|
2015-10-02 01:21:20 +00:00
|
|
|
});
|
2015-10-02 19:14:34 +00:00
|
|
|
this.set({errors: errors});
|
2015-10-02 01:21:20 +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' ||
|
|
|
|
e.name === 'SignedPreKeyRotationError');
|
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' ||
|
|
|
|
e.name === 'SignedPreKeyRotationError');
|
|
|
|
},
|
2015-10-03 01:31:07 +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-02-18 02:03:05 +00:00
|
|
|
resolveConflict: function(number) {
|
|
|
|
var error = this.getKeyConflict(number);
|
|
|
|
if (error) {
|
2016-02-22 21:25:34 +00:00
|
|
|
this.removeConflictFor(number);
|
2015-02-18 02:03:05 +00:00
|
|
|
var promise = new textsecure.ReplayableError(error).replay();
|
|
|
|
if (this.isIncoming()) {
|
2015-10-02 01:21:20 +00:00
|
|
|
promise = promise.then(function(dataMessage) {
|
2015-11-07 01:37:12 +00:00
|
|
|
this.removeConflictFor(number);
|
2015-06-17 00:46:53 +00:00
|
|
|
this.handleDataMessage(dataMessage);
|
2015-02-18 02:03:05 +00:00
|
|
|
}.bind(this));
|
2015-10-02 19:14:34 +00:00
|
|
|
} else {
|
2016-02-18 20:24:46 +00:00
|
|
|
promise = this.send(promise).then(function() {
|
2015-11-06 02:40:10 +00:00
|
|
|
this.removeConflictFor(number);
|
2015-10-02 19:14:34 +00:00
|
|
|
this.save();
|
|
|
|
}.bind(this));
|
2015-02-18 02:03:05 +00:00
|
|
|
}
|
2015-10-02 19:14:34 +00:00
|
|
|
promise.catch(function(e) {
|
2016-02-11 22:15:28 +00:00
|
|
|
this.removeConflictFor(number);
|
2015-10-02 01:21:20 +00:00
|
|
|
this.saveErrors(e);
|
|
|
|
}.bind(this));
|
2015-10-02 19:14:34 +00:00
|
|
|
|
2015-08-01 00:20:33 +00:00
|
|
|
return promise;
|
2015-02-18 02:03:05 +00:00
|
|
|
}
|
2015-03-18 23:26:55 +00:00
|
|
|
},
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (dataMessage.group.type === textsecure.protobuf.GroupContext.Type.QUIT) {
|
|
|
|
if (source == textsecure.storage.user.getNumber()) {
|
|
|
|
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;
|
|
|
|
if (type === 'incoming') {
|
2016-04-11 22:11:20 +00:00
|
|
|
// experimental
|
2016-09-29 02:28:23 +00:00
|
|
|
if (Whisper.ReadReceipts.forMessage(message) || message.isExpirationTimerUpdate()) {
|
2016-04-11 22:11:20 +00:00
|
|
|
message.unset('unread');
|
|
|
|
} else {
|
|
|
|
attributes.unreadCount = conversation.get('unreadCount') + 1;
|
|
|
|
}
|
2016-03-15 20:09:06 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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);
|
2016-08-04 22:31:13 +00:00
|
|
|
if (message.get('unread')) {
|
|
|
|
conversation.notify(message);
|
|
|
|
}
|
2016-03-15 20:09:06 +00:00
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
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');
|
|
|
|
},
|
|
|
|
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);
|
|
|
|
ExpiringMessagesListener.update();
|
|
|
|
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
|
|
|
},
|
|
|
|
|
2016-02-19 01:27:57 +00:00
|
|
|
fetchConversation: function(conversationId, limit) {
|
|
|
|
if (typeof limit !== 'number') {
|
|
|
|
limit = 100;
|
|
|
|
}
|
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);
|
|
|
|
}.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
|
|
|
|
});
|
2016-09-21 00:19:51 +00:00
|
|
|
},
|
|
|
|
|
2015-02-18 02:03:05 +00:00
|
|
|
hasKeyConflicts: function() {
|
|
|
|
return this.any(function(m) { return m.hasKeyConflicts(); });
|
2014-11-13 22:35:37 +00:00
|
|
|
}
|
|
|
|
});
|
2015-02-19 08:20:22 +00:00
|
|
|
})();
|