Unleash eslint on models/messages.js

Fun fact: eslint was not running on this file, despite the eslint
directives previously in the file!
This commit is contained in:
Scott Nonnenberg 2018-04-10 16:51:48 -07:00
parent 33ef967dd7
commit e99192dca7
No known key found for this signature in database
GPG key ID: 5F82280C35134661
3 changed files with 756 additions and 726 deletions

View file

@ -22,6 +22,7 @@ ts/**/*.js
!js/database.js !js/database.js
!js/logging.js !js/logging.js
!js/models/conversations.js !js/models/conversations.js
!js/models/messages.js
!js/views/attachment_view.js !js/views/attachment_view.js
!js/views/conversation_search_view.js !js/views/conversation_search_view.js
!js/views/backbone_wrapper_view.js !js/views/backbone_wrapper_view.js

View file

@ -112,6 +112,7 @@ module.exports = function(grunt) {
'!js/views/debug_log_view.js', '!js/views/debug_log_view.js',
'!js/views/message_view.js', '!js/views/message_view.js',
'!js/models/conversations.js', '!js/models/conversations.js',
'!js/models/messages.js',
'!js/WebAudioRecorderMp3.js', '!js/WebAudioRecorderMp3.js',
'!libtextsecure/message_receiver.js', '!libtextsecure/message_receiver.js',
'_locales/**/*' '_locales/**/*'

View file

@ -1,16 +1,26 @@
/* eslint-disable */ /* global _: false */
/* global Backbone: false */
/* global Whisper: false */
/* global textsecure: false */
/* global ConversationController: false */
/* global i18n: false */
/* global getAccountManager: false */
/* eslint-disable more/no-then */
// eslint-disable-next-line func-names
(function () { (function () {
'use strict'; 'use strict';
window.Whisper = window.Whisper || {}; window.Whisper = window.Whisper || {};
const { Attachment, Message: TypedMessage } = window.Signal.Types; const { Message: TypedMessage } = window.Signal.Types;
const { deleteAttachmentData } = window.Signal.Migrations; const { deleteAttachmentData } = window.Signal.Migrations;
var Message = window.Whisper.Message = Backbone.Model.extend({ window.Whisper.Message = Backbone.Model.extend({
database: Whisper.Database, database: Whisper.Database,
storeName: 'messages', storeName: 'messages',
initialize: function(attributes) { initialize(attributes) {
if (_.isObject(attributes)) { if (_.isObject(attributes)) {
this.set(TypedMessage.initializeSchemaVersion(attributes)); this.set(TypedMessage.initializeSchemaVersion(attributes));
} }
@ -22,84 +32,89 @@
this.on('unload', this.revokeImageUrl); this.on('unload', this.revokeImageUrl);
this.setToExpire(); this.setToExpire();
}, },
idForLogging: function() { idForLogging() {
return this.get('source') + '.' + this.get('sourceDevice') + ' ' + this.get('sent_at'); return `${this.get('source')}.${this.get('sourceDevice')} ${this.get('sent_at')}`;
}, },
defaults: function() { defaults() {
return { return {
timestamp: new Date().getTime(), timestamp: new Date().getTime(),
attachments: [] attachments: [],
}; };
}, },
validate: function(attributes, options) { validate(attributes) {
var required = ['conversationId', 'received_at', 'sent_at']; const required = ['conversationId', 'received_at', 'sent_at'];
var missing = _.filter(required, function(attr) { return !attributes[attr]; }); const missing = _.filter(required, attr => !attributes[attr]);
if (missing.length) { if (missing.length) {
console.log("Message missing attributes: " + missing); console.log(`Message missing attributes: ${missing}`);
} }
}, },
isEndSession: function() { isEndSession() {
var flag = textsecure.protobuf.DataMessage.Flags.END_SESSION; const flag = textsecure.protobuf.DataMessage.Flags.END_SESSION;
// eslint-disable-next-line no-bitwise
return !!(this.get('flags') & flag); return !!(this.get('flags') & flag);
}, },
isExpirationTimerUpdate: function() { isExpirationTimerUpdate() {
var 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); return !!(this.get('flags') & flag);
}, },
isGroupUpdate: function() { isGroupUpdate() {
return !!(this.get('group_update')); return !!(this.get('group_update'));
}, },
isIncoming: function() { isIncoming() {
return this.get('type') === 'incoming'; return this.get('type') === 'incoming';
}, },
isUnread: function() { isUnread() {
return !!this.get('unread'); return !!this.get('unread');
}, },
// overriding this to allow for this.unset('unread'), save to db, then fetch() // overriding this to allow for this.unset('unread'), save to db, then fetch()
// to propagate. We don't want the unset key in the db so our unread index stays // to propagate. We don't want the unset key in the db so our unread index stays
// small. // small.
// jscs:disable /* eslint-disable */
fetch: function(options) { /* jscs:disable */
fetch(options) {
options = options ? _.clone(options) : {}; options = options ? _.clone(options) : {};
if (options.parse === void 0) options.parse = true; if (options.parse === void 0) options.parse = true;
var model = this; const model = this;
var success = options.success; const success = options.success;
options.success = function (resp) { options.success = function (resp) {
model.attributes = {}; // this is the only changed line model.attributes = {}; // this is the only changed line
if (!model.set(model.parse(resp, options), options)) return false; if (!model.set(model.parse(resp, options), options)) return false;
if (success) success(model, resp, options); if (success) success(model, resp, options);
model.trigger('sync', model, resp, options); model.trigger('sync', model, resp, options);
}; };
var error = options.error; const error = options.error;
options.error = function (resp) { options.error = function (resp) {
if (error) error(model, resp, options); if (error) error(model, resp, options);
model.trigger('error', model, resp, options); model.trigger('error', model, resp, options);
}; };
return this.sync('read', this, options); return this.sync('read', this, options);
}, },
// jscs:enable /* jscs:enable */
getNameForNumber: function(number) { /* eslint-enable */
var conversation = ConversationController.get(number); /* eslint-disable more/no-then */
getNameForNumber(number) {
const conversation = ConversationController.get(number);
if (!conversation) { if (!conversation) {
return number; return number;
} }
return conversation.getDisplayName(); return conversation.getDisplayName();
}, },
getDescription: function() { getDescription() {
if (this.isGroupUpdate()) { if (this.isGroupUpdate()) {
var group_update = this.get('group_update'); const groupUpdate = this.get('group_update');
if (group_update.left === 'You') { if (groupUpdate.left === 'You') {
return i18n('youLeftTheGroup'); return i18n('youLeftTheGroup');
} else if (group_update.left) { } else if (groupUpdate.left) {
return i18n('leftTheGroup', this.getNameForNumber(group_update.left)); return i18n('leftTheGroup', this.getNameForNumber(groupUpdate.left));
} }
var messages = [i18n('updatedTheGroup')]; const messages = [i18n('updatedTheGroup')];
if (group_update.name) { if (groupUpdate.name) {
messages.push(i18n('titleIsNow', group_update.name)); messages.push(i18n('titleIsNow', groupUpdate.name));
} }
if (group_update.joined && group_update.joined.length) { if (groupUpdate.joined && groupUpdate.joined.length) {
var names = _.map(group_update.joined, this.getNameForNumber.bind(this)); const names = _.map(groupUpdate.joined, this.getNameForNumber.bind(this));
if (names.length > 1) { if (names.length > 1) {
messages.push(i18n('multipleJoinedTheGroup', names.join(', '))); messages.push(i18n('multipleJoinedTheGroup', names.join(', ')));
} else { } else {
@ -117,11 +132,11 @@
} }
return this.get('body'); return this.get('body');
}, },
isKeyChange: function() { isKeyChange() {
return this.get('type') === 'keychange'; return this.get('type') === 'keychange';
}, },
getNotificationText: function() { getNotificationText() {
var description = this.getDescription(); const description = this.getDescription();
if (description) { if (description) {
return description; return description;
} }
@ -129,176 +144,174 @@
return i18n('mediaMessage'); return i18n('mediaMessage');
} }
if (this.isExpirationTimerUpdate()) { if (this.isExpirationTimerUpdate()) {
return i18n('timerSetTo', const { expireTimer } = this.get('expirationTimerUpdate');
Whisper.ExpirationTimerOptions.getAbbreviated( return i18n(
this.get('expirationTimerUpdate').expireTimer 'timerSetTo',
) Whisper.ExpirationTimerOptions.getAbbreviated(expireTimer)
); );
} }
if (this.isKeyChange()) { if (this.isKeyChange()) {
var conversation = this.getModelForKeyChange(); const conversation = this.getModelForKeyChange();
return i18n('keychanged', conversation.getTitle()); return i18n('keychanged', conversation.getTitle());
} }
return ''; return '';
}, },
/* eslint-enable */
/* jshint ignore:start */
async onDestroy() { async onDestroy() {
this.revokeImageUrl(); this.revokeImageUrl();
const attachments = this.get('attachments'); const attachments = this.get('attachments');
await Promise.all(attachments.map(deleteAttachmentData)); await Promise.all(attachments.map(deleteAttachmentData));
return;
}, },
/* jshint ignore:end */ updateImageUrl() {
/* eslint-disable */
updateImageUrl: function() {
this.revokeImageUrl(); this.revokeImageUrl();
var attachment = this.get('attachments')[0]; const attachment = this.get('attachments')[0];
if (attachment) { if (attachment) {
var blob = new Blob([attachment.data], { const blob = new Blob([attachment.data], {
type: attachment.contentType type: attachment.contentType,
}); });
this.imageUrl = URL.createObjectURL(blob); this.imageUrl = URL.createObjectURL(blob);
} else { } else {
this.imageUrl = null; this.imageUrl = null;
} }
}, },
revokeImageUrl: function() { revokeImageUrl() {
if (this.imageUrl) { if (this.imageUrl) {
URL.revokeObjectURL(this.imageUrl); URL.revokeObjectURL(this.imageUrl);
this.imageUrl = null; this.imageUrl = null;
} }
}, },
getImageUrl: function() { getImageUrl() {
if (this.imageUrl === undefined) { if (this.imageUrl === undefined) {
this.updateImageUrl(); this.updateImageUrl();
} }
return this.imageUrl; return this.imageUrl;
}, },
getConversation: function() { getConversation() {
// This needs to be an unsafe call, because this method is called during // This needs to be an unsafe call, because this method is called during
// initial module setup. We may be in the middle of the initial fetch to // initial module setup. We may be in the middle of the initial fetch to
// the database. // the database.
return ConversationController.getUnsafe(this.get('conversationId')); return ConversationController.getUnsafe(this.get('conversationId'));
}, },
getExpirationTimerUpdateSource: function() { getExpirationTimerUpdateSource() {
if (this.isExpirationTimerUpdate()) { if (this.isExpirationTimerUpdate()) {
var conversationId = this.get('expirationTimerUpdate').source; const conversationId = this.get('expirationTimerUpdate').source;
return ConversationController.getOrCreate(conversationId, 'private'); return ConversationController.getOrCreate(conversationId, 'private');
} }
return Promise.resolve();
}, },
getContact: function() { getContact() {
var conversationId = this.get('source'); let conversationId = this.get('source');
if (!this.isIncoming()) { if (!this.isIncoming()) {
conversationId = textsecure.storage.user.getNumber(); conversationId = textsecure.storage.user.getNumber();
} }
return ConversationController.getOrCreate(conversationId, 'private'); return ConversationController.getOrCreate(conversationId, 'private');
}, },
getModelForKeyChange: function() { getModelForKeyChange() {
var id = this.get('key_changed'); const id = this.get('key_changed');
if (!this.modelForKeyChange) { if (!this.modelForKeyChange) {
var c = ConversationController.getOrCreate(id, 'private'); const c = ConversationController.getOrCreate(id, 'private');
this.modelForKeyChange = c; this.modelForKeyChange = c;
} }
return this.modelForKeyChange; return this.modelForKeyChange;
}, },
getModelForVerifiedChange: function() { getModelForVerifiedChange() {
var id = this.get('verifiedChanged'); const id = this.get('verifiedChanged');
if (!this.modelForVerifiedChange) { if (!this.modelForVerifiedChange) {
var c = ConversationController.getOrCreate(id, 'private'); const c = ConversationController.getOrCreate(id, 'private');
this.modelForVerifiedChange = c; this.modelForVerifiedChange = c;
} }
return this.modelForVerifiedChange; return this.modelForVerifiedChange;
}, },
isOutgoing: function() { isOutgoing() {
return this.get('type') === 'outgoing'; return this.get('type') === 'outgoing';
}, },
hasErrors: function() { hasErrors() {
return _.size(this.get('errors')) > 0; return _.size(this.get('errors')) > 0;
}, },
getStatus: function(number) { getStatus(number) {
var read_by = this.get('read_by') || []; const readBy = this.get('read_by') || [];
if (read_by.indexOf(number) >= 0) { if (readBy.indexOf(number) >= 0) {
return 'read'; return 'read';
} }
var delivered_to = this.get('delivered_to') || []; const deliveredTo = this.get('delivered_to') || [];
if (delivered_to.indexOf(number) >= 0) { if (deliveredTo.indexOf(number) >= 0) {
return 'delivered'; return 'delivered';
} }
var sent_to = this.get('sent_to') || []; const sentTo = this.get('sent_to') || [];
if (sent_to.indexOf(number) >= 0) { if (sentTo.indexOf(number) >= 0) {
return 'sent'; return 'sent';
} }
return null;
}, },
send: function(promise) { send(promise) {
this.trigger('pending'); this.trigger('pending');
return promise.then(function(result) { return promise.then((result) => {
var now = Date.now(); const now = Date.now();
this.trigger('done'); this.trigger('done');
if (result.dataMessage) { if (result.dataMessage) {
this.set({ dataMessage: result.dataMessage }); this.set({ dataMessage: result.dataMessage });
} }
var sent_to = this.get('sent_to') || []; const sentTo = this.get('sent_to') || [];
this.save({ this.save({
sent_to: _.union(sent_to, result.successfulNumbers), sent_to: _.union(sentTo, result.successfulNumbers),
sent: true, sent: true,
expirationStartTimestamp: now expirationStartTimestamp: now,
}); });
this.sendSyncMessage(); this.sendSyncMessage();
}.bind(this)).catch(function(result) { }).catch((result) => {
var now = Date.now(); const now = Date.now();
this.trigger('done'); this.trigger('done');
if (result.dataMessage) { if (result.dataMessage) {
this.set({ dataMessage: result.dataMessage }); this.set({ dataMessage: result.dataMessage });
} }
var promises = []; let promises = [];
if (result instanceof Error) { if (result instanceof Error) {
this.saveErrors(result); this.saveErrors(result);
if (result.name === 'SignedPreKeyRotationError') { if (result.name === 'SignedPreKeyRotationError') {
promises.push(getAccountManager().rotateSignedPreKey()); promises.push(getAccountManager().rotateSignedPreKey());
} } else if (result.name === 'OutgoingIdentityKeyError') {
else if (result.name === 'OutgoingIdentityKeyError') { const c = ConversationController.get(result.number);
var c = ConversationController.get(result.number);
promises.push(c.getProfiles()); promises.push(c.getProfiles());
} }
} else { } else {
this.saveErrors(result.errors); this.saveErrors(result.errors);
if (result.successfulNumbers.length > 0) { if (result.successfulNumbers.length > 0) {
var sent_to = this.get('sent_to') || []; const sentTo = this.get('sent_to') || [];
this.set({ this.set({
sent_to: _.union(sent_to, result.successfulNumbers), sent_to: _.union(sentTo, result.successfulNumbers),
sent: true, sent: true,
expirationStartTimestamp: now expirationStartTimestamp: now,
}); });
promises.push(this.sendSyncMessage()); promises.push(this.sendSyncMessage());
} }
promises = promises.concat(_.map(result.errors, function(error) { promises = promises.concat(_.map(result.errors, (error) => {
if (error.name === 'OutgoingIdentityKeyError') { if (error.name === 'OutgoingIdentityKeyError') {
var c = ConversationController.get(error.number); const c = ConversationController.get(error.number);
promises.push(c.getProfiles()); promises.push(c.getProfiles());
} }
})); }));
} }
return Promise.all(promises).then(function() { return Promise.all(promises).then(() => {
this.trigger('send-error', this.get('errors')); this.trigger('send-error', this.get('errors'));
}.bind(this)); });
}.bind(this)); });
}, },
someRecipientsFailed: function() { someRecipientsFailed() {
var c = this.getConversation(); const c = this.getConversation();
if (!c || c.isPrivate()) { if (!c || c.isPrivate()) {
return false; return false;
} }
var recipients = c.contactCollection.length - 1; const recipients = c.contactCollection.length - 1;
var errors = this.get('errors'); const errors = this.get('errors');
if (!errors) { if (!errors) {
return false; return false;
} }
@ -310,33 +323,38 @@
return false; return false;
}, },
sendSyncMessage: function() { sendSyncMessage() {
this.syncPromise = this.syncPromise || Promise.resolve(); this.syncPromise = this.syncPromise || Promise.resolve();
this.syncPromise = this.syncPromise.then(function() { this.syncPromise = this.syncPromise.then(() => {
var dataMessage = this.get('dataMessage'); const dataMessage = this.get('dataMessage');
if (this.get('synced') || !dataMessage) { if (this.get('synced') || !dataMessage) {
return; return Promise.resolve();
} }
return textsecure.messaging.sendSyncMessage( return textsecure.messaging.sendSyncMessage(
dataMessage, this.get('sent_at'), this.get('destination'), this.get('expirationStartTimestamp') dataMessage,
).then(function() { this.get('sent_at'),
this.get('destination'),
this.get('expirationStartTimestamp')
).then(() => {
this.save({ synced: true, dataMessage: null }); this.save({ synced: true, dataMessage: null });
}.bind(this)); });
}.bind(this)); });
}, },
saveErrors: function(errors) { saveErrors(providedErrors) {
let errors = providedErrors;
if (!(errors instanceof Array)) { if (!(errors instanceof Array)) {
errors = [errors]; errors = [errors];
} }
errors.forEach(function(e) { errors.forEach((e) => {
console.log( console.log(
'Message.saveErrors:', 'Message.saveErrors:',
e && e.reason ? e.reason : null, e && e.reason ? e.reason : null,
e && e.stack ? e.stack : e e && e.stack ? e.stack : e
); );
}); });
errors = errors.map(function(e) { errors = errors.map((e) => {
if (e.constructor === Error || if (e.constructor === Error ||
e.constructor === TypeError || e.constructor === TypeError ||
e.constructor === ReferenceError) { e.constructor === ReferenceError) {
@ -346,71 +364,72 @@
}); });
errors = errors.concat(this.get('errors') || []); errors = errors.concat(this.get('errors') || []);
return this.save({errors : errors}); return this.save({ errors });
}, },
hasNetworkError: function(number) { hasNetworkError() {
var error = _.find(this.get('errors'), function(e) { const error = _.find(
return (e.name === 'MessageError' || this.get('errors'),
e => (e.name === 'MessageError' ||
e.name === 'OutgoingMessageError' || e.name === 'OutgoingMessageError' ||
e.name === 'SendMessageNetworkError' || e.name === 'SendMessageNetworkError' ||
e.name === 'SignedPreKeyRotationError'); e.name === 'SignedPreKeyRotationError')
}); );
return !!error; return !!error;
}, },
removeOutgoingErrors: function(number) { removeOutgoingErrors(number) {
var errors = _.partition(this.get('errors'), function(e) { const errors = _.partition(
return e.number === number && this.get('errors'),
e => e.number === number &&
(e.name === 'MessageError' || (e.name === 'MessageError' ||
e.name === 'OutgoingMessageError' || e.name === 'OutgoingMessageError' ||
e.name === 'SendMessageNetworkError' || e.name === 'SendMessageNetworkError' ||
e.name === 'SignedPreKeyRotationError' || e.name === 'SignedPreKeyRotationError' ||
e.name === 'OutgoingIdentityKeyError'); e.name === 'OutgoingIdentityKeyError')
}); );
this.set({ errors: errors[1] }); this.set({ errors: errors[1] });
return errors[0][0]; return errors[0][0];
}, },
isReplayableError: function(e) { isReplayableError(e) {
return (e.name === 'MessageError' || return (e.name === 'MessageError' ||
e.name === 'OutgoingMessageError' || e.name === 'OutgoingMessageError' ||
e.name === 'SendMessageNetworkError' || e.name === 'SendMessageNetworkError' ||
e.name === 'SignedPreKeyRotationError' || e.name === 'SignedPreKeyRotationError' ||
e.name === 'OutgoingIdentityKeyError'); e.name === 'OutgoingIdentityKeyError');
}, },
resend: function(number) { resend(number) {
var error = this.removeOutgoingErrors(number); const error = this.removeOutgoingErrors(number);
if (error) { if (error) {
var promise = new textsecure.ReplayableError(error).replay(); const promise = new textsecure.ReplayableError(error).replay();
this.send(promise); this.send(promise);
} }
}, },
handleDataMessage: function(dataMessage, confirm) { handleDataMessage(dataMessage, confirm) {
// This function is called from the background script in a few scenarios: // This function is called from the background script in a few scenarios:
// 1. on an incoming message // 1. on an incoming message
// 2. on a sent message sync'd from another device // 2. on a sent message sync'd from another device
// 3. in rare cases, an incoming message can be retried, though it will // 3. in rare cases, an incoming message can be retried, though it will
// still go through one of the previous two codepaths // still go through one of the previous two codepaths
var message = this; const message = this;
var source = message.get('source'); const source = message.get('source');
var type = message.get('type'); const type = message.get('type');
var timestamp = message.get('sent_at'); let conversationId = message.get('conversationId');
var conversationId = message.get('conversationId');
if (dataMessage.group) { if (dataMessage.group) {
conversationId = dataMessage.group.id; conversationId = dataMessage.group.id;
} }
const GROUP_TYPES = textsecure.protobuf.GroupContext.Type;
var conversation = ConversationController.get(conversationId); const conversation = ConversationController.get(conversationId);
return conversation.queueJob(function() { return conversation.queueJob(() => new Promise(((resolve) => {
return new Promise(function(resolve) { const now = new Date().getTime();
var now = new Date().getTime(); let attributes = { type: 'private' };
var attributes = { type: 'private' };
if (dataMessage.group) { if (dataMessage.group) {
var group_update = null; let groupUpdate = null;
attributes = { attributes = {
type: 'group', type: 'group',
groupId: dataMessage.group.id, groupId: dataMessage.group.id,
}; };
if (dataMessage.group.type === textsecure.protobuf.GroupContext.Type.UPDATE) { if (dataMessage.group.type === GROUP_TYPES.UPDATE) {
attributes = { attributes = {
type: 'group', type: 'group',
groupId: dataMessage.group.id, groupId: dataMessage.group.id,
@ -418,28 +437,34 @@
avatar: dataMessage.group.avatar, avatar: dataMessage.group.avatar,
members: _.union(dataMessage.group.members, conversation.get('members')), members: _.union(dataMessage.group.members, conversation.get('members')),
}; };
group_update = conversation.changedAttributes(_.pick(dataMessage.group, 'name', 'avatar')) || {}; groupUpdate = conversation.changedAttributes(_.pick(
var difference = _.difference(attributes.members, conversation.get('members')); dataMessage.group,
'name',
'avatar'
)) || {};
const difference = _.difference(
attributes.members,
conversation.get('members')
);
if (difference.length > 0) { if (difference.length > 0) {
group_update.joined = difference; groupUpdate.joined = difference;
} }
if (conversation.get('left')) { if (conversation.get('left')) {
console.log('re-added to a left group'); console.log('re-added to a left group');
attributes.left = false; attributes.left = false;
} }
} } else if (dataMessage.group.type === GROUP_TYPES.QUIT) {
else if (dataMessage.group.type === textsecure.protobuf.GroupContext.Type.QUIT) { if (source === textsecure.storage.user.getNumber()) {
if (source == textsecure.storage.user.getNumber()) {
attributes.left = true; attributes.left = true;
group_update = { left: "You" }; groupUpdate = { left: 'You' };
} else { } else {
group_update = { left: source }; groupUpdate = { left: source };
} }
attributes.members = _.without(conversation.get('members'), source); attributes.members = _.without(conversation.get('members'), source);
} }
if (group_update !== null) { if (groupUpdate !== null) {
message.set({group_update: group_update}); message.set({ group_update: groupUpdate });
} }
} }
message.set({ message.set({
@ -450,15 +475,13 @@
quote: dataMessage.quote, quote: dataMessage.quote,
decrypted_at: now, decrypted_at: now,
flags: dataMessage.flags, flags: dataMessage.flags,
errors : [] errors: [],
}); });
if (type === 'outgoing') { if (type === 'outgoing') {
var receipts = Whisper.DeliveryReceipts.forMessage(conversation, message); const receipts = Whisper.DeliveryReceipts.forMessage(conversation, message);
receipts.forEach(function(receipt) { receipts.forEach(() => message.set({
message.set({ delivered: (message.get('delivered') || 0) + 1,
delivered: (message.get('delivered') || 0) + 1 }));
});
});
} }
attributes.active_at = now; attributes.active_at = now;
conversation.set(attributes); conversation.set(attributes);
@ -466,9 +489,9 @@
if (message.isExpirationTimerUpdate()) { if (message.isExpirationTimerUpdate()) {
message.set({ message.set({
expirationTimerUpdate: { expirationTimerUpdate: {
source : source, source,
expireTimer : dataMessage.expireTimer expireTimer: dataMessage.expireTimer,
} },
}); });
conversation.set({ expireTimer: dataMessage.expireTimer }); conversation.set({ expireTimer: dataMessage.expireTimer });
} else if (dataMessage.expireTimer) { } else if (dataMessage.expireTimer) {
@ -495,15 +518,18 @@
if (dataMessage.expireTimer !== conversation.get('expireTimer')) { if (dataMessage.expireTimer !== conversation.get('expireTimer')) {
conversation.updateExpirationTimer( conversation.updateExpirationTimer(
dataMessage.expireTimer, source, dataMessage.expireTimer, source,
message.get('received_at')); message.get('received_at')
);
} }
} else if (conversation.get('expireTimer')) { } else if (conversation.get('expireTimer')) {
conversation.updateExpirationTimer(null, source, conversation.updateExpirationTimer(
message.get('received_at')); null, source,
message.get('received_at')
);
} }
} }
if (type === 'incoming') { if (type === 'incoming') {
var readSync = Whisper.ReadSyncs.forMessage(message); const readSync = Whisper.ReadSyncs.forMessage(message);
if (readSync) { if (readSync) {
if (message.get('expireTimer') && !message.get('expirationStartTimestamp')) { if (message.get('expireTimer') && !message.get('expirationStartTimestamp')) {
message.set('expirationStartTimestamp', readSync.get('read_at')); message.set('expirationStartTimestamp', readSync.get('read_at'));
@ -521,59 +547,59 @@
} }
if (type === 'outgoing') { if (type === 'outgoing') {
var reads = Whisper.ReadReceipts.forMessage(conversation, message); const reads = Whisper.ReadReceipts.forMessage(conversation, message);
if (reads.length) { if (reads.length) {
var read_by = reads.map(function(receipt) { const readBy = reads.map(receipt => receipt.get('reader'));
return receipt.get('reader');
});
message.set({ message.set({
read_by: _.union(message.get('read_by'), read_by) read_by: _.union(message.get('read_by'), readBy),
}); });
} }
message.set({ recipients: conversation.getRecipients() }); message.set({ recipients: conversation.getRecipients() });
} }
var conversation_timestamp = conversation.get('timestamp'); const conversationTimestamp = conversation.get('timestamp');
if (!conversation_timestamp || message.get('sent_at') > conversation_timestamp) { if (!conversationTimestamp || message.get('sent_at') > conversationTimestamp) {
conversation.set({ conversation.set({
lastMessage: message.getNotificationText(), lastMessage: message.getNotificationText(),
timestamp: message.get('sent_at') timestamp: message.get('sent_at'),
}); });
} }
if (dataMessage.profileKey) { if (dataMessage.profileKey) {
var profileKey = dataMessage.profileKey.toArrayBuffer(); const profileKey = dataMessage.profileKey.toArrayBuffer();
if (source == textsecure.storage.user.getNumber()) { if (source === textsecure.storage.user.getNumber()) {
conversation.set({ profileSharing: true }); conversation.set({ profileSharing: true });
} else if (conversation.isPrivate()) { } else if (conversation.isPrivate()) {
conversation.set({profileKey: profileKey}); conversation.set({ profileKey });
} else { } else {
ConversationController.getOrCreateAndWait(source, 'private').then(function(sender) { ConversationController.getOrCreateAndWait(
source,
'private'
).then((sender) => {
sender.setProfileKey(profileKey); sender.setProfileKey(profileKey);
}); });
} }
} }
var handleError = function(error) { const handleError = (error) => {
error = error && error.stack ? error.stack : error; const errorForLog = error && error.stack ? error.stack : error;
console.log('handleDataMessage', message.idForLogging(), 'error:', error); console.log('handleDataMessage', message.idForLogging(), 'error:', errorForLog);
return resolve(); return resolve();
}; };
message.save().then(function() { message.save().then(() => {
conversation.save().then(function() { conversation.save().then(() => {
try { try {
conversation.trigger('newmessage', message); conversation.trigger('newmessage', message);
} } catch (e) {
catch (e) {
return handleError(e); return handleError(e);
} }
// We fetch() here because, between the message.save() above and the previous // 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 // line's trigger() call, we might have marked all messages unread in the
// database. This message might already be read! // database. This message might already be read!
var previousUnread = message.get('unread'); const previousUnread = message.get('unread');
message.fetch().then(function() { return message.fetch().then(() => {
try { try {
if (previousUnread !== message.get('unread')) { if (previousUnread !== message.get('unread')) {
console.log('Caught race condition on new message read state! ' + console.log('Caught race condition on new message read state! ' +
@ -584,130 +610,130 @@
} }
if (message.get('unread')) { if (message.get('unread')) {
conversation.notify(message).then(function() { return conversation.notify(message).then(() => {
confirm(); confirm();
return resolve(); return resolve();
}, handleError); }, handleError);
} else {
confirm();
return resolve();
} }
}
catch (e) {
handleError(e);
}
}, function(error) {
try {
console.log('handleDataMessage: Message', message.idForLogging(), 'was deleted');
confirm(); confirm();
return resolve(); return resolve();
} catch (e) {
return handleError(e);
} }
catch (e) { }, () => {
handleError(e); try {
console.log(
'handleDataMessage: Message',
message.idForLogging(),
'was deleted'
);
confirm();
return resolve();
} catch (e) {
return handleError(e);
} }
}); });
}, handleError); }, handleError);
}, handleError); }, handleError);
}); })));
});
}, },
markRead: function(read_at) { markRead(readAt) {
this.unset('unread'); this.unset('unread');
if (this.get('expireTimer') && !this.get('expirationStartTimestamp')) { if (this.get('expireTimer') && !this.get('expirationStartTimestamp')) {
this.set('expirationStartTimestamp', read_at || Date.now()); this.set('expirationStartTimestamp', readAt || Date.now());
} }
Whisper.Notifications.remove(Whisper.Notifications.where({ Whisper.Notifications.remove(Whisper.Notifications.where({
messageId: this.id messageId: this.id,
})); }));
return new Promise(function(resolve, reject) { return new Promise(((resolve, reject) => {
this.save().then(resolve, reject); this.save().then(resolve, reject);
}.bind(this)); }));
}, },
isExpiring: function() { isExpiring() {
return this.get('expireTimer') && this.get('expirationStartTimestamp'); return this.get('expireTimer') && this.get('expirationStartTimestamp');
}, },
isExpired: function() { isExpired() {
return this.msTilExpire() <= 0; return this.msTilExpire() <= 0;
}, },
msTilExpire: function() { msTilExpire() {
if (!this.isExpiring()) { if (!this.isExpiring()) {
return Infinity; return Infinity;
} }
var now = Date.now(); const now = Date.now();
var start = this.get('expirationStartTimestamp'); const start = this.get('expirationStartTimestamp');
var delta = this.get('expireTimer') * 1000; const delta = this.get('expireTimer') * 1000;
var ms_from_now = start + delta - now; let msFromNow = (start + delta) - now;
if (ms_from_now < 0) { if (msFromNow < 0) {
ms_from_now = 0; msFromNow = 0;
} }
return ms_from_now; return msFromNow;
}, },
setToExpire: function() { setToExpire() {
if (this.isExpiring() && !this.get('expires_at')) { if (this.isExpiring() && !this.get('expires_at')) {
var start = this.get('expirationStartTimestamp'); const start = this.get('expirationStartTimestamp');
var delta = this.get('expireTimer') * 1000; const delta = this.get('expireTimer') * 1000;
var expires_at = start + delta; const expiresAt = start + delta;
// This method can be called due to the expiration-related .set() calls in // This method can be called due to the expiration-related .set() calls in
// handleDataMessage(), but the .save() here would conflict with the // handleDataMessage(), but the .save() here would conflict with the
// same call at the end of handleDataMessage(). So we only call .save() // same call at the end of handleDataMessage(). So we only call .save()
// here if we've previously saved this model. // here if we've previously saved this model.
if (!this.isNew()) { if (!this.isNew()) {
this.save('expires_at', expires_at); this.save('expires_at', expiresAt);
} }
Whisper.ExpiringMessagesListener.update(); Whisper.ExpiringMessagesListener.update();
console.log('message', this.get('sent_at'), 'expires at', expires_at); console.log('message', this.get('sent_at'), 'expires at', expiresAt);
}
} }
},
}); });
Whisper.MessageCollection = Backbone.Collection.extend({ Whisper.MessageCollection = Backbone.Collection.extend({
model : Message, model: Whisper.Message,
database: Whisper.Database, database: Whisper.Database,
storeName: 'messages', storeName: 'messages',
comparator : function(left, right) { comparator(left, right) {
if (left.get('received_at') === right.get('received_at')) { if (left.get('received_at') === right.get('received_at')) {
return (left.get('sent_at') || 0) - (right.get('sent_at') || 0); return (left.get('sent_at') || 0) - (right.get('sent_at') || 0);
} }
return (left.get('received_at') || 0) - (right.get('received_at') || 0); return (left.get('received_at') || 0) - (right.get('received_at') || 0);
}, },
initialize : function(models, options) { initialize(models, options) {
if (options) { if (options) {
this.conversation = options.conversation; this.conversation = options.conversation;
} }
}, },
destroyAll : function () { destroyAll() {
return Promise.all(this.models.map(function(m) { return Promise.all(this.models.map(m => new Promise(((resolve, reject) => {
return new Promise(function(resolve, reject) {
m.destroy().then(resolve).fail(reject); m.destroy().then(resolve).fail(reject);
}); }))));
}));
}, },
fetchSentAt: function(timestamp) { fetchSentAt(timestamp) {
return new Promise(function(resolve) { return new Promise((resolve => this.fetch({
return this.fetch({
index: { index: {
// 'receipt' index on sent_at // 'receipt' index on sent_at
name: 'receipt', name: 'receipt',
only: timestamp only: timestamp,
} },
}).always(resolve); }).always(resolve)));
}.bind(this));
}, },
getLoadedUnreadCount: function() { getLoadedUnreadCount() {
return this.reduce(function(total, model) { return this.reduce((total, model) => {
var unread = model.get('unread') && model.isIncoming(); const unread = model.get('unread') && model.isIncoming();
return total + (unread ? 1 : 0); return total + (unread ? 1 : 0);
}, 0); }, 0);
}, },
fetchConversation: function(conversationId, limit, unreadCount) { fetchConversation(conversationId, providedLimit, providedUnreadCount) {
let limit = providedLimit;
let unreadCount = providedUnreadCount;
if (typeof limit !== 'number') { if (typeof limit !== 'number') {
limit = 100; limit = 100;
} }
@ -715,12 +741,12 @@
unreadCount = 0; unreadCount = 0;
} }
var startingLoadedUnread = 0; let startingLoadedUnread = 0;
if (unreadCount > 0) { if (unreadCount > 0) {
startingLoadedUnread = this.getLoadedUnreadCount(); startingLoadedUnread = this.getLoadedUnreadCount();
} }
return new Promise(function(resolve) { return new Promise(((resolve) => {
var upper; let upper;
if (this.length === 0) { if (this.length === 0) {
// fetch the most recent messages first // fetch the most recent messages first
upper = Number.MAX_VALUE; upper = Number.MAX_VALUE;
@ -728,45 +754,47 @@
// not our first rodeo, fetch older messages. // not our first rodeo, fetch older messages.
upper = this.at(0).get('received_at'); upper = this.at(0).get('received_at');
} }
var options = {remove: false, limit: limit}; const options = { remove: false, limit };
options.index = { options.index = {
// 'conversation' index on [conversationId, received_at] // 'conversation' index on [conversationId, received_at]
name: 'conversation', name: 'conversation',
lower: [conversationId], lower: [conversationId],
upper: [conversationId, upper], upper: [conversationId, upper],
order : 'desc' order: 'desc',
// SELECT messages WHERE conversationId = this.id ORDER // SELECT messages WHERE conversationId = this.id ORDER
// received_at DESC // received_at DESC
}; };
this.fetch(options).always(resolve); this.fetch(options).always(resolve);
}.bind(this)).then(function() { })).then(() => {
if (unreadCount > 0) { if (unreadCount <= 0) {
var loadedUnread = this.getLoadedUnreadCount(); return Promise.resolve();
}
const loadedUnread = this.getLoadedUnreadCount();
if (loadedUnread >= unreadCount) { if (loadedUnread >= unreadCount) {
return; return Promise.resolve();
} }
if (startingLoadedUnread === loadedUnread) { if (startingLoadedUnread === loadedUnread) {
// that fetch didn't get us any more unread. stop fetching more. // that fetch didn't get us any more unread. stop fetching more.
return; return Promise.resolve();
} }
console.log('fetchConversation: doing another fetch to get all unread'); console.log('fetchConversation: doing another fetch to get all unread');
return this.fetchConversation(conversationId, limit, unreadCount); return this.fetchConversation(conversationId, limit, unreadCount);
} });
}.bind(this));
}, },
fetchNextExpiring: function() { fetchNextExpiring() {
this.fetch({ index: { name: 'expires_at' }, limit: 1 }); this.fetch({ index: { name: 'expires_at' }, limit: 1 });
}, },
fetchExpired: function() { fetchExpired() {
console.log('loading expired messages'); console.log('loading expired messages');
this.fetch({ this.fetch({
conditions: { expires_at: { $lte: Date.now() } }, conditions: { expires_at: { $lte: Date.now() } },
addIndividually: true addIndividually: true,
}); });
} },
}); });
})(); }());