Format all source code using Prettier
This commit is contained in:
parent
b4dee3f30b
commit
1dd87ad197
149 changed files with 17847 additions and 15439 deletions
|
@ -9,7 +9,7 @@
|
|||
/* eslint-disable more/no-then */
|
||||
|
||||
// eslint-disable-next-line func-names
|
||||
(function () {
|
||||
(function() {
|
||||
'use strict';
|
||||
|
||||
window.Whisper = window.Whisper || {};
|
||||
|
@ -32,10 +32,13 @@
|
|||
this.on('unload', this.unload);
|
||||
this.setToExpire();
|
||||
|
||||
this.VOICE_FLAG = textsecure.protobuf.AttachmentPointer.Flags.VOICE_MESSAGE;
|
||||
this.VOICE_FLAG =
|
||||
textsecure.protobuf.AttachmentPointer.Flags.VOICE_MESSAGE;
|
||||
},
|
||||
idForLogging() {
|
||||
return `${this.get('source')}.${this.get('sourceDevice')} ${this.get('sent_at')}`;
|
||||
return `${this.get('source')}.${this.get('sourceDevice')} ${this.get(
|
||||
'sent_at'
|
||||
)}`;
|
||||
},
|
||||
defaults() {
|
||||
return {
|
||||
|
@ -56,12 +59,13 @@
|
|||
return !!(this.get('flags') & flag);
|
||||
},
|
||||
isExpirationTimerUpdate() {
|
||||
const flag = textsecure.protobuf.DataMessage.Flags.EXPIRATION_TIMER_UPDATE;
|
||||
const flag =
|
||||
textsecure.protobuf.DataMessage.Flags.EXPIRATION_TIMER_UPDATE;
|
||||
// eslint-disable-next-line no-bitwise
|
||||
return !!(this.get('flags') & flag);
|
||||
},
|
||||
isGroupUpdate() {
|
||||
return !!(this.get('group_update'));
|
||||
return !!this.get('group_update');
|
||||
},
|
||||
isIncoming() {
|
||||
return this.get('type') === 'incoming';
|
||||
|
@ -79,14 +83,14 @@
|
|||
if (options.parse === void 0) options.parse = true;
|
||||
const model = this;
|
||||
const success = options.success;
|
||||
options.success = function (resp) {
|
||||
options.success = function(resp) {
|
||||
model.attributes = {}; // this is the only changed line
|
||||
if (!model.set(model.parse(resp, options), options)) return false;
|
||||
if (success) success(model, resp, options);
|
||||
model.trigger('sync', model, resp, options);
|
||||
};
|
||||
const error = options.error;
|
||||
options.error = function (resp) {
|
||||
options.error = function(resp) {
|
||||
if (error) error(model, resp, options);
|
||||
model.trigger('error', model, resp, options);
|
||||
};
|
||||
|
@ -116,7 +120,10 @@
|
|||
messages.push(i18n('titleIsNow', groupUpdate.name));
|
||||
}
|
||||
if (groupUpdate.joined && groupUpdate.joined.length) {
|
||||
const names = _.map(groupUpdate.joined, this.getNameForNumber.bind(this));
|
||||
const names = _.map(
|
||||
groupUpdate.joined,
|
||||
this.getNameForNumber.bind(this)
|
||||
);
|
||||
if (names.length > 1) {
|
||||
messages.push(i18n('multipleJoinedTheGroup', names.join(', ')));
|
||||
} else {
|
||||
|
@ -186,7 +193,7 @@
|
|||
}
|
||||
const quote = this.get('quote');
|
||||
const attachments = (quote && quote.attachments) || [];
|
||||
attachments.forEach((attachment) => {
|
||||
attachments.forEach(attachment => {
|
||||
if (attachment.thumbnail && attachment.thumbnail.objectUrl) {
|
||||
URL.revokeObjectURL(attachment.thumbnail.objectUrl);
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
|
@ -235,8 +242,8 @@
|
|||
const thumbnailWithObjectUrl = !objectUrl
|
||||
? null
|
||||
: Object.assign({}, attachment.thumbnail || {}, {
|
||||
objectUrl,
|
||||
});
|
||||
objectUrl,
|
||||
});
|
||||
|
||||
return Object.assign({}, attachment, {
|
||||
// eslint-disable-next-line no-bitwise
|
||||
|
@ -269,7 +276,8 @@
|
|||
|
||||
return {
|
||||
attachments: (quote.attachments || []).map(attachment =>
|
||||
this.processAttachment(attachment, objectUrl)),
|
||||
this.processAttachment(attachment, objectUrl)
|
||||
),
|
||||
authorColor,
|
||||
authorProfileName,
|
||||
authorTitle,
|
||||
|
@ -342,59 +350,63 @@
|
|||
|
||||
send(promise) {
|
||||
this.trigger('pending');
|
||||
return promise.then((result) => {
|
||||
const now = Date.now();
|
||||
this.trigger('done');
|
||||
if (result.dataMessage) {
|
||||
this.set({ dataMessage: result.dataMessage });
|
||||
}
|
||||
const sentTo = this.get('sent_to') || [];
|
||||
this.save({
|
||||
sent_to: _.union(sentTo, result.successfulNumbers),
|
||||
sent: true,
|
||||
expirationStartTimestamp: now,
|
||||
});
|
||||
this.sendSyncMessage();
|
||||
}).catch((result) => {
|
||||
const now = Date.now();
|
||||
this.trigger('done');
|
||||
if (result.dataMessage) {
|
||||
this.set({ dataMessage: result.dataMessage });
|
||||
}
|
||||
|
||||
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);
|
||||
promises.push(c.getProfiles());
|
||||
return promise
|
||||
.then(result => {
|
||||
const now = Date.now();
|
||||
this.trigger('done');
|
||||
if (result.dataMessage) {
|
||||
this.set({ dataMessage: result.dataMessage });
|
||||
}
|
||||
} else {
|
||||
this.saveErrors(result.errors);
|
||||
if (result.successfulNumbers.length > 0) {
|
||||
const sentTo = this.get('sent_to') || [];
|
||||
this.set({
|
||||
sent_to: _.union(sentTo, result.successfulNumbers),
|
||||
sent: true,
|
||||
expirationStartTimestamp: now,
|
||||
});
|
||||
promises.push(this.sendSyncMessage());
|
||||
const sentTo = this.get('sent_to') || [];
|
||||
this.save({
|
||||
sent_to: _.union(sentTo, result.successfulNumbers),
|
||||
sent: true,
|
||||
expirationStartTimestamp: now,
|
||||
});
|
||||
this.sendSyncMessage();
|
||||
})
|
||||
.catch(result => {
|
||||
const now = Date.now();
|
||||
this.trigger('done');
|
||||
if (result.dataMessage) {
|
||||
this.set({ dataMessage: result.dataMessage });
|
||||
}
|
||||
promises = promises.concat(_.map(result.errors, (error) => {
|
||||
if (error.name === 'OutgoingIdentityKeyError') {
|
||||
const c = ConversationController.get(error.number);
|
||||
|
||||
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);
|
||||
promises.push(c.getProfiles());
|
||||
}
|
||||
}));
|
||||
}
|
||||
} else {
|
||||
this.saveErrors(result.errors);
|
||||
if (result.successfulNumbers.length > 0) {
|
||||
const sentTo = this.get('sent_to') || [];
|
||||
this.set({
|
||||
sent_to: _.union(sentTo, result.successfulNumbers),
|
||||
sent: true,
|
||||
expirationStartTimestamp: now,
|
||||
});
|
||||
promises.push(this.sendSyncMessage());
|
||||
}
|
||||
promises = promises.concat(
|
||||
_.map(result.errors, error => {
|
||||
if (error.name === 'OutgoingIdentityKeyError') {
|
||||
const c = ConversationController.get(error.number);
|
||||
promises.push(c.getProfiles());
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
return Promise.all(promises).then(() => {
|
||||
this.trigger('send-error', this.get('errors'));
|
||||
return Promise.all(promises).then(() => {
|
||||
this.trigger('send-error', this.get('errors'));
|
||||
});
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
someRecipientsFailed() {
|
||||
|
@ -423,14 +435,16 @@
|
|||
if (this.get('synced') || !dataMessage) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
return textsecure.messaging.sendSyncMessage(
|
||||
dataMessage,
|
||||
this.get('sent_at'),
|
||||
this.get('destination'),
|
||||
this.get('expirationStartTimestamp')
|
||||
).then(() => {
|
||||
this.save({ synced: true, dataMessage: null });
|
||||
});
|
||||
return textsecure.messaging
|
||||
.sendSyncMessage(
|
||||
dataMessage,
|
||||
this.get('sent_at'),
|
||||
this.get('destination'),
|
||||
this.get('expirationStartTimestamp')
|
||||
)
|
||||
.then(() => {
|
||||
this.save({ synced: true, dataMessage: null });
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
|
@ -440,17 +454,19 @@
|
|||
if (!(errors instanceof Array)) {
|
||||
errors = [errors];
|
||||
}
|
||||
errors.forEach((e) => {
|
||||
errors.forEach(e => {
|
||||
console.log(
|
||||
'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) {
|
||||
errors = errors.map(e => {
|
||||
if (
|
||||
e.constructor === Error ||
|
||||
e.constructor === TypeError ||
|
||||
e.constructor === ReferenceError
|
||||
) {
|
||||
return _.pick(e, 'name', 'message', 'code', 'number', 'reason');
|
||||
}
|
||||
return e;
|
||||
|
@ -463,32 +479,36 @@
|
|||
hasNetworkError() {
|
||||
const error = _.find(
|
||||
this.get('errors'),
|
||||
e => (e.name === 'MessageError' ||
|
||||
e.name === 'OutgoingMessageError' ||
|
||||
e.name === 'SendMessageNetworkError' ||
|
||||
e.name === 'SignedPreKeyRotationError')
|
||||
e =>
|
||||
e.name === 'MessageError' ||
|
||||
e.name === 'OutgoingMessageError' ||
|
||||
e.name === 'SendMessageNetworkError' ||
|
||||
e.name === 'SignedPreKeyRotationError'
|
||||
);
|
||||
return !!error;
|
||||
},
|
||||
removeOutgoingErrors(number) {
|
||||
const errors = _.partition(
|
||||
this.get('errors'),
|
||||
e => e.number === number &&
|
||||
(e.name === 'MessageError' ||
|
||||
e.name === 'OutgoingMessageError' ||
|
||||
e.name === 'SendMessageNetworkError' ||
|
||||
e.name === 'SignedPreKeyRotationError' ||
|
||||
e.name === 'OutgoingIdentityKeyError')
|
||||
e =>
|
||||
e.number === number &&
|
||||
(e.name === 'MessageError' ||
|
||||
e.name === 'OutgoingMessageError' ||
|
||||
e.name === 'SendMessageNetworkError' ||
|
||||
e.name === 'SignedPreKeyRotationError' ||
|
||||
e.name === 'OutgoingIdentityKeyError')
|
||||
);
|
||||
this.set({ errors: errors[1] });
|
||||
return errors[0][0];
|
||||
},
|
||||
isReplayableError(e) {
|
||||
return (e.name === 'MessageError' ||
|
||||
e.name === 'OutgoingMessageError' ||
|
||||
e.name === 'SendMessageNetworkError' ||
|
||||
e.name === 'SignedPreKeyRotationError' ||
|
||||
e.name === 'OutgoingIdentityKeyError');
|
||||
return (
|
||||
e.name === 'MessageError' ||
|
||||
e.name === 'OutgoingMessageError' ||
|
||||
e.name === 'SendMessageNetworkError' ||
|
||||
e.name === 'SignedPreKeyRotationError' ||
|
||||
e.name === 'OutgoingIdentityKeyError'
|
||||
);
|
||||
},
|
||||
resend(number) {
|
||||
const error = this.removeOutgoingErrors(number);
|
||||
|
@ -513,236 +533,280 @@
|
|||
const GROUP_TYPES = textsecure.protobuf.GroupContext.Type;
|
||||
|
||||
const conversation = ConversationController.get(conversationId);
|
||||
return conversation.queueJob(() => new Promise((resolve) => {
|
||||
const now = new Date().getTime();
|
||||
let attributes = { type: 'private' };
|
||||
if (dataMessage.group) {
|
||||
let groupUpdate = null;
|
||||
attributes = {
|
||||
type: 'group',
|
||||
groupId: dataMessage.group.id,
|
||||
};
|
||||
if (dataMessage.group.type === GROUP_TYPES.UPDATE) {
|
||||
attributes = {
|
||||
type: 'group',
|
||||
groupId: dataMessage.group.id,
|
||||
name: dataMessage.group.name,
|
||||
avatar: dataMessage.group.avatar,
|
||||
members: _.union(dataMessage.group.members, conversation.get('members')),
|
||||
};
|
||||
groupUpdate = conversation.changedAttributes(_.pick(
|
||||
dataMessage.group,
|
||||
'name',
|
||||
'avatar'
|
||||
)) || {};
|
||||
const difference = _.difference(
|
||||
attributes.members,
|
||||
conversation.get('members')
|
||||
);
|
||||
if (difference.length > 0) {
|
||||
groupUpdate.joined = difference;
|
||||
return conversation.queueJob(
|
||||
() =>
|
||||
new Promise(resolve => {
|
||||
const now = new Date().getTime();
|
||||
let attributes = { type: 'private' };
|
||||
if (dataMessage.group) {
|
||||
let groupUpdate = null;
|
||||
attributes = {
|
||||
type: 'group',
|
||||
groupId: dataMessage.group.id,
|
||||
};
|
||||
if (dataMessage.group.type === GROUP_TYPES.UPDATE) {
|
||||
attributes = {
|
||||
type: 'group',
|
||||
groupId: dataMessage.group.id,
|
||||
name: dataMessage.group.name,
|
||||
avatar: dataMessage.group.avatar,
|
||||
members: _.union(
|
||||
dataMessage.group.members,
|
||||
conversation.get('members')
|
||||
),
|
||||
};
|
||||
groupUpdate =
|
||||
conversation.changedAttributes(
|
||||
_.pick(dataMessage.group, 'name', 'avatar')
|
||||
) || {};
|
||||
const difference = _.difference(
|
||||
attributes.members,
|
||||
conversation.get('members')
|
||||
);
|
||||
if (difference.length > 0) {
|
||||
groupUpdate.joined = difference;
|
||||
}
|
||||
if (conversation.get('left')) {
|
||||
console.log('re-added to a left group');
|
||||
attributes.left = false;
|
||||
}
|
||||
} else if (dataMessage.group.type === GROUP_TYPES.QUIT) {
|
||||
if (source === textsecure.storage.user.getNumber()) {
|
||||
attributes.left = true;
|
||||
groupUpdate = { left: 'You' };
|
||||
} else {
|
||||
groupUpdate = { left: source };
|
||||
}
|
||||
attributes.members = _.without(
|
||||
conversation.get('members'),
|
||||
source
|
||||
);
|
||||
}
|
||||
|
||||
if (groupUpdate !== null) {
|
||||
message.set({ group_update: groupUpdate });
|
||||
}
|
||||
}
|
||||
if (conversation.get('left')) {
|
||||
console.log('re-added to a left group');
|
||||
attributes.left = false;
|
||||
}
|
||||
} else if (dataMessage.group.type === GROUP_TYPES.QUIT) {
|
||||
if (source === textsecure.storage.user.getNumber()) {
|
||||
attributes.left = true;
|
||||
groupUpdate = { left: 'You' };
|
||||
} else {
|
||||
groupUpdate = { left: source };
|
||||
}
|
||||
attributes.members = _.without(conversation.get('members'), source);
|
||||
}
|
||||
|
||||
if (groupUpdate !== null) {
|
||||
message.set({ group_update: groupUpdate });
|
||||
}
|
||||
}
|
||||
message.set({
|
||||
attachments: dataMessage.attachments,
|
||||
body: dataMessage.body,
|
||||
conversationId: conversation.id,
|
||||
decrypted_at: now,
|
||||
errors: [],
|
||||
flags: dataMessage.flags,
|
||||
hasAttachments: dataMessage.hasAttachments,
|
||||
hasFileAttachments: dataMessage.hasFileAttachments,
|
||||
hasVisualMediaAttachments: dataMessage.hasVisualMediaAttachments,
|
||||
quote: dataMessage.quote,
|
||||
schemaVersion: dataMessage.schemaVersion,
|
||||
});
|
||||
if (type === 'outgoing') {
|
||||
const receipts = Whisper.DeliveryReceipts.forMessage(conversation, message);
|
||||
receipts.forEach(() => message.set({
|
||||
delivered: (message.get('delivered') || 0) + 1,
|
||||
}));
|
||||
}
|
||||
attributes.active_at = now;
|
||||
conversation.set(attributes);
|
||||
|
||||
if (message.isExpirationTimerUpdate()) {
|
||||
message.set({
|
||||
expirationTimerUpdate: {
|
||||
source,
|
||||
expireTimer: dataMessage.expireTimer,
|
||||
},
|
||||
});
|
||||
conversation.set({ expireTimer: dataMessage.expireTimer });
|
||||
} else if (dataMessage.expireTimer) {
|
||||
message.set({ expireTimer: dataMessage.expireTimer });
|
||||
}
|
||||
|
||||
// NOTE: Remove once the above uses
|
||||
// `Conversation::updateExpirationTimer`:
|
||||
const { expireTimer } = dataMessage;
|
||||
const shouldLogExpireTimerChange =
|
||||
message.isExpirationTimerUpdate() || expireTimer;
|
||||
if (shouldLogExpireTimerChange) {
|
||||
console.log(
|
||||
'Updating expireTimer for conversation',
|
||||
conversation.idForLogging(),
|
||||
'to',
|
||||
expireTimer,
|
||||
'via `handleDataMessage`'
|
||||
);
|
||||
}
|
||||
|
||||
if (!message.isEndSession() && !message.isGroupUpdate()) {
|
||||
if (dataMessage.expireTimer) {
|
||||
if (dataMessage.expireTimer !== conversation.get('expireTimer')) {
|
||||
conversation.updateExpirationTimer(
|
||||
dataMessage.expireTimer, source,
|
||||
message.get('received_at')
|
||||
message.set({
|
||||
attachments: dataMessage.attachments,
|
||||
body: dataMessage.body,
|
||||
conversationId: conversation.id,
|
||||
decrypted_at: now,
|
||||
errors: [],
|
||||
flags: dataMessage.flags,
|
||||
hasAttachments: dataMessage.hasAttachments,
|
||||
hasFileAttachments: dataMessage.hasFileAttachments,
|
||||
hasVisualMediaAttachments: dataMessage.hasVisualMediaAttachments,
|
||||
quote: dataMessage.quote,
|
||||
schemaVersion: dataMessage.schemaVersion,
|
||||
});
|
||||
if (type === 'outgoing') {
|
||||
const receipts = Whisper.DeliveryReceipts.forMessage(
|
||||
conversation,
|
||||
message
|
||||
);
|
||||
receipts.forEach(() =>
|
||||
message.set({
|
||||
delivered: (message.get('delivered') || 0) + 1,
|
||||
})
|
||||
);
|
||||
}
|
||||
} else if (conversation.get('expireTimer')) {
|
||||
conversation.updateExpirationTimer(
|
||||
null, source,
|
||||
message.get('received_at')
|
||||
);
|
||||
}
|
||||
}
|
||||
if (type === 'incoming') {
|
||||
const readSync = Whisper.ReadSyncs.forMessage(message);
|
||||
if (readSync) {
|
||||
if (message.get('expireTimer') && !message.get('expirationStartTimestamp')) {
|
||||
message.set('expirationStartTimestamp', readSync.get('read_at'));
|
||||
attributes.active_at = now;
|
||||
conversation.set(attributes);
|
||||
|
||||
if (message.isExpirationTimerUpdate()) {
|
||||
message.set({
|
||||
expirationTimerUpdate: {
|
||||
source,
|
||||
expireTimer: dataMessage.expireTimer,
|
||||
},
|
||||
});
|
||||
conversation.set({ expireTimer: dataMessage.expireTimer });
|
||||
} else if (dataMessage.expireTimer) {
|
||||
message.set({ expireTimer: dataMessage.expireTimer });
|
||||
}
|
||||
}
|
||||
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.
|
||||
Whisper.ReadSyncs.notifyConversation(message);
|
||||
} else {
|
||||
conversation.set('unreadCount', conversation.get('unreadCount') + 1);
|
||||
}
|
||||
}
|
||||
|
||||
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),
|
||||
});
|
||||
}
|
||||
|
||||
message.set({ recipients: conversation.getRecipients() });
|
||||
}
|
||||
|
||||
const conversationTimestamp = conversation.get('timestamp');
|
||||
if (!conversationTimestamp || message.get('sent_at') > conversationTimestamp) {
|
||||
conversation.set({
|
||||
lastMessage: message.getNotificationText(),
|
||||
timestamp: message.get('sent_at'),
|
||||
});
|
||||
}
|
||||
|
||||
if (dataMessage.profileKey) {
|
||||
const profileKey = dataMessage.profileKey.toArrayBuffer();
|
||||
if (source === textsecure.storage.user.getNumber()) {
|
||||
conversation.set({ profileSharing: true });
|
||||
} else if (conversation.isPrivate()) {
|
||||
conversation.set({ profileKey });
|
||||
} else {
|
||||
ConversationController.getOrCreateAndWait(
|
||||
source,
|
||||
'private'
|
||||
).then((sender) => {
|
||||
sender.setProfileKey(profileKey);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const handleError = (error) => {
|
||||
const errorForLog = error && error.stack ? error.stack : error;
|
||||
console.log('handleDataMessage', message.idForLogging(), 'error:', errorForLog);
|
||||
return resolve();
|
||||
};
|
||||
|
||||
message.save().then(() => {
|
||||
conversation.save().then(() => {
|
||||
try {
|
||||
conversation.trigger('newmessage', message);
|
||||
} catch (e) {
|
||||
return handleError(e);
|
||||
// NOTE: Remove once the above uses
|
||||
// `Conversation::updateExpirationTimer`:
|
||||
const { expireTimer } = dataMessage;
|
||||
const shouldLogExpireTimerChange =
|
||||
message.isExpirationTimerUpdate() || expireTimer;
|
||||
if (shouldLogExpireTimerChange) {
|
||||
console.log(
|
||||
'Updating expireTimer for conversation',
|
||||
conversation.idForLogging(),
|
||||
'to',
|
||||
expireTimer,
|
||||
'via `handleDataMessage`'
|
||||
);
|
||||
}
|
||||
// We fetch() here because, between the message.save() above and the previous
|
||||
// line's trigger() call, we might have marked all messages unread in the
|
||||
// database. This message might already be read!
|
||||
const previousUnread = message.get('unread');
|
||||
return message.fetch().then(() => {
|
||||
try {
|
||||
if (previousUnread !== message.get('unread')) {
|
||||
console.log('Caught race condition on new message read state! ' +
|
||||
'Manually starting timers.');
|
||||
// We call markRead() even though the message is already marked read
|
||||
// because we need to start expiration timers, etc.
|
||||
message.markRead();
|
||||
}
|
||||
|
||||
if (message.get('unread')) {
|
||||
return conversation.notify(message).then(() => {
|
||||
confirm();
|
||||
return resolve();
|
||||
}, handleError);
|
||||
if (!message.isEndSession() && !message.isGroupUpdate()) {
|
||||
if (dataMessage.expireTimer) {
|
||||
if (
|
||||
dataMessage.expireTimer !== conversation.get('expireTimer')
|
||||
) {
|
||||
conversation.updateExpirationTimer(
|
||||
dataMessage.expireTimer,
|
||||
source,
|
||||
message.get('received_at')
|
||||
);
|
||||
}
|
||||
|
||||
confirm();
|
||||
return resolve();
|
||||
} catch (e) {
|
||||
return handleError(e);
|
||||
}
|
||||
}, () => {
|
||||
try {
|
||||
console.log(
|
||||
'handleDataMessage: Message',
|
||||
message.idForLogging(),
|
||||
'was deleted'
|
||||
} else if (conversation.get('expireTimer')) {
|
||||
conversation.updateExpirationTimer(
|
||||
null,
|
||||
source,
|
||||
message.get('received_at')
|
||||
);
|
||||
|
||||
confirm();
|
||||
return resolve();
|
||||
} catch (e) {
|
||||
return handleError(e);
|
||||
}
|
||||
});
|
||||
}, handleError);
|
||||
}, handleError);
|
||||
}));
|
||||
}
|
||||
if (type === 'incoming') {
|
||||
const readSync = Whisper.ReadSyncs.forMessage(message);
|
||||
if (readSync) {
|
||||
if (
|
||||
message.get('expireTimer') &&
|
||||
!message.get('expirationStartTimestamp')
|
||||
) {
|
||||
message.set(
|
||||
'expirationStartTimestamp',
|
||||
readSync.get('read_at')
|
||||
);
|
||||
}
|
||||
}
|
||||
if (readSync || message.isExpirationTimerUpdate()) {
|
||||
message.unset('unread');
|
||||
// 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.
|
||||
Whisper.ReadSyncs.notifyConversation(message);
|
||||
} else {
|
||||
conversation.set(
|
||||
'unreadCount',
|
||||
conversation.get('unreadCount') + 1
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
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),
|
||||
});
|
||||
}
|
||||
|
||||
message.set({ recipients: conversation.getRecipients() });
|
||||
}
|
||||
|
||||
const conversationTimestamp = conversation.get('timestamp');
|
||||
if (
|
||||
!conversationTimestamp ||
|
||||
message.get('sent_at') > conversationTimestamp
|
||||
) {
|
||||
conversation.set({
|
||||
lastMessage: message.getNotificationText(),
|
||||
timestamp: message.get('sent_at'),
|
||||
});
|
||||
}
|
||||
|
||||
if (dataMessage.profileKey) {
|
||||
const profileKey = dataMessage.profileKey.toArrayBuffer();
|
||||
if (source === textsecure.storage.user.getNumber()) {
|
||||
conversation.set({ profileSharing: true });
|
||||
} else if (conversation.isPrivate()) {
|
||||
conversation.set({ profileKey });
|
||||
} else {
|
||||
ConversationController.getOrCreateAndWait(
|
||||
source,
|
||||
'private'
|
||||
).then(sender => {
|
||||
sender.setProfileKey(profileKey);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const handleError = error => {
|
||||
const errorForLog = error && error.stack ? error.stack : error;
|
||||
console.log(
|
||||
'handleDataMessage',
|
||||
message.idForLogging(),
|
||||
'error:',
|
||||
errorForLog
|
||||
);
|
||||
return resolve();
|
||||
};
|
||||
|
||||
message.save().then(() => {
|
||||
conversation.save().then(() => {
|
||||
try {
|
||||
conversation.trigger('newmessage', message);
|
||||
} catch (e) {
|
||||
return handleError(e);
|
||||
}
|
||||
// We fetch() here because, between the message.save() above and the previous
|
||||
// line's trigger() call, we might have marked all messages unread in the
|
||||
// database. This message might already be read!
|
||||
const previousUnread = message.get('unread');
|
||||
return message.fetch().then(
|
||||
() => {
|
||||
try {
|
||||
if (previousUnread !== message.get('unread')) {
|
||||
console.log(
|
||||
'Caught race condition on new message read state! ' +
|
||||
'Manually starting timers.'
|
||||
);
|
||||
// We call markRead() even though the message is already marked read
|
||||
// because we need to start expiration timers, etc.
|
||||
message.markRead();
|
||||
}
|
||||
|
||||
if (message.get('unread')) {
|
||||
return conversation.notify(message).then(() => {
|
||||
confirm();
|
||||
return resolve();
|
||||
}, handleError);
|
||||
}
|
||||
|
||||
confirm();
|
||||
return resolve();
|
||||
} catch (e) {
|
||||
return handleError(e);
|
||||
}
|
||||
},
|
||||
() => {
|
||||
try {
|
||||
console.log(
|
||||
'handleDataMessage: Message',
|
||||
message.idForLogging(),
|
||||
'was deleted'
|
||||
);
|
||||
|
||||
confirm();
|
||||
return resolve();
|
||||
} catch (e) {
|
||||
return handleError(e);
|
||||
}
|
||||
}
|
||||
);
|
||||
}, handleError);
|
||||
}, handleError);
|
||||
})
|
||||
);
|
||||
},
|
||||
markRead(readAt) {
|
||||
this.unset('unread');
|
||||
if (this.get('expireTimer') && !this.get('expirationStartTimestamp')) {
|
||||
this.set('expirationStartTimestamp', readAt || Date.now());
|
||||
}
|
||||
Whisper.Notifications.remove(Whisper.Notifications.where({
|
||||
messageId: this.id,
|
||||
}));
|
||||
Whisper.Notifications.remove(
|
||||
Whisper.Notifications.where({
|
||||
messageId: this.id,
|
||||
})
|
||||
);
|
||||
return new Promise((resolve, reject) => {
|
||||
this.save().then(resolve, reject);
|
||||
});
|
||||
|
@ -760,7 +824,7 @@
|
|||
const now = Date.now();
|
||||
const start = this.get('expirationStartTimestamp');
|
||||
const delta = this.get('expireTimer') * 1000;
|
||||
let msFromNow = (start + delta) - now;
|
||||
let msFromNow = start + delta - now;
|
||||
if (msFromNow < 0) {
|
||||
msFromNow = 0;
|
||||
}
|
||||
|
@ -784,7 +848,6 @@
|
|||
console.log('message', this.get('sent_at'), 'expires at', expiresAt);
|
||||
}
|
||||
},
|
||||
|
||||
});
|
||||
|
||||
Whisper.MessageCollection = Backbone.Collection.extend({
|
||||
|
@ -804,19 +867,29 @@
|
|||
}
|
||||
},
|
||||
destroyAll() {
|
||||
return Promise.all(this.models.map(m => new Promise((resolve, reject) => {
|
||||
m.destroy().then(resolve).fail(reject);
|
||||
})));
|
||||
return Promise.all(
|
||||
this.models.map(
|
||||
m =>
|
||||
new Promise((resolve, reject) => {
|
||||
m
|
||||
.destroy()
|
||||
.then(resolve)
|
||||
.fail(reject);
|
||||
})
|
||||
)
|
||||
);
|
||||
},
|
||||
|
||||
fetchSentAt(timestamp) {
|
||||
return new Promise((resolve => this.fetch({
|
||||
index: {
|
||||
// 'receipt' index on sent_at
|
||||
name: 'receipt',
|
||||
only: timestamp,
|
||||
},
|
||||
}).always(resolve)));
|
||||
return new Promise(resolve =>
|
||||
this.fetch({
|
||||
index: {
|
||||
// 'receipt' index on sent_at
|
||||
name: 'receipt',
|
||||
only: timestamp,
|
||||
},
|
||||
}).always(resolve)
|
||||
);
|
||||
},
|
||||
|
||||
getLoadedUnreadCount() {
|
||||
|
@ -841,7 +914,7 @@
|
|||
if (unreadCount > 0) {
|
||||
startingLoadedUnread = this.getLoadedUnreadCount();
|
||||
}
|
||||
return new Promise((resolve) => {
|
||||
return new Promise(resolve => {
|
||||
let upper;
|
||||
if (this.length === 0) {
|
||||
// fetch the most recent messages first
|
||||
|
@ -893,4 +966,4 @@
|
|||
});
|
||||
},
|
||||
});
|
||||
}());
|
||||
})();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue