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);
|
2017-07-26 21:55:59 +00:00
|
|
|
this.on('unload', this.revokeImageUrl);
|
2016-09-21 00:19:51 +00:00
|
|
|
this.setToExpire();
|
2015-09-14 02:42:49 +00:00
|
|
|
},
|
2017-07-17 22:46:00 +00:00
|
|
|
idForLogging: function() {
|
|
|
|
return this.get('source') + '.' + this.get('sourceDevice') + ' ' + this.get('sent_at');
|
|
|
|
},
|
|
|
|
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) {
|
2017-10-10 15:49:12 +00:00
|
|
|
return i18n('leftTheGroup', group_update.left);
|
2015-03-23 22:44:47 +00:00
|
|
|
}
|
|
|
|
|
2017-10-10 15:49:12 +00:00
|
|
|
var messages = [i18n('updatedTheGroup')];
|
2015-03-23 22:44:47 +00:00
|
|
|
if (group_update.name) {
|
2017-10-10 15:49:12 +00:00
|
|
|
messages.push(i18n('titleIsNow', group_update.name));
|
2015-03-23 22:44:47 +00:00
|
|
|
}
|
|
|
|
if (group_update.joined) {
|
2017-10-10 15:49:12 +00:00
|
|
|
messages.push(i18n('joinedTheGroup', group_update.joined.join(', ')));
|
2015-03-23 22:44:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2017-09-07 01:18:46 +00:00
|
|
|
// This needs to be an unsafe call, because this method is called during
|
|
|
|
// initial module setup. We may be in the middle of the initial fetch to
|
|
|
|
// the database.
|
|
|
|
return ConversationController.getUnsafe(this.get('conversationId'));
|
2016-09-21 23:26:42 +00:00
|
|
|
},
|
2016-10-05 13:31:27 +00:00
|
|
|
getExpirationTimerUpdateSource: function() {
|
|
|
|
if (this.isExpirationTimerUpdate()) {
|
|
|
|
var conversationId = this.get('expirationTimerUpdate').source;
|
2017-09-01 16:10:41 +00:00
|
|
|
return ConversationController.getOrCreate(conversationId, 'private');
|
2016-10-05 13:31:27 +00:00
|
|
|
}
|
|
|
|
},
|
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
|
|
|
}
|
2017-09-01 16:10:41 +00:00
|
|
|
return ConversationController.getOrCreate(conversationId, 'private');
|
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) {
|
2017-09-01 16:10:41 +00:00
|
|
|
var c = ConversationController.getOrCreate(id, 'private');
|
2017-01-23 04:20:53 +00:00
|
|
|
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) {
|
2017-09-01 16:10:41 +00:00
|
|
|
var c = ConversationController.getOrCreate(id, 'private');
|
2017-06-15 19:27:41 +00:00
|
|
|
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
|
|
|
|
Feature: Blue check marks for read messages if opted in (#1489)
* Refactor delivery receipt event handler
* Rename the delivery receipt event
For less ambiguity with read receipts.
* Rename synced read event
For less ambiguity with read receipts from other Signal users.
* Add support for incoming receipt messages
Handle ReceiptMessages, which may include encrypted delivery receipts or read
receipts from recipients of our sent messages.
// FREEBIE
* Rename ReadReceipts to ReadSyncs
* Render read messages with blue double checks
* Send read receipts to senders of incoming messages
// FREEBIE
* Move ReadSyncs to their own file
// FREEBIE
* Fixup old comments on read receipts (now read syncs)
And some variable renaming for extra clarity.
// FREEBIE
* Add global setting for read receipts
Don't send read receipt messages unless the setting is enabled.
Don't process read receipts if the setting is disabled.
// FREEBIE
* Sync read receipt setting from mobile
Toggling this setting on your mobile device should sync it to Desktop. When
linking, use the setting in the provisioning message.
// FREEBIE
* Send receipt messages silently
Avoid generating phantom messages on ios
// FREEBIE
* Save recipients on the outgoing message models
For accurate tracking and display of sent/delivered/read state, even if group
membership changes later.
// FREEBIE
* Fix conversation type in profile key update handling
// FREEBIE
* Set recipients on synced sent messages
* Render saved recipients in message detail if available
For older messages, where we did not save the intended set of recipients at the
time of sending, fall back to the current group membership.
// FREEBIE
* Record who has been successfully sent to
// FREEBIE
* Record who a message has been delivered to
* Invert the not-clickable class
* Fix readReceipt setting sync when linking
* Render per recipient sent/delivered/read status
In the message detail view for outgoing messages, render each recipient's
individual sent/delivered/read status with respect to this message, as long as
there are no errors associated with the recipient (ie, safety number changes,
user not registered, etc...) since the error icon is displayed in that case.
*Messages sent before this change may not have per-recipient status lists
and will simply show no status icon.
// FREEBIE
* Add configuration sync request
Send these requests in a one-off fashion when:
1. We have just setup from a chrome app import
2. We have just upgraded to read-receipt support
// FREEBIE
* Expose sendRequestConfigurationSyncMessage
// FREEBIE
* Fix handling of incoming delivery receipts - union with array
FREEBIE
2017-10-04 22:28:43 +00:00
|
|
|
getStatus: function(number) {
|
|
|
|
var read_by = this.get('read_by') || [];
|
|
|
|
if (read_by.indexOf(number) >= 0) {
|
|
|
|
return 'read';
|
|
|
|
}
|
|
|
|
var delivered_to = this.get('delivered_to') || [];
|
|
|
|
if (delivered_to.indexOf(number) >= 0) {
|
|
|
|
return 'delivered';
|
|
|
|
}
|
|
|
|
var sent_to = this.get('sent_to') || [];
|
|
|
|
if (sent_to.indexOf(number) >= 0) {
|
|
|
|
return 'sent';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
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});
|
|
|
|
}
|
Feature: Blue check marks for read messages if opted in (#1489)
* Refactor delivery receipt event handler
* Rename the delivery receipt event
For less ambiguity with read receipts.
* Rename synced read event
For less ambiguity with read receipts from other Signal users.
* Add support for incoming receipt messages
Handle ReceiptMessages, which may include encrypted delivery receipts or read
receipts from recipients of our sent messages.
// FREEBIE
* Rename ReadReceipts to ReadSyncs
* Render read messages with blue double checks
* Send read receipts to senders of incoming messages
// FREEBIE
* Move ReadSyncs to their own file
// FREEBIE
* Fixup old comments on read receipts (now read syncs)
And some variable renaming for extra clarity.
// FREEBIE
* Add global setting for read receipts
Don't send read receipt messages unless the setting is enabled.
Don't process read receipts if the setting is disabled.
// FREEBIE
* Sync read receipt setting from mobile
Toggling this setting on your mobile device should sync it to Desktop. When
linking, use the setting in the provisioning message.
// FREEBIE
* Send receipt messages silently
Avoid generating phantom messages on ios
// FREEBIE
* Save recipients on the outgoing message models
For accurate tracking and display of sent/delivered/read state, even if group
membership changes later.
// FREEBIE
* Fix conversation type in profile key update handling
// FREEBIE
* Set recipients on synced sent messages
* Render saved recipients in message detail if available
For older messages, where we did not save the intended set of recipients at the
time of sending, fall back to the current group membership.
// FREEBIE
* Record who has been successfully sent to
// FREEBIE
* Record who a message has been delivered to
* Invert the not-clickable class
* Fix readReceipt setting sync when linking
* Render per recipient sent/delivered/read status
In the message detail view for outgoing messages, render each recipient's
individual sent/delivered/read status with respect to this message, as long as
there are no errors associated with the recipient (ie, safety number changes,
user not registered, etc...) since the error icon is displayed in that case.
*Messages sent before this change may not have per-recipient status lists
and will simply show no status icon.
// FREEBIE
* Add configuration sync request
Send these requests in a one-off fashion when:
1. We have just setup from a chrome app import
2. We have just upgraded to read-receipt support
// FREEBIE
* Expose sendRequestConfigurationSyncMessage
// FREEBIE
* Fix handling of incoming delivery receipts - union with array
FREEBIE
2017-10-04 22:28:43 +00:00
|
|
|
var sent_to = this.get('sent_to') || [];
|
|
|
|
this.save({
|
|
|
|
sent_to: _.union(sent_to, result.successfulNumbers),
|
|
|
|
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) {
|
Feature: Blue check marks for read messages if opted in (#1489)
* Refactor delivery receipt event handler
* Rename the delivery receipt event
For less ambiguity with read receipts.
* Rename synced read event
For less ambiguity with read receipts from other Signal users.
* Add support for incoming receipt messages
Handle ReceiptMessages, which may include encrypted delivery receipts or read
receipts from recipients of our sent messages.
// FREEBIE
* Rename ReadReceipts to ReadSyncs
* Render read messages with blue double checks
* Send read receipts to senders of incoming messages
// FREEBIE
* Move ReadSyncs to their own file
// FREEBIE
* Fixup old comments on read receipts (now read syncs)
And some variable renaming for extra clarity.
// FREEBIE
* Add global setting for read receipts
Don't send read receipt messages unless the setting is enabled.
Don't process read receipts if the setting is disabled.
// FREEBIE
* Sync read receipt setting from mobile
Toggling this setting on your mobile device should sync it to Desktop. When
linking, use the setting in the provisioning message.
// FREEBIE
* Send receipt messages silently
Avoid generating phantom messages on ios
// FREEBIE
* Save recipients on the outgoing message models
For accurate tracking and display of sent/delivered/read state, even if group
membership changes later.
// FREEBIE
* Fix conversation type in profile key update handling
// FREEBIE
* Set recipients on synced sent messages
* Render saved recipients in message detail if available
For older messages, where we did not save the intended set of recipients at the
time of sending, fall back to the current group membership.
// FREEBIE
* Record who has been successfully sent to
// FREEBIE
* Record who a message has been delivered to
* Invert the not-clickable class
* Fix readReceipt setting sync when linking
* Render per recipient sent/delivered/read status
In the message detail view for outgoing messages, render each recipient's
individual sent/delivered/read status with respect to this message, as long as
there are no errors associated with the recipient (ie, safety number changes,
user not registered, etc...) since the error icon is displayed in that case.
*Messages sent before this change may not have per-recipient status lists
and will simply show no status icon.
// FREEBIE
* Add configuration sync request
Send these requests in a one-off fashion when:
1. We have just setup from a chrome app import
2. We have just upgraded to read-receipt support
// FREEBIE
* Expose sendRequestConfigurationSyncMessage
// FREEBIE
* Fix handling of incoming delivery receipts - union with array
FREEBIE
2017-10-04 22:28:43 +00:00
|
|
|
var sent_to = this.get('sent_to') || [];
|
|
|
|
this.set({
|
|
|
|
sent_to: _.union(sent_to, result.successfulNumbers),
|
|
|
|
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));
|
|
|
|
},
|
|
|
|
|
2017-07-03 23:46:39 +00:00
|
|
|
someRecipientsFailed: function() {
|
|
|
|
var c = this.getConversation();
|
2017-09-07 01:18:46 +00:00
|
|
|
if (!c || c.isPrivate()) {
|
2017-07-03 23:46:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var recipients = c.contactCollection.length - 1;
|
|
|
|
var errors = this.get('errors');
|
|
|
|
if (!errors) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errors.length > 0 && recipients > 0 && errors.length < recipients) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2015-11-20 00:57:48 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
},
|
2017-09-29 16:15:28 +00:00
|
|
|
handleDataMessage: function(dataMessage, confirm) {
|
2017-09-01 16:10:41 +00:00
|
|
|
// This function is called from the background script in a few scenarios:
|
|
|
|
// 1. on an incoming message
|
|
|
|
// 2. on a sent message sync'd from another device
|
|
|
|
// 3. in rare cases, an incoming message can be retried, though it will
|
|
|
|
// still through one of the previous two codepaths.
|
2015-03-18 23:26:55 +00:00
|
|
|
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
|
|
|
}
|
2017-07-13 17:07:18 +00:00
|
|
|
|
2017-09-01 16:10:41 +00:00
|
|
|
var conversation = ConversationController.get(conversationId);
|
2017-07-25 01:43:35 +00:00
|
|
|
return conversation.queueJob(function() {
|
2016-03-15 20:09:06 +00:00
|
|
|
return new Promise(function(resolve) {
|
2017-09-01 16:10:41 +00:00
|
|
|
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) {
|
2016-03-15 20:09:06 +00:00
|
|
|
attributes = {
|
2017-09-01 16:10:41 +00:00
|
|
|
type : 'group',
|
|
|
|
groupId : dataMessage.group.id,
|
|
|
|
name : dataMessage.group.name,
|
|
|
|
avatar : dataMessage.group.avatar,
|
|
|
|
members : _.union(dataMessage.group.members, conversation.get('members')),
|
2016-03-15 20:09:06 +00:00
|
|
|
};
|
2017-09-01 16:10:41 +00:00
|
|
|
group_update = conversation.changedAttributes(_.pick(dataMessage.group, 'name', 'avatar')) || {};
|
|
|
|
var difference = _.difference(attributes.members, conversation.get('members'));
|
|
|
|
if (difference.length > 0) {
|
|
|
|
group_update.joined = difference;
|
2016-03-15 20:09:06 +00:00
|
|
|
}
|
2017-09-01 16:10:41 +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
|
|
|
}
|
2015-03-18 23:26:55 +00:00
|
|
|
}
|
2017-09-01 16:10:41 +00:00
|
|
|
else if (dataMessage.group.type === textsecure.protobuf.GroupContext.Type.QUIT) {
|
|
|
|
if (source == textsecure.storage.user.getNumber()) {
|
|
|
|
attributes.left = true;
|
|
|
|
group_update = { left: "You" };
|
|
|
|
} else {
|
|
|
|
group_update = { left: source };
|
|
|
|
}
|
|
|
|
attributes.members = _.without(conversation.get('members'), source);
|
2015-12-04 02:48:04 +00:00
|
|
|
}
|
2015-05-18 21:23:09 +00:00
|
|
|
|
2017-09-01 16:10:41 +00:00
|
|
|
if (group_update !== null) {
|
|
|
|
message.set({group_update: group_update});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
message.set({
|
|
|
|
body : dataMessage.body,
|
|
|
|
conversationId : conversation.id,
|
|
|
|
attachments : dataMessage.attachments,
|
|
|
|
decrypted_at : now,
|
|
|
|
flags : dataMessage.flags,
|
|
|
|
errors : []
|
|
|
|
});
|
|
|
|
if (type === 'outgoing') {
|
|
|
|
var receipts = Whisper.DeliveryReceipts.forMessage(conversation, message);
|
|
|
|
receipts.forEach(function(receipt) {
|
2016-09-28 23:47:57 +00:00
|
|
|
message.set({
|
2017-09-01 16:10:41 +00:00
|
|
|
delivered: (message.get('delivered') || 0) + 1
|
2016-09-28 23:47:57 +00:00
|
|
|
});
|
2017-09-01 16:10:41 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
attributes.active_at = now;
|
|
|
|
conversation.set(attributes);
|
2016-10-05 13:32:40 +00:00
|
|
|
|
2017-09-01 16:10:41 +00:00
|
|
|
if (message.isExpirationTimerUpdate()) {
|
|
|
|
message.set({
|
|
|
|
expirationTimerUpdate: {
|
|
|
|
source : source,
|
|
|
|
expireTimer : dataMessage.expireTimer
|
2016-09-29 01:33:17 +00:00
|
|
|
}
|
2017-09-01 16:10:41 +00:00
|
|
|
});
|
|
|
|
conversation.set({expireTimer: dataMessage.expireTimer});
|
|
|
|
} else if (dataMessage.expireTimer) {
|
|
|
|
message.set({expireTimer: dataMessage.expireTimer});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!message.isEndSession() && !message.isGroupUpdate()) {
|
|
|
|
if (dataMessage.expireTimer) {
|
|
|
|
if (dataMessage.expireTimer !== conversation.get('expireTimer')) {
|
|
|
|
conversation.updateExpirationTimer(
|
|
|
|
dataMessage.expireTimer, source,
|
|
|
|
message.get('received_at'));
|
2017-05-10 01:37:20 +00:00
|
|
|
}
|
2017-09-01 16:10:41 +00:00
|
|
|
} else if (conversation.get('expireTimer')) {
|
|
|
|
conversation.updateExpirationTimer(null, source,
|
|
|
|
message.get('received_at'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (type === 'incoming') {
|
Feature: Blue check marks for read messages if opted in (#1489)
* Refactor delivery receipt event handler
* Rename the delivery receipt event
For less ambiguity with read receipts.
* Rename synced read event
For less ambiguity with read receipts from other Signal users.
* Add support for incoming receipt messages
Handle ReceiptMessages, which may include encrypted delivery receipts or read
receipts from recipients of our sent messages.
// FREEBIE
* Rename ReadReceipts to ReadSyncs
* Render read messages with blue double checks
* Send read receipts to senders of incoming messages
// FREEBIE
* Move ReadSyncs to their own file
// FREEBIE
* Fixup old comments on read receipts (now read syncs)
And some variable renaming for extra clarity.
// FREEBIE
* Add global setting for read receipts
Don't send read receipt messages unless the setting is enabled.
Don't process read receipts if the setting is disabled.
// FREEBIE
* Sync read receipt setting from mobile
Toggling this setting on your mobile device should sync it to Desktop. When
linking, use the setting in the provisioning message.
// FREEBIE
* Send receipt messages silently
Avoid generating phantom messages on ios
// FREEBIE
* Save recipients on the outgoing message models
For accurate tracking and display of sent/delivered/read state, even if group
membership changes later.
// FREEBIE
* Fix conversation type in profile key update handling
// FREEBIE
* Set recipients on synced sent messages
* Render saved recipients in message detail if available
For older messages, where we did not save the intended set of recipients at the
time of sending, fall back to the current group membership.
// FREEBIE
* Record who has been successfully sent to
// FREEBIE
* Record who a message has been delivered to
* Invert the not-clickable class
* Fix readReceipt setting sync when linking
* Render per recipient sent/delivered/read status
In the message detail view for outgoing messages, render each recipient's
individual sent/delivered/read status with respect to this message, as long as
there are no errors associated with the recipient (ie, safety number changes,
user not registered, etc...) since the error icon is displayed in that case.
*Messages sent before this change may not have per-recipient status lists
and will simply show no status icon.
// FREEBIE
* Add configuration sync request
Send these requests in a one-off fashion when:
1. We have just setup from a chrome app import
2. We have just upgraded to read-receipt support
// FREEBIE
* Expose sendRequestConfigurationSyncMessage
// FREEBIE
* Fix handling of incoming delivery receipts - union with array
FREEBIE
2017-10-04 22:28:43 +00:00
|
|
|
var readSync = Whisper.ReadSyncs.forMessage(message);
|
|
|
|
if (readSync) {
|
2017-09-01 16:10:41 +00:00
|
|
|
if (message.get('expireTimer') && !message.get('expirationStartTimestamp')) {
|
Feature: Blue check marks for read messages if opted in (#1489)
* Refactor delivery receipt event handler
* Rename the delivery receipt event
For less ambiguity with read receipts.
* Rename synced read event
For less ambiguity with read receipts from other Signal users.
* Add support for incoming receipt messages
Handle ReceiptMessages, which may include encrypted delivery receipts or read
receipts from recipients of our sent messages.
// FREEBIE
* Rename ReadReceipts to ReadSyncs
* Render read messages with blue double checks
* Send read receipts to senders of incoming messages
// FREEBIE
* Move ReadSyncs to their own file
// FREEBIE
* Fixup old comments on read receipts (now read syncs)
And some variable renaming for extra clarity.
// FREEBIE
* Add global setting for read receipts
Don't send read receipt messages unless the setting is enabled.
Don't process read receipts if the setting is disabled.
// FREEBIE
* Sync read receipt setting from mobile
Toggling this setting on your mobile device should sync it to Desktop. When
linking, use the setting in the provisioning message.
// FREEBIE
* Send receipt messages silently
Avoid generating phantom messages on ios
// FREEBIE
* Save recipients on the outgoing message models
For accurate tracking and display of sent/delivered/read state, even if group
membership changes later.
// FREEBIE
* Fix conversation type in profile key update handling
// FREEBIE
* Set recipients on synced sent messages
* Render saved recipients in message detail if available
For older messages, where we did not save the intended set of recipients at the
time of sending, fall back to the current group membership.
// FREEBIE
* Record who has been successfully sent to
// FREEBIE
* Record who a message has been delivered to
* Invert the not-clickable class
* Fix readReceipt setting sync when linking
* Render per recipient sent/delivered/read status
In the message detail view for outgoing messages, render each recipient's
individual sent/delivered/read status with respect to this message, as long as
there are no errors associated with the recipient (ie, safety number changes,
user not registered, etc...) since the error icon is displayed in that case.
*Messages sent before this change may not have per-recipient status lists
and will simply show no status icon.
// FREEBIE
* Add configuration sync request
Send these requests in a one-off fashion when:
1. We have just setup from a chrome app import
2. We have just upgraded to read-receipt support
// FREEBIE
* Expose sendRequestConfigurationSyncMessage
// FREEBIE
* Fix handling of incoming delivery receipts - union with array
FREEBIE
2017-10-04 22:28:43 +00:00
|
|
|
message.set('expirationStartTimestamp', readSync.get('read_at'));
|
2017-05-10 01:37:20 +00:00
|
|
|
}
|
|
|
|
}
|
Feature: Blue check marks for read messages if opted in (#1489)
* Refactor delivery receipt event handler
* Rename the delivery receipt event
For less ambiguity with read receipts.
* Rename synced read event
For less ambiguity with read receipts from other Signal users.
* Add support for incoming receipt messages
Handle ReceiptMessages, which may include encrypted delivery receipts or read
receipts from recipients of our sent messages.
// FREEBIE
* Rename ReadReceipts to ReadSyncs
* Render read messages with blue double checks
* Send read receipts to senders of incoming messages
// FREEBIE
* Move ReadSyncs to their own file
// FREEBIE
* Fixup old comments on read receipts (now read syncs)
And some variable renaming for extra clarity.
// FREEBIE
* Add global setting for read receipts
Don't send read receipt messages unless the setting is enabled.
Don't process read receipts if the setting is disabled.
// FREEBIE
* Sync read receipt setting from mobile
Toggling this setting on your mobile device should sync it to Desktop. When
linking, use the setting in the provisioning message.
// FREEBIE
* Send receipt messages silently
Avoid generating phantom messages on ios
// FREEBIE
* Save recipients on the outgoing message models
For accurate tracking and display of sent/delivered/read state, even if group
membership changes later.
// FREEBIE
* Fix conversation type in profile key update handling
// FREEBIE
* Set recipients on synced sent messages
* Render saved recipients in message detail if available
For older messages, where we did not save the intended set of recipients at the
time of sending, fall back to the current group membership.
// FREEBIE
* Record who has been successfully sent to
// FREEBIE
* Record who a message has been delivered to
* Invert the not-clickable class
* Fix readReceipt setting sync when linking
* Render per recipient sent/delivered/read status
In the message detail view for outgoing messages, render each recipient's
individual sent/delivered/read status with respect to this message, as long as
there are no errors associated with the recipient (ie, safety number changes,
user not registered, etc...) since the error icon is displayed in that case.
*Messages sent before this change may not have per-recipient status lists
and will simply show no status icon.
// FREEBIE
* Add configuration sync request
Send these requests in a one-off fashion when:
1. We have just setup from a chrome app import
2. We have just upgraded to read-receipt support
// FREEBIE
* Expose sendRequestConfigurationSyncMessage
// FREEBIE
* Fix handling of incoming delivery receipts - union with array
FREEBIE
2017-10-04 22:28:43 +00:00
|
|
|
if (readSync || message.isExpirationTimerUpdate()) {
|
2017-09-01 16:10:41 +00:00
|
|
|
message.unset('unread');
|
|
|
|
// This is primarily to allow the conversation to mark all older messages as
|
Feature: Blue check marks for read messages if opted in (#1489)
* Refactor delivery receipt event handler
* Rename the delivery receipt event
For less ambiguity with read receipts.
* Rename synced read event
For less ambiguity with read receipts from other Signal users.
* Add support for incoming receipt messages
Handle ReceiptMessages, which may include encrypted delivery receipts or read
receipts from recipients of our sent messages.
// FREEBIE
* Rename ReadReceipts to ReadSyncs
* Render read messages with blue double checks
* Send read receipts to senders of incoming messages
// FREEBIE
* Move ReadSyncs to their own file
// FREEBIE
* Fixup old comments on read receipts (now read syncs)
And some variable renaming for extra clarity.
// FREEBIE
* Add global setting for read receipts
Don't send read receipt messages unless the setting is enabled.
Don't process read receipts if the setting is disabled.
// FREEBIE
* Sync read receipt setting from mobile
Toggling this setting on your mobile device should sync it to Desktop. When
linking, use the setting in the provisioning message.
// FREEBIE
* Send receipt messages silently
Avoid generating phantom messages on ios
// FREEBIE
* Save recipients on the outgoing message models
For accurate tracking and display of sent/delivered/read state, even if group
membership changes later.
// FREEBIE
* Fix conversation type in profile key update handling
// FREEBIE
* Set recipients on synced sent messages
* Render saved recipients in message detail if available
For older messages, where we did not save the intended set of recipients at the
time of sending, fall back to the current group membership.
// FREEBIE
* Record who has been successfully sent to
// FREEBIE
* Record who a message has been delivered to
* Invert the not-clickable class
* Fix readReceipt setting sync when linking
* Render per recipient sent/delivered/read status
In the message detail view for outgoing messages, render each recipient's
individual sent/delivered/read status with respect to this message, as long as
there are no errors associated with the recipient (ie, safety number changes,
user not registered, etc...) since the error icon is displayed in that case.
*Messages sent before this change may not have per-recipient status lists
and will simply show no status icon.
// FREEBIE
* Add configuration sync request
Send these requests in a one-off fashion when:
1. We have just setup from a chrome app import
2. We have just upgraded to read-receipt support
// FREEBIE
* Expose sendRequestConfigurationSyncMessage
// FREEBIE
* Fix handling of incoming delivery receipts - union with array
FREEBIE
2017-10-04 22:28:43 +00:00
|
|
|
// read, as is done when we receive a read sync for a message we already
|
2017-09-01 16:10:41 +00:00
|
|
|
// know about.
|
Feature: Blue check marks for read messages if opted in (#1489)
* Refactor delivery receipt event handler
* Rename the delivery receipt event
For less ambiguity with read receipts.
* Rename synced read event
For less ambiguity with read receipts from other Signal users.
* Add support for incoming receipt messages
Handle ReceiptMessages, which may include encrypted delivery receipts or read
receipts from recipients of our sent messages.
// FREEBIE
* Rename ReadReceipts to ReadSyncs
* Render read messages with blue double checks
* Send read receipts to senders of incoming messages
// FREEBIE
* Move ReadSyncs to their own file
// FREEBIE
* Fixup old comments on read receipts (now read syncs)
And some variable renaming for extra clarity.
// FREEBIE
* Add global setting for read receipts
Don't send read receipt messages unless the setting is enabled.
Don't process read receipts if the setting is disabled.
// FREEBIE
* Sync read receipt setting from mobile
Toggling this setting on your mobile device should sync it to Desktop. When
linking, use the setting in the provisioning message.
// FREEBIE
* Send receipt messages silently
Avoid generating phantom messages on ios
// FREEBIE
* Save recipients on the outgoing message models
For accurate tracking and display of sent/delivered/read state, even if group
membership changes later.
// FREEBIE
* Fix conversation type in profile key update handling
// FREEBIE
* Set recipients on synced sent messages
* Render saved recipients in message detail if available
For older messages, where we did not save the intended set of recipients at the
time of sending, fall back to the current group membership.
// FREEBIE
* Record who has been successfully sent to
// FREEBIE
* Record who a message has been delivered to
* Invert the not-clickable class
* Fix readReceipt setting sync when linking
* Render per recipient sent/delivered/read status
In the message detail view for outgoing messages, render each recipient's
individual sent/delivered/read status with respect to this message, as long as
there are no errors associated with the recipient (ie, safety number changes,
user not registered, etc...) since the error icon is displayed in that case.
*Messages sent before this change may not have per-recipient status lists
and will simply show no status icon.
// FREEBIE
* Add configuration sync request
Send these requests in a one-off fashion when:
1. We have just setup from a chrome app import
2. We have just upgraded to read-receipt support
// FREEBIE
* Expose sendRequestConfigurationSyncMessage
// FREEBIE
* Fix handling of incoming delivery receipts - union with array
FREEBIE
2017-10-04 22:28:43 +00:00
|
|
|
Whisper.ReadSyncs.notifyConversation(message);
|
2017-09-01 16:10:41 +00:00
|
|
|
} else {
|
|
|
|
conversation.set('unreadCount', conversation.get('unreadCount') + 1);
|
2016-03-15 20:09:06 +00:00
|
|
|
}
|
2017-09-01 16:10:41 +00:00
|
|
|
}
|
2017-07-13 17:07:18 +00:00
|
|
|
|
Feature: Blue check marks for read messages if opted in (#1489)
* Refactor delivery receipt event handler
* Rename the delivery receipt event
For less ambiguity with read receipts.
* Rename synced read event
For less ambiguity with read receipts from other Signal users.
* Add support for incoming receipt messages
Handle ReceiptMessages, which may include encrypted delivery receipts or read
receipts from recipients of our sent messages.
// FREEBIE
* Rename ReadReceipts to ReadSyncs
* Render read messages with blue double checks
* Send read receipts to senders of incoming messages
// FREEBIE
* Move ReadSyncs to their own file
// FREEBIE
* Fixup old comments on read receipts (now read syncs)
And some variable renaming for extra clarity.
// FREEBIE
* Add global setting for read receipts
Don't send read receipt messages unless the setting is enabled.
Don't process read receipts if the setting is disabled.
// FREEBIE
* Sync read receipt setting from mobile
Toggling this setting on your mobile device should sync it to Desktop. When
linking, use the setting in the provisioning message.
// FREEBIE
* Send receipt messages silently
Avoid generating phantom messages on ios
// FREEBIE
* Save recipients on the outgoing message models
For accurate tracking and display of sent/delivered/read state, even if group
membership changes later.
// FREEBIE
* Fix conversation type in profile key update handling
// FREEBIE
* Set recipients on synced sent messages
* Render saved recipients in message detail if available
For older messages, where we did not save the intended set of recipients at the
time of sending, fall back to the current group membership.
// FREEBIE
* Record who has been successfully sent to
// FREEBIE
* Record who a message has been delivered to
* Invert the not-clickable class
* Fix readReceipt setting sync when linking
* Render per recipient sent/delivered/read status
In the message detail view for outgoing messages, render each recipient's
individual sent/delivered/read status with respect to this message, as long as
there are no errors associated with the recipient (ie, safety number changes,
user not registered, etc...) since the error icon is displayed in that case.
*Messages sent before this change may not have per-recipient status lists
and will simply show no status icon.
// FREEBIE
* Add configuration sync request
Send these requests in a one-off fashion when:
1. We have just setup from a chrome app import
2. We have just upgraded to read-receipt support
// FREEBIE
* Expose sendRequestConfigurationSyncMessage
// FREEBIE
* Fix handling of incoming delivery receipts - union with array
FREEBIE
2017-10-04 22:28:43 +00:00
|
|
|
if (type === 'outgoing') {
|
|
|
|
var reads = Whisper.ReadReceipts.forMessage(conversation, message);
|
|
|
|
if (reads.length) {
|
|
|
|
var read_by = reads.map(function(receipt) {
|
|
|
|
return receipt.get('reader');
|
|
|
|
});
|
|
|
|
message.set({
|
|
|
|
read_by: _.union(message.get('read_by'), read_by)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
message.set({recipients: conversation.getRecipients()});
|
|
|
|
}
|
|
|
|
|
2017-09-01 16:10:41 +00:00
|
|
|
var conversation_timestamp = conversation.get('timestamp');
|
|
|
|
if (!conversation_timestamp || message.get('sent_at') > conversation_timestamp) {
|
|
|
|
conversation.set({
|
|
|
|
lastMessage : message.getNotificationText(),
|
|
|
|
timestamp: message.get('sent_at')
|
|
|
|
});
|
|
|
|
}
|
2017-07-13 17:07:18 +00:00
|
|
|
|
2017-09-11 16:50:35 +00:00
|
|
|
if (dataMessage.profileKey) {
|
|
|
|
var profileKey = dataMessage.profileKey.toArrayBuffer();
|
|
|
|
if (source == textsecure.storage.user.getNumber()) {
|
|
|
|
conversation.set({profileSharing: true});
|
|
|
|
} else if (conversation.isPrivate()) {
|
|
|
|
conversation.set({profileKey: profileKey});
|
|
|
|
} else {
|
|
|
|
ConversationController.getOrCreateAndWait(source, 'private').then(function(sender) {
|
|
|
|
sender.setProfileKey(profileKey);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-01 16:10:41 +00:00
|
|
|
var handleError = function(error) {
|
|
|
|
error = error && error.stack ? error.stack : error;
|
|
|
|
console.log('handleDataMessage', message.idForLogging(), 'error:', error);
|
|
|
|
return resolve();
|
|
|
|
};
|
|
|
|
|
|
|
|
message.save().then(function() {
|
|
|
|
conversation.save().then(function() {
|
|
|
|
try {
|
|
|
|
conversation.trigger('newmessage', message);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
return handleError(e);
|
|
|
|
}
|
|
|
|
// 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() {
|
2017-07-13 17:07:18 +00:00
|
|
|
try {
|
2017-09-01 16:10:41 +00:00
|
|
|
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();
|
|
|
|
}
|
2017-09-29 16:58:37 +00:00
|
|
|
|
2017-09-29 16:15:28 +00:00
|
|
|
if (message.get('unread')) {
|
2017-09-29 16:58:37 +00:00
|
|
|
conversation.notify(message).then(function() {
|
|
|
|
confirm();
|
|
|
|
return resolve();
|
|
|
|
}, handleError);
|
|
|
|
} else {
|
|
|
|
confirm();
|
|
|
|
return resolve();
|
2017-09-01 16:10:41 +00:00
|
|
|
}
|
2017-07-13 17:07:18 +00:00
|
|
|
}
|
|
|
|
catch (e) {
|
2017-09-01 16:10:41 +00:00
|
|
|
handleError(e);
|
2017-07-13 17:07:18 +00:00
|
|
|
}
|
2017-09-01 16:10:41 +00:00
|
|
|
}, function(error) {
|
|
|
|
try {
|
|
|
|
console.log('handleDataMessage: Message', message.idForLogging(), 'was deleted');
|
2017-08-04 01:15:47 +00:00
|
|
|
|
2017-09-01 16:10:41 +00:00
|
|
|
confirm();
|
|
|
|
return resolve();
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
handleError(e);
|
|
|
|
}
|
|
|
|
});
|
2017-07-13 17:07:18 +00:00
|
|
|
}, handleError);
|
2017-09-01 16:10:41 +00:00
|
|
|
}, handleError);
|
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
|
|
|
|
}));
|
2017-07-28 22:12:51 +00:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
this.save().then(resolve, reject);
|
|
|
|
}.bind(this));
|
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;
|
2017-08-05 01:19:26 +00:00
|
|
|
|
|
|
|
// This method can be called due to the expiration-related .set() calls in
|
|
|
|
// handleDataMessage(), but the .save() here would conflict with the
|
|
|
|
// same call at the end of handleDataMessage(). So we only call .save()
|
|
|
|
// here if we've previously saved this model.
|
|
|
|
if (!this.isNew()) {
|
|
|
|
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',
|
2017-08-14 18:23:08 +00:00
|
|
|
comparator : function(left, right) {
|
|
|
|
if (left.get('received_at') === right.get('received_at')) {
|
|
|
|
return (left.get('sent_at') || 0) - (right.get('sent_at') || 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (left.get('received_at') || 0) - (right.get('received_at') || 0);
|
|
|
|
},
|
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() {
|
2017-07-28 00:31:15 +00:00
|
|
|
return this.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
|
|
|
|
};
|
2017-07-15 00:05:23 +00:00
|
|
|
this.fetch(options).always(resolve);
|
2017-05-19 21:06:56 +00:00
|
|
|
}.bind(this)).then(function() {
|
2017-05-25 20:25:08 +00:00
|
|
|
if (unreadCount > 0) {
|
2017-07-28 00:31:15 +00:00
|
|
|
var loadedUnread = this.getLoadedUnreadCount();
|
|
|
|
if (loadedUnread >= unreadCount) {
|
2017-05-25 20:25:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (startingLoadedUnread === loadedUnread) {
|
|
|
|
// that fetch didn't get us any more unread. stop fetching more.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-15 00:05:23 +00:00
|
|
|
console.log('fetchConversation: doing another fetch to get all unread');
|
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
|
|
|
})();
|