2019-03-26 01:10:30 +00:00
|
|
|
/* global
|
|
|
|
_,
|
|
|
|
Backbone,
|
|
|
|
storage,
|
|
|
|
filesize,
|
|
|
|
ConversationController,
|
|
|
|
MessageController,
|
|
|
|
getAccountManager,
|
|
|
|
i18n,
|
|
|
|
Signal,
|
|
|
|
textsecure,
|
|
|
|
Whisper
|
|
|
|
*/
|
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 || {};
|
|
|
|
|
2019-02-20 18:28:57 +00:00
|
|
|
const { Message: TypedMessage, Contact, PhoneNumber, Errors } = Signal.Types;
|
2018-07-09 21:29:13 +00:00
|
|
|
const {
|
2018-07-24 18:55:24 +00:00
|
|
|
deleteExternalMessageFiles,
|
2018-07-09 21:29:13 +00:00
|
|
|
getAbsoluteAttachmentPath,
|
2018-07-27 01:13:56 +00:00
|
|
|
loadAttachmentData,
|
|
|
|
loadQuoteData,
|
2019-01-16 03:03:56 +00:00
|
|
|
loadPreviewData,
|
2019-05-16 22:32:11 +00:00
|
|
|
loadStickerData,
|
2019-02-20 18:28:57 +00:00
|
|
|
upgradeMessageSchema,
|
2018-07-27 01:13:56 +00:00
|
|
|
} = window.Signal.Migrations;
|
2019-05-16 22:32:11 +00:00
|
|
|
const {
|
|
|
|
copyStickerToAttachments,
|
|
|
|
deletePackReference,
|
2019-05-24 01:27:42 +00:00
|
|
|
savePackMetadata,
|
2019-05-16 22:32:11 +00:00
|
|
|
getStickerPackStatus,
|
|
|
|
} = window.Signal.Stickers;
|
2019-10-03 19:03:46 +00:00
|
|
|
const { GoogleChrome } = window.Signal.Util;
|
|
|
|
|
2019-12-13 21:25:36 +00:00
|
|
|
const { addStickerPackReference, getMessageBySender } = window.Signal.Data;
|
2019-03-13 20:38:28 +00:00
|
|
|
const { bytesFromString } = window.Signal.Crypto;
|
2018-07-09 21:29:13 +00:00
|
|
|
|
|
|
|
window.AccountCache = Object.create(null);
|
|
|
|
window.AccountJobs = Object.create(null);
|
|
|
|
|
2019-03-20 17:42:28 +00:00
|
|
|
window.doesAccountCheckJobExist = number =>
|
2018-07-09 21:29:13 +00:00
|
|
|
Boolean(window.AccountJobs[number]);
|
|
|
|
window.checkForSignalAccount = number => {
|
|
|
|
if (window.AccountJobs[number]) {
|
|
|
|
return window.AccountJobs[number];
|
|
|
|
}
|
|
|
|
|
2018-08-07 19:33:56 +00:00
|
|
|
let job;
|
|
|
|
if (textsecure.messaging) {
|
|
|
|
// eslint-disable-next-line more/no-then
|
|
|
|
job = textsecure.messaging
|
|
|
|
.getProfile(number)
|
|
|
|
.then(() => {
|
|
|
|
window.AccountCache[number] = true;
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
window.AccountCache[number] = false;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// We're offline!
|
|
|
|
job = Promise.resolve().then(() => {
|
2018-07-09 21:29:13 +00:00
|
|
|
window.AccountCache[number] = false;
|
|
|
|
});
|
2018-08-07 19:33:56 +00:00
|
|
|
}
|
2018-07-09 21:29:13 +00:00
|
|
|
|
|
|
|
window.AccountJobs[number] = job;
|
|
|
|
|
|
|
|
return job;
|
|
|
|
};
|
|
|
|
|
|
|
|
window.isSignalAccountCheckComplete = number =>
|
|
|
|
window.AccountCache[number] !== undefined;
|
|
|
|
window.hasSignalAccount = number => window.AccountCache[number];
|
2018-04-10 23:51:48 +00:00
|
|
|
|
2020-03-05 21:14:58 +00:00
|
|
|
const includesAny = (haystack, ...needles) =>
|
|
|
|
needles.some(needle => haystack.includes(needle));
|
|
|
|
|
2018-04-10 23:51:48 +00:00
|
|
|
window.Whisper.Message = Backbone.Model.extend({
|
|
|
|
initialize(attributes) {
|
|
|
|
if (_.isObject(attributes)) {
|
2018-07-21 19:00:08 +00:00
|
|
|
this.set(
|
|
|
|
TypedMessage.initializeSchemaVersion({
|
|
|
|
message: attributes,
|
|
|
|
logger: window.log,
|
|
|
|
})
|
|
|
|
);
|
2018-04-10 23:51:48 +00:00
|
|
|
}
|
|
|
|
|
2019-06-10 21:40:02 +00:00
|
|
|
this.CURRENT_PROTOCOL_VERSION =
|
|
|
|
textsecure.protobuf.DataMessage.ProtocolVersion.CURRENT;
|
|
|
|
this.INITIAL_PROTOCOL_VERSION =
|
|
|
|
textsecure.protobuf.DataMessage.ProtocolVersion.INITIAL;
|
2018-07-09 21:29:13 +00:00
|
|
|
this.OUR_NUMBER = textsecure.storage.user.getNumber();
|
2020-03-05 21:14:58 +00:00
|
|
|
this.OUR_UUID = textsecure.storage.user.getUuid();
|
2018-07-09 21:29:13 +00:00
|
|
|
|
2018-04-10 23:51:48 +00:00
|
|
|
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-08-09 23:18:10 +00:00
|
|
|
this.on('expired', this.onExpired);
|
2018-04-10 23:51:48 +00:00
|
|
|
this.setToExpire();
|
2019-01-14 21:49:58 +00:00
|
|
|
|
2019-05-31 22:42:01 +00:00
|
|
|
this.on('change', this.notifyRedux);
|
|
|
|
},
|
2019-01-14 21:49:58 +00:00
|
|
|
|
2019-05-31 22:42:01 +00:00
|
|
|
notifyRedux() {
|
|
|
|
const { messageChanged } = window.reduxActions.conversations;
|
2019-01-14 21:49:58 +00:00
|
|
|
|
2019-05-31 22:42:01 +00:00
|
|
|
if (messageChanged) {
|
|
|
|
const conversationId = this.get('conversationId');
|
|
|
|
// Note: The clone is important for triggering a re-run of selectors
|
2019-09-18 20:39:03 +00:00
|
|
|
messageChanged(this.id, conversationId, this.getReduxData());
|
2019-01-14 21:49:58 +00:00
|
|
|
}
|
2019-05-31 22:42:01 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getReduxData() {
|
|
|
|
const contact = this.getPropsForEmbeddedContact();
|
2019-01-14 21:49:58 +00:00
|
|
|
|
2019-05-31 22:42:01 +00:00
|
|
|
return {
|
|
|
|
...this.attributes,
|
|
|
|
// We need this in the reducer to detect if the message's height has changed
|
|
|
|
hasSignalAccount: contact ? Boolean(contact.signalAccount) : null,
|
|
|
|
};
|
2018-04-10 23:51:48 +00:00
|
|
|
},
|
2019-03-20 17:42:28 +00:00
|
|
|
|
2019-11-07 21:36:16 +00:00
|
|
|
isNormalBubble() {
|
|
|
|
return (
|
2020-07-31 00:10:54 +00:00
|
|
|
!this.isCallHistory() &&
|
|
|
|
!this.isEndSession() &&
|
2019-11-07 21:36:16 +00:00
|
|
|
!this.isExpirationTimerUpdate() &&
|
2020-07-31 00:10:54 +00:00
|
|
|
!this.isGroupUpdate() &&
|
2019-11-07 21:36:16 +00:00
|
|
|
!this.isKeyChange() &&
|
2020-03-10 00:43:09 +00:00
|
|
|
!this.isMessageHistoryUnsynced() &&
|
2020-07-31 00:10:54 +00:00
|
|
|
!this.isProfileChange() &&
|
|
|
|
!this.isUnsupportedMessage() &&
|
|
|
|
!this.isVerifiedChange()
|
2019-11-07 21:36:16 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2019-03-20 17:42:28 +00:00
|
|
|
// Top-level prop generation for the message bubble
|
2019-05-31 22:42:01 +00:00
|
|
|
getPropsForBubble() {
|
2019-06-10 21:40:02 +00:00
|
|
|
if (this.isUnsupportedMessage()) {
|
2019-05-31 22:42:01 +00:00
|
|
|
return {
|
2019-06-10 21:40:02 +00:00
|
|
|
type: 'unsupportedMessage',
|
|
|
|
data: this.getPropsForUnsupportedMessage(),
|
|
|
|
};
|
2020-03-10 00:43:09 +00:00
|
|
|
} else if (this.isMessageHistoryUnsynced()) {
|
|
|
|
return {
|
|
|
|
type: 'linkNotification',
|
|
|
|
data: null,
|
|
|
|
};
|
2019-06-10 21:40:02 +00:00
|
|
|
} else if (this.isExpirationTimerUpdate()) {
|
2019-05-31 22:42:01 +00:00
|
|
|
return {
|
2019-03-20 17:42:28 +00:00
|
|
|
type: 'timerNotification',
|
|
|
|
data: this.getPropsForTimerNotification(),
|
|
|
|
};
|
|
|
|
} else if (this.isKeyChange()) {
|
2019-05-31 22:42:01 +00:00
|
|
|
return {
|
2019-03-20 17:42:28 +00:00
|
|
|
type: 'safetyNumberNotification',
|
|
|
|
data: this.getPropsForSafetyNumberNotification(),
|
|
|
|
};
|
|
|
|
} else if (this.isVerifiedChange()) {
|
2019-05-31 22:42:01 +00:00
|
|
|
return {
|
2019-03-20 17:42:28 +00:00
|
|
|
type: 'verificationNotification',
|
|
|
|
data: this.getPropsForVerificationNotification(),
|
|
|
|
};
|
|
|
|
} else if (this.isGroupUpdate()) {
|
2019-05-31 22:42:01 +00:00
|
|
|
return {
|
2019-03-20 17:42:28 +00:00
|
|
|
type: 'groupNotification',
|
|
|
|
data: this.getPropsForGroupNotification(),
|
|
|
|
};
|
|
|
|
} else if (this.isEndSession()) {
|
2019-05-31 22:42:01 +00:00
|
|
|
return {
|
2019-03-20 17:42:28 +00:00
|
|
|
type: 'resetSessionNotification',
|
|
|
|
data: this.getPropsForResetSessionNotification(),
|
|
|
|
};
|
2020-06-04 18:16:19 +00:00
|
|
|
} else if (this.isCallHistory()) {
|
|
|
|
return {
|
|
|
|
type: 'callHistory',
|
|
|
|
data: this.getPropsForCallHistory(),
|
|
|
|
};
|
2020-07-29 23:20:05 +00:00
|
|
|
} else if (this.isProfileChange()) {
|
|
|
|
return {
|
|
|
|
type: 'profileChange',
|
|
|
|
data: this.getPropsForProfileChange(),
|
|
|
|
};
|
2019-03-20 17:42:28 +00:00
|
|
|
}
|
2019-05-31 22:42:01 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
type: 'message',
|
|
|
|
data: this.getPropsForMessage(),
|
|
|
|
};
|
2018-04-10 23:51:48 +00:00
|
|
|
},
|
2019-03-20 17:42:28 +00:00
|
|
|
|
|
|
|
// Other top-level prop-generation
|
|
|
|
getPropsForSearchResult() {
|
2020-07-10 18:28:49 +00:00
|
|
|
const ourId = ConversationController.getOurConversationId();
|
|
|
|
const sourceId = this.getContactId();
|
|
|
|
const fromContact = this.findAndFormatContact(sourceId);
|
2020-03-05 21:14:58 +00:00
|
|
|
|
2020-07-10 18:28:49 +00:00
|
|
|
if (ourId === sourceId) {
|
2020-03-05 21:14:58 +00:00
|
|
|
fromContact.isMe = true;
|
2019-03-20 17:42:28 +00:00
|
|
|
}
|
|
|
|
|
2020-06-18 22:28:45 +00:00
|
|
|
const convo = this.getConversation();
|
|
|
|
|
2020-07-10 18:28:49 +00:00
|
|
|
const to = convo ? this.findAndFormatContact(convo.get('id')) : {};
|
2020-06-18 22:28:45 +00:00
|
|
|
|
2020-07-10 18:28:49 +00:00
|
|
|
if (to && convo && convo.isMe()) {
|
2019-03-20 17:42:28 +00:00
|
|
|
to.isMe = true;
|
|
|
|
}
|
|
|
|
|
2018-04-10 23:51:48 +00:00
|
|
|
return {
|
2020-06-18 22:28:45 +00:00
|
|
|
from: fromContact || {},
|
2019-03-20 17:42:28 +00:00
|
|
|
to,
|
|
|
|
|
|
|
|
isSelected: this.isSelected,
|
|
|
|
|
|
|
|
id: this.id,
|
|
|
|
conversationId: this.get('conversationId'),
|
2019-12-02 21:40:38 +00:00
|
|
|
sentAt: this.get('sent_at'),
|
2019-03-20 17:42:28 +00:00
|
|
|
snippet: this.get('snippet'),
|
2018-04-10 23:51:48 +00:00
|
|
|
};
|
|
|
|
},
|
2019-03-20 17:42:28 +00:00
|
|
|
getPropsForMessageDetail() {
|
|
|
|
const newIdentity = i18n('newIdentity');
|
|
|
|
const OUTGOING_KEY_ERROR = 'OutgoingIdentityKeyError';
|
|
|
|
|
|
|
|
const unidentifiedLookup = (
|
|
|
|
this.get('unidentifiedDeliveries') || []
|
2020-07-10 18:28:49 +00:00
|
|
|
).reduce((accumulator, identifier) => {
|
2019-03-20 17:42:28 +00:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
2020-03-19 17:57:50 +00:00
|
|
|
accumulator[
|
2020-07-10 18:28:49 +00:00
|
|
|
ConversationController.getConversationId(identifier)
|
2020-03-19 17:57:50 +00:00
|
|
|
] = true;
|
2019-03-20 17:42:28 +00:00
|
|
|
return accumulator;
|
|
|
|
}, Object.create(null));
|
|
|
|
|
|
|
|
// We include numbers we didn't successfully send to so we can display errors.
|
|
|
|
// Older messages don't have the recipients included on the message, so we fall
|
|
|
|
// back to the conversation's current recipients
|
2020-03-05 21:14:58 +00:00
|
|
|
const conversationIds = this.isIncoming()
|
2020-07-10 18:28:49 +00:00
|
|
|
? [this.getContactId()]
|
2019-03-20 17:42:28 +00:00
|
|
|
: _.union(
|
2020-03-05 21:14:58 +00:00
|
|
|
(this.get('sent_to') || []).map(id =>
|
|
|
|
ConversationController.getConversationId(id)
|
|
|
|
),
|
|
|
|
(
|
|
|
|
this.get('recipients') || this.getConversation().getRecipients()
|
|
|
|
).map(id => ConversationController.getConversationId(id))
|
2019-03-20 17:42:28 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// This will make the error message for outgoing key errors a bit nicer
|
|
|
|
const allErrors = (this.get('errors') || []).map(error => {
|
|
|
|
if (error.name === OUTGOING_KEY_ERROR) {
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
error.message = newIdentity;
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
});
|
|
|
|
|
|
|
|
// If an error has a specific number it's associated with, we'll show it next to
|
|
|
|
// that contact. Otherwise, it will be a standalone entry.
|
2020-03-20 19:01:15 +00:00
|
|
|
const errors = _.reject(allErrors, error =>
|
|
|
|
Boolean(error.identifer || error.number)
|
|
|
|
);
|
2019-03-20 17:42:28 +00:00
|
|
|
const errorsGroupedById = _.groupBy(allErrors, 'number');
|
2020-03-05 21:14:58 +00:00
|
|
|
const finalContacts = (conversationIds || []).map(id => {
|
2019-03-20 17:42:28 +00:00
|
|
|
const errorsForContact = errorsGroupedById[id];
|
|
|
|
const isOutgoingKeyError = Boolean(
|
|
|
|
_.find(errorsForContact, error => error.name === OUTGOING_KEY_ERROR)
|
|
|
|
);
|
|
|
|
const isUnidentifiedDelivery =
|
|
|
|
storage.get('unidentifiedDeliveryIndicators') &&
|
|
|
|
this.isUnidentifiedDelivery(id, unidentifiedLookup);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...this.findAndFormatContact(id),
|
|
|
|
|
|
|
|
status: this.getStatus(id),
|
|
|
|
errors: errorsForContact,
|
|
|
|
isOutgoingKeyError,
|
|
|
|
isUnidentifiedDelivery,
|
|
|
|
onSendAnyway: () =>
|
|
|
|
this.trigger('force-send', { contactId: id, messageId: this.id }),
|
|
|
|
onShowSafetyNumber: () => this.trigger('show-identity', id),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
// The prefix created here ensures that contacts with errors are listed
|
|
|
|
// first; otherwise it's alphabetical
|
|
|
|
const sortedContacts = _.sortBy(
|
|
|
|
finalContacts,
|
|
|
|
contact => `${contact.errors ? '0' : '1'}${contact.title}`
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
sentAt: this.get('sent_at'),
|
|
|
|
receivedAt: this.get('received_at'),
|
|
|
|
message: {
|
|
|
|
...this.getPropsForMessage(),
|
|
|
|
disableMenu: true,
|
|
|
|
disableScroll: true,
|
|
|
|
// To ensure that group avatar doesn't show up
|
|
|
|
conversationType: 'direct',
|
2019-05-31 22:42:01 +00:00
|
|
|
downloadNewVersion: () => {
|
|
|
|
this.trigger('download-new-version');
|
|
|
|
},
|
|
|
|
deleteMessage: messageId => {
|
|
|
|
this.trigger('delete', messageId);
|
|
|
|
},
|
|
|
|
showVisualAttachment: options => {
|
|
|
|
this.trigger('show-visual-attachment', options);
|
|
|
|
},
|
|
|
|
displayTapToViewMessage: messageId => {
|
|
|
|
this.trigger('display-tap-to-view-message', messageId);
|
|
|
|
},
|
|
|
|
openLink: url => {
|
|
|
|
this.trigger('navigate-to', url);
|
|
|
|
},
|
2020-01-23 23:57:37 +00:00
|
|
|
reactWith: emoji => {
|
|
|
|
this.trigger('react-with', emoji);
|
|
|
|
},
|
2019-03-20 17:42:28 +00:00
|
|
|
},
|
|
|
|
errors,
|
|
|
|
contacts: sortedContacts,
|
|
|
|
};
|
2018-04-10 23:51:48 +00:00
|
|
|
},
|
2019-03-20 17:42:28 +00:00
|
|
|
|
|
|
|
// Bucketing messages
|
2019-06-10 21:40:02 +00:00
|
|
|
isUnsupportedMessage() {
|
|
|
|
const versionAtReceive = this.get('supportedVersionAtReceive');
|
|
|
|
const requiredVersion = this.get('requiredProtocolVersion');
|
|
|
|
|
|
|
|
return (
|
|
|
|
_.isNumber(versionAtReceive) &&
|
|
|
|
_.isNumber(requiredVersion) &&
|
|
|
|
versionAtReceive < requiredVersion
|
|
|
|
);
|
|
|
|
},
|
2018-04-10 23:51:48 +00:00
|
|
|
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
|
2019-05-31 22:42:01 +00:00
|
|
|
return Boolean(this.get('flags') & flag);
|
2018-04-10 23:51:48 +00:00
|
|
|
},
|
|
|
|
isKeyChange() {
|
|
|
|
return this.get('type') === 'keychange';
|
|
|
|
},
|
2019-03-20 17:42:28 +00:00
|
|
|
isVerifiedChange() {
|
|
|
|
return this.get('type') === 'verified-change';
|
2018-04-10 23:51:48 +00:00
|
|
|
},
|
2020-03-10 00:43:09 +00:00
|
|
|
isMessageHistoryUnsynced() {
|
|
|
|
return this.get('type') === 'message-history-unsynced';
|
|
|
|
},
|
2019-03-20 17:42:28 +00:00
|
|
|
isGroupUpdate() {
|
|
|
|
return !!this.get('group_update');
|
2018-04-10 23:51:48 +00:00
|
|
|
},
|
2019-03-20 17:42:28 +00:00
|
|
|
isEndSession() {
|
|
|
|
const flag = textsecure.protobuf.DataMessage.Flags.END_SESSION;
|
|
|
|
// eslint-disable-next-line no-bitwise
|
|
|
|
return !!(this.get('flags') & flag);
|
2018-08-09 23:18:10 +00:00
|
|
|
},
|
2020-06-04 18:16:19 +00:00
|
|
|
isCallHistory() {
|
|
|
|
return this.get('type') === 'call-history';
|
|
|
|
},
|
2020-07-29 23:20:05 +00:00
|
|
|
isProfileChange() {
|
|
|
|
return this.get('type') === 'profile-change';
|
|
|
|
},
|
2019-03-20 17:42:28 +00:00
|
|
|
|
|
|
|
// Props for each message type
|
2019-06-10 21:40:02 +00:00
|
|
|
getPropsForUnsupportedMessage() {
|
|
|
|
const requiredVersion = this.get('requiredProtocolVersion');
|
|
|
|
const canProcessNow = this.CURRENT_PROTOCOL_VERSION >= requiredVersion;
|
2020-07-10 18:28:49 +00:00
|
|
|
const sourceId = this.getContactId();
|
2019-06-10 21:40:02 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
canProcessNow,
|
2020-07-10 18:28:49 +00:00
|
|
|
contact: this.findAndFormatContact(sourceId),
|
2019-06-10 21:40:02 +00:00
|
|
|
};
|
|
|
|
},
|
2018-07-09 21:29:13 +00:00
|
|
|
getPropsForTimerNotification() {
|
2019-03-13 17:46:00 +00:00
|
|
|
const timerUpdate = this.get('expirationTimerUpdate');
|
|
|
|
if (!timerUpdate) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-03-05 21:14:58 +00:00
|
|
|
const { expireTimer, fromSync, source, sourceUuid } = timerUpdate;
|
2018-07-09 21:29:13 +00:00
|
|
|
const timespan = Whisper.ExpirationTimerOptions.getName(expireTimer || 0);
|
2018-07-24 21:38:58 +00:00
|
|
|
const disabled = !expireTimer;
|
2018-07-09 21:29:13 +00:00
|
|
|
|
2020-07-10 18:28:49 +00:00
|
|
|
const sourceId = ConversationController.ensureContactIds({
|
|
|
|
e164: source,
|
|
|
|
uuid: sourceUuid,
|
|
|
|
});
|
|
|
|
const ourId = ConversationController.getOurConversationId();
|
|
|
|
|
2018-07-09 21:29:13 +00:00
|
|
|
const basicProps = {
|
2020-07-10 18:28:49 +00:00
|
|
|
...this.findAndFormatContact(sourceId),
|
2019-03-20 17:42:28 +00:00
|
|
|
type: 'fromOther',
|
2018-07-09 21:29:13 +00:00
|
|
|
timespan,
|
2018-07-24 21:38:58 +00:00
|
|
|
disabled,
|
2018-07-09 21:29:13 +00:00
|
|
|
};
|
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
if (fromSync) {
|
2018-07-09 21:29:13 +00:00
|
|
|
return {
|
|
|
|
...basicProps,
|
2018-07-27 01:13:56 +00:00
|
|
|
type: 'fromSync',
|
2018-07-09 21:29:13 +00:00
|
|
|
};
|
2020-07-10 18:28:49 +00:00
|
|
|
} else if (sourceId && sourceId === ourId) {
|
2018-07-09 21:29:13 +00:00
|
|
|
return {
|
|
|
|
...basicProps,
|
2018-07-27 01:13:56 +00:00
|
|
|
type: 'fromMe',
|
2018-07-09 21:29:13 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return basicProps;
|
|
|
|
},
|
|
|
|
getPropsForSafetyNumberNotification() {
|
|
|
|
const conversation = this.getConversation();
|
|
|
|
const isGroup = conversation && !conversation.isPrivate();
|
2020-04-13 18:49:10 +00:00
|
|
|
const identifier = this.get('key_changed');
|
2018-07-09 21:29:13 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
isGroup,
|
2020-04-13 18:49:10 +00:00
|
|
|
contact: this.findAndFormatContact(identifier),
|
2018-07-09 21:29:13 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
getPropsForVerificationNotification() {
|
|
|
|
const type = this.get('verified') ? 'markVerified' : 'markNotVerified';
|
|
|
|
const isLocal = this.get('local');
|
2020-07-10 18:28:49 +00:00
|
|
|
const identifier = this.get('verifiedChanged');
|
2018-07-09 21:29:13 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
type,
|
|
|
|
isLocal,
|
2020-07-10 18:28:49 +00:00
|
|
|
contact: this.findAndFormatContact(identifier),
|
2018-07-09 21:29:13 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
getPropsForGroupNotification() {
|
|
|
|
const groupUpdate = this.get('group_update');
|
|
|
|
const changes = [];
|
|
|
|
|
2020-03-26 21:47:35 +00:00
|
|
|
if (
|
|
|
|
!groupUpdate.avatarUpdated &&
|
|
|
|
!groupUpdate.left &&
|
|
|
|
!groupUpdate.joined &&
|
|
|
|
!groupUpdate.name
|
|
|
|
) {
|
2018-07-09 21:29:13 +00:00
|
|
|
changes.push({
|
|
|
|
type: 'general',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-05 01:13:19 +00:00
|
|
|
const placeholderContact = {
|
|
|
|
title: i18n('unknownContact'),
|
|
|
|
};
|
|
|
|
|
2018-07-09 21:29:13 +00:00
|
|
|
if (groupUpdate.joined) {
|
|
|
|
changes.push({
|
|
|
|
type: 'add',
|
|
|
|
contacts: _.map(
|
|
|
|
Array.isArray(groupUpdate.joined)
|
|
|
|
? groupUpdate.joined
|
|
|
|
: [groupUpdate.joined],
|
2020-08-05 01:13:19 +00:00
|
|
|
identifier =>
|
|
|
|
this.findAndFormatContact(identifier) || placeholderContact
|
2018-07-09 21:29:13 +00:00
|
|
|
),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (groupUpdate.left === 'You') {
|
|
|
|
changes.push({
|
|
|
|
type: 'remove',
|
|
|
|
isMe: true,
|
|
|
|
});
|
|
|
|
} else if (groupUpdate.left) {
|
|
|
|
changes.push({
|
|
|
|
type: 'remove',
|
|
|
|
contacts: _.map(
|
|
|
|
Array.isArray(groupUpdate.left)
|
|
|
|
? groupUpdate.left
|
|
|
|
: [groupUpdate.left],
|
2020-08-05 01:13:19 +00:00
|
|
|
identifier =>
|
|
|
|
this.findAndFormatContact(identifier) || placeholderContact
|
2018-07-09 21:29:13 +00:00
|
|
|
),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (groupUpdate.name) {
|
|
|
|
changes.push({
|
|
|
|
type: 'name',
|
|
|
|
newName: groupUpdate.name,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:47:35 +00:00
|
|
|
if (groupUpdate.avatarUpdated) {
|
|
|
|
changes.push({
|
|
|
|
type: 'avatar',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-10 18:28:49 +00:00
|
|
|
const sourceId = this.getContactId();
|
|
|
|
const from = this.findAndFormatContact(sourceId);
|
2020-03-26 21:47:35 +00:00
|
|
|
|
2018-07-09 21:29:13 +00:00
|
|
|
return {
|
2020-03-26 21:47:35 +00:00
|
|
|
from,
|
2018-07-09 21:29:13 +00:00
|
|
|
changes,
|
|
|
|
};
|
|
|
|
},
|
2019-03-20 17:42:28 +00:00
|
|
|
getPropsForResetSessionNotification() {
|
|
|
|
// It doesn't need anything right now!
|
|
|
|
return {};
|
2019-01-14 21:49:58 +00:00
|
|
|
},
|
2020-06-04 18:16:19 +00:00
|
|
|
getPropsForCallHistory() {
|
|
|
|
return {
|
|
|
|
callHistoryDetails: this.get('callHistoryDetails'),
|
|
|
|
};
|
|
|
|
},
|
2020-07-29 23:20:05 +00:00
|
|
|
getPropsForProfileChange() {
|
|
|
|
const change = this.get('profileChange');
|
|
|
|
const changedId = this.get('changedId');
|
|
|
|
|
|
|
|
return {
|
|
|
|
changedContact: this.findAndFormatContact(changedId),
|
|
|
|
change,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
getAttachmentsForMessage() {
|
|
|
|
const sticker = this.get('sticker');
|
|
|
|
if (sticker && sticker.data) {
|
|
|
|
const { data } = sticker;
|
|
|
|
|
|
|
|
// We don't show anything if we're still loading a sticker
|
|
|
|
if (data.pending || !data.path) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
...data,
|
|
|
|
url: getAbsoluteAttachmentPath(data.path),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
const attachments = this.get('attachments') || [];
|
|
|
|
return attachments
|
|
|
|
.filter(attachment => !attachment.error)
|
|
|
|
.map(attachment => this.getPropsForAttachment(attachment));
|
|
|
|
},
|
2019-03-15 22:18:00 +00:00
|
|
|
getPropsForMessage() {
|
2020-07-10 18:28:49 +00:00
|
|
|
const sourceId = this.getContactId();
|
|
|
|
const contact = this.findAndFormatContact(sourceId);
|
|
|
|
const contactModel = this.findContact(sourceId);
|
2018-07-09 21:29:13 +00:00
|
|
|
|
2018-10-09 22:56:14 +00:00
|
|
|
const authorColor = contactModel ? contactModel.getColor() : null;
|
2018-09-27 00:23:17 +00:00
|
|
|
const authorAvatarPath = contactModel
|
|
|
|
? contactModel.getAvatarPath()
|
|
|
|
: null;
|
2018-07-09 21:29:13 +00:00
|
|
|
|
|
|
|
const expirationLength = this.get('expireTimer') * 1000;
|
|
|
|
const expireTimerStart = this.get('expirationStartTimestamp');
|
|
|
|
const expirationTimestamp =
|
|
|
|
expirationLength && expireTimerStart
|
|
|
|
? expireTimerStart + expirationLength
|
|
|
|
: null;
|
|
|
|
|
|
|
|
const conversation = this.getConversation();
|
|
|
|
const isGroup = conversation && !conversation.isPrivate();
|
2020-05-27 21:37:06 +00:00
|
|
|
const conversationAccepted = Boolean(
|
|
|
|
conversation && conversation.getAccepted()
|
|
|
|
);
|
2019-05-16 22:32:11 +00:00
|
|
|
const sticker = this.get('sticker');
|
2018-07-09 21:29:13 +00:00
|
|
|
|
2019-06-26 19:33:13 +00:00
|
|
|
const isTapToView = this.isTapToView();
|
|
|
|
|
2020-01-17 22:23:19 +00:00
|
|
|
const reactions = (this.get('reactions') || []).map(re => {
|
|
|
|
const c = this.findAndFormatContact(re.fromId);
|
|
|
|
|
|
|
|
if (!c) {
|
|
|
|
return {
|
|
|
|
emoji: re.emoji,
|
|
|
|
from: {
|
|
|
|
id: re.fromId,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
emoji: re.emoji,
|
|
|
|
timestamp: re.timestamp,
|
|
|
|
from: c,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-01-23 23:57:37 +00:00
|
|
|
const selectedReaction = (
|
|
|
|
(this.get('reactions') || []).find(
|
2020-08-13 22:19:04 +00:00
|
|
|
re => re.fromId === ConversationController.getOurConversationId()
|
2020-01-23 23:57:37 +00:00
|
|
|
) || {}
|
|
|
|
).emoji;
|
|
|
|
|
2018-07-09 21:29:13 +00:00
|
|
|
return {
|
|
|
|
text: this.createNonBreakingLastSeparator(this.get('body')),
|
2019-03-13 20:38:28 +00:00
|
|
|
textPending: this.get('bodyPending'),
|
2018-07-09 21:29:13 +00:00
|
|
|
id: this.id,
|
2019-11-07 21:36:16 +00:00
|
|
|
conversationId: this.get('conversationId'),
|
2020-05-27 21:37:06 +00:00
|
|
|
conversationAccepted,
|
2019-05-16 22:32:11 +00:00
|
|
|
isSticker: Boolean(sticker),
|
2018-07-09 21:29:13 +00:00
|
|
|
direction: this.isIncoming() ? 'incoming' : 'outgoing',
|
|
|
|
timestamp: this.get('sent_at'),
|
|
|
|
status: this.getMessagePropStatus(),
|
|
|
|
contact: this.getPropsForEmbeddedContact(),
|
2020-02-07 23:13:46 +00:00
|
|
|
canReply: this.canReply(),
|
2020-07-24 01:35:32 +00:00
|
|
|
authorTitle: contact.title,
|
2018-10-09 22:56:14 +00:00
|
|
|
authorColor,
|
2018-07-09 21:29:13 +00:00
|
|
|
authorName: contact.name,
|
|
|
|
authorProfileName: contact.profileName,
|
|
|
|
authorPhoneNumber: contact.phoneNumber,
|
|
|
|
conversationType: isGroup ? 'group' : 'direct',
|
2019-05-16 22:32:11 +00:00
|
|
|
attachments: this.getAttachmentsForMessage(),
|
2019-01-16 03:03:56 +00:00
|
|
|
previews: this.getPropsForPreview(),
|
2019-03-15 22:18:00 +00:00
|
|
|
quote: this.getPropsForQuote(),
|
2018-07-09 21:29:13 +00:00
|
|
|
authorAvatarPath,
|
2018-08-09 23:18:10 +00:00
|
|
|
isExpired: this.hasExpired,
|
2018-07-09 21:29:13 +00:00
|
|
|
expirationLength,
|
|
|
|
expirationTimestamp,
|
2020-01-17 22:23:19 +00:00
|
|
|
reactions,
|
2020-01-23 23:57:37 +00:00
|
|
|
selectedReaction,
|
2019-03-15 22:18:00 +00:00
|
|
|
|
2019-06-26 19:33:13 +00:00
|
|
|
isTapToView,
|
2019-08-05 20:53:15 +00:00
|
|
|
isTapToViewExpired: isTapToView && this.get('isErased'),
|
2019-06-26 19:33:13 +00:00
|
|
|
isTapToViewError:
|
|
|
|
isTapToView && this.isIncoming() && this.get('isTapToViewInvalid'),
|
2020-04-29 21:24:12 +00:00
|
|
|
|
|
|
|
deletedForEveryone: this.get('deletedForEveryone') || false,
|
2018-07-09 21:29:13 +00:00
|
|
|
};
|
|
|
|
},
|
2019-03-20 17:42:28 +00:00
|
|
|
|
|
|
|
// Dependencies of prop-generation functions
|
2020-03-05 21:14:58 +00:00
|
|
|
findAndFormatContact(identifier) {
|
|
|
|
const contactModel = this.findContact(identifier);
|
2019-03-20 17:42:28 +00:00
|
|
|
if (contactModel) {
|
2019-09-06 19:16:54 +00:00
|
|
|
return contactModel.format();
|
2019-03-20 17:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const { format } = PhoneNumber;
|
|
|
|
const regionCode = storage.get('regionCode');
|
|
|
|
return {
|
2020-03-05 21:14:58 +00:00
|
|
|
phoneNumber: format(identifier, {
|
2019-03-20 17:42:28 +00:00
|
|
|
ourRegionCode: regionCode,
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
},
|
2020-03-05 21:14:58 +00:00
|
|
|
findContact(identifier) {
|
|
|
|
return ConversationController.get(identifier);
|
2019-03-20 17:42:28 +00:00
|
|
|
},
|
|
|
|
getConversation() {
|
2020-08-11 23:15:06 +00:00
|
|
|
return ConversationController.get(this.get('conversationId'));
|
2019-03-20 17:42:28 +00:00
|
|
|
},
|
2018-07-09 21:29:13 +00:00
|
|
|
createNonBreakingLastSeparator(text) {
|
|
|
|
if (!text) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nbsp = '\xa0';
|
|
|
|
const regex = /(\S)( +)(\S+\s*)$/;
|
|
|
|
return text.replace(regex, (match, start, spaces, end) => {
|
2019-02-22 18:55:37 +00:00
|
|
|
const newSpaces =
|
|
|
|
end.length < 12
|
|
|
|
? _.reduce(spaces, accumulator => accumulator + nbsp, '')
|
|
|
|
: spaces;
|
2018-07-09 21:29:13 +00:00
|
|
|
return `${start}${newSpaces}${end}`;
|
|
|
|
});
|
|
|
|
},
|
2019-03-20 17:42:28 +00:00
|
|
|
isIncoming() {
|
|
|
|
return this.get('type') === 'incoming';
|
|
|
|
},
|
|
|
|
getMessagePropStatus() {
|
2020-08-07 01:22:52 +00:00
|
|
|
const sent = this.get('sent');
|
|
|
|
const sentTo = this.get('sent_to') || [];
|
|
|
|
|
2019-03-20 17:42:28 +00:00
|
|
|
if (this.hasErrors()) {
|
2020-08-07 01:22:52 +00:00
|
|
|
if (sent || sentTo.length > 0) {
|
|
|
|
return 'partial-sent';
|
|
|
|
}
|
2019-03-20 17:42:28 +00:00
|
|
|
return 'error';
|
|
|
|
}
|
|
|
|
if (!this.isOutgoing()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const readBy = this.get('read_by') || [];
|
|
|
|
if (storage.get('read-receipt-setting') && readBy.length > 0) {
|
|
|
|
return 'read';
|
|
|
|
}
|
|
|
|
const delivered = this.get('delivered');
|
|
|
|
const deliveredTo = this.get('delivered_to') || [];
|
|
|
|
if (delivered || deliveredTo.length > 0) {
|
|
|
|
return 'delivered';
|
|
|
|
}
|
|
|
|
if (sent || sentTo.length > 0) {
|
|
|
|
return 'sent';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'sending';
|
|
|
|
},
|
2018-07-09 21:29:13 +00:00
|
|
|
getPropsForEmbeddedContact() {
|
|
|
|
const contacts = this.get('contact');
|
|
|
|
if (!contacts || !contacts.length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-09-18 20:39:03 +00:00
|
|
|
const regionCode = storage.get('regionCode');
|
|
|
|
const { contactSelector } = Contact;
|
2018-07-09 21:29:13 +00:00
|
|
|
const contact = contacts[0];
|
|
|
|
const firstNumber =
|
|
|
|
contact.number && contact.number[0] && contact.number[0].value;
|
|
|
|
|
|
|
|
// Would be nice to do this before render, on initial load of message
|
|
|
|
if (!window.isSignalAccountCheckComplete(firstNumber)) {
|
|
|
|
window.checkForSignalAccount(firstNumber).then(() => {
|
2019-01-14 21:49:58 +00:00
|
|
|
this.trigger('change', this);
|
2018-07-09 21:29:13 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return contactSelector(contact, {
|
|
|
|
regionCode,
|
|
|
|
getAbsoluteAttachmentPath,
|
2019-03-15 22:18:00 +00:00
|
|
|
signalAccount: window.hasSignalAccount(firstNumber)
|
|
|
|
? firstNumber
|
|
|
|
: null,
|
2018-07-09 21:29:13 +00:00
|
|
|
});
|
|
|
|
},
|
2019-03-20 17:42:28 +00:00
|
|
|
getPropsForAttachment(attachment) {
|
|
|
|
if (!attachment) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-08-15 19:31:29 +00:00
|
|
|
|
2019-03-20 17:42:28 +00:00
|
|
|
const { path, pending, flags, size, screenshot, thumbnail } = attachment;
|
2018-08-15 19:31:29 +00:00
|
|
|
|
2019-03-20 17:42:28 +00:00
|
|
|
return {
|
|
|
|
...attachment,
|
|
|
|
fileSize: size ? filesize(size) : null,
|
|
|
|
isVoiceMessage:
|
|
|
|
flags &&
|
|
|
|
// eslint-disable-next-line no-bitwise
|
|
|
|
flags & textsecure.protobuf.AttachmentPointer.Flags.VOICE_MESSAGE,
|
|
|
|
pending,
|
|
|
|
url: path ? getAbsoluteAttachmentPath(path) : null,
|
|
|
|
screenshot: screenshot
|
|
|
|
? {
|
|
|
|
...screenshot,
|
|
|
|
url: getAbsoluteAttachmentPath(screenshot.path),
|
|
|
|
}
|
|
|
|
: null,
|
|
|
|
thumbnail: thumbnail
|
|
|
|
? {
|
|
|
|
...thumbnail,
|
|
|
|
url: getAbsoluteAttachmentPath(thumbnail.path),
|
|
|
|
}
|
|
|
|
: null,
|
|
|
|
};
|
2018-08-15 19:31:29 +00:00
|
|
|
},
|
2019-01-16 03:03:56 +00:00
|
|
|
getPropsForPreview() {
|
|
|
|
const previews = this.get('preview') || [];
|
|
|
|
|
|
|
|
return previews.map(preview => ({
|
|
|
|
...preview,
|
2019-05-16 22:32:11 +00:00
|
|
|
isStickerPack: window.Signal.LinkPreviews.isStickerPack(preview.url),
|
2019-01-16 03:03:56 +00:00
|
|
|
domain: window.Signal.LinkPreviews.getDomain(preview.url),
|
|
|
|
image: preview.image ? this.getPropsForAttachment(preview.image) : null,
|
|
|
|
}));
|
|
|
|
},
|
2019-03-15 22:18:00 +00:00
|
|
|
getPropsForQuote() {
|
2018-04-17 21:31:16 +00:00
|
|
|
const quote = this.get('quote');
|
|
|
|
if (!quote) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-09-27 00:23:17 +00:00
|
|
|
const { format } = PhoneNumber;
|
|
|
|
const regionCode = storage.get('regionCode');
|
|
|
|
|
2020-03-05 21:14:58 +00:00
|
|
|
const {
|
|
|
|
author,
|
|
|
|
authorUuid,
|
|
|
|
id: sentAt,
|
|
|
|
referencedMessageNotFound,
|
|
|
|
} = quote;
|
|
|
|
const contact =
|
2020-07-10 18:28:49 +00:00
|
|
|
(author || authorUuid) &&
|
|
|
|
ConversationController.get(
|
|
|
|
ConversationController.ensureContactIds({
|
|
|
|
e164: author,
|
|
|
|
uuid: authorUuid,
|
|
|
|
})
|
|
|
|
);
|
2018-10-09 22:56:14 +00:00
|
|
|
const authorColor = contact ? contact.getColor() : 'grey';
|
2018-04-17 21:31:16 +00:00
|
|
|
|
2018-09-27 00:23:17 +00:00
|
|
|
const authorPhoneNumber = format(author, {
|
|
|
|
ourRegionCode: regionCode,
|
|
|
|
});
|
2018-04-17 21:31:16 +00:00
|
|
|
const authorProfileName = contact ? contact.getProfileName() : null;
|
2020-07-24 01:35:32 +00:00
|
|
|
const authorName = contact ? contact.get('name') : null;
|
|
|
|
const authorTitle = contact ? contact.getTitle() : null;
|
2020-03-05 21:14:58 +00:00
|
|
|
const isFromMe = contact ? contact.isMe() : false;
|
2018-07-18 03:40:19 +00:00
|
|
|
const firstAttachment = quote.attachments && quote.attachments[0];
|
2018-07-09 21:29:13 +00:00
|
|
|
|
2018-04-17 21:31:16 +00:00
|
|
|
return {
|
2018-07-09 21:29:13 +00:00
|
|
|
text: this.createNonBreakingLastSeparator(quote.text),
|
|
|
|
attachment: firstAttachment
|
2018-08-15 19:31:29 +00:00
|
|
|
? this.processQuoteAttachment(firstAttachment)
|
2018-07-09 21:29:13 +00:00
|
|
|
: null,
|
2018-04-17 21:31:16 +00:00
|
|
|
isFromMe,
|
2019-03-15 22:18:00 +00:00
|
|
|
sentAt,
|
|
|
|
authorId: author,
|
2018-07-09 21:29:13 +00:00
|
|
|
authorPhoneNumber,
|
|
|
|
authorProfileName,
|
2020-07-24 01:35:32 +00:00
|
|
|
authorTitle,
|
2018-07-09 21:29:13 +00:00
|
|
|
authorName,
|
2018-10-09 22:56:14 +00:00
|
|
|
authorColor,
|
2018-08-15 19:31:29 +00:00
|
|
|
referencedMessageNotFound,
|
2019-05-31 22:42:01 +00:00
|
|
|
onClick: () => this.trigger('scroll-to-message'),
|
2018-04-17 21:31:16 +00:00
|
|
|
};
|
|
|
|
},
|
2020-03-05 21:14:58 +00:00
|
|
|
getStatus(identifier) {
|
|
|
|
const conversation = ConversationController.get(identifier);
|
|
|
|
|
|
|
|
if (!conversation) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const e164 = conversation.get('e164');
|
|
|
|
const uuid = conversation.get('uuid');
|
2020-07-10 18:28:49 +00:00
|
|
|
const conversationId = conversation.get('id');
|
2020-03-05 21:14:58 +00:00
|
|
|
|
2019-03-20 17:42:28 +00:00
|
|
|
const readBy = this.get('read_by') || [];
|
2020-07-10 18:28:49 +00:00
|
|
|
if (includesAny(readBy, conversationId, e164, uuid)) {
|
2019-03-20 17:42:28 +00:00
|
|
|
return 'read';
|
|
|
|
}
|
|
|
|
const deliveredTo = this.get('delivered_to') || [];
|
2020-07-10 18:28:49 +00:00
|
|
|
if (includesAny(deliveredTo, conversationId, e164, uuid)) {
|
2019-03-20 17:42:28 +00:00
|
|
|
return 'delivered';
|
|
|
|
}
|
|
|
|
const sentTo = this.get('sent_to') || [];
|
2020-07-10 18:28:49 +00:00
|
|
|
if (includesAny(sentTo, conversationId, e164, uuid)) {
|
2019-03-20 17:42:28 +00:00
|
|
|
return 'sent';
|
2018-07-09 21:29:13 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 17:42:28 +00:00
|
|
|
return null;
|
|
|
|
},
|
|
|
|
processQuoteAttachment(attachment) {
|
|
|
|
const { thumbnail } = attachment;
|
|
|
|
const path =
|
|
|
|
thumbnail &&
|
|
|
|
thumbnail.path &&
|
|
|
|
getAbsoluteAttachmentPath(thumbnail.path);
|
|
|
|
const objectUrl = thumbnail && thumbnail.objectUrl;
|
|
|
|
|
|
|
|
const thumbnailWithObjectUrl =
|
|
|
|
!path && !objectUrl
|
|
|
|
? null
|
|
|
|
: Object.assign({}, attachment.thumbnail || {}, {
|
|
|
|
objectUrl: path || objectUrl,
|
|
|
|
});
|
|
|
|
|
|
|
|
return Object.assign({}, attachment, {
|
|
|
|
isVoiceMessage: Signal.Types.Attachment.isVoiceMessage(attachment),
|
|
|
|
thumbnail: thumbnailWithObjectUrl,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// More display logic
|
|
|
|
getDescription() {
|
2019-06-10 21:40:02 +00:00
|
|
|
if (this.isUnsupportedMessage()) {
|
|
|
|
return i18n('message--getDescription--unsupported-message');
|
|
|
|
}
|
2020-07-29 23:20:05 +00:00
|
|
|
if (this.isProfileChange()) {
|
|
|
|
const change = this.get('profileChange');
|
|
|
|
const changedId = this.get('changedId');
|
|
|
|
const changedContact = this.findAndFormatContact(changedId);
|
|
|
|
|
|
|
|
return Signal.Util.getStringForProfileChange(
|
|
|
|
change,
|
|
|
|
changedContact,
|
|
|
|
i18n
|
|
|
|
);
|
|
|
|
}
|
2019-06-26 19:33:13 +00:00
|
|
|
if (this.isTapToView()) {
|
2019-10-03 19:03:46 +00:00
|
|
|
if (this.isErased()) {
|
2020-01-10 16:29:51 +00:00
|
|
|
return i18n('message--getDescription--disappearing-media');
|
2019-10-03 19:03:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const attachments = this.get('attachments');
|
|
|
|
if (!attachments || !attachments[0]) {
|
|
|
|
return i18n('mediaMessage');
|
|
|
|
}
|
|
|
|
|
|
|
|
const { contentType } = attachments[0];
|
|
|
|
if (GoogleChrome.isImageTypeSupported(contentType)) {
|
|
|
|
return i18n('message--getDescription--disappearing-photo');
|
|
|
|
} else if (GoogleChrome.isVideoTypeSupported(contentType)) {
|
|
|
|
return i18n('message--getDescription--disappearing-video');
|
|
|
|
}
|
|
|
|
|
|
|
|
return i18n('mediaMessage');
|
2019-06-26 19:33:13 +00:00
|
|
|
}
|
2020-03-26 21:47:35 +00:00
|
|
|
|
2019-03-20 17:42:28 +00:00
|
|
|
if (this.isGroupUpdate()) {
|
|
|
|
const groupUpdate = this.get('group_update');
|
2020-03-26 21:47:35 +00:00
|
|
|
const fromContact = this.getContact();
|
|
|
|
const messages = [];
|
|
|
|
|
2019-03-20 17:42:28 +00:00
|
|
|
if (groupUpdate.left === 'You') {
|
|
|
|
return i18n('youLeftTheGroup');
|
|
|
|
} else if (groupUpdate.left) {
|
2020-07-29 23:20:05 +00:00
|
|
|
return i18n('leftTheGroup', [
|
|
|
|
this.getNameForNumber(groupUpdate.left),
|
|
|
|
]);
|
2019-03-20 17:42:28 +00:00
|
|
|
}
|
2018-07-09 21:29:13 +00:00
|
|
|
|
2020-03-26 21:47:35 +00:00
|
|
|
if (!fromContact) {
|
|
|
|
return '';
|
2019-03-20 17:42:28 +00:00
|
|
|
}
|
2020-03-26 21:47:35 +00:00
|
|
|
|
|
|
|
if (fromContact.isMe()) {
|
|
|
|
messages.push(i18n('youUpdatedTheGroup'));
|
|
|
|
} else {
|
2020-07-29 23:20:05 +00:00
|
|
|
messages.push(i18n('updatedTheGroup', [fromContact.getTitle()]));
|
2019-03-20 17:42:28 +00:00
|
|
|
}
|
2020-03-26 21:47:35 +00:00
|
|
|
|
2019-03-20 17:42:28 +00:00
|
|
|
if (groupUpdate.joined && groupUpdate.joined.length) {
|
2020-03-26 21:47:35 +00:00
|
|
|
const joinedContacts = _.map(groupUpdate.joined, item =>
|
|
|
|
ConversationController.getOrCreate(item, 'private')
|
|
|
|
);
|
|
|
|
const joinedWithoutMe = joinedContacts.filter(
|
|
|
|
contact => !contact.isMe()
|
2019-03-20 17:42:28 +00:00
|
|
|
);
|
2020-03-26 21:47:35 +00:00
|
|
|
|
|
|
|
if (joinedContacts.length > 1) {
|
|
|
|
messages.push(
|
2020-07-29 23:20:05 +00:00
|
|
|
i18n('multipleJoinedTheGroup', [
|
|
|
|
_.map(joinedWithoutMe, contact => contact.getTitle()).join(
|
|
|
|
', '
|
|
|
|
),
|
|
|
|
])
|
2020-03-26 21:47:35 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (joinedWithoutMe.length < joinedContacts.length) {
|
|
|
|
messages.push(i18n('youJoinedTheGroup'));
|
|
|
|
}
|
2019-03-20 17:42:28 +00:00
|
|
|
} else {
|
2020-03-26 21:47:35 +00:00
|
|
|
const joinedContact = ConversationController.getOrCreate(
|
|
|
|
groupUpdate.joined[0],
|
|
|
|
'private'
|
|
|
|
);
|
|
|
|
if (joinedContact.isMe()) {
|
|
|
|
messages.push(i18n('youJoinedTheGroup'));
|
|
|
|
} else {
|
|
|
|
messages.push(
|
2020-07-29 23:20:05 +00:00
|
|
|
i18n('joinedTheGroup', [joinedContacts[0].getTitle()])
|
2020-03-26 21:47:35 +00:00
|
|
|
);
|
|
|
|
}
|
2019-03-20 17:42:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:47:35 +00:00
|
|
|
if (groupUpdate.name) {
|
2020-07-29 23:20:05 +00:00
|
|
|
messages.push(i18n('titleIsNow', [groupUpdate.name]));
|
2020-03-26 21:47:35 +00:00
|
|
|
}
|
|
|
|
if (groupUpdate.avatarUpdated) {
|
|
|
|
messages.push(i18n('updatedGroupAvatar'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return messages.join(' ');
|
2019-03-20 17:42:28 +00:00
|
|
|
}
|
|
|
|
if (this.isEndSession()) {
|
|
|
|
return i18n('sessionEnded');
|
|
|
|
}
|
|
|
|
if (this.isIncoming() && this.hasErrors()) {
|
|
|
|
return i18n('incomingError');
|
|
|
|
}
|
|
|
|
return this.get('body');
|
|
|
|
},
|
|
|
|
getNotificationText() {
|
|
|
|
const description = this.getDescription();
|
|
|
|
if (description) {
|
|
|
|
return description;
|
|
|
|
}
|
|
|
|
if (this.get('attachments').length > 0) {
|
|
|
|
return i18n('mediaMessage');
|
|
|
|
}
|
2019-05-16 22:32:11 +00:00
|
|
|
if (this.get('sticker')) {
|
|
|
|
return i18n('message--getNotificationText--stickers');
|
|
|
|
}
|
2020-08-12 23:52:34 +00:00
|
|
|
if (this.isCallHistory()) {
|
|
|
|
return window.Signal.Components.getCallingNotificationText(
|
|
|
|
this.get('callHistoryDetails'),
|
|
|
|
window.i18n
|
|
|
|
);
|
|
|
|
}
|
2019-03-20 17:42:28 +00:00
|
|
|
if (this.isExpirationTimerUpdate()) {
|
|
|
|
const { expireTimer } = this.get('expirationTimerUpdate');
|
|
|
|
if (!expireTimer) {
|
|
|
|
return i18n('disappearingMessagesDisabled');
|
|
|
|
}
|
|
|
|
|
2020-07-29 23:20:05 +00:00
|
|
|
return i18n('timerSetTo', [
|
|
|
|
Whisper.ExpirationTimerOptions.getAbbreviated(expireTimer || 0),
|
|
|
|
]);
|
2019-03-20 17:42:28 +00:00
|
|
|
}
|
|
|
|
if (this.isKeyChange()) {
|
2020-04-13 18:49:10 +00:00
|
|
|
const identifier = this.get('key_changed');
|
|
|
|
const conversation = this.findContact(identifier);
|
2020-07-29 23:20:05 +00:00
|
|
|
return i18n('safetyNumberChangedGroup', [
|
|
|
|
conversation ? conversation.getTitle() : null,
|
|
|
|
]);
|
2019-03-20 17:42:28 +00:00
|
|
|
}
|
|
|
|
const contacts = this.get('contact');
|
|
|
|
if (contacts && contacts.length) {
|
|
|
|
return Contact.getName(contacts[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
},
|
|
|
|
|
|
|
|
// General
|
|
|
|
idForLogging() {
|
2020-06-12 00:00:40 +00:00
|
|
|
const source = this.getSource();
|
|
|
|
const device = this.getSourceDevice();
|
|
|
|
const timestamp = this.get('sent_at');
|
|
|
|
|
|
|
|
return `${source}.${device} ${timestamp}`;
|
2019-03-20 17:42:28 +00:00
|
|
|
},
|
|
|
|
defaults() {
|
2018-07-09 21:29:13 +00:00
|
|
|
return {
|
2019-03-20 17:42:28 +00:00
|
|
|
timestamp: new Date().getTime(),
|
|
|
|
attachments: [],
|
2018-07-09 21:29:13 +00:00
|
|
|
};
|
|
|
|
},
|
2019-03-20 17:42:28 +00:00
|
|
|
validate(attributes) {
|
|
|
|
const required = ['conversationId', 'received_at', 'sent_at'];
|
|
|
|
const missing = _.filter(required, attr => !attributes[attr]);
|
|
|
|
if (missing.length) {
|
|
|
|
window.log.warn(`Message missing attributes: ${missing}`);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
isUnread() {
|
|
|
|
return !!this.get('unread');
|
|
|
|
},
|
|
|
|
merge(model) {
|
|
|
|
const attributes = model.attributes || model;
|
|
|
|
this.set(attributes);
|
|
|
|
},
|
|
|
|
getNameForNumber(number) {
|
|
|
|
const conversation = ConversationController.get(number);
|
|
|
|
if (!conversation) {
|
|
|
|
return number;
|
|
|
|
}
|
2020-07-24 01:35:32 +00:00
|
|
|
return conversation.getTitle();
|
2019-03-20 17:42:28 +00:00
|
|
|
},
|
|
|
|
onDestroy() {
|
|
|
|
this.cleanup();
|
|
|
|
},
|
|
|
|
async cleanup() {
|
2019-05-31 22:42:01 +00:00
|
|
|
const { messageDeleted } = window.reduxActions.conversations;
|
|
|
|
messageDeleted(this.id, this.get('conversationId'));
|
2019-03-20 17:42:28 +00:00
|
|
|
MessageController.unregister(this.id);
|
|
|
|
this.unload();
|
2019-06-26 19:33:13 +00:00
|
|
|
await this.deleteData();
|
|
|
|
},
|
|
|
|
async deleteData() {
|
2019-03-20 17:42:28 +00:00
|
|
|
await deleteExternalMessageFiles(this.attributes);
|
2019-05-16 22:32:11 +00:00
|
|
|
|
|
|
|
const sticker = this.get('sticker');
|
|
|
|
if (!sticker) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { packId } = sticker;
|
|
|
|
if (packId) {
|
|
|
|
await deletePackReference(this.id, packId);
|
|
|
|
}
|
2019-03-20 17:42:28 +00:00
|
|
|
},
|
2019-06-26 19:33:13 +00:00
|
|
|
isTapToView() {
|
2019-08-05 20:53:15 +00:00
|
|
|
return Boolean(this.get('isViewOnce') || this.get('messageTimer'));
|
2019-06-26 19:33:13 +00:00
|
|
|
},
|
|
|
|
isValidTapToView() {
|
|
|
|
const body = this.get('body');
|
|
|
|
if (body) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const attachments = this.get('attachments');
|
|
|
|
if (!attachments || attachments.length !== 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const firstAttachment = attachments[0];
|
2019-11-07 20:07:37 +00:00
|
|
|
if (
|
|
|
|
!GoogleChrome.isImageTypeSupported(firstAttachment.contentType) &&
|
|
|
|
!GoogleChrome.isVideoTypeSupported(firstAttachment.contentType)
|
|
|
|
) {
|
2019-06-26 19:33:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const quote = this.get('quote');
|
|
|
|
const sticker = this.get('sticker');
|
|
|
|
const contact = this.get('contact');
|
|
|
|
const preview = this.get('preview');
|
|
|
|
|
|
|
|
if (
|
|
|
|
quote ||
|
|
|
|
sticker ||
|
|
|
|
(contact && contact.length > 0) ||
|
|
|
|
(preview && preview.length > 0)
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
2019-08-05 20:53:15 +00:00
|
|
|
async markViewed(options) {
|
2019-06-26 19:33:13 +00:00
|
|
|
const { fromSync } = options || {};
|
|
|
|
|
2019-08-05 20:53:15 +00:00
|
|
|
if (!this.isValidTapToView()) {
|
|
|
|
window.log.warn(
|
|
|
|
`markViewed: Message ${this.idForLogging()} is not a valid tap to view message!`
|
|
|
|
);
|
|
|
|
return;
|
2019-06-26 19:33:13 +00:00
|
|
|
}
|
2019-08-05 20:53:15 +00:00
|
|
|
if (this.isErased()) {
|
2019-06-26 19:33:13 +00:00
|
|
|
window.log.warn(
|
2019-08-05 20:53:15 +00:00
|
|
|
`markViewed: Message ${this.idForLogging()} is already erased!`
|
2019-06-26 19:33:13 +00:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-05 20:53:15 +00:00
|
|
|
if (this.get('unread')) {
|
|
|
|
await this.markRead();
|
2019-06-26 19:33:13 +00:00
|
|
|
}
|
|
|
|
|
2019-08-05 20:53:15 +00:00
|
|
|
await this.eraseContents();
|
2019-06-26 19:33:13 +00:00
|
|
|
|
|
|
|
if (!fromSync) {
|
|
|
|
const sender = this.getSource();
|
2020-03-05 21:14:58 +00:00
|
|
|
const senderUuid = this.getSourceUuid();
|
2019-06-26 19:33:13 +00:00
|
|
|
const timestamp = this.get('sent_at');
|
|
|
|
const ourNumber = textsecure.storage.user.getNumber();
|
2020-03-05 21:14:58 +00:00
|
|
|
const ourUuid = textsecure.storage.user.getUuid();
|
2019-06-26 19:33:13 +00:00
|
|
|
const { wrap, sendOptions } = ConversationController.prepareForSend(
|
2020-03-05 21:14:58 +00:00
|
|
|
ourNumber || ourUuid,
|
2020-01-08 17:44:54 +00:00
|
|
|
{
|
|
|
|
syncMessage: true,
|
|
|
|
}
|
2019-06-26 19:33:13 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
await wrap(
|
2020-03-05 21:14:58 +00:00
|
|
|
textsecure.messaging.syncViewOnceOpen(
|
|
|
|
sender,
|
|
|
|
senderUuid,
|
|
|
|
timestamp,
|
|
|
|
sendOptions
|
|
|
|
)
|
2019-06-26 19:33:13 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
2019-08-05 20:53:15 +00:00
|
|
|
isErased() {
|
|
|
|
return Boolean(this.get('isErased'));
|
|
|
|
},
|
2020-07-27 18:15:32 +00:00
|
|
|
async eraseContents(additionalProperties = {}, shouldPersist = true) {
|
2019-06-26 19:33:13 +00:00
|
|
|
if (this.get('isErased')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
window.log.info(`Erasing data for message ${this.idForLogging()}`);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.deleteData();
|
|
|
|
} catch (error) {
|
|
|
|
window.log.error(
|
|
|
|
`Error erasing data for message ${this.idForLogging()}:`,
|
|
|
|
error && error.stack ? error.stack : error
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set({
|
|
|
|
isErased: true,
|
|
|
|
body: '',
|
|
|
|
attachments: [],
|
|
|
|
quote: null,
|
|
|
|
contact: [],
|
|
|
|
sticker: null,
|
|
|
|
preview: [],
|
2020-04-29 21:24:12 +00:00
|
|
|
...additionalProperties,
|
2019-06-26 19:33:13 +00:00
|
|
|
});
|
2019-11-07 20:07:37 +00:00
|
|
|
this.trigger('content-changed');
|
2019-06-26 19:33:13 +00:00
|
|
|
|
2020-07-27 18:15:32 +00:00
|
|
|
if (shouldPersist) {
|
|
|
|
await window.Signal.Data.saveMessage(this.attributes, {
|
|
|
|
Message: Whisper.Message,
|
|
|
|
});
|
|
|
|
}
|
2019-06-26 19:33:13 +00:00
|
|
|
},
|
2020-05-27 21:37:06 +00:00
|
|
|
isEmpty() {
|
2020-07-31 00:10:54 +00:00
|
|
|
// Core message types - we check for all four because they can each stand alone
|
|
|
|
const hasBody = Boolean(this.get('body'));
|
2020-05-27 21:37:06 +00:00
|
|
|
const hasAttachment = (this.get('attachments') || []).length > 0;
|
2020-07-31 00:10:54 +00:00
|
|
|
const hasEmbeddedContact = (this.get('contact') || []).length > 0;
|
|
|
|
const isSticker = Boolean(this.get('sticker'));
|
|
|
|
|
|
|
|
// Rendered sync messages
|
|
|
|
const isCallHistory = this.isCallHistory();
|
|
|
|
const isGroupUpdate = this.isGroupUpdate();
|
|
|
|
const isEndSession = this.isEndSession();
|
|
|
|
const isExpirationTimerUpdate = this.isExpirationTimerUpdate();
|
|
|
|
const isVerifiedChange = this.isVerifiedChange();
|
|
|
|
|
|
|
|
// Placeholder messages
|
|
|
|
const isUnsupportedMessage = this.isUnsupportedMessage();
|
|
|
|
const isTapToView = this.isTapToView();
|
|
|
|
|
|
|
|
// Errors
|
|
|
|
const hasErrors = this.hasErrors();
|
|
|
|
|
|
|
|
// Locally-generated notifications
|
|
|
|
const isKeyChange = this.isKeyChange();
|
|
|
|
const isMessageHistoryUnsynced = this.isMessageHistoryUnsynced();
|
|
|
|
const isProfileChange = this.isProfileChange();
|
|
|
|
|
|
|
|
// Note: not all of these message types go through message.handleDataMessage
|
|
|
|
|
|
|
|
const hasSomethingToDisplay =
|
|
|
|
// Core message types
|
|
|
|
hasBody ||
|
|
|
|
hasAttachment ||
|
|
|
|
hasEmbeddedContact ||
|
|
|
|
isSticker ||
|
|
|
|
// Rendered sync messages
|
|
|
|
isCallHistory ||
|
|
|
|
isGroupUpdate ||
|
|
|
|
isEndSession ||
|
|
|
|
isExpirationTimerUpdate ||
|
|
|
|
isVerifiedChange ||
|
|
|
|
// Placeholder messages
|
|
|
|
isUnsupportedMessage ||
|
|
|
|
isTapToView ||
|
|
|
|
// Errors
|
|
|
|
hasErrors ||
|
|
|
|
// Locally-generated notifications
|
|
|
|
isKeyChange ||
|
|
|
|
isMessageHistoryUnsynced ||
|
|
|
|
isProfileChange;
|
|
|
|
|
|
|
|
return !hasSomethingToDisplay;
|
2020-05-27 21:37:06 +00:00
|
|
|
},
|
2019-03-20 17:42:28 +00:00
|
|
|
unload() {
|
|
|
|
if (this.quotedMessage) {
|
|
|
|
this.quotedMessage = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onExpired() {
|
|
|
|
this.hasExpired = true;
|
|
|
|
},
|
2018-10-18 01:01:21 +00:00
|
|
|
isUnidentifiedDelivery(contactId, lookup) {
|
|
|
|
if (this.isIncoming()) {
|
|
|
|
return this.get('unidentifiedDeliveryReceived');
|
|
|
|
}
|
|
|
|
|
|
|
|
return Boolean(lookup[contactId]);
|
|
|
|
},
|
2019-03-20 17:42:28 +00:00
|
|
|
getSource() {
|
|
|
|
if (this.isIncoming()) {
|
|
|
|
return this.get('source');
|
|
|
|
}
|
2018-07-09 21:29:13 +00:00
|
|
|
|
2019-03-20 17:42:28 +00:00
|
|
|
return this.OUR_NUMBER;
|
|
|
|
},
|
2020-06-12 00:00:40 +00:00
|
|
|
getSourceDevice() {
|
|
|
|
if (this.isIncoming()) {
|
|
|
|
return this.get('sourceDevice');
|
|
|
|
}
|
|
|
|
|
|
|
|
return window.textsecure.storage.user.getDeviceId();
|
|
|
|
},
|
2020-03-05 21:14:58 +00:00
|
|
|
getSourceUuid() {
|
|
|
|
if (this.isIncoming()) {
|
|
|
|
return this.get('sourceUuid');
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.OUR_UUID;
|
|
|
|
},
|
2020-07-10 18:28:49 +00:00
|
|
|
getContactId() {
|
2019-03-20 17:42:28 +00:00
|
|
|
const source = this.getSource();
|
2020-03-05 21:14:58 +00:00
|
|
|
const sourceUuid = this.getSourceUuid();
|
2018-10-18 01:01:21 +00:00
|
|
|
|
2020-03-05 21:14:58 +00:00
|
|
|
if (!source && !sourceUuid) {
|
2020-07-10 18:28:49 +00:00
|
|
|
return ConversationController.getOurConversationId();
|
2019-03-20 17:42:28 +00:00
|
|
|
}
|
2018-07-09 21:29:13 +00:00
|
|
|
|
2020-07-10 18:28:49 +00:00
|
|
|
return ConversationController.ensureContactIds({
|
2020-06-12 22:36:32 +00:00
|
|
|
e164: source,
|
|
|
|
uuid: sourceUuid,
|
|
|
|
});
|
2020-07-10 18:28:49 +00:00
|
|
|
},
|
|
|
|
getContact() {
|
|
|
|
const id = this.getContactId();
|
|
|
|
return ConversationController.get(id);
|
2019-03-20 17:42:28 +00:00
|
|
|
},
|
|
|
|
isOutgoing() {
|
|
|
|
return this.get('type') === 'outgoing';
|
|
|
|
},
|
|
|
|
hasErrors() {
|
|
|
|
return _.size(this.get('errors')) > 0;
|
|
|
|
},
|
2019-09-26 19:56:31 +00:00
|
|
|
async saveErrors(providedErrors, options = {}) {
|
|
|
|
const { skipSave } = options;
|
|
|
|
|
2019-03-20 17:42:28 +00:00
|
|
|
let errors = providedErrors;
|
|
|
|
|
|
|
|
if (!(errors instanceof Array)) {
|
|
|
|
errors = [errors];
|
|
|
|
}
|
|
|
|
errors.forEach(e => {
|
|
|
|
window.log.error(
|
|
|
|
'Message.saveErrors:',
|
|
|
|
e && e.reason ? e.reason : null,
|
|
|
|
e && e.stack ? e.stack : e
|
|
|
|
);
|
|
|
|
});
|
|
|
|
errors = errors.map(e => {
|
|
|
|
if (
|
|
|
|
e.constructor === Error ||
|
|
|
|
e.constructor === TypeError ||
|
|
|
|
e.constructor === ReferenceError
|
|
|
|
) {
|
|
|
|
return _.pick(e, 'name', 'message', 'code', 'number', 'reason');
|
2018-07-09 21:29:13 +00:00
|
|
|
}
|
2019-03-20 17:42:28 +00:00
|
|
|
return e;
|
|
|
|
});
|
|
|
|
errors = errors.concat(this.get('errors') || []);
|
2018-07-09 21:29:13 +00:00
|
|
|
|
2019-03-20 17:42:28 +00:00
|
|
|
this.set({ errors });
|
2019-09-26 19:56:31 +00:00
|
|
|
|
|
|
|
if (!skipSave) {
|
|
|
|
await window.Signal.Data.saveMessage(this.attributes, {
|
|
|
|
Message: Whisper.Message,
|
|
|
|
});
|
|
|
|
}
|
2019-03-20 17:42:28 +00:00
|
|
|
},
|
2019-09-26 19:56:31 +00:00
|
|
|
async markRead(readAt, options = {}) {
|
|
|
|
const { skipSave } = options;
|
|
|
|
|
2019-03-20 17:42:28 +00:00
|
|
|
this.unset('unread');
|
2018-07-09 21:29:13 +00:00
|
|
|
|
2019-03-20 17:42:28 +00:00
|
|
|
if (this.get('expireTimer') && !this.get('expirationStartTimestamp')) {
|
|
|
|
const expirationStartTimestamp = Math.min(
|
|
|
|
Date.now(),
|
|
|
|
readAt || Date.now()
|
2018-07-09 21:29:13 +00:00
|
|
|
);
|
2019-03-20 17:42:28 +00:00
|
|
|
this.set({ expirationStartTimestamp });
|
|
|
|
}
|
|
|
|
|
|
|
|
Whisper.Notifications.remove(
|
|
|
|
Whisper.Notifications.where({
|
|
|
|
messageId: this.id,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2019-09-26 19:56:31 +00:00
|
|
|
if (!skipSave) {
|
|
|
|
await window.Signal.Data.saveMessage(this.attributes, {
|
|
|
|
Message: Whisper.Message,
|
|
|
|
});
|
|
|
|
}
|
2019-03-20 17:42:28 +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;
|
|
|
|
let msFromNow = start + delta - now;
|
|
|
|
if (msFromNow < 0) {
|
|
|
|
msFromNow = 0;
|
|
|
|
}
|
|
|
|
return msFromNow;
|
|
|
|
},
|
2020-01-09 00:19:22 +00:00
|
|
|
async setToExpire(force = false, options) {
|
|
|
|
const { skipSave } = options || {};
|
2019-09-26 19:56:31 +00:00
|
|
|
|
2019-03-20 17:42:28 +00:00
|
|
|
if (this.isExpiring() && (force || !this.get('expires_at'))) {
|
|
|
|
const start = this.get('expirationStartTimestamp');
|
|
|
|
const delta = this.get('expireTimer') * 1000;
|
|
|
|
const expiresAt = start + delta;
|
2018-07-09 21:29:13 +00:00
|
|
|
|
2019-03-20 17:42:28 +00:00
|
|
|
this.set({ expires_at: expiresAt });
|
|
|
|
const id = this.get('id');
|
2019-09-26 19:56:31 +00:00
|
|
|
if (id && !skipSave) {
|
2019-03-20 17:42:28 +00:00
|
|
|
await window.Signal.Data.saveMessage(this.attributes, {
|
|
|
|
Message: Whisper.Message,
|
|
|
|
});
|
|
|
|
}
|
2019-03-15 22:18:00 +00:00
|
|
|
|
2019-03-20 17:42:28 +00:00
|
|
|
window.log.info('Set message expiration', {
|
|
|
|
expiresAt,
|
|
|
|
sentAt: this.get('sent_at'),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
getIncomingContact() {
|
|
|
|
if (!this.isIncoming()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const source = this.get('source');
|
|
|
|
if (!source) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-07-09 21:29:13 +00:00
|
|
|
|
2019-03-20 17:42:28 +00:00
|
|
|
return ConversationController.getOrCreate(source, 'private');
|
|
|
|
},
|
|
|
|
getQuoteContact() {
|
|
|
|
const quote = this.get('quote');
|
|
|
|
if (!quote) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const { author } = quote;
|
|
|
|
if (!author) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-07-09 21:29:13 +00:00
|
|
|
|
2019-03-20 17:42:28 +00:00
|
|
|
return ConversationController.get(author);
|
2018-07-09 21:29:13 +00:00
|
|
|
},
|
2018-07-27 01:13:56 +00:00
|
|
|
|
2019-03-20 17:42:28 +00:00
|
|
|
// Send infrastructure
|
2018-07-27 01:13:56 +00:00
|
|
|
// One caller today: event handler for the 'Retry Send' entry in triple-dot menu
|
|
|
|
async retrySend() {
|
2018-08-07 19:33:56 +00:00
|
|
|
if (!textsecure.messaging) {
|
|
|
|
window.log.error('retrySend: Cannot retry since we are offline!');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-03-19 01:25:42 +00:00
|
|
|
this.set({ errors: null });
|
2018-07-27 01:13:56 +00:00
|
|
|
|
2019-02-11 23:59:21 +00:00
|
|
|
const conversation = this.getConversation();
|
2020-03-20 19:01:15 +00:00
|
|
|
const intendedRecipients = (this.get('recipients') || [])
|
|
|
|
.map(identifier => ConversationController.getConversationId(identifier))
|
|
|
|
.filter(Boolean);
|
|
|
|
const successfulRecipients = (this.get('sent_to') || [])
|
|
|
|
.map(identifier => ConversationController.getConversationId(identifier))
|
|
|
|
.filter(Boolean);
|
|
|
|
const currentRecipients = conversation
|
|
|
|
.getRecipients()
|
|
|
|
.map(identifier => ConversationController.getConversationId(identifier))
|
|
|
|
.filter(Boolean);
|
2019-02-11 23:59:21 +00:00
|
|
|
|
|
|
|
const profileKey = conversation.get('profileSharing')
|
|
|
|
? storage.get('profileKey')
|
|
|
|
: null;
|
|
|
|
|
2020-03-20 19:01:15 +00:00
|
|
|
// Determine retry recipients and get their most up-to-date addressing information
|
2019-03-19 01:25:42 +00:00
|
|
|
let recipients = _.intersection(intendedRecipients, currentRecipients);
|
2020-03-20 19:01:15 +00:00
|
|
|
recipients = _.without(recipients, successfulRecipients).map(id => {
|
|
|
|
const c = ConversationController.get(id);
|
2020-07-10 18:28:49 +00:00
|
|
|
return c.getSendTarget();
|
2020-03-20 19:01:15 +00:00
|
|
|
});
|
2018-07-27 01:13:56 +00:00
|
|
|
|
2019-03-19 01:25:42 +00:00
|
|
|
if (!recipients.length) {
|
|
|
|
window.log.warn('retrySend: Nobody to send to!');
|
2019-02-11 23:59:21 +00:00
|
|
|
|
2019-03-19 01:25:42 +00:00
|
|
|
return window.Signal.Data.saveMessage(this.attributes, {
|
|
|
|
Message: Whisper.Message,
|
|
|
|
});
|
2018-07-27 01:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const attachmentsWithData = await Promise.all(
|
|
|
|
(this.get('attachments') || []).map(loadAttachmentData)
|
|
|
|
);
|
2019-03-13 20:38:28 +00:00
|
|
|
const { body, attachments } = Whisper.Message.getLongMessageAttachment({
|
|
|
|
body: this.get('body'),
|
|
|
|
attachments: attachmentsWithData,
|
|
|
|
now: this.get('sent_at'),
|
|
|
|
});
|
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
const quoteWithData = await loadQuoteData(this.get('quote'));
|
2019-01-16 03:03:56 +00:00
|
|
|
const previewWithData = await loadPreviewData(this.get('preview'));
|
2019-05-16 22:32:11 +00:00
|
|
|
const stickerWithData = await loadStickerData(this.get('sticker'));
|
2018-07-27 01:13:56 +00:00
|
|
|
|
2019-01-31 01:45:58 +00:00
|
|
|
// Special-case the self-send case - we send only a sync message
|
2020-03-20 19:01:15 +00:00
|
|
|
if (
|
|
|
|
recipients.length === 1 &&
|
|
|
|
(recipients[0] === this.OUR_NUMBER || recipients[0] === this.OUR_UUID)
|
|
|
|
) {
|
2020-03-05 21:14:58 +00:00
|
|
|
const [identifier] = recipients;
|
2019-01-31 01:45:58 +00:00
|
|
|
const dataMessage = await textsecure.messaging.getMessageProto(
|
2020-03-05 21:14:58 +00:00
|
|
|
identifier,
|
2019-03-13 20:38:28 +00:00
|
|
|
body,
|
|
|
|
attachments,
|
2019-01-31 01:45:58 +00:00
|
|
|
quoteWithData,
|
|
|
|
previewWithData,
|
2019-05-16 22:32:11 +00:00
|
|
|
stickerWithData,
|
2020-01-23 23:57:37 +00:00
|
|
|
null,
|
2019-01-31 01:45:58 +00:00
|
|
|
this.get('sent_at'),
|
|
|
|
this.get('expireTimer'),
|
|
|
|
profileKey
|
|
|
|
);
|
|
|
|
return this.sendSyncMessageOnly(dataMessage);
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
let promise;
|
2019-01-31 01:45:58 +00:00
|
|
|
const options = conversation.getSendOptions();
|
2018-07-27 01:13:56 +00:00
|
|
|
|
|
|
|
if (conversation.isPrivate()) {
|
2020-03-05 21:14:58 +00:00
|
|
|
const [identifer] = recipients;
|
|
|
|
promise = textsecure.messaging.sendMessageToIdentifier(
|
|
|
|
identifer,
|
2019-03-13 20:38:28 +00:00
|
|
|
body,
|
|
|
|
attachments,
|
2018-07-27 01:13:56 +00:00
|
|
|
quoteWithData,
|
2019-01-16 03:03:56 +00:00
|
|
|
previewWithData,
|
2019-05-16 22:32:11 +00:00
|
|
|
stickerWithData,
|
2020-01-23 23:57:37 +00:00
|
|
|
null,
|
2018-07-27 01:13:56 +00:00
|
|
|
this.get('sent_at'),
|
|
|
|
this.get('expireTimer'),
|
2018-10-18 01:01:21 +00:00
|
|
|
profileKey,
|
|
|
|
options
|
2018-07-27 01:13:56 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// Because this is a partial group send, we manually construct the request like
|
|
|
|
// sendMessageToGroup does.
|
2018-10-18 01:01:21 +00:00
|
|
|
|
|
|
|
promise = textsecure.messaging.sendMessage(
|
|
|
|
{
|
2019-03-19 01:25:42 +00:00
|
|
|
recipients,
|
2019-03-13 20:38:28 +00:00
|
|
|
body,
|
2018-10-18 01:01:21 +00:00
|
|
|
timestamp: this.get('sent_at'),
|
2019-03-13 20:38:28 +00:00
|
|
|
attachments,
|
2018-10-18 01:01:21 +00:00
|
|
|
quote: quoteWithData,
|
2019-01-16 03:03:56 +00:00
|
|
|
preview: previewWithData,
|
2019-05-16 22:32:11 +00:00
|
|
|
sticker: stickerWithData,
|
2018-10-18 01:01:21 +00:00
|
|
|
expireTimer: this.get('expireTimer'),
|
|
|
|
profileKey,
|
|
|
|
group: {
|
2020-03-20 19:01:15 +00:00
|
|
|
id: this.getConversation().get('groupId'),
|
2018-10-18 01:01:21 +00:00
|
|
|
type: textsecure.protobuf.GroupContext.Type.DELIVER,
|
|
|
|
},
|
2018-07-27 01:13:56 +00:00
|
|
|
},
|
2018-10-18 01:01:21 +00:00
|
|
|
options
|
|
|
|
);
|
2018-07-27 01:13:56 +00:00
|
|
|
}
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
return this.send(conversation.wrapSend(promise));
|
2018-07-09 21:29:13 +00:00
|
|
|
},
|
2018-07-27 01:13:56 +00:00
|
|
|
isReplayableError(e) {
|
|
|
|
return (
|
|
|
|
e.name === 'MessageError' ||
|
|
|
|
e.name === 'OutgoingMessageError' ||
|
|
|
|
e.name === 'SendMessageNetworkError' ||
|
|
|
|
e.name === 'SignedPreKeyRotationError' ||
|
|
|
|
e.name === 'OutgoingIdentityKeyError'
|
|
|
|
);
|
|
|
|
},
|
2020-02-07 23:13:46 +00:00
|
|
|
canReply() {
|
2020-05-27 21:37:06 +00:00
|
|
|
const isAccepted = this.getConversation().getAccepted();
|
2020-02-07 23:13:46 +00:00
|
|
|
const errors = this.get('errors');
|
|
|
|
const isOutgoing = this.get('type') === 'outgoing';
|
|
|
|
const numDelivered = this.get('delivered');
|
|
|
|
|
2020-05-27 21:37:06 +00:00
|
|
|
// Case 1: We cannot reply if we have accepted the message request
|
|
|
|
if (!isAccepted) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case 2: We cannot reply if this message is deleted for everyone
|
2020-04-29 21:24:12 +00:00
|
|
|
if (this.get('deletedForEveryone')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-27 21:37:06 +00:00
|
|
|
// Case 3: We can reply if this is outgoing and delievered to at least one recipient
|
2020-02-07 23:13:46 +00:00
|
|
|
if (isOutgoing && numDelivered > 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-27 21:37:06 +00:00
|
|
|
// Case 4: We can reply if there are no errors
|
2020-02-29 00:26:50 +00:00
|
|
|
if (!errors || (errors && errors.length === 0)) {
|
2020-02-07 23:13:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-27 21:37:06 +00:00
|
|
|
// Case 5: default
|
2020-02-07 23:13:46 +00:00
|
|
|
return false;
|
|
|
|
},
|
2018-07-27 01:13:56 +00:00
|
|
|
|
|
|
|
// Called when the user ran into an error with a specific user, wants to send to them
|
|
|
|
// One caller today: ConversationView.forceSend()
|
2020-03-05 21:14:58 +00:00
|
|
|
async resend(identifier) {
|
|
|
|
const error = this.removeOutgoingErrors(identifier);
|
2019-01-31 01:45:58 +00:00
|
|
|
if (!error) {
|
|
|
|
window.log.warn('resend: requested number was not present in errors');
|
|
|
|
return null;
|
|
|
|
}
|
2018-07-27 01:13:56 +00:00
|
|
|
|
2019-01-31 01:45:58 +00:00
|
|
|
const profileKey = null;
|
|
|
|
const attachmentsWithData = await Promise.all(
|
|
|
|
(this.get('attachments') || []).map(loadAttachmentData)
|
|
|
|
);
|
2019-03-13 20:38:28 +00:00
|
|
|
const { body, attachments } = Whisper.Message.getLongMessageAttachment({
|
|
|
|
body: this.get('body'),
|
|
|
|
attachments: attachmentsWithData,
|
|
|
|
now: this.get('sent_at'),
|
|
|
|
});
|
|
|
|
|
2019-01-31 01:45:58 +00:00
|
|
|
const quoteWithData = await loadQuoteData(this.get('quote'));
|
|
|
|
const previewWithData = await loadPreviewData(this.get('preview'));
|
2019-05-16 22:32:11 +00:00
|
|
|
const stickerWithData = await loadStickerData(this.get('sticker'));
|
2019-01-31 01:45:58 +00:00
|
|
|
|
|
|
|
// Special-case the self-send case - we send only a sync message
|
2020-03-05 21:14:58 +00:00
|
|
|
if (identifier === this.OUR_NUMBER || identifier === this.OUR_UUID) {
|
2019-01-31 01:45:58 +00:00
|
|
|
const dataMessage = await textsecure.messaging.getMessageProto(
|
2020-03-05 21:14:58 +00:00
|
|
|
identifier,
|
2019-03-13 20:38:28 +00:00
|
|
|
body,
|
|
|
|
attachments,
|
2018-07-27 01:13:56 +00:00
|
|
|
quoteWithData,
|
2019-01-16 03:03:56 +00:00
|
|
|
previewWithData,
|
2019-05-16 22:32:11 +00:00
|
|
|
stickerWithData,
|
2020-01-23 23:57:37 +00:00
|
|
|
null,
|
2018-07-27 01:13:56 +00:00
|
|
|
this.get('sent_at'),
|
|
|
|
this.get('expireTimer'),
|
2019-01-31 01:45:58 +00:00
|
|
|
profileKey
|
2018-07-27 01:13:56 +00:00
|
|
|
);
|
2019-01-31 01:45:58 +00:00
|
|
|
return this.sendSyncMessageOnly(dataMessage);
|
2018-07-27 01:13:56 +00:00
|
|
|
}
|
2019-01-31 01:45:58 +00:00
|
|
|
|
|
|
|
const { wrap, sendOptions } = ConversationController.prepareForSend(
|
2020-03-05 21:14:58 +00:00
|
|
|
identifier
|
2019-01-31 01:45:58 +00:00
|
|
|
);
|
2020-03-20 19:01:15 +00:00
|
|
|
const promise = textsecure.messaging.sendMessageToIdentifier(
|
2020-03-05 21:14:58 +00:00
|
|
|
identifier,
|
2019-03-13 20:38:28 +00:00
|
|
|
body,
|
|
|
|
attachments,
|
2019-01-31 01:45:58 +00:00
|
|
|
quoteWithData,
|
|
|
|
previewWithData,
|
2019-05-16 22:32:11 +00:00
|
|
|
stickerWithData,
|
2020-03-20 19:01:15 +00:00
|
|
|
null,
|
2019-01-31 01:45:58 +00:00
|
|
|
this.get('sent_at'),
|
|
|
|
this.get('expireTimer'),
|
|
|
|
profileKey,
|
|
|
|
sendOptions
|
|
|
|
);
|
|
|
|
|
|
|
|
return this.send(wrap(promise));
|
2018-07-27 01:13:56 +00:00
|
|
|
},
|
2020-03-20 19:01:15 +00:00
|
|
|
removeOutgoingErrors(incomingIdentifier) {
|
|
|
|
const incomingConversationId = ConversationController.getConversationId(
|
|
|
|
incomingIdentifier
|
|
|
|
);
|
2018-07-27 01:13:56 +00:00
|
|
|
const errors = _.partition(
|
|
|
|
this.get('errors'),
|
|
|
|
e =>
|
2020-03-20 19:01:15 +00:00
|
|
|
ConversationController.getConversationId(e.identifer || e.number) ===
|
|
|
|
incomingConversationId &&
|
2018-07-27 01:13:56 +00:00
|
|
|
(e.name === 'MessageError' ||
|
|
|
|
e.name === 'OutgoingMessageError' ||
|
|
|
|
e.name === 'SendMessageNetworkError' ||
|
|
|
|
e.name === 'SignedPreKeyRotationError' ||
|
|
|
|
e.name === 'OutgoingIdentityKeyError')
|
|
|
|
);
|
|
|
|
this.set({ errors: errors[1] });
|
|
|
|
return errors[0][0];
|
|
|
|
},
|
|
|
|
|
2018-04-10 23:51:48 +00:00
|
|
|
send(promise) {
|
|
|
|
this.trigger('pending');
|
2018-04-27 21:25:04 +00:00
|
|
|
return promise
|
2018-07-25 22:02:27 +00:00
|
|
|
.then(async result => {
|
2018-04-27 21:25:04 +00:00
|
|
|
this.trigger('done');
|
2018-07-27 01:13:56 +00:00
|
|
|
|
|
|
|
// This is used by sendSyncMessage, then set to null
|
2019-05-09 15:38:05 +00:00
|
|
|
if (result.dataMessage) {
|
2020-02-14 21:28:35 +00:00
|
|
|
this.set({ dataMessage: result.dataMessage });
|
2018-04-10 23:51:48 +00:00
|
|
|
}
|
2018-07-27 01:13:56 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
const sentTo = this.get('sent_to') || [];
|
2018-07-25 22:02:27 +00:00
|
|
|
this.set({
|
2020-03-05 21:14:58 +00:00
|
|
|
sent_to: _.union(sentTo, result.successfulIdentifiers),
|
2018-04-27 21:25:04 +00:00
|
|
|
sent: true,
|
2018-08-06 18:33:51 +00:00
|
|
|
expirationStartTimestamp: Date.now(),
|
2018-10-18 01:01:21 +00:00
|
|
|
unidentifiedDeliveries: result.unidentifiedDeliveries,
|
2018-04-27 21:25:04 +00:00
|
|
|
});
|
2018-07-25 22:02:27 +00:00
|
|
|
|
|
|
|
await window.Signal.Data.saveMessage(this.attributes, {
|
|
|
|
Message: Whisper.Message,
|
|
|
|
});
|
2018-10-18 01:01:21 +00:00
|
|
|
|
2018-07-18 03:25:55 +00:00
|
|
|
this.trigger('sent', this);
|
2018-04-27 21:25:04 +00:00
|
|
|
this.sendSyncMessage();
|
|
|
|
})
|
|
|
|
.catch(result => {
|
|
|
|
this.trigger('done');
|
2018-07-27 01:13:56 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
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 {
|
2020-03-05 21:14:58 +00:00
|
|
|
if (result.successfulIdentifiers.length > 0) {
|
2018-04-27 21:25:04 +00:00
|
|
|
const sentTo = this.get('sent_to') || [];
|
2018-08-06 18:33:51 +00:00
|
|
|
|
2018-09-04 23:19:51 +00:00
|
|
|
// In groups, we don't treat unregistered users as a user-visible
|
|
|
|
// error. The message will look successful, but the details
|
|
|
|
// screen will show that we didn't send to these unregistered users.
|
|
|
|
const filteredErrors = _.reject(
|
|
|
|
result.errors,
|
|
|
|
error => error.name === 'UnregisteredUserError'
|
|
|
|
);
|
|
|
|
|
|
|
|
// We don't start the expiration timer if there are real errors
|
|
|
|
// left after filtering out all of the unregistered user errors.
|
|
|
|
const expirationStartTimestamp = filteredErrors.length
|
|
|
|
? null
|
|
|
|
: Date.now();
|
|
|
|
|
|
|
|
this.saveErrors(filteredErrors);
|
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
this.set({
|
2020-03-05 21:14:58 +00:00
|
|
|
sent_to: _.union(sentTo, result.successfulIdentifiers),
|
2018-04-27 21:25:04 +00:00
|
|
|
sent: true,
|
2018-09-04 23:19:51 +00:00
|
|
|
expirationStartTimestamp,
|
2018-10-18 01:01:21 +00:00
|
|
|
unidentifiedDeliveries: result.unidentifiedDeliveries,
|
2018-04-27 21:25:04 +00:00
|
|
|
});
|
|
|
|
promises.push(this.sendSyncMessage());
|
2018-09-04 23:19:51 +00:00
|
|
|
} else {
|
|
|
|
this.saveErrors(result.errors);
|
2018-04-27 21:25:04 +00:00
|
|
|
}
|
|
|
|
promises = promises.concat(
|
|
|
|
_.map(result.errors, error => {
|
|
|
|
if (error.name === 'OutgoingIdentityKeyError') {
|
2020-03-20 19:01:15 +00:00
|
|
|
const c = ConversationController.get(
|
|
|
|
error.identifer || error.number
|
|
|
|
);
|
2018-04-27 21:25:04 +00:00
|
|
|
promises.push(c.getProfiles());
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2015-09-14 03:25:04 +00:00
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
this.trigger('send-error', this.get('errors'));
|
|
|
|
|
|
|
|
return Promise.all(promises);
|
2018-04-10 23:51:48 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-01-31 01:45:58 +00:00
|
|
|
async sendSyncMessageOnly(dataMessage) {
|
2020-03-05 21:14:58 +00:00
|
|
|
const conv = this.getConversation();
|
2019-01-31 01:45:58 +00:00
|
|
|
this.set({ dataMessage });
|
|
|
|
|
|
|
|
try {
|
|
|
|
this.set({
|
2019-02-27 00:14:47 +00:00
|
|
|
// These are the same as a normal send()
|
2020-07-10 18:28:49 +00:00
|
|
|
sent_to: [conv.getSendTarget()],
|
2019-02-14 17:36:24 +00:00
|
|
|
sent: true,
|
|
|
|
expirationStartTimestamp: Date.now(),
|
2019-02-27 00:14:47 +00:00
|
|
|
});
|
|
|
|
const result = await this.sendSyncMessage();
|
|
|
|
this.set({
|
|
|
|
// We have to do this afterward, since we didn't have a previous send!
|
2019-02-14 17:36:24 +00:00
|
|
|
unidentifiedDeliveries: result ? result.unidentifiedDeliveries : null,
|
|
|
|
|
|
|
|
// These are unique to a Note to Self message - immediately read/delivered
|
2020-07-10 18:28:49 +00:00
|
|
|
delivered_to: [ConversationController.getOurConversationId()],
|
|
|
|
read_by: [ConversationController.getOurConversationId()],
|
2019-01-31 01:45:58 +00:00
|
|
|
});
|
|
|
|
} catch (result) {
|
|
|
|
const errors = (result && result.errors) || [
|
|
|
|
new Error('Unknown error'),
|
|
|
|
];
|
|
|
|
this.set({ errors });
|
|
|
|
} finally {
|
|
|
|
await window.Signal.Data.saveMessage(this.attributes, {
|
|
|
|
Message: Whisper.Message,
|
|
|
|
});
|
|
|
|
this.trigger('done');
|
|
|
|
|
|
|
|
const errors = this.get('errors');
|
|
|
|
if (errors) {
|
|
|
|
this.trigger('send-error', errors);
|
|
|
|
} else {
|
|
|
|
this.trigger('sent');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-04-10 23:51:48 +00:00
|
|
|
sendSyncMessage() {
|
2018-10-18 01:01:21 +00:00
|
|
|
const ourNumber = textsecure.storage.user.getNumber();
|
2020-03-05 21:14:58 +00:00
|
|
|
const ourUuid = textsecure.storage.user.getUuid();
|
2018-10-18 01:01:21 +00:00
|
|
|
const { wrap, sendOptions } = ConversationController.prepareForSend(
|
2020-03-05 21:14:58 +00:00
|
|
|
ourUuid || ourNumber,
|
2020-01-08 17:44:54 +00:00
|
|
|
{
|
|
|
|
syncMessage: true,
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
);
|
|
|
|
|
2018-04-10 23:51:48 +00:00
|
|
|
this.syncPromise = this.syncPromise || Promise.resolve();
|
2019-01-31 01:45:58 +00:00
|
|
|
const next = () => {
|
2018-04-10 23:51:48 +00:00
|
|
|
const dataMessage = this.get('dataMessage');
|
2019-05-09 15:38:05 +00:00
|
|
|
if (!dataMessage) {
|
2018-04-10 23:51:48 +00:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2019-05-09 15:38:05 +00:00
|
|
|
const isUpdate = Boolean(this.get('synced'));
|
2020-03-05 21:14:58 +00:00
|
|
|
const conv = this.getConversation();
|
2019-05-09 15:38:05 +00:00
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
return wrap(
|
|
|
|
textsecure.messaging.sendSyncMessage(
|
2018-04-27 21:25:04 +00:00
|
|
|
dataMessage,
|
|
|
|
this.get('sent_at'),
|
2020-03-05 21:14:58 +00:00
|
|
|
conv.get('e164'),
|
|
|
|
conv.get('uuid'),
|
2018-10-18 01:01:21 +00:00
|
|
|
this.get('expirationStartTimestamp'),
|
|
|
|
this.get('sent_to'),
|
|
|
|
this.get('unidentifiedDeliveries'),
|
2019-05-09 15:38:05 +00:00
|
|
|
isUpdate,
|
2018-10-18 01:01:21 +00:00
|
|
|
sendOptions
|
2018-04-27 21:25:04 +00:00
|
|
|
)
|
2019-01-31 01:45:58 +00:00
|
|
|
).then(result => {
|
2018-10-18 01:01:21 +00:00
|
|
|
this.set({
|
|
|
|
synced: true,
|
|
|
|
dataMessage: null,
|
2018-04-27 21:25:04 +00:00
|
|
|
});
|
2018-10-18 01:01:21 +00:00
|
|
|
return window.Signal.Data.saveMessage(this.attributes, {
|
|
|
|
Message: Whisper.Message,
|
2019-01-31 01:45:58 +00:00
|
|
|
}).then(() => result);
|
2018-10-18 01:01:21 +00:00
|
|
|
});
|
2019-01-31 01:45:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.syncPromise = this.syncPromise.then(next, next);
|
|
|
|
|
|
|
|
return this.syncPromise;
|
2018-04-10 23:51:48 +00:00
|
|
|
},
|
|
|
|
|
2019-03-20 17:42:28 +00:00
|
|
|
// Receive logic
|
2019-01-30 20:15:07 +00:00
|
|
|
async queueAttachmentDownloads() {
|
2020-06-09 22:33:37 +00:00
|
|
|
const attachmentsToQueue = this.get('attachments') || [];
|
2019-01-30 20:15:07 +00:00
|
|
|
const messageId = this.id;
|
|
|
|
let count = 0;
|
2019-03-13 20:38:28 +00:00
|
|
|
let bodyPending;
|
|
|
|
|
2020-06-09 22:33:37 +00:00
|
|
|
window.log.info(
|
|
|
|
`Queueing ${
|
|
|
|
attachmentsToQueue.length
|
|
|
|
} attachment downloads for message ${this.idForLogging()}`
|
|
|
|
);
|
|
|
|
|
2019-03-13 20:38:28 +00:00
|
|
|
const [longMessageAttachments, normalAttachments] = _.partition(
|
2020-06-09 22:33:37 +00:00
|
|
|
attachmentsToQueue,
|
2019-03-13 20:38:28 +00:00
|
|
|
attachment =>
|
|
|
|
attachment.contentType === Whisper.Message.LONG_MESSAGE_CONTENT_TYPE
|
|
|
|
);
|
|
|
|
|
|
|
|
if (longMessageAttachments.length > 1) {
|
|
|
|
window.log.error(
|
|
|
|
`Received more than one long message attachment in message ${this.idForLogging()}`
|
|
|
|
);
|
|
|
|
}
|
2020-06-09 22:33:37 +00:00
|
|
|
|
|
|
|
window.log.info(
|
|
|
|
`Queueing ${
|
|
|
|
longMessageAttachments.length
|
|
|
|
} long message attachment downloads for message ${this.idForLogging()}`
|
|
|
|
);
|
|
|
|
|
2019-03-13 20:38:28 +00:00
|
|
|
if (longMessageAttachments.length > 0) {
|
|
|
|
count += 1;
|
|
|
|
bodyPending = true;
|
|
|
|
await window.Signal.AttachmentDownloads.addJob(
|
|
|
|
longMessageAttachments[0],
|
|
|
|
{
|
|
|
|
messageId,
|
|
|
|
type: 'long-message',
|
|
|
|
index: 0,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2019-01-30 20:15:07 +00:00
|
|
|
|
2020-06-09 22:33:37 +00:00
|
|
|
window.log.info(
|
|
|
|
`Queueing ${
|
|
|
|
normalAttachments.length
|
|
|
|
} normal attachment downloads for message ${this.idForLogging()}`
|
|
|
|
);
|
2019-01-30 20:15:07 +00:00
|
|
|
const attachments = await Promise.all(
|
2019-03-13 20:38:28 +00:00
|
|
|
normalAttachments.map((attachment, index) => {
|
2019-01-30 20:15:07 +00:00
|
|
|
count += 1;
|
|
|
|
return window.Signal.AttachmentDownloads.addJob(attachment, {
|
|
|
|
messageId,
|
|
|
|
type: 'attachment',
|
|
|
|
index,
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2020-06-09 22:33:37 +00:00
|
|
|
const previewsToQueue = this.get('preview') || [];
|
|
|
|
window.log.info(
|
|
|
|
`Queueing ${
|
|
|
|
previewsToQueue.length
|
|
|
|
} preview attachment downloads for message ${this.idForLogging()}`
|
|
|
|
);
|
2019-01-30 20:15:07 +00:00
|
|
|
const preview = await Promise.all(
|
2020-06-09 22:33:37 +00:00
|
|
|
previewsToQueue.map(async (item, index) => {
|
2019-01-30 20:15:07 +00:00
|
|
|
if (!item.image) {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
count += 1;
|
|
|
|
return {
|
|
|
|
...item,
|
|
|
|
image: await window.Signal.AttachmentDownloads.addJob(item.image, {
|
|
|
|
messageId,
|
|
|
|
type: 'preview',
|
|
|
|
index,
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2020-06-09 22:33:37 +00:00
|
|
|
const contactsToQueue = this.get('contact') || [];
|
|
|
|
window.log.info(
|
|
|
|
`Queueing ${
|
|
|
|
contactsToQueue.length
|
|
|
|
} contact attachment downloads for message ${this.idForLogging()}`
|
|
|
|
);
|
2019-01-30 20:15:07 +00:00
|
|
|
const contact = await Promise.all(
|
2020-06-09 22:33:37 +00:00
|
|
|
contactsToQueue.map(async (item, index) => {
|
2019-01-30 20:15:07 +00:00
|
|
|
if (!item.avatar || !item.avatar.avatar) {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
count += 1;
|
|
|
|
return {
|
|
|
|
...item,
|
|
|
|
avatar: {
|
|
|
|
...item.avatar,
|
|
|
|
avatar: await window.Signal.AttachmentDownloads.addJob(
|
|
|
|
item.avatar.avatar,
|
|
|
|
{
|
|
|
|
messageId,
|
|
|
|
type: 'contact',
|
|
|
|
index,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
let quote = this.get('quote');
|
2020-06-09 22:33:37 +00:00
|
|
|
const quoteAttachmentsToQueue =
|
|
|
|
quote && quote.attachments ? quote.attachments : [];
|
|
|
|
window.log.info(
|
|
|
|
`Queueing ${
|
|
|
|
quoteAttachmentsToQueue.length
|
|
|
|
} quote attachment downloads for message ${this.idForLogging()}`
|
|
|
|
);
|
|
|
|
if (quoteAttachmentsToQueue.length > 0) {
|
2019-01-30 20:15:07 +00:00
|
|
|
quote = {
|
|
|
|
...quote,
|
|
|
|
attachments: await Promise.all(
|
|
|
|
(quote.attachments || []).map(async (item, index) => {
|
|
|
|
// If we already have a path, then we copied this image from the quoted
|
|
|
|
// message and we don't need to download the attachment.
|
|
|
|
if (!item.thumbnail || item.thumbnail.path) {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
count += 1;
|
|
|
|
return {
|
|
|
|
...item,
|
|
|
|
thumbnail: await window.Signal.AttachmentDownloads.addJob(
|
|
|
|
item.thumbnail,
|
|
|
|
{
|
|
|
|
messageId,
|
|
|
|
type: 'quote',
|
|
|
|
index,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
};
|
|
|
|
})
|
|
|
|
),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
let sticker = this.get('sticker');
|
|
|
|
if (sticker) {
|
2020-06-09 22:33:37 +00:00
|
|
|
window.log.info(
|
|
|
|
`Queueing sticker download for message ${this.idForLogging()}`
|
|
|
|
);
|
2019-05-16 22:32:11 +00:00
|
|
|
count += 1;
|
|
|
|
const { packId, stickerId, packKey } = sticker;
|
|
|
|
|
|
|
|
const status = getStickerPackStatus(packId);
|
|
|
|
let data;
|
|
|
|
|
2019-05-24 01:27:42 +00:00
|
|
|
if (status && (status === 'downloaded' || status === 'installed')) {
|
2019-05-16 22:32:11 +00:00
|
|
|
try {
|
|
|
|
const copiedSticker = await copyStickerToAttachments(
|
|
|
|
packId,
|
|
|
|
stickerId
|
|
|
|
);
|
|
|
|
data = {
|
|
|
|
...copiedSticker,
|
|
|
|
contentType: 'image/webp',
|
|
|
|
};
|
|
|
|
} catch (error) {
|
|
|
|
window.log.error(
|
|
|
|
`Problem copying sticker (${packId}, ${stickerId}) to attachments:`,
|
|
|
|
error && error.stack ? error.stack : error
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!data) {
|
|
|
|
data = await window.Signal.AttachmentDownloads.addJob(sticker.data, {
|
|
|
|
messageId,
|
|
|
|
type: 'sticker',
|
|
|
|
index: 0,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (!status) {
|
2019-05-24 01:27:42 +00:00
|
|
|
// Save the packId/packKey for future download/install
|
|
|
|
savePackMetadata(packId, packKey, { messageId });
|
2019-05-16 22:32:11 +00:00
|
|
|
} else {
|
|
|
|
await addStickerPackReference(messageId, packId);
|
|
|
|
}
|
|
|
|
|
|
|
|
sticker = {
|
|
|
|
...sticker,
|
|
|
|
packId,
|
|
|
|
data,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-09 22:33:37 +00:00
|
|
|
window.log.info(
|
|
|
|
`Queued ${count} total attachment downloads for message ${this.idForLogging()}`
|
|
|
|
);
|
|
|
|
|
2019-01-30 20:15:07 +00:00
|
|
|
if (count > 0) {
|
2019-05-16 22:32:11 +00:00
|
|
|
this.set({
|
|
|
|
bodyPending,
|
|
|
|
attachments,
|
|
|
|
preview,
|
|
|
|
contact,
|
|
|
|
quote,
|
|
|
|
sticker,
|
|
|
|
});
|
2019-01-30 20:15:07 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
2019-02-20 18:28:57 +00:00
|
|
|
|
|
|
|
async copyFromQuotedMessage(message) {
|
|
|
|
const { quote } = message;
|
|
|
|
if (!quote) {
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2020-07-10 18:28:49 +00:00
|
|
|
const { attachments, id, author, authorUuid } = quote;
|
2019-02-20 18:28:57 +00:00
|
|
|
const firstAttachment = attachments[0];
|
2020-07-10 18:28:49 +00:00
|
|
|
const authorConversationId = ConversationController.ensureContactIds({
|
|
|
|
e164: author,
|
|
|
|
uuid: authorUuid,
|
|
|
|
});
|
2019-02-20 18:28:57 +00:00
|
|
|
|
|
|
|
const collection = await window.Signal.Data.getMessagesBySentAt(id, {
|
|
|
|
MessageCollection: Whisper.MessageCollection,
|
|
|
|
});
|
2019-03-26 01:10:30 +00:00
|
|
|
const found = collection.find(item => {
|
2020-07-10 18:28:49 +00:00
|
|
|
const messageAuthorId = item.getContactId();
|
2019-02-20 18:28:57 +00:00
|
|
|
|
2020-07-10 18:28:49 +00:00
|
|
|
return authorConversationId === messageAuthorId;
|
2019-02-20 18:28:57 +00:00
|
|
|
});
|
|
|
|
|
2019-03-26 01:10:30 +00:00
|
|
|
if (!found) {
|
2019-02-20 18:28:57 +00:00
|
|
|
quote.referencedMessageNotFound = true;
|
|
|
|
return message;
|
|
|
|
}
|
2019-06-26 19:33:13 +00:00
|
|
|
if (found.isTapToView()) {
|
|
|
|
quote.text = null;
|
|
|
|
quote.attachments = [
|
|
|
|
{
|
|
|
|
contentType: 'image/jpeg',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
return message;
|
|
|
|
}
|
2019-02-20 18:28:57 +00:00
|
|
|
|
2019-03-26 01:10:30 +00:00
|
|
|
const queryMessage = MessageController.register(found.id, found);
|
2019-02-20 18:28:57 +00:00
|
|
|
quote.text = queryMessage.get('body');
|
|
|
|
if (firstAttachment) {
|
|
|
|
firstAttachment.thumbnail = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
!firstAttachment ||
|
2019-10-03 19:03:46 +00:00
|
|
|
(!GoogleChrome.isImageTypeSupported(firstAttachment.contentType) &&
|
|
|
|
!GoogleChrome.isVideoTypeSupported(firstAttachment.contentType))
|
2019-02-20 18:28:57 +00:00
|
|
|
) {
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (
|
|
|
|
queryMessage.get('schemaVersion') <
|
|
|
|
TypedMessage.VERSION_NEEDED_FOR_DISPLAY
|
|
|
|
) {
|
|
|
|
const upgradedMessage = await upgradeMessageSchema(
|
|
|
|
queryMessage.attributes
|
|
|
|
);
|
|
|
|
queryMessage.set(upgradedMessage);
|
|
|
|
await window.Signal.Data.saveMessage(upgradedMessage, {
|
|
|
|
Message: Whisper.Message,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
window.log.error(
|
|
|
|
'Problem upgrading message quoted message from database',
|
|
|
|
Errors.toLogFormat(error)
|
|
|
|
);
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
|
|
|
const queryAttachments = queryMessage.get('attachments') || [];
|
|
|
|
if (queryAttachments.length > 0) {
|
|
|
|
const queryFirst = queryAttachments[0];
|
|
|
|
const { thumbnail } = queryFirst;
|
|
|
|
|
|
|
|
if (thumbnail && thumbnail.path) {
|
|
|
|
firstAttachment.thumbnail = {
|
|
|
|
...thumbnail,
|
|
|
|
copied: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const queryPreview = queryMessage.get('preview') || [];
|
|
|
|
if (queryPreview.length > 0) {
|
|
|
|
const queryFirst = queryPreview[0];
|
|
|
|
const { image } = queryFirst;
|
|
|
|
|
|
|
|
if (image && image.path) {
|
|
|
|
firstAttachment.thumbnail = {
|
|
|
|
...image,
|
|
|
|
copied: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
const sticker = queryMessage.get('sticker');
|
|
|
|
if (sticker && sticker.data && sticker.data.path) {
|
|
|
|
firstAttachment.thumbnail = {
|
|
|
|
...sticker.data,
|
|
|
|
copied: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-02-20 18:28:57 +00:00
|
|
|
return message;
|
|
|
|
},
|
|
|
|
|
2020-02-14 21:28:35 +00:00
|
|
|
handleDataMessage(initialMessage, confirm, options = {}) {
|
|
|
|
const { data } = options;
|
|
|
|
|
2018-04-10 23:51:48 +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 go through one of the previous two codepaths
|
|
|
|
const message = this;
|
|
|
|
const source = message.get('source');
|
2020-03-05 21:14:58 +00:00
|
|
|
const sourceUuid = message.get('sourceUuid');
|
2018-04-10 23:51:48 +00:00
|
|
|
const type = message.get('type');
|
2020-07-10 18:28:49 +00:00
|
|
|
const conversationId = message.get('conversationId');
|
2018-04-10 23:51:48 +00:00
|
|
|
const GROUP_TYPES = textsecure.protobuf.GroupContext.Type;
|
|
|
|
|
|
|
|
const conversation = ConversationController.get(conversationId);
|
2018-07-25 22:02:27 +00:00
|
|
|
return conversation.queueJob(async () => {
|
2019-04-08 23:37:00 +00:00
|
|
|
window.log.info(
|
|
|
|
`Starting handleDataMessage for message ${message.idForLogging()} in conversation ${conversation.idForLogging()}`
|
|
|
|
);
|
2019-12-13 21:25:36 +00:00
|
|
|
|
|
|
|
// First, check for duplicates. If we find one, stop processing here.
|
|
|
|
const existingMessage = await getMessageBySender(this.attributes, {
|
|
|
|
Message: Whisper.Message,
|
|
|
|
});
|
2020-02-14 21:28:35 +00:00
|
|
|
const isUpdate = Boolean(data && data.isRecipientUpdate);
|
|
|
|
|
|
|
|
if (existingMessage && type === 'incoming') {
|
2019-12-13 21:25:36 +00:00
|
|
|
window.log.warn('Received duplicate message', this.idForLogging());
|
|
|
|
confirm();
|
|
|
|
return;
|
|
|
|
}
|
2020-02-14 21:28:35 +00:00
|
|
|
if (type === 'outgoing') {
|
|
|
|
if (isUpdate && existingMessage) {
|
|
|
|
window.log.info(
|
|
|
|
`handleDataMessage: Updating message ${message.idForLogging()} with received transcript`
|
|
|
|
);
|
|
|
|
|
|
|
|
let sentTo = [];
|
|
|
|
let unidentifiedDeliveries = [];
|
|
|
|
if (Array.isArray(data.unidentifiedStatus)) {
|
|
|
|
sentTo = data.unidentifiedStatus.map(item => item.destination);
|
|
|
|
|
|
|
|
const unidentified = _.filter(data.unidentifiedStatus, item =>
|
|
|
|
Boolean(item.unidentified)
|
|
|
|
);
|
|
|
|
unidentifiedDeliveries = unidentified.map(
|
|
|
|
item => item.destination
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const toUpdate = MessageController.register(
|
|
|
|
existingMessage.id,
|
|
|
|
existingMessage
|
|
|
|
);
|
|
|
|
toUpdate.set({
|
|
|
|
sent_to: _.union(toUpdate.get('sent_to'), sentTo),
|
|
|
|
unidentifiedDeliveries: _.union(
|
|
|
|
toUpdate.get('unidentifiedDeliveries'),
|
|
|
|
unidentifiedDeliveries
|
|
|
|
),
|
|
|
|
});
|
|
|
|
await window.Signal.Data.saveMessage(toUpdate.attributes, {
|
|
|
|
Message: Whisper.Message,
|
|
|
|
});
|
|
|
|
|
|
|
|
confirm();
|
|
|
|
return;
|
|
|
|
} else if (isUpdate) {
|
|
|
|
window.log.warn(
|
|
|
|
`handleDataMessage: Received update transcript, but no existing entry for message ${message.idForLogging()}. Dropping.`
|
|
|
|
);
|
|
|
|
|
|
|
|
confirm();
|
|
|
|
return;
|
|
|
|
} else if (existingMessage) {
|
|
|
|
window.log.warn(
|
|
|
|
`handleDataMessage: Received duplicate transcript for message ${message.idForLogging()}, but it was not an update transcript. Dropping.`
|
|
|
|
);
|
|
|
|
|
|
|
|
confirm();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We drop incoming messages for groups we already know about, which we're not a
|
|
|
|
// part of, except for group updates.
|
2020-03-05 21:14:58 +00:00
|
|
|
const ourUuid = textsecure.storage.user.getUuid();
|
2020-02-14 21:28:35 +00:00
|
|
|
const ourNumber = textsecure.storage.user.getNumber();
|
|
|
|
const isGroupUpdate =
|
|
|
|
initialMessage.group &&
|
|
|
|
initialMessage.group.type !==
|
|
|
|
textsecure.protobuf.GroupContext.Type.DELIVER;
|
|
|
|
if (
|
|
|
|
type === 'incoming' &&
|
|
|
|
!conversation.isPrivate() &&
|
2020-03-05 21:14:58 +00:00
|
|
|
!conversation.hasMember(ourNumber || ourUuid) &&
|
2020-02-14 21:28:35 +00:00
|
|
|
!isGroupUpdate
|
|
|
|
) {
|
|
|
|
window.log.warn(
|
|
|
|
`Received message destined for group ${conversation.idForLogging()}, which we're not a part of. Dropping.`
|
|
|
|
);
|
|
|
|
confirm();
|
|
|
|
return;
|
|
|
|
}
|
2019-12-13 21:25:36 +00:00
|
|
|
|
|
|
|
// Send delivery receipts, but only for incoming sealed sender messages
|
2020-05-27 21:37:06 +00:00
|
|
|
// and not for messages from unaccepted conversations
|
2019-12-13 21:25:36 +00:00
|
|
|
if (
|
|
|
|
type === 'incoming' &&
|
|
|
|
this.get('unidentifiedDeliveryReceived') &&
|
2020-05-27 21:37:06 +00:00
|
|
|
!this.hasErrors() &&
|
|
|
|
conversation.getAccepted()
|
2019-12-13 21:25:36 +00:00
|
|
|
) {
|
|
|
|
// Note: We both queue and batch because we want to wait until we are done
|
|
|
|
// processing incoming messages to start sending outgoing delivery receipts.
|
|
|
|
// The queue can be paused easily.
|
|
|
|
Whisper.deliveryReceiptQueue.add(() => {
|
|
|
|
Whisper.deliveryReceiptBatcher.add({
|
|
|
|
source,
|
2020-03-05 21:14:58 +00:00
|
|
|
sourceUuid,
|
2019-12-13 21:25:36 +00:00
|
|
|
timestamp: this.get('sent_at'),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-20 18:28:57 +00:00
|
|
|
const withQuoteReference = await this.copyFromQuotedMessage(
|
|
|
|
initialMessage
|
|
|
|
);
|
|
|
|
const dataMessage = await upgradeMessageSchema(withQuoteReference);
|
|
|
|
|
2018-07-25 22:02:27 +00:00
|
|
|
try {
|
|
|
|
const now = new Date().getTime();
|
2019-01-16 03:03:56 +00:00
|
|
|
|
|
|
|
const urls = window.Signal.LinkPreviews.findLinks(dataMessage.body);
|
|
|
|
const incomingPreview = dataMessage.preview || [];
|
|
|
|
const preview = incomingPreview.filter(
|
|
|
|
item =>
|
|
|
|
(item.image || item.title) &&
|
|
|
|
urls.includes(item.url) &&
|
2020-08-29 01:27:45 +00:00
|
|
|
window.Signal.LinkPreviews.isLinkSafeToPreview(item.url)
|
2019-01-16 03:03:56 +00:00
|
|
|
);
|
2019-01-31 17:53:11 +00:00
|
|
|
if (preview.length < incomingPreview.length) {
|
2019-01-16 03:03:56 +00:00
|
|
|
window.log.info(
|
|
|
|
`${message.idForLogging()}: Eliminated ${preview.length -
|
|
|
|
incomingPreview.length} previews with invalid urls'`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-07-25 22:02:27 +00:00
|
|
|
message.set({
|
2019-09-26 19:56:31 +00:00
|
|
|
id: window.getGuid(),
|
2018-07-25 22:02:27 +00:00
|
|
|
attachments: dataMessage.attachments,
|
|
|
|
body: dataMessage.body,
|
|
|
|
contact: dataMessage.contact,
|
|
|
|
conversationId: conversation.id,
|
|
|
|
decrypted_at: now,
|
|
|
|
errors: [],
|
|
|
|
flags: dataMessage.flags,
|
|
|
|
hasAttachments: dataMessage.hasAttachments,
|
|
|
|
hasFileAttachments: dataMessage.hasFileAttachments,
|
|
|
|
hasVisualMediaAttachments: dataMessage.hasVisualMediaAttachments,
|
2019-08-05 20:53:15 +00:00
|
|
|
isViewOnce: Boolean(dataMessage.isViewOnce),
|
2019-01-16 03:03:56 +00:00
|
|
|
preview,
|
2019-06-10 21:40:02 +00:00
|
|
|
requiredProtocolVersion:
|
|
|
|
dataMessage.requiredProtocolVersion ||
|
|
|
|
this.INITIAL_PROTOCOL_VERSION,
|
|
|
|
supportedVersionAtReceive: this.CURRENT_PROTOCOL_VERSION,
|
2019-05-16 22:32:11 +00:00
|
|
|
quote: dataMessage.quote,
|
2018-07-25 22:02:27 +00:00
|
|
|
schemaVersion: dataMessage.schemaVersion,
|
2019-05-16 22:32:11 +00:00
|
|
|
sticker: dataMessage.sticker,
|
2018-07-25 22:02:27 +00:00
|
|
|
});
|
2019-12-13 21:29:20 +00:00
|
|
|
|
|
|
|
const isSupported = !message.isUnsupportedMessage();
|
|
|
|
if (!isSupported) {
|
|
|
|
await message.eraseContents();
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
|
2019-12-13 21:29:20 +00:00
|
|
|
if (isSupported) {
|
|
|
|
let attributes = {
|
|
|
|
...conversation.attributes,
|
|
|
|
};
|
|
|
|
if (dataMessage.group) {
|
2020-07-07 00:39:55 +00:00
|
|
|
const pendingGroupUpdate = [];
|
2020-03-05 21:14:58 +00:00
|
|
|
const memberConversations = await Promise.all(
|
2020-07-24 20:20:58 +00:00
|
|
|
dataMessage.group.membersE164.map(e164 =>
|
|
|
|
ConversationController.getOrCreateAndWait(e164, 'private')
|
|
|
|
)
|
2020-03-05 21:14:58 +00:00
|
|
|
);
|
|
|
|
const members = memberConversations.map(c => c.get('id'));
|
2019-12-13 21:29:20 +00:00
|
|
|
attributes = {
|
|
|
|
...attributes,
|
|
|
|
type: 'group',
|
|
|
|
groupId: dataMessage.group.id,
|
|
|
|
};
|
|
|
|
if (dataMessage.group.type === GROUP_TYPES.UPDATE) {
|
|
|
|
attributes = {
|
|
|
|
...attributes,
|
|
|
|
name: dataMessage.group.name,
|
2020-03-05 21:14:58 +00:00
|
|
|
members: _.union(members, conversation.get('members')),
|
2019-12-13 21:29:20 +00:00
|
|
|
};
|
|
|
|
|
2020-03-26 21:47:35 +00:00
|
|
|
if (dataMessage.group.name !== conversation.get('name')) {
|
2020-07-07 00:39:55 +00:00
|
|
|
pendingGroupUpdate.push(['name', dataMessage.group.name]);
|
2020-03-26 21:47:35 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 00:39:55 +00:00
|
|
|
const avatarAttachment = dataMessage.group.avatar;
|
|
|
|
|
|
|
|
let downloadedAvatar;
|
|
|
|
let hash;
|
|
|
|
if (avatarAttachment) {
|
|
|
|
try {
|
|
|
|
downloadedAvatar = await window.Signal.Util.downloadAttachment(
|
|
|
|
avatarAttachment
|
|
|
|
);
|
|
|
|
|
|
|
|
if (downloadedAvatar) {
|
|
|
|
const loadedAttachment = await Signal.Migrations.loadAttachmentData(
|
|
|
|
downloadedAvatar
|
|
|
|
);
|
|
|
|
|
|
|
|
hash = await Signal.Types.Conversation.computeHash(
|
|
|
|
loadedAttachment.data
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
window.log.info(
|
|
|
|
'handleDataMessage: group avatar download failed'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const existingAvatar = conversation.get('avatar');
|
|
|
|
|
|
|
|
if (
|
|
|
|
// Avatar added
|
2020-07-24 01:35:32 +00:00
|
|
|
(!existingAvatar && avatarAttachment) ||
|
2020-07-07 00:39:55 +00:00
|
|
|
// Avatar changed
|
|
|
|
(existingAvatar && existingAvatar.hash !== hash) ||
|
|
|
|
// Avatar removed
|
2020-07-24 01:35:32 +00:00
|
|
|
(existingAvatar && !avatarAttachment)
|
2020-07-07 00:39:55 +00:00
|
|
|
) {
|
|
|
|
// Removes existing avatar from disk
|
|
|
|
if (existingAvatar && existingAvatar.path) {
|
|
|
|
await Signal.Migrations.deleteAttachmentData(
|
|
|
|
existingAvatar.path
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let avatar = null;
|
|
|
|
if (downloadedAvatar && avatarAttachment !== null) {
|
|
|
|
const onDiskAttachment = await window.Signal.Types.Attachment.migrateDataToFileSystem(
|
|
|
|
downloadedAvatar,
|
|
|
|
{
|
|
|
|
writeNewAttachmentData:
|
|
|
|
window.Signal.Migrations.writeNewAttachmentData,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
avatar = {
|
|
|
|
...onDiskAttachment,
|
|
|
|
hash,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
attributes.avatar = avatar;
|
|
|
|
|
|
|
|
pendingGroupUpdate.push(['avatarUpdated', true]);
|
|
|
|
} else {
|
|
|
|
window.log.info(
|
|
|
|
'handleDataMessage: Group avatar hash matched; not replacing group avatar'
|
|
|
|
);
|
|
|
|
}
|
2019-12-13 21:29:20 +00:00
|
|
|
|
|
|
|
const difference = _.difference(
|
2020-03-05 21:14:58 +00:00
|
|
|
members,
|
2019-12-13 21:29:20 +00:00
|
|
|
conversation.get('members')
|
2018-04-27 21:25:04 +00:00
|
|
|
);
|
2019-12-13 21:29:20 +00:00
|
|
|
if (difference.length > 0) {
|
2020-08-05 01:13:19 +00:00
|
|
|
// Because GroupV1 groups are based on e164 only
|
|
|
|
const e164s = difference.map(id => {
|
|
|
|
const c = ConversationController.get(id);
|
|
|
|
return c ? c.get('e164') : null;
|
|
|
|
});
|
|
|
|
pendingGroupUpdate.push(['joined', e164s]);
|
2019-12-13 21:29:20 +00:00
|
|
|
}
|
|
|
|
if (conversation.get('left')) {
|
|
|
|
window.log.warn('re-added to a left group');
|
|
|
|
attributes.left = false;
|
2020-07-10 18:28:49 +00:00
|
|
|
conversation.set({ addedBy: message.getContactId() });
|
2019-12-13 21:29:20 +00:00
|
|
|
}
|
|
|
|
} else if (dataMessage.group.type === GROUP_TYPES.QUIT) {
|
2020-07-24 01:35:32 +00:00
|
|
|
const senderId = ConversationController.ensureContactIds({
|
2020-07-10 18:28:49 +00:00
|
|
|
e164: source,
|
|
|
|
uuid: sourceUuid,
|
|
|
|
});
|
2020-07-24 01:35:32 +00:00
|
|
|
const sender = ConversationController.get(senderId);
|
2020-06-09 00:28:48 +00:00
|
|
|
const inGroup = Boolean(
|
|
|
|
sender &&
|
|
|
|
(conversation.get('members') || []).includes(sender.id)
|
|
|
|
);
|
|
|
|
if (!inGroup) {
|
|
|
|
const senderString = sender ? sender.idForLogging() : null;
|
|
|
|
window.log.info(
|
|
|
|
`Got 'left' message from someone not in group: ${senderString}. Dropping.`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sender.isMe()) {
|
2019-12-13 21:29:20 +00:00
|
|
|
attributes.left = true;
|
2020-07-07 00:39:55 +00:00
|
|
|
pendingGroupUpdate.push(['left', 'You']);
|
2019-12-13 21:29:20 +00:00
|
|
|
} else {
|
2020-07-07 00:39:55 +00:00
|
|
|
pendingGroupUpdate.push(['left', sender.get('id')]);
|
2019-12-13 21:29:20 +00:00
|
|
|
}
|
|
|
|
attributes.members = _.without(
|
|
|
|
conversation.get('members'),
|
2020-06-09 00:28:48 +00:00
|
|
|
sender.get('id')
|
2018-04-27 21:25:04 +00:00
|
|
|
);
|
|
|
|
}
|
2019-12-13 21:29:20 +00:00
|
|
|
|
2020-07-07 00:39:55 +00:00
|
|
|
if (pendingGroupUpdate.length) {
|
|
|
|
const groupUpdate = pendingGroupUpdate.reduce(
|
|
|
|
(acc, [key, value]) => {
|
|
|
|
acc[key] = value;
|
|
|
|
return acc;
|
|
|
|
},
|
|
|
|
{}
|
|
|
|
);
|
2019-12-13 21:29:20 +00:00
|
|
|
message.set({ group_update: groupUpdate });
|
2018-08-07 19:33:56 +00:00
|
|
|
}
|
2018-04-27 21:25:04 +00:00
|
|
|
}
|
2017-07-03 23:46:39 +00:00
|
|
|
|
2019-12-13 21:29:20 +00:00
|
|
|
if (type === 'outgoing') {
|
|
|
|
const receipts = Whisper.DeliveryReceipts.forMessage(
|
|
|
|
conversation,
|
|
|
|
message
|
|
|
|
);
|
|
|
|
receipts.forEach(receipt =>
|
|
|
|
message.set({
|
|
|
|
delivered: (message.get('delivered') || 0) + 1,
|
|
|
|
delivered_to: _.union(message.get('delivered_to') || [], [
|
2020-03-05 21:14:58 +00:00
|
|
|
receipt.get('deliveredTo'),
|
2019-12-13 21:29:20 +00:00
|
|
|
]),
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
attributes.active_at = now;
|
|
|
|
conversation.set(attributes);
|
|
|
|
|
|
|
|
if (message.isExpirationTimerUpdate()) {
|
2018-07-25 22:02:27 +00:00
|
|
|
message.set({
|
2019-12-13 21:29:20 +00:00
|
|
|
expirationTimerUpdate: {
|
|
|
|
source,
|
2020-07-10 18:28:49 +00:00
|
|
|
sourceUuid,
|
2019-12-13 21:29:20 +00:00
|
|
|
expireTimer: dataMessage.expireTimer,
|
|
|
|
},
|
2018-04-27 21:25:04 +00:00
|
|
|
});
|
2019-12-13 21:29:20 +00:00
|
|
|
conversation.set({ expireTimer: dataMessage.expireTimer });
|
|
|
|
} else if (dataMessage.expireTimer) {
|
|
|
|
message.set({ expireTimer: dataMessage.expireTimer });
|
2018-04-27 21:25:04 +00:00
|
|
|
}
|
2018-04-10 23:51:48 +00:00
|
|
|
|
2019-12-13 21:29:20 +00:00
|
|
|
// NOTE: Remove once the above uses
|
|
|
|
// `Conversation::updateExpirationTimer`:
|
|
|
|
const { expireTimer } = dataMessage;
|
|
|
|
const shouldLogExpireTimerChange =
|
|
|
|
message.isExpirationTimerUpdate() || expireTimer;
|
|
|
|
if (shouldLogExpireTimerChange) {
|
|
|
|
window.log.info("Update conversation 'expireTimer'", {
|
|
|
|
id: conversation.idForLogging(),
|
|
|
|
expireTimer,
|
|
|
|
source: 'handleDataMessage',
|
2019-01-31 01:45:58 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-13 21:29:20 +00:00
|
|
|
if (!message.isEndSession()) {
|
|
|
|
if (dataMessage.expireTimer) {
|
|
|
|
if (
|
|
|
|
dataMessage.expireTimer !== conversation.get('expireTimer')
|
|
|
|
) {
|
|
|
|
conversation.updateExpirationTimer(
|
|
|
|
dataMessage.expireTimer,
|
|
|
|
source,
|
|
|
|
message.get('received_at'),
|
|
|
|
{
|
|
|
|
fromGroupUpdate: message.isGroupUpdate(),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else if (
|
|
|
|
conversation.get('expireTimer') &&
|
|
|
|
// We only turn off timers if it's not a group update
|
|
|
|
!message.isGroupUpdate()
|
|
|
|
) {
|
|
|
|
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',
|
|
|
|
Math.min(readSync.get('read_at'), Date.now())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (readSync || message.isExpirationTimerUpdate()) {
|
|
|
|
message.unset('unread');
|
|
|
|
// 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.
|
|
|
|
const c = message.getConversation();
|
|
|
|
if (c) {
|
|
|
|
c.onReadMessage(message);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
conversation.set({
|
|
|
|
unreadCount: conversation.get('unreadCount') + 1,
|
|
|
|
isArchived: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-07-25 22:02:27 +00:00
|
|
|
|
2019-12-13 21:29:20 +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),
|
|
|
|
});
|
|
|
|
}
|
2018-07-25 22:02:27 +00:00
|
|
|
|
2019-12-13 21:29:20 +00:00
|
|
|
// A sync'd message to ourself is automatically considered read/delivered
|
|
|
|
if (conversation.isMe()) {
|
|
|
|
message.set({
|
|
|
|
read_by: conversation.getRecipients(),
|
|
|
|
delivered_to: conversation.getRecipients(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
message.set({ recipients: conversation.getRecipients() });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dataMessage.profileKey) {
|
|
|
|
const profileKey = dataMessage.profileKey.toString('base64');
|
2020-03-05 21:14:58 +00:00
|
|
|
if (
|
|
|
|
source === textsecure.storage.user.getNumber() ||
|
|
|
|
sourceUuid === textsecure.storage.user.getUuid()
|
|
|
|
) {
|
2019-12-13 21:29:20 +00:00
|
|
|
conversation.set({ profileSharing: true });
|
|
|
|
} else if (conversation.isPrivate()) {
|
|
|
|
conversation.setProfileKey(profileKey);
|
|
|
|
} else {
|
2020-06-12 22:36:32 +00:00
|
|
|
const localId = ConversationController.ensureContactIds({
|
|
|
|
e164: source,
|
|
|
|
uuid: sourceUuid,
|
2019-12-13 21:29:20 +00:00
|
|
|
});
|
2020-07-10 18:28:49 +00:00
|
|
|
ConversationController.get(localId).setProfileKey(profileKey);
|
2019-12-13 21:29:20 +00:00
|
|
|
}
|
2018-04-10 23:51:48 +00:00
|
|
|
}
|
2018-07-25 22:02:27 +00:00
|
|
|
|
2019-12-13 21:29:20 +00:00
|
|
|
if (message.isTapToView() && type === 'outgoing') {
|
|
|
|
await message.eraseContents();
|
|
|
|
}
|
2019-06-26 19:33:13 +00:00
|
|
|
|
2019-12-13 21:29:20 +00:00
|
|
|
if (
|
|
|
|
type === 'incoming' &&
|
|
|
|
message.isTapToView() &&
|
|
|
|
!message.isValidTapToView()
|
|
|
|
) {
|
|
|
|
window.log.warn(
|
|
|
|
`Received tap to view message ${message.idForLogging()} with invalid data. Erasing contents.`
|
|
|
|
);
|
|
|
|
message.set({
|
|
|
|
isTapToViewInvalid: true,
|
|
|
|
});
|
|
|
|
await message.eraseContents();
|
|
|
|
}
|
|
|
|
// Check for out-of-order view syncs
|
|
|
|
if (type === 'incoming' && message.isTapToView()) {
|
|
|
|
const viewSync = Whisper.ViewSyncs.forMessage(message);
|
|
|
|
if (viewSync) {
|
2020-01-14 20:03:25 +00:00
|
|
|
await message.markViewed({ fromSync: true });
|
2019-12-13 21:29:20 +00:00
|
|
|
}
|
2019-06-26 19:33:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 21:37:06 +00:00
|
|
|
// Drop empty messages. This needs to happen after the initial
|
|
|
|
// message.set call to make sure all possible properties are set
|
|
|
|
// before we determine that a message is empty.
|
|
|
|
if (message.isEmpty()) {
|
|
|
|
window.log.info(
|
|
|
|
`Dropping empty datamessage ${message.idForLogging()} in conversation ${conversation.idForLogging()}`
|
|
|
|
);
|
|
|
|
confirm();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-07 21:29:57 +00:00
|
|
|
const conversationTimestamp = conversation.get('timestamp');
|
|
|
|
if (
|
|
|
|
!conversationTimestamp ||
|
|
|
|
message.get('sent_at') > conversationTimestamp
|
|
|
|
) {
|
|
|
|
conversation.set({
|
|
|
|
lastMessage: message.getNotificationText(),
|
|
|
|
timestamp: message.get('sent_at'),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-09-26 19:56:31 +00:00
|
|
|
MessageController.register(message.id, message);
|
2020-05-27 21:37:06 +00:00
|
|
|
conversation.incrementMessageCount();
|
2020-04-01 18:59:11 +00:00
|
|
|
window.Signal.Data.updateConversation(conversation.attributes);
|
2019-09-26 19:56:31 +00:00
|
|
|
|
2020-05-27 21:37:06 +00:00
|
|
|
// Only queue attachments for downloads if this is an outgoing message
|
|
|
|
// or we've accepted the conversation
|
|
|
|
if (this.getConversation().getAccepted() || message.isOutgoing()) {
|
|
|
|
await message.queueAttachmentDownloads();
|
|
|
|
}
|
2020-07-10 18:24:58 +00:00
|
|
|
|
|
|
|
// Does this message have any pending, previously-received associated reactions?
|
|
|
|
const reactions = Whisper.Reactions.forMessage(message);
|
|
|
|
reactions.forEach(reaction => {
|
|
|
|
message.handleReaction(reaction, false);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Does this message have any pending, previously-received associated
|
|
|
|
// delete for everyone messages?
|
|
|
|
const deletes = Whisper.Deletes.forMessage(message);
|
2020-07-27 18:15:32 +00:00
|
|
|
deletes.forEach(del => {
|
|
|
|
window.Signal.Util.deleteForEveryone(message, del, false);
|
|
|
|
});
|
2020-07-10 18:24:58 +00:00
|
|
|
|
2019-09-26 19:56:31 +00:00
|
|
|
await window.Signal.Data.saveMessage(message.attributes, {
|
|
|
|
Message: Whisper.Message,
|
|
|
|
forceSave: true,
|
|
|
|
});
|
2018-07-25 22:02:27 +00:00
|
|
|
|
|
|
|
conversation.trigger('newmessage', message);
|
|
|
|
|
|
|
|
if (message.get('unread')) {
|
|
|
|
await conversation.notify(message);
|
|
|
|
}
|
|
|
|
|
2020-05-27 21:37:06 +00:00
|
|
|
// Increment the sent message count if this is an outgoing message
|
|
|
|
if (type === 'outgoing') {
|
|
|
|
conversation.incrementSentMessageCount();
|
|
|
|
}
|
|
|
|
|
2019-10-17 17:18:10 +00:00
|
|
|
Whisper.events.trigger('incrementProgress');
|
2018-07-25 22:02:27 +00:00
|
|
|
confirm();
|
|
|
|
} catch (error) {
|
|
|
|
const errorForLog = error && error.stack ? error.stack : error;
|
|
|
|
window.log.error(
|
|
|
|
'handleDataMessage',
|
|
|
|
message.idForLogging(),
|
|
|
|
'error:',
|
|
|
|
errorForLog
|
|
|
|
);
|
2018-08-08 16:40:12 +00:00
|
|
|
throw error;
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
});
|
2018-04-10 23:51:48 +00:00
|
|
|
},
|
2020-01-17 22:23:19 +00:00
|
|
|
|
2020-07-10 18:24:58 +00:00
|
|
|
async handleReaction(reaction, shouldPersist = true) {
|
2020-04-29 21:24:12 +00:00
|
|
|
if (this.get('deletedForEveryone')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-17 22:23:19 +00:00
|
|
|
const reactions = this.get('reactions') || [];
|
2020-02-07 23:12:47 +00:00
|
|
|
const messageId = this.idForLogging();
|
|
|
|
const count = reactions.length;
|
2020-01-17 22:23:19 +00:00
|
|
|
|
|
|
|
if (reaction.get('remove')) {
|
2020-02-07 23:12:47 +00:00
|
|
|
window.log.info('Removing reaction for message', messageId);
|
2020-01-17 22:23:19 +00:00
|
|
|
const newReactions = reactions.filter(
|
|
|
|
re =>
|
|
|
|
re.emoji !== reaction.get('emoji') ||
|
|
|
|
re.fromId !== reaction.get('fromId')
|
|
|
|
);
|
|
|
|
this.set({ reactions: newReactions });
|
|
|
|
} else {
|
2020-02-07 23:12:47 +00:00
|
|
|
window.log.info('Adding reaction for message', messageId);
|
2020-01-17 22:23:19 +00:00
|
|
|
const newReactions = reactions.filter(
|
|
|
|
re => re.fromId !== reaction.get('fromId')
|
|
|
|
);
|
|
|
|
newReactions.push(reaction.toJSON());
|
|
|
|
this.set({ reactions: newReactions });
|
|
|
|
|
|
|
|
const conversation = ConversationController.get(
|
|
|
|
this.get('conversationId')
|
|
|
|
);
|
|
|
|
|
|
|
|
// Only notify for reactions to our own messages
|
2020-01-23 23:57:37 +00:00
|
|
|
if (conversation && this.isOutgoing() && !reaction.get('fromSync')) {
|
2020-01-17 22:23:19 +00:00
|
|
|
conversation.notify(this, reaction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 23:12:47 +00:00
|
|
|
const newCount = this.get('reactions').length;
|
|
|
|
window.log.info(
|
|
|
|
`Done processing reaction for message ${messageId}. Went from ${count} to ${newCount} reactions.`
|
|
|
|
);
|
|
|
|
|
2020-07-10 18:24:58 +00:00
|
|
|
if (shouldPersist) {
|
|
|
|
await window.Signal.Data.saveMessage(this.attributes, {
|
|
|
|
Message: Whisper.Message,
|
|
|
|
});
|
|
|
|
}
|
2020-01-17 22:23:19 +00:00
|
|
|
},
|
2020-04-29 21:24:12 +00:00
|
|
|
|
2020-07-27 18:15:32 +00:00
|
|
|
async handleDeleteForEveryone(del, shouldPersist = true) {
|
2020-04-29 21:24:12 +00:00
|
|
|
window.log.info('Handling DOE.', {
|
|
|
|
fromId: del.get('fromId'),
|
|
|
|
targetSentTimestamp: del.get('targetSentTimestamp'),
|
|
|
|
messageServerTimestamp: this.get('serverTimestamp'),
|
|
|
|
deleteServerTimestamp: del.get('serverTimestamp'),
|
|
|
|
});
|
|
|
|
|
|
|
|
// Remove any notifications for this message
|
|
|
|
const notificationForMessage = Whisper.Notifications.findWhere({
|
|
|
|
messageId: this.get('id'),
|
|
|
|
});
|
|
|
|
Whisper.Notifications.remove(notificationForMessage);
|
|
|
|
|
|
|
|
// Erase the contents of this message
|
2020-07-27 18:15:32 +00:00
|
|
|
await this.eraseContents(
|
|
|
|
{ deletedForEveryone: true, reactions: [] },
|
|
|
|
shouldPersist
|
|
|
|
);
|
2020-04-29 21:24:12 +00:00
|
|
|
|
|
|
|
// Update the conversation's last message in case this was the last message
|
|
|
|
this.getConversation().updateLastMessage();
|
|
|
|
},
|
2018-04-10 23:51:48 +00:00
|
|
|
});
|
|
|
|
|
2019-03-13 20:38:28 +00:00
|
|
|
// Receive will be enabled before we enable send
|
|
|
|
Whisper.Message.LONG_MESSAGE_CONTENT_TYPE = 'text/x-signal-plain';
|
|
|
|
|
|
|
|
Whisper.Message.getLongMessageAttachment = ({ body, attachments, now }) => {
|
2019-05-16 22:32:11 +00:00
|
|
|
if (!body || body.length <= 2048) {
|
2019-03-13 20:38:28 +00:00
|
|
|
return {
|
|
|
|
body,
|
|
|
|
attachments,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = bytesFromString(body);
|
|
|
|
const attachment = {
|
|
|
|
contentType: Whisper.Message.LONG_MESSAGE_CONTENT_TYPE,
|
|
|
|
fileName: `long-message-${now}.txt`,
|
|
|
|
data,
|
|
|
|
size: data.byteLength,
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
body: body.slice(0, 2048),
|
|
|
|
attachments: [attachment, ...attachments],
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-06-26 19:33:13 +00:00
|
|
|
Whisper.Message.updateTimers = () => {
|
2018-08-03 04:12:27 +00:00
|
|
|
Whisper.ExpiringMessagesListener.update();
|
2019-06-26 19:33:13 +00:00
|
|
|
Whisper.TapToViewMessagesListener.update();
|
|
|
|
};
|
2018-08-03 04:12:27 +00:00
|
|
|
|
2018-04-10 23:51:48 +00:00
|
|
|
Whisper.MessageCollection = Backbone.Collection.extend({
|
|
|
|
model: Whisper.Message,
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
});
|
2018-04-27 21:25:04 +00:00
|
|
|
})();
|