Commit e7f211ba authored by JC Brand's avatar JC Brand

Avoid an unnecessary promise and create message as soon as possible

under non-OMEMO circumstances. Otherwise, when messages are fetched in
bulk via MAM, then a message referring to a previous one (e.g.
a correction) may be processed before the message being referred to has
been created.
parent 9a7f56db
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
<link rel="shortcut icon" type="image/ico" href="css/images/favicon.ico"/> <link rel="shortcut icon" type="image/ico" href="css/images/favicon.ico"/>
<link type="text/css" rel="stylesheet" media="screen" href="css/fullpage.css" /> <link type="text/css" rel="stylesheet" media="screen" href="css/fullpage.css" />
<link type="text/css" rel="stylesheet" media="screen" href="css/converse.css" /> <link type="text/css" rel="stylesheet" media="screen" href="css/converse.css" />
<script src="3rdparty/libsignal-protocol-javascript/dist/libsignal-protocol.js"></script>
<script src="dist/converse.js"></script> <script src="dist/converse.js"></script>
</head> </head>
......
...@@ -63111,29 +63111,39 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_ ...@@ -63111,29 +63111,39 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
attrs.spoiler_hint = spoiler.textContent.length > 0 ? spoiler.textContent : ''; attrs.spoiler_hint = spoiler.textContent.length > 0 ? spoiler.textContent : '';
} }
return Promise.resolve(attrs); return attrs;
}, },
createMessage(message, original_stanza) { createMessage(message, original_stanza) {
/* Create a Backbone.Message object inside this chat box /* Create a Backbone.Message object inside this chat box
* based on the identified message stanza. * based on the identified message stanza.
*/ */
return new Promise((resolve, reject) => { const that = this;
this.getMessageAttributesFromStanza(message, original_stanza).then(attrs => {
const is_csn = u.isOnlyChatStateNotification(attrs);
if (is_csn && (attrs.is_delayed || attrs.type === 'groupchat' && Strophe.getResourceFromJid(attrs.from) == this.get('nick'))) { function _create(attrs) {
// XXX: MUC leakage const is_csn = u.isOnlyChatStateNotification(attrs);
// No need showing delayed or our own CSN messages
resolve(); if (is_csn && (attrs.is_delayed || attrs.type === 'groupchat' && Strophe.getResourceFromJid(attrs.from) == that.get('nick'))) {
} else if (!is_csn && !attrs.file && !attrs.message && !attrs.oob_url && attrs.type !== 'error') { // XXX: MUC leakage
// TODO: handle <subject> messages (currently being done by ChatRoom) // No need showing delayed or our own CSN messages
resolve(); return;
} else { } else if (!is_csn && !attrs.file && !attrs.message && !attrs.oob_url && attrs.type !== 'error') {
resolve(this.messages.create(attrs)); // TODO: handle <subject> messages (currently being done by ChatRoom)
} return;
}).catch(e => reject(e)); } else {
}); return that.messages.create(attrs);
}
}
const result = this.getMessageAttributesFromStanza(message, original_stanza);
if (result instanceof Promise) {
return new Promise((resolve, reject) => result.then(attrs => resolve(_create(attrs))).catch(reject));
} else {
const message = _create(result);
return Promise.resolve(message);
}
}, },
isHidden() { isHidden() {
...@@ -68657,17 +68667,23 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_ ...@@ -68657,17 +68667,23 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
// New functions which don't exist yet can also be added. // New functions which don't exist yet can also be added.
ChatBox: { ChatBox: {
getMessageAttributesFromStanza(message, original_stanza) { getMessageAttributesFromStanza(message, original_stanza) {
return new Promise((resolve, reject) => { function _process(attrs) {
this.__super__.getMessageAttributesFromStanza.apply(this, arguments).then(attrs => { const archive_id = getMessageArchiveID(original_stanza);
const archive_id = getMessageArchiveID(original_stanza);
if (archive_id) { if (archive_id) {
attrs.archive_id = archive_id; attrs.archive_id = archive_id;
} }
resolve(attrs); return attrs;
}).catch(reject); }
});
const result = this.__super__.getMessageAttributesFromStanza.apply(this, arguments);
if (result instanceof Promise) {
return new Promise((resolve, reject) => result.then(attrs => resolve(_process(attrs))).catch(reject));
} else {
return _process(result);
}
} }
}, },
...@@ -74164,38 +74180,38 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_ ...@@ -74164,38 +74180,38 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
}); });
}, },
getEncryptionAttributesfromStanza(stanza, original_stanza) { getEncryptionAttributesfromStanza(stanza, original_stanza, attrs) {
const _converse = this.__super__._converse; const _converse = this.__super__._converse,
const encrypted = sizzle(`encrypted[xmlns="${Strophe.NS.OMEMO}"]`, original_stanza).pop(); encrypted = sizzle(`encrypted[xmlns="${Strophe.NS.OMEMO}"]`, original_stanza).pop();
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.__super__.getMessageAttributesFromStanza.apply(this, arguments).then(attrs => { const _converse = this.__super__._converse,
const _converse = this.__super__._converse, header = encrypted.querySelector('header'),
header = encrypted.querySelector('header'), key = sizzle(`key[rid="${_converse.omemo_store.get('device_id')}"]`, encrypted).pop();
key = sizzle(`key[rid="${_converse.omemo_store.get('device_id')}"]`, encrypted).pop();
if (key) {
if (key) { attrs['encrypted'] = {
attrs['encrypted'] = { 'device_id': header.getAttribute('sid'),
'device_id': header.getAttribute('sid'), 'iv': header.querySelector('iv').textContent,
'iv': header.querySelector('iv').textContent, 'key': key.textContent,
'key': key.textContent, 'payload': _.get(encrypted.querySelector('payload'), 'textContent', null),
'payload': _.get(encrypted.querySelector('payload'), 'textContent', null), 'prekey': key.getAttribute('prekey')
'prekey': key.getAttribute('prekey') };
}; this.decrypt(attrs).then(plaintext => resolve(_.extend(attrs, {
this.decrypt(attrs).then(plaintext => resolve(_.extend(attrs, { 'plaintext': plaintext
'plaintext': plaintext }))).catch(reject);
}))).catch(reject); }
}
}).catch(_.partial(_converse.log, _, Strophe.LogLevel.ERROR));
}); });
}, },
getMessageAttributesFromStanza(stanza, original_stanza) { getMessageAttributesFromStanza(stanza, original_stanza) {
const encrypted = sizzle(`encrypted[xmlns="${Strophe.NS.OMEMO}"]`, original_stanza).pop(); const encrypted = sizzle(`encrypted[xmlns="${Strophe.NS.OMEMO}"]`, original_stanza).pop();
const attrs = this.__super__.getMessageAttributesFromStanza.apply(this, arguments);
if (!encrypted) { if (!encrypted) {
return this.__super__.getMessageAttributesFromStanza.apply(this, arguments); return attrs;
} else { } else {
return this.getEncryptionAttributesfromStanza(stanza, original_stanza); return this.getEncryptionAttributesfromStanza(stanza, original_stanza, attrs);
} }
}, },
...@@ -74442,8 +74458,7 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_ ...@@ -74442,8 +74458,7 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
'identity_keypair': identity_keypair, 'identity_keypair': identity_keypair,
'prekeys': {} 'prekeys': {}
}; };
const signed_prekey_id = '0'; libsignal.KeyHelper.generateSignedPreKey(identity_keypair, 0).then(signed_prekey => {
libsignal.KeyHelper.generateSignedPreKey(identity_keypair, signed_prekey_id).then(signed_prekey => {
data['signed_prekey'] = signed_prekey; data['signed_prekey'] = signed_prekey;
const key_promises = _.map(_.range(0, _converse.NUM_PREKEYS), id => libsignal.KeyHelper.generatePreKey(id)); const key_promises = _.map(_.range(0, _converse.NUM_PREKEYS), id => libsignal.KeyHelper.generatePreKey(id));
...@@ -74690,13 +74705,14 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_ ...@@ -74690,13 +74705,14 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
'type': 'get', 'type': 'get',
'from': _converse.bare_jid, 'from': _converse.bare_jid,
'to': this.get('jid') 'to': this.get('jid')
}).c('query', { }).c('pubsub', {
'xmlns': Strophe.NS.DISCO_ITEMS, 'xmlns': Strophe.NS.PUBSUB
}).c('items', {
'node': Strophe.NS.OMEMO_DEVICELIST 'node': Strophe.NS.OMEMO_DEVICELIST
}); });
_converse.connection.sendIQ(stanza, iq => { _converse.connection.sendIQ(stanza, iq => {
_.forEach(iq.querySelectorAll('device'), dev => this.devices.create({ _.forEach(sizzle(`list[xmlns="${Strophe.NS.OMEMO}"] device`, iq), dev => this.devices.create({
'id': dev.getAttribute('id'), 'id': dev.getAttribute('id'),
'jid': this.get('jid') 'jid': this.get('jid')
})); }));
...@@ -74725,7 +74741,7 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_ ...@@ -74725,7 +74741,7 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
'node': Strophe.NS.OMEMO_DEVICELIST 'node': Strophe.NS.OMEMO_DEVICELIST
}).c('item').c('list', { }).c('item').c('list', {
'xmlns': Strophe.NS.OMEMO 'xmlns': Strophe.NS.OMEMO
}).up(); });
_.each(this.devices.where({ _.each(this.devices.where({
'active': true 'active': true
...@@ -74905,7 +74921,7 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_ ...@@ -74905,7 +74921,7 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
fetchOwnDevices().then(() => restoreOMEMOSession()).then(() => updateOwnDeviceList()).then(() => _converse.omemo.publishBundle()).then(() => _converse.emit('OMEMOInitialized')).catch(_.partial(_converse.log, _, Strophe.LogLevel.ERROR)); fetchOwnDevices().then(() => restoreOMEMOSession()).then(() => updateOwnDeviceList()).then(() => _converse.omemo.publishBundle()).then(() => _converse.emit('OMEMOInitialized')).catch(_.partial(_converse.log, _, Strophe.LogLevel.ERROR));
} }
_converse.api.listen.on('afterTearDown', () => _converse.devices.reset()); _converse.api.listen.on('afterTearDown', () => _converse.devicelists.reset());
_converse.api.listen.on('connected', registerPEPPushHandler); _converse.api.listen.on('connected', registerPEPPushHandler);
This diff is collapsed.
...@@ -529,29 +529,35 @@ ...@@ -529,29 +529,35 @@
if (spoiler) { if (spoiler) {
attrs.spoiler_hint = spoiler.textContent.length > 0 ? spoiler.textContent : ''; attrs.spoiler_hint = spoiler.textContent.length > 0 ? spoiler.textContent : '';
} }
return Promise.resolve(attrs); return attrs;
}, },
createMessage (message, original_stanza) { createMessage (message, original_stanza) {
/* Create a Backbone.Message object inside this chat box /* Create a Backbone.Message object inside this chat box
* based on the identified message stanza. * based on the identified message stanza.
*/ */
return new Promise((resolve, reject) => { const that = this;
this.getMessageAttributesFromStanza(message, original_stanza) function _create (attrs) {
.then((attrs) => { const is_csn = u.isOnlyChatStateNotification(attrs);
const is_csn = u.isOnlyChatStateNotification(attrs); if (is_csn && (attrs.is_delayed ||
if (is_csn && (attrs.is_delayed || (attrs.type === 'groupchat' && Strophe.getResourceFromJid(attrs.from) == this.get('nick')))) { (attrs.type === 'groupchat' && Strophe.getResourceFromJid(attrs.from) == that.get('nick')))) {
// XXX: MUC leakage // XXX: MUC leakage
// No need showing delayed or our own CSN messages // No need showing delayed or our own CSN messages
resolve(); return;
} else if (!is_csn && !attrs.file && !attrs.message && !attrs.oob_url && attrs.type !== 'error') { } else if (!is_csn && !attrs.file && !attrs.message && !attrs.oob_url && attrs.type !== 'error') {
// TODO: handle <subject> messages (currently being done by ChatRoom) // TODO: handle <subject> messages (currently being done by ChatRoom)
resolve(); return;
} else { } else {
resolve(this.messages.create(attrs)); return that.messages.create(attrs);
} }
}).catch(e => reject(e)) }
}); const result = this.getMessageAttributesFromStanza(message, original_stanza)
if (result instanceof Promise) {
return new Promise((resolve, reject) => result.then(attrs => resolve(_create(attrs))).catch(reject));
} else {
const message = _create(result)
return Promise.resolve(message);
}
}, },
isHidden () { isHidden () {
......
...@@ -130,16 +130,19 @@ ...@@ -130,16 +130,19 @@
ChatBox: { ChatBox: {
getMessageAttributesFromStanza (message, original_stanza) { getMessageAttributesFromStanza (message, original_stanza) {
return new Promise((resolve, reject) => { function _process (attrs) {
this.__super__.getMessageAttributesFromStanza.apply(this, arguments) const archive_id = getMessageArchiveID(original_stanza);
.then((attrs) => { if (archive_id) {
const archive_id = getMessageArchiveID(original_stanza); attrs.archive_id = archive_id;
if (archive_id) { }
attrs.archive_id = archive_id; return attrs;
} }
resolve(attrs); const result = this.__super__.getMessageAttributesFromStanza.apply(this, arguments)
}).catch(reject); if (result instanceof Promise) {
}); return new Promise((resolve, reject) => result.then((attrs) => resolve(_process(attrs))).catch(reject));
} else {
return _process(result);
}
} }
}, },
......
...@@ -203,39 +203,37 @@ ...@@ -203,39 +203,37 @@
}); });
}, },
getEncryptionAttributesfromStanza (stanza, original_stanza) { getEncryptionAttributesfromStanza (stanza, original_stanza, attrs) {
const { _converse } = this.__super__; const { _converse } = this.__super__,
const encrypted = sizzle(`encrypted[xmlns="${Strophe.NS.OMEMO}"]`, original_stanza).pop(); encrypted = sizzle(`encrypted[xmlns="${Strophe.NS.OMEMO}"]`, original_stanza).pop();
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.__super__.getMessageAttributesFromStanza.apply(this, arguments) const { _converse } = this.__super__,
.then((attrs) => { header = encrypted.querySelector('header'),
const { _converse } = this.__super__, key = sizzle(`key[rid="${_converse.omemo_store.get('device_id')}"]`, encrypted).pop();
header = encrypted.querySelector('header'),
key = sizzle(`key[rid="${_converse.omemo_store.get('device_id')}"]`, encrypted).pop(); if (key) {
attrs['encrypted'] = {
if (key) { 'device_id': header.getAttribute('sid'),
attrs['encrypted'] = { 'iv': header.querySelector('iv').textContent,
'device_id': header.getAttribute('sid'), 'key': key.textContent,
'iv': header.querySelector('iv').textContent, 'payload': _.get(encrypted.querySelector('payload'), 'textContent', null),
'key': key.textContent, 'prekey': key.getAttribute('prekey')
'payload': _.get(encrypted.querySelector('payload'), 'textContent', null),
'prekey': key.getAttribute('prekey')
}
this.decrypt(attrs)
.then((plaintext) => resolve(_.extend(attrs, {'plaintext': plaintext})))
.catch(reject);
} }
}).catch(_.partial(_converse.log, _, Strophe.LogLevel.ERROR)); this.decrypt(attrs)
.then((plaintext) => resolve(_.extend(attrs, {'plaintext': plaintext})))
.catch(reject);
}
}); });
}, },
getMessageAttributesFromStanza (stanza, original_stanza) { getMessageAttributesFromStanza (stanza, original_stanza) {
const encrypted = sizzle(`encrypted[xmlns="${Strophe.NS.OMEMO}"]`, original_stanza).pop(); const encrypted = sizzle(`encrypted[xmlns="${Strophe.NS.OMEMO}"]`, original_stanza).pop();
const attrs = this.__super__.getMessageAttributesFromStanza.apply(this, arguments);
if (!encrypted) { if (!encrypted) {
return this.__super__.getMessageAttributesFromStanza.apply(this, arguments); return attrs;
} else { } else {
return this.getEncryptionAttributesfromStanza(stanza, original_stanza); return this.getEncryptionAttributesfromStanza(stanza, original_stanza, attrs);
} }
}, },
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment