2018-04-10 23:51:48 +00:00
|
|
|
/* global _: false */
|
|
|
|
/* global Backbone: false */
|
2018-05-07 16:50:11 +00:00
|
|
|
|
2018-04-10 23:51:48 +00:00
|
|
|
/* global ConversationController: false */
|
|
|
|
/* global getAccountManager: false */
|
2018-05-07 16:50:11 +00:00
|
|
|
/* global i18n: false */
|
|
|
|
/* global Signal: false */
|
|
|
|
/* global textsecure: false */
|
|
|
|
/* global Whisper: false */
|
2018-03-20 19:03:26 +00:00
|
|
|
|
2018-04-10 23:51:48 +00:00
|
|
|
/* eslint-disable more/no-then */
|
|
|
|
|
|
|
|
// eslint-disable-next-line func-names
|
2018-04-27 21:25:04 +00:00
|
|
|
(function() {
|
2018-04-10 23:51:48 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
window.Whisper = window.Whisper || {};
|
|
|
|
|
2018-05-07 16:50:11 +00:00
|
|
|
const { Message: TypedMessage } = Signal.Types;
|
|
|
|
const { deleteAttachmentData } = Signal.Migrations;
|
2018-04-10 23:51:48 +00:00
|
|
|
|
|
|
|
window.Whisper.Message = Backbone.Model.extend({
|
|
|
|
database: Whisper.Database,
|
|
|
|
storeName: 'messages',
|
|
|
|
initialize(attributes) {
|
|
|
|
if (_.isObject(attributes)) {
|
|
|
|
this.set(TypedMessage.initializeSchemaVersion(attributes));
|
|
|
|
}
|
|
|
|
|
|
|
|
this.on('change:attachments', this.updateImageUrl);
|
|
|
|
this.on('destroy', this.onDestroy);
|
|
|
|
this.on('change:expirationStartTimestamp', this.setToExpire);
|
|
|
|
this.on('change:expireTimer', this.setToExpire);
|
2018-04-12 06:55:32 +00:00
|
|
|
this.on('unload', this.unload);
|
2018-04-10 23:51:48 +00:00
|
|
|
this.setToExpire();
|
|
|
|
},
|
|
|
|
idForLogging() {
|
2018-04-27 21:25:04 +00:00
|
|
|
return `${this.get('source')}.${this.get('sourceDevice')} ${this.get(
|
|
|
|
'sent_at'
|
|
|
|
)}`;
|
2018-04-10 23:51:48 +00:00
|
|
|
},
|
|
|
|
defaults() {
|
|
|
|
return {
|
|
|
|
timestamp: new Date().getTime(),
|
|
|
|
attachments: [],
|
|
|
|
};
|
|
|
|
},
|
|
|
|
validate(attributes) {
|
|
|
|
const required = ['conversationId', 'received_at', 'sent_at'];
|
|
|
|
const missing = _.filter(required, attr => !attributes[attr]);
|
|
|
|
if (missing.length) {
|
|
|
|
console.log(`Message missing attributes: ${missing}`);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
isEndSession() {
|
|
|
|
const flag = textsecure.protobuf.DataMessage.Flags.END_SESSION;
|
|
|
|
// eslint-disable-next-line no-bitwise
|
|
|
|
return !!(this.get('flags') & flag);
|
|
|
|
},
|
|
|
|
isExpirationTimerUpdate() {
|
2018-04-27 21:25:04 +00:00
|
|
|
const flag =
|
|
|
|
textsecure.protobuf.DataMessage.Flags.EXPIRATION_TIMER_UPDATE;
|
2018-04-10 23:51:48 +00:00
|
|
|
// eslint-disable-next-line no-bitwise
|
|
|
|
return !!(this.get('flags') & flag);
|
|
|
|
},
|
|
|
|
isGroupUpdate() {
|
2018-04-27 21:25:04 +00:00
|
|
|
return !!this.get('group_update');
|
2018-04-10 23:51:48 +00:00
|
|
|
},
|
|
|
|
isIncoming() {
|
|
|
|
return this.get('type') === 'incoming';
|
|
|
|
},
|
|
|
|
isUnread() {
|
|
|
|
return !!this.get('unread');
|
|
|
|
},
|
|
|
|
// 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.
|
|
|
|
/* eslint-disable */
|
|
|
|
/* jscs:disable */
|
|
|
|
fetch(options) {
|
|
|
|
options = options ? _.clone(options) : {};
|
|
|
|
if (options.parse === void 0) options.parse = true;
|
|
|
|
const model = this;
|
|
|
|
const success = options.success;
|
2018-04-27 21:25:04 +00:00
|
|
|
options.success = function(resp) {
|
2018-04-10 23:51:48 +00:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
const error = options.error;
|
2018-04-27 21:25:04 +00:00
|
|
|
options.error = function(resp) {
|
2018-04-10 23:51:48 +00:00
|
|
|
if (error) error(model, resp, options);
|
|
|
|
model.trigger('error', model, resp, options);
|
|
|
|
};
|
|
|
|
return this.sync('read', this, options);
|
|
|
|
},
|
|
|
|
/* jscs:enable */
|
|
|
|
/* eslint-enable */
|
|
|
|
/* eslint-disable more/no-then */
|
|
|
|
getNameForNumber(number) {
|
|
|
|
const conversation = ConversationController.get(number);
|
|
|
|
if (!conversation) {
|
|
|
|
return number;
|
|
|
|
}
|
|
|
|
return conversation.getDisplayName();
|
|
|
|
},
|
|
|
|
getDescription() {
|
|
|
|
if (this.isGroupUpdate()) {
|
|
|
|
const groupUpdate = this.get('group_update');
|
|
|
|
if (groupUpdate.left === 'You') {
|
|
|
|
return i18n('youLeftTheGroup');
|
|
|
|
} else if (groupUpdate.left) {
|
|
|
|
return i18n('leftTheGroup', this.getNameForNumber(groupUpdate.left));
|
|
|
|
}
|
2018-03-16 22:35:27 +00:00
|
|
|
|
2018-04-10 23:51:48 +00:00
|
|
|
const messages = [i18n('updatedTheGroup')];
|
|
|
|
if (groupUpdate.name) {
|
|
|
|
messages.push(i18n('titleIsNow', groupUpdate.name));
|
|
|
|
}
|
|
|
|
if (groupUpdate.joined && groupUpdate.joined.length) {
|
2018-04-27 21:25:04 +00:00
|
|
|
const names = _.map(
|
|
|
|
groupUpdate.joined,
|
|
|
|
this.getNameForNumber.bind(this)
|
|
|
|
);
|
2018-04-10 23:51:48 +00:00
|
|
|
if (names.length > 1) {
|
|
|
|
messages.push(i18n('multipleJoinedTheGroup', names.join(', ')));
|
|
|
|
} else {
|
|
|
|
messages.push(i18n('joinedTheGroup', names[0]));
|
|
|
|
}
|
|
|
|
}
|
2015-03-23 22:44:47 +00:00
|
|
|
|
2018-04-10 23:51:48 +00:00
|
|
|
return messages.join(' ');
|
|
|
|
}
|
|
|
|
if (this.isEndSession()) {
|
|
|
|
return i18n('sessionEnded');
|
|
|
|
}
|
|
|
|
if (this.isIncoming() && this.hasErrors()) {
|
|
|
|
return i18n('incomingError');
|
|
|
|
}
|
|
|
|
return this.get('body');
|
|
|
|
},
|
|
|
|
isKeyChange() {
|
|
|
|
return this.get('type') === 'keychange';
|
|
|
|
},
|
|
|
|
getNotificationText() {
|
|
|
|
const description = this.getDescription();
|
|
|
|
if (description) {
|
|
|
|
return description;
|
|
|
|
}
|
|
|
|
if (this.get('attachments').length > 0) {
|
|
|
|
return i18n('mediaMessage');
|
|
|
|
}
|
|
|
|
if (this.isExpirationTimerUpdate()) {
|
|
|
|
const { expireTimer } = this.get('expirationTimerUpdate');
|
|
|
|
return i18n(
|
|
|
|
'timerSetTo',
|
|
|
|
Whisper.ExpirationTimerOptions.getAbbreviated(expireTimer)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (this.isKeyChange()) {
|
|
|
|
const conversation = this.getModelForKeyChange();
|
|
|
|
return i18n('keychanged', conversation.getTitle());
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
},
|
|
|
|
async onDestroy() {
|
|
|
|
this.revokeImageUrl();
|
|
|
|
const attachments = this.get('attachments');
|
|
|
|
await Promise.all(attachments.map(deleteAttachmentData));
|
|
|
|
},
|
|
|
|
updateImageUrl() {
|
|
|
|
this.revokeImageUrl();
|
|
|
|
const attachment = this.get('attachments')[0];
|
|
|
|
if (attachment) {
|
|
|
|
const blob = new Blob([attachment.data], {
|
|
|
|
type: attachment.contentType,
|
|
|
|
});
|
|
|
|
this.imageUrl = URL.createObjectURL(blob);
|
|
|
|
} else {
|
|
|
|
this.imageUrl = null;
|
|
|
|
}
|
|
|
|
},
|
2018-04-12 06:55:32 +00:00
|
|
|
unload() {
|
|
|
|
if (this.quoteThumbnail) {
|
|
|
|
URL.revokeObjectURL(this.quoteThumbnail.objectUrl);
|
|
|
|
this.quoteThumbnail = null;
|
|
|
|
}
|
|
|
|
if (this.quotedMessage) {
|
|
|
|
this.quotedMessage = null;
|
|
|
|
}
|
2018-04-19 01:25:55 +00:00
|
|
|
const quote = this.get('quote');
|
|
|
|
const attachments = (quote && quote.attachments) || [];
|
2018-04-27 21:25:04 +00:00
|
|
|
attachments.forEach(attachment => {
|
2018-04-19 01:25:55 +00:00
|
|
|
if (attachment.thumbnail && attachment.thumbnail.objectUrl) {
|
|
|
|
URL.revokeObjectURL(attachment.thumbnail.objectUrl);
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
attachment.thumbnail.objectUrl = null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-04-12 06:55:32 +00:00
|
|
|
this.revokeImageUrl();
|
|
|
|
},
|
2018-04-10 23:51:48 +00:00
|
|
|
revokeImageUrl() {
|
|
|
|
if (this.imageUrl) {
|
|
|
|
URL.revokeObjectURL(this.imageUrl);
|
|
|
|
this.imageUrl = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
getImageUrl() {
|
|
|
|
if (this.imageUrl === undefined) {
|
|
|
|
this.updateImageUrl();
|
|
|
|
}
|
|
|
|
return this.imageUrl;
|
|
|
|
},
|
2018-04-17 21:31:16 +00:00
|
|
|
getQuoteObjectUrl() {
|
|
|
|
const thumbnail = this.quoteThumbnail;
|
2018-04-23 22:36:47 +00:00
|
|
|
if (!thumbnail || !thumbnail.objectUrl) {
|
|
|
|
return null;
|
2018-04-17 21:31:16 +00:00
|
|
|
}
|
|
|
|
|
2018-04-23 22:36:47 +00:00
|
|
|
return thumbnail.objectUrl;
|
2018-04-17 21:31:16 +00:00
|
|
|
},
|
|
|
|
getQuoteContact() {
|
|
|
|
const quote = this.get('quote');
|
|
|
|
if (!quote) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const { author } = quote;
|
|
|
|
if (!author) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ConversationController.get(author);
|
|
|
|
},
|
2018-04-19 01:25:55 +00:00
|
|
|
processAttachment(attachment, externalObjectUrl) {
|
|
|
|
const { thumbnail } = attachment;
|
|
|
|
const objectUrl = (thumbnail && thumbnail.objectUrl) || externalObjectUrl;
|
|
|
|
|
|
|
|
const thumbnailWithObjectUrl = !objectUrl
|
2018-04-17 21:31:16 +00:00
|
|
|
? null
|
|
|
|
: Object.assign({}, attachment.thumbnail || {}, {
|
2018-04-27 21:25:04 +00:00
|
|
|
objectUrl,
|
|
|
|
});
|
2018-04-17 21:31:16 +00:00
|
|
|
|
|
|
|
return Object.assign({}, attachment, {
|
2018-05-07 16:52:27 +00:00
|
|
|
isVoiceMessage: Signal.Types.Attachment.isVoiceMessage(attachment),
|
2018-04-19 01:25:55 +00:00
|
|
|
thumbnail: thumbnailWithObjectUrl,
|
2018-04-17 21:31:16 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
getPropsForQuote() {
|
|
|
|
const quote = this.get('quote');
|
|
|
|
if (!quote) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const objectUrl = this.getQuoteObjectUrl();
|
|
|
|
const OUR_NUMBER = textsecure.storage.user.getNumber();
|
|
|
|
const { author } = quote;
|
|
|
|
const contact = this.getQuoteContact();
|
|
|
|
|
|
|
|
const authorTitle = contact ? contact.getTitle() : author;
|
|
|
|
const authorProfileName = contact ? contact.getProfileName() : null;
|
|
|
|
const authorColor = contact ? contact.getColor() : 'grey';
|
|
|
|
const isFromMe = contact ? contact.id === OUR_NUMBER : false;
|
|
|
|
const isIncoming = this.isIncoming();
|
|
|
|
const onClick = () => {
|
|
|
|
const { quotedMessage } = this;
|
|
|
|
if (quotedMessage) {
|
|
|
|
this.trigger('scroll-to-message', { id: quotedMessage.id });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
attachments: (quote.attachments || []).map(attachment =>
|
2018-04-27 21:25:04 +00:00
|
|
|
this.processAttachment(attachment, objectUrl)
|
|
|
|
),
|
2018-04-17 21:31:16 +00:00
|
|
|
authorColor,
|
|
|
|
authorProfileName,
|
|
|
|
authorTitle,
|
|
|
|
isFromMe,
|
|
|
|
isIncoming,
|
|
|
|
onClick: this.quotedMessage ? onClick : null,
|
|
|
|
text: quote.text,
|
|
|
|
};
|
|
|
|
},
|
2018-04-10 23:51:48 +00:00
|
|
|
getConversation() {
|
|
|
|
// 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'));
|
|
|
|
},
|
|
|
|
getExpirationTimerUpdateSource() {
|
2018-04-17 01:17:38 +00:00
|
|
|
if (!this.isExpirationTimerUpdate()) {
|
|
|
|
throw new Error('Message is not a timer update!');
|
2018-04-10 23:51:48 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 01:17:38 +00:00
|
|
|
const conversationId = this.get('expirationTimerUpdate').source;
|
|
|
|
return ConversationController.getOrCreate(conversationId, 'private');
|
2018-04-10 23:51:48 +00:00
|
|
|
},
|
|
|
|
getContact() {
|
|
|
|
let conversationId = this.get('source');
|
|
|
|
if (!this.isIncoming()) {
|
|
|
|
conversationId = textsecure.storage.user.getNumber();
|
|
|
|
}
|
|
|
|
return ConversationController.getOrCreate(conversationId, 'private');
|
|
|
|
},
|
|
|
|
getModelForKeyChange() {
|
|
|
|
const id = this.get('key_changed');
|
|
|
|
if (!this.modelForKeyChange) {
|
|
|
|
const c = ConversationController.getOrCreate(id, 'private');
|
|
|
|
this.modelForKeyChange = c;
|
|
|
|
}
|
|
|
|
return this.modelForKeyChange;
|
|
|
|
},
|
|
|
|
getModelForVerifiedChange() {
|
|
|
|
const id = this.get('verifiedChanged');
|
|
|
|
if (!this.modelForVerifiedChange) {
|
|
|
|
const c = ConversationController.getOrCreate(id, 'private');
|
|
|
|
this.modelForVerifiedChange = c;
|
|
|
|
}
|
|
|
|
return this.modelForVerifiedChange;
|
|
|
|
},
|
|
|
|
isOutgoing() {
|
|
|
|
return this.get('type') === 'outgoing';
|
|
|
|
},
|
|
|
|
hasErrors() {
|
|
|
|
return _.size(this.get('errors')) > 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
getStatus(number) {
|
|
|
|
const readBy = this.get('read_by') || [];
|
|
|
|
if (readBy.indexOf(number) >= 0) {
|
|
|
|
return 'read';
|
|
|
|
}
|
|
|
|
const deliveredTo = this.get('delivered_to') || [];
|
|
|
|
if (deliveredTo.indexOf(number) >= 0) {
|
|
|
|
return 'delivered';
|
|
|
|
}
|
|
|
|
const sentTo = this.get('sent_to') || [];
|
|
|
|
if (sentTo.indexOf(number) >= 0) {
|
|
|
|
return 'sent';
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
send(promise) {
|
|
|
|
this.trigger('pending');
|
2018-04-27 21:25:04 +00:00
|
|
|
return promise
|
|
|
|
.then(result => {
|
|
|
|
const now = Date.now();
|
|
|
|
this.trigger('done');
|
|
|
|
if (result.dataMessage) {
|
|
|
|
this.set({ dataMessage: result.dataMessage });
|
2018-04-10 23:51:48 +00:00
|
|
|
}
|
2018-04-27 21:25:04 +00:00
|
|
|
const sentTo = this.get('sent_to') || [];
|
|
|
|
this.save({
|
|
|
|
sent_to: _.union(sentTo, result.successfulNumbers),
|
|
|
|
sent: true,
|
|
|
|
expirationStartTimestamp: now,
|
|
|
|
});
|
|
|
|
this.sendSyncMessage();
|
|
|
|
})
|
|
|
|
.catch(result => {
|
|
|
|
const now = Date.now();
|
|
|
|
this.trigger('done');
|
|
|
|
if (result.dataMessage) {
|
|
|
|
this.set({ dataMessage: result.dataMessage });
|
2018-04-10 23:51:48 +00:00
|
|
|
}
|
2018-04-27 21:25:04 +00:00
|
|
|
|
|
|
|
let promises = [];
|
|
|
|
|
|
|
|
if (result instanceof Error) {
|
|
|
|
this.saveErrors(result);
|
|
|
|
if (result.name === 'SignedPreKeyRotationError') {
|
|
|
|
promises.push(getAccountManager().rotateSignedPreKey());
|
|
|
|
} else if (result.name === 'OutgoingIdentityKeyError') {
|
|
|
|
const c = ConversationController.get(result.number);
|
2018-04-10 23:51:48 +00:00
|
|
|
promises.push(c.getProfiles());
|
|
|
|
}
|
2018-04-27 21:25:04 +00:00
|
|
|
} else {
|
|
|
|
this.saveErrors(result.errors);
|
|
|
|
if (result.successfulNumbers.length > 0) {
|
|
|
|
const sentTo = this.get('sent_to') || [];
|
|
|
|
this.set({
|
|
|
|
sent_to: _.union(sentTo, result.successfulNumbers),
|
|
|
|
sent: true,
|
|
|
|
expirationStartTimestamp: now,
|
|
|
|
});
|
|
|
|
promises.push(this.sendSyncMessage());
|
|
|
|
}
|
|
|
|
promises = promises.concat(
|
|
|
|
_.map(result.errors, error => {
|
|
|
|
if (error.name === 'OutgoingIdentityKeyError') {
|
|
|
|
const c = ConversationController.get(error.number);
|
|
|
|
promises.push(c.getProfiles());
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2015-09-14 03:25:04 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
return Promise.all(promises).then(() => {
|
|
|
|
this.trigger('send-error', this.get('errors'));
|
|
|
|
});
|
2018-04-10 23:51:48 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
someRecipientsFailed() {
|
|
|
|
const c = this.getConversation();
|
|
|
|
if (!c || c.isPrivate()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const recipients = c.contactCollection.length - 1;
|
|
|
|
const errors = this.get('errors');
|
|
|
|
if (!errors) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errors.length > 0 && recipients > 0 && errors.length < recipients) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
sendSyncMessage() {
|
|
|
|
this.syncPromise = this.syncPromise || Promise.resolve();
|
|
|
|
this.syncPromise = this.syncPromise.then(() => {
|
|
|
|
const dataMessage = this.get('dataMessage');
|
|
|
|
if (this.get('synced') || !dataMessage) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2018-04-27 21:25:04 +00:00
|
|
|
return textsecure.messaging
|
|
|
|
.sendSyncMessage(
|
|
|
|
dataMessage,
|
|
|
|
this.get('sent_at'),
|
|
|
|
this.get('destination'),
|
|
|
|
this.get('expirationStartTimestamp')
|
|
|
|
)
|
|
|
|
.then(() => {
|
|
|
|
this.save({ synced: true, dataMessage: null });
|
|
|
|
});
|
2018-04-10 23:51:48 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
saveErrors(providedErrors) {
|
|
|
|
let errors = providedErrors;
|
|
|
|
|
|
|
|
if (!(errors instanceof Array)) {
|
|
|
|
errors = [errors];
|
|
|
|
}
|
2018-04-27 21:25:04 +00:00
|
|
|
errors.forEach(e => {
|
2018-04-10 23:51:48 +00:00
|
|
|
console.log(
|
|
|
|
'Message.saveErrors:',
|
|
|
|
e && e.reason ? e.reason : null,
|
|
|
|
e && e.stack ? e.stack : e
|
|
|
|
);
|
|
|
|
});
|
2018-04-27 21:25:04 +00:00
|
|
|
errors = errors.map(e => {
|
|
|
|
if (
|
|
|
|
e.constructor === Error ||
|
|
|
|
e.constructor === TypeError ||
|
|
|
|
e.constructor === ReferenceError
|
|
|
|
) {
|
2018-04-10 23:51:48 +00:00
|
|
|
return _.pick(e, 'name', 'message', 'code', 'number', 'reason');
|
|
|
|
}
|
|
|
|
return e;
|
|
|
|
});
|
|
|
|
errors = errors.concat(this.get('errors') || []);
|
|
|
|
|
|
|
|
return this.save({ errors });
|
|
|
|
},
|
|
|
|
|
|
|
|
hasNetworkError() {
|
|
|
|
const error = _.find(
|
|
|
|
this.get('errors'),
|
2018-04-27 21:25:04 +00:00
|
|
|
e =>
|
|
|
|
e.name === 'MessageError' ||
|
|
|
|
e.name === 'OutgoingMessageError' ||
|
|
|
|
e.name === 'SendMessageNetworkError' ||
|
|
|
|
e.name === 'SignedPreKeyRotationError'
|
2018-04-10 23:51:48 +00:00
|
|
|
);
|
|
|
|
return !!error;
|
|
|
|
},
|
|
|
|
removeOutgoingErrors(number) {
|
|
|
|
const errors = _.partition(
|
|
|
|
this.get('errors'),
|
2018-04-27 21:25:04 +00:00
|
|
|
e =>
|
|
|
|
e.number === number &&
|
|
|
|
(e.name === 'MessageError' ||
|
|
|
|
e.name === 'OutgoingMessageError' ||
|
|
|
|
e.name === 'SendMessageNetworkError' ||
|
|
|
|
e.name === 'SignedPreKeyRotationError' ||
|
|
|
|
e.name === 'OutgoingIdentityKeyError')
|
2018-04-10 23:51:48 +00:00
|
|
|
);
|
|
|
|
this.set({ errors: errors[1] });
|
|
|
|
return errors[0][0];
|
|
|
|
},
|
|
|
|
isReplayableError(e) {
|
2018-04-27 21:25:04 +00:00
|
|
|
return (
|
|
|
|
e.name === 'MessageError' ||
|
|
|
|
e.name === 'OutgoingMessageError' ||
|
|
|
|
e.name === 'SendMessageNetworkError' ||
|
|
|
|
e.name === 'SignedPreKeyRotationError' ||
|
|
|
|
e.name === 'OutgoingIdentityKeyError'
|
|
|
|
);
|
2018-04-10 23:51:48 +00:00
|
|
|
},
|
|
|
|
resend(number) {
|
|
|
|
const error = this.removeOutgoingErrors(number);
|
|
|
|
if (error) {
|
|
|
|
const promise = new textsecure.ReplayableError(error).replay();
|
|
|
|
this.send(promise);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
handleDataMessage(dataMessage, confirm) {
|
|
|
|
// 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 go through one of the previous two codepaths
|
|
|
|
const message = this;
|
|
|
|
const source = message.get('source');
|
|
|
|
const type = message.get('type');
|
|
|
|
let conversationId = message.get('conversationId');
|
|
|
|
if (dataMessage.group) {
|
|
|
|
conversationId = dataMessage.group.id;
|
|
|
|
}
|
|
|
|
const GROUP_TYPES = textsecure.protobuf.GroupContext.Type;
|
|
|
|
|
|
|
|
const conversation = ConversationController.get(conversationId);
|
2018-04-27 21:25:04 +00:00
|
|
|
return conversation.queueJob(
|
|
|
|
() =>
|
|
|
|
new Promise(resolve => {
|
|
|
|
const now = new Date().getTime();
|
|
|
|
let attributes = { type: 'private' };
|
|
|
|
if (dataMessage.group) {
|
|
|
|
let groupUpdate = null;
|
|
|
|
attributes = {
|
|
|
|
type: 'group',
|
|
|
|
groupId: dataMessage.group.id,
|
|
|
|
};
|
|
|
|
if (dataMessage.group.type === GROUP_TYPES.UPDATE) {
|
|
|
|
attributes = {
|
|
|
|
type: 'group',
|
|
|
|
groupId: dataMessage.group.id,
|
|
|
|
name: dataMessage.group.name,
|
|
|
|
avatar: dataMessage.group.avatar,
|
|
|
|
members: _.union(
|
|
|
|
dataMessage.group.members,
|
|
|
|
conversation.get('members')
|
|
|
|
),
|
|
|
|
};
|
|
|
|
groupUpdate =
|
|
|
|
conversation.changedAttributes(
|
|
|
|
_.pick(dataMessage.group, 'name', 'avatar')
|
|
|
|
) || {};
|
|
|
|
const difference = _.difference(
|
|
|
|
attributes.members,
|
|
|
|
conversation.get('members')
|
|
|
|
);
|
|
|
|
if (difference.length > 0) {
|
|
|
|
groupUpdate.joined = difference;
|
|
|
|
}
|
|
|
|
if (conversation.get('left')) {
|
|
|
|
console.log('re-added to a left group');
|
|
|
|
attributes.left = false;
|
|
|
|
}
|
|
|
|
} else if (dataMessage.group.type === GROUP_TYPES.QUIT) {
|
|
|
|
if (source === textsecure.storage.user.getNumber()) {
|
|
|
|
attributes.left = true;
|
|
|
|
groupUpdate = { left: 'You' };
|
|
|
|
} else {
|
|
|
|
groupUpdate = { left: source };
|
|
|
|
}
|
|
|
|
attributes.members = _.without(
|
|
|
|
conversation.get('members'),
|
|
|
|
source
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (groupUpdate !== null) {
|
|
|
|
message.set({ group_update: groupUpdate });
|
|
|
|
}
|
2018-04-10 23:51:48 +00:00
|
|
|
}
|
2018-04-27 21:25:04 +00:00
|
|
|
message.set({
|
|
|
|
attachments: dataMessage.attachments,
|
|
|
|
body: dataMessage.body,
|
2018-04-27 16:32:31 +00:00
|
|
|
contact: dataMessage.contact,
|
2018-04-27 21:25:04 +00:00
|
|
|
conversationId: conversation.id,
|
|
|
|
decrypted_at: now,
|
|
|
|
errors: [],
|
|
|
|
flags: dataMessage.flags,
|
|
|
|
hasAttachments: dataMessage.hasAttachments,
|
|
|
|
hasFileAttachments: dataMessage.hasFileAttachments,
|
|
|
|
hasVisualMediaAttachments: dataMessage.hasVisualMediaAttachments,
|
|
|
|
quote: dataMessage.quote,
|
|
|
|
schemaVersion: dataMessage.schemaVersion,
|
|
|
|
});
|
|
|
|
if (type === 'outgoing') {
|
|
|
|
const receipts = Whisper.DeliveryReceipts.forMessage(
|
|
|
|
conversation,
|
|
|
|
message
|
|
|
|
);
|
|
|
|
receipts.forEach(() =>
|
|
|
|
message.set({
|
|
|
|
delivered: (message.get('delivered') || 0) + 1,
|
|
|
|
})
|
|
|
|
);
|
2018-04-10 23:51:48 +00:00
|
|
|
}
|
2018-04-27 21:25:04 +00:00
|
|
|
attributes.active_at = now;
|
|
|
|
conversation.set(attributes);
|
|
|
|
|
|
|
|
if (message.isExpirationTimerUpdate()) {
|
|
|
|
message.set({
|
|
|
|
expirationTimerUpdate: {
|
|
|
|
source,
|
|
|
|
expireTimer: dataMessage.expireTimer,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
conversation.set({ expireTimer: dataMessage.expireTimer });
|
|
|
|
} else if (dataMessage.expireTimer) {
|
|
|
|
message.set({ expireTimer: dataMessage.expireTimer });
|
2017-06-15 19:27:41 +00:00
|
|
|
}
|
2015-12-04 20:02:19 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
// NOTE: Remove once the above uses
|
|
|
|
// `Conversation::updateExpirationTimer`:
|
|
|
|
const { expireTimer } = dataMessage;
|
|
|
|
const shouldLogExpireTimerChange =
|
|
|
|
message.isExpirationTimerUpdate() || expireTimer;
|
|
|
|
if (shouldLogExpireTimerChange) {
|
2018-05-03 15:53:39 +00:00
|
|
|
console.log("Update conversation 'expireTimer'", {
|
|
|
|
id: conversation.idForLogging(),
|
2018-04-27 21:25:04 +00:00
|
|
|
expireTimer,
|
2018-05-03 15:53:39 +00:00
|
|
|
source: 'handleDataMessage',
|
|
|
|
});
|
2018-04-10 23:51:48 +00:00
|
|
|
}
|
2015-11-20 00:57:48 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
if (!message.isEndSession() && !message.isGroupUpdate()) {
|
|
|
|
if (dataMessage.expireTimer) {
|
|
|
|
if (
|
|
|
|
dataMessage.expireTimer !== conversation.get('expireTimer')
|
|
|
|
) {
|
|
|
|
conversation.updateExpirationTimer(
|
|
|
|
dataMessage.expireTimer,
|
|
|
|
source,
|
|
|
|
message.get('received_at')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else if (conversation.get('expireTimer')) {
|
|
|
|
conversation.updateExpirationTimer(
|
|
|
|
null,
|
|
|
|
source,
|
|
|
|
message.get('received_at')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (type === 'incoming') {
|
|
|
|
const readSync = Whisper.ReadSyncs.forMessage(message);
|
|
|
|
if (readSync) {
|
|
|
|
if (
|
|
|
|
message.get('expireTimer') &&
|
|
|
|
!message.get('expirationStartTimestamp')
|
|
|
|
) {
|
|
|
|
message.set(
|
|
|
|
'expirationStartTimestamp',
|
|
|
|
readSync.get('read_at')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (readSync || message.isExpirationTimerUpdate()) {
|
|
|
|
message.unset('unread');
|
2018-04-27 21:27:32 +00:00
|
|
|
// This is primarily to allow the conversation to mark all older
|
|
|
|
// messages as read, as is done when we receive a read sync for
|
|
|
|
// a message we already know about.
|
2018-04-27 21:25:04 +00:00
|
|
|
Whisper.ReadSyncs.notifyConversation(message);
|
|
|
|
} else {
|
|
|
|
conversation.set(
|
|
|
|
'unreadCount',
|
|
|
|
conversation.get('unreadCount') + 1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2017-07-03 23:46:39 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
if (type === 'outgoing') {
|
|
|
|
const reads = Whisper.ReadReceipts.forMessage(
|
|
|
|
conversation,
|
|
|
|
message
|
|
|
|
);
|
|
|
|
if (reads.length) {
|
|
|
|
const readBy = reads.map(receipt => receipt.get('reader'));
|
|
|
|
message.set({
|
|
|
|
read_by: _.union(message.get('read_by'), readBy),
|
|
|
|
});
|
|
|
|
}
|
2017-07-03 23:46:39 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
message.set({ recipients: conversation.getRecipients() });
|
|
|
|
}
|
2017-07-03 23:46:39 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
const conversationTimestamp = conversation.get('timestamp');
|
|
|
|
if (
|
|
|
|
!conversationTimestamp ||
|
|
|
|
message.get('sent_at') > conversationTimestamp
|
|
|
|
) {
|
|
|
|
conversation.set({
|
|
|
|
lastMessage: message.getNotificationText(),
|
|
|
|
timestamp: message.get('sent_at'),
|
|
|
|
});
|
|
|
|
}
|
2018-04-10 23:51:48 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
if (dataMessage.profileKey) {
|
|
|
|
const profileKey = dataMessage.profileKey.toArrayBuffer();
|
|
|
|
if (source === textsecure.storage.user.getNumber()) {
|
|
|
|
conversation.set({ profileSharing: true });
|
|
|
|
} else if (conversation.isPrivate()) {
|
|
|
|
conversation.set({ profileKey });
|
|
|
|
} else {
|
|
|
|
ConversationController.getOrCreateAndWait(
|
|
|
|
source,
|
|
|
|
'private'
|
|
|
|
).then(sender => {
|
|
|
|
sender.setProfileKey(profileKey);
|
|
|
|
});
|
|
|
|
}
|
2018-04-10 23:51:48 +00:00
|
|
|
}
|
2017-07-03 23:46:39 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
const handleError = error => {
|
|
|
|
const errorForLog = error && error.stack ? error.stack : error;
|
|
|
|
console.log(
|
|
|
|
'handleDataMessage',
|
|
|
|
message.idForLogging(),
|
|
|
|
'error:',
|
|
|
|
errorForLog
|
|
|
|
);
|
|
|
|
return resolve();
|
|
|
|
};
|
2015-09-28 20:33:26 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
message.save().then(() => {
|
|
|
|
conversation.save().then(() => {
|
|
|
|
try {
|
|
|
|
conversation.trigger('newmessage', message);
|
|
|
|
} catch (e) {
|
|
|
|
return handleError(e);
|
|
|
|
}
|
2018-04-27 21:27:32 +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!
|
2018-04-27 21:25:04 +00:00
|
|
|
const previousUnread = message.get('unread');
|
|
|
|
return message.fetch().then(
|
|
|
|
() => {
|
|
|
|
try {
|
|
|
|
if (previousUnread !== message.get('unread')) {
|
|
|
|
console.log(
|
|
|
|
'Caught race condition on new message read state! ' +
|
|
|
|
'Manually starting timers.'
|
|
|
|
);
|
2018-04-27 21:27:32 +00:00
|
|
|
// We call markRead() even though the message is already
|
|
|
|
// marked read because we need to start expiration
|
|
|
|
// timers, etc.
|
2018-04-27 21:25:04 +00:00
|
|
|
message.markRead();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message.get('unread')) {
|
|
|
|
return conversation.notify(message).then(() => {
|
|
|
|
confirm();
|
|
|
|
return resolve();
|
|
|
|
}, handleError);
|
|
|
|
}
|
|
|
|
|
|
|
|
confirm();
|
|
|
|
return resolve();
|
|
|
|
} catch (e) {
|
|
|
|
return handleError(e);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
try {
|
|
|
|
console.log(
|
|
|
|
'handleDataMessage: Message',
|
|
|
|
message.idForLogging(),
|
|
|
|
'was deleted'
|
|
|
|
);
|
|
|
|
|
|
|
|
confirm();
|
|
|
|
return resolve();
|
|
|
|
} catch (e) {
|
|
|
|
return handleError(e);
|
|
|
|
}
|
|
|
|
}
|
2017-11-30 19:56:29 +00:00
|
|
|
);
|
2018-04-27 21:25:04 +00:00
|
|
|
}, handleError);
|
|
|
|
}, handleError);
|
|
|
|
})
|
|
|
|
);
|
2018-04-10 23:51:48 +00:00
|
|
|
},
|
|
|
|
markRead(readAt) {
|
|
|
|
this.unset('unread');
|
|
|
|
if (this.get('expireTimer') && !this.get('expirationStartTimestamp')) {
|
|
|
|
this.set('expirationStartTimestamp', readAt || Date.now());
|
|
|
|
}
|
2018-04-27 21:25:04 +00:00
|
|
|
Whisper.Notifications.remove(
|
|
|
|
Whisper.Notifications.where({
|
|
|
|
messageId: this.id,
|
|
|
|
})
|
|
|
|
);
|
2018-04-11 00:00:30 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-04-10 23:51:48 +00:00
|
|
|
this.save().then(resolve, reject);
|
2018-04-11 00:00:30 +00:00
|
|
|
});
|
2018-04-10 23:51:48 +00:00
|
|
|
},
|
|
|
|
isExpiring() {
|
|
|
|
return this.get('expireTimer') && this.get('expirationStartTimestamp');
|
|
|
|
},
|
|
|
|
isExpired() {
|
|
|
|
return this.msTilExpire() <= 0;
|
|
|
|
},
|
|
|
|
msTilExpire() {
|
|
|
|
if (!this.isExpiring()) {
|
|
|
|
return Infinity;
|
|
|
|
}
|
|
|
|
const now = Date.now();
|
|
|
|
const start = this.get('expirationStartTimestamp');
|
|
|
|
const delta = this.get('expireTimer') * 1000;
|
2018-04-27 21:25:04 +00:00
|
|
|
let msFromNow = start + delta - now;
|
2018-04-10 23:51:48 +00:00
|
|
|
if (msFromNow < 0) {
|
|
|
|
msFromNow = 0;
|
|
|
|
}
|
|
|
|
return msFromNow;
|
|
|
|
},
|
|
|
|
setToExpire() {
|
|
|
|
if (this.isExpiring() && !this.get('expires_at')) {
|
|
|
|
const start = this.get('expirationStartTimestamp');
|
|
|
|
const delta = this.get('expireTimer') * 1000;
|
|
|
|
const expiresAt = start + delta;
|
|
|
|
|
|
|
|
// 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', expiresAt);
|
2014-11-13 22:35:37 +00:00
|
|
|
}
|
2015-03-18 23:26:55 +00:00
|
|
|
|
2018-04-10 23:51:48 +00:00
|
|
|
Whisper.ExpiringMessagesListener.update();
|
2018-05-03 17:10:44 +00:00
|
|
|
console.log('Set message expiration', {
|
|
|
|
expiresAt,
|
|
|
|
sentAt: this.get('sent_at'),
|
|
|
|
});
|
2018-04-10 23:51:48 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Whisper.MessageCollection = Backbone.Collection.extend({
|
|
|
|
model: Whisper.Message,
|
|
|
|
database: Whisper.Database,
|
|
|
|
storeName: 'messages',
|
|
|
|
comparator(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);
|
|
|
|
},
|
|
|
|
initialize(models, options) {
|
|
|
|
if (options) {
|
|
|
|
this.conversation = options.conversation;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
destroyAll() {
|
2018-04-27 21:25:04 +00:00
|
|
|
return Promise.all(
|
|
|
|
this.models.map(
|
|
|
|
m =>
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
m
|
|
|
|
.destroy()
|
|
|
|
.then(resolve)
|
|
|
|
.fail(reject);
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
2018-04-10 23:51:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
fetchSentAt(timestamp) {
|
2018-04-27 21:25:04 +00:00
|
|
|
return new Promise(resolve =>
|
|
|
|
this.fetch({
|
|
|
|
index: {
|
|
|
|
// 'receipt' index on sent_at
|
|
|
|
name: 'receipt',
|
|
|
|
only: timestamp,
|
|
|
|
},
|
|
|
|
}).always(resolve)
|
|
|
|
);
|
2018-04-10 23:51:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getLoadedUnreadCount() {
|
|
|
|
return this.reduce((total, model) => {
|
|
|
|
const unread = model.get('unread') && model.isIncoming();
|
|
|
|
return total + (unread ? 1 : 0);
|
|
|
|
}, 0);
|
|
|
|
},
|
|
|
|
|
|
|
|
fetchConversation(conversationId, providedLimit, providedUnreadCount) {
|
|
|
|
let limit = providedLimit;
|
|
|
|
let unreadCount = providedUnreadCount;
|
|
|
|
|
|
|
|
if (typeof limit !== 'number') {
|
|
|
|
limit = 100;
|
|
|
|
}
|
|
|
|
if (typeof unreadCount !== 'number') {
|
|
|
|
unreadCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
let startingLoadedUnread = 0;
|
|
|
|
if (unreadCount > 0) {
|
|
|
|
startingLoadedUnread = this.getLoadedUnreadCount();
|
|
|
|
}
|
2018-04-27 21:25:04 +00:00
|
|
|
return new Promise(resolve => {
|
2018-04-10 23:51:48 +00:00
|
|
|
let upper;
|
|
|
|
if (this.length === 0) {
|
|
|
|
// fetch the most recent messages first
|
|
|
|
upper = Number.MAX_VALUE;
|
|
|
|
} else {
|
|
|
|
// not our first rodeo, fetch older messages.
|
|
|
|
upper = this.at(0).get('received_at');
|
|
|
|
}
|
|
|
|
const options = { remove: false, limit };
|
|
|
|
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).always(resolve);
|
2018-04-11 00:00:30 +00:00
|
|
|
}).then(() => {
|
2018-04-10 23:51:48 +00:00
|
|
|
if (unreadCount <= 0) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2015-02-18 02:03:05 +00:00
|
|
|
|
2018-04-10 23:51:48 +00:00
|
|
|
const loadedUnread = this.getLoadedUnreadCount();
|
|
|
|
if (loadedUnread >= unreadCount) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2017-02-21 23:32:40 +00:00
|
|
|
|
2018-04-10 23:51:48 +00:00
|
|
|
if (startingLoadedUnread === loadedUnread) {
|
|
|
|
// that fetch didn't get us any more unread. stop fetching more.
|
|
|
|
return Promise.resolve();
|
2014-11-13 22:35:37 +00:00
|
|
|
}
|
2018-04-10 23:51:48 +00:00
|
|
|
|
|
|
|
console.log('fetchConversation: doing another fetch to get all unread');
|
|
|
|
return this.fetchConversation(conversationId, limit, unreadCount);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
fetchNextExpiring() {
|
|
|
|
this.fetch({ index: { name: 'expires_at' }, limit: 1 });
|
|
|
|
},
|
|
|
|
|
|
|
|
fetchExpired() {
|
2018-05-03 17:10:44 +00:00
|
|
|
console.log('Load expired messages');
|
2018-04-10 23:51:48 +00:00
|
|
|
this.fetch({
|
|
|
|
conditions: { expires_at: { $lte: Date.now() } },
|
|
|
|
addIndividually: true,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
2018-04-27 21:25:04 +00:00
|
|
|
})();
|