signal-desktop/libtextsecure/sync_request.js

91 lines
2.7 KiB
JavaScript
Raw Normal View History

/* global Event, textsecure, window, ConversationController */
2018-07-21 21:51:20 +00:00
/* eslint-disable more/no-then */
// eslint-disable-next-line func-names
2018-05-02 16:51:22 +00:00
(function() {
window.textsecure = window.textsecure || {};
2018-05-02 16:51:22 +00:00
function SyncRequest(sender, receiver) {
if (
!(sender instanceof textsecure.MessageSender) ||
!(receiver instanceof textsecure.MessageReceiver)
) {
throw new Error(
'Tried to construct a SyncRequest without MessageSender and MessageReceiver'
);
}
2018-05-02 16:51:22 +00:00
this.receiver = receiver;
this.oncontact = this.onContactSyncComplete.bind(this);
receiver.addEventListener('contactsync', this.oncontact);
2018-05-02 16:51:22 +00:00
this.ongroup = this.onGroupSyncComplete.bind(this);
receiver.addEventListener('groupsync', this.ongroup);
const ourNumber = textsecure.storage.user.getNumber();
const { wrap, sendOptions } = ConversationController.prepareForSend(
2018-11-07 19:20:43 +00:00
ourNumber,
{ syncMessage: true }
);
window.log.info('SyncRequest created. Sending contact sync message...');
wrap(sender.sendRequestContactSyncMessage(sendOptions))
2018-07-21 21:51:20 +00:00
.then(() => {
window.log.info('SyncRequest now sending group sync messsage...');
return wrap(sender.sendRequestGroupSyncMessage(sendOptions));
2018-05-02 16:51:22 +00:00
})
2018-07-21 21:51:20 +00:00
.catch(error => {
window.log.error(
2018-05-02 16:51:22 +00:00
'SyncRequest error:',
error && error.stack ? error.stack : error
);
});
this.timeout = setTimeout(this.onTimeout.bind(this), 60000);
}
2018-05-02 16:51:22 +00:00
SyncRequest.prototype = new textsecure.EventTarget();
SyncRequest.prototype.extend({
constructor: SyncRequest,
2018-07-21 21:51:20 +00:00
onContactSyncComplete() {
2018-05-02 16:51:22 +00:00
this.contactSync = true;
this.update();
},
2018-07-21 21:51:20 +00:00
onGroupSyncComplete() {
2018-05-02 16:51:22 +00:00
this.groupSync = true;
this.update();
},
2018-07-21 21:51:20 +00:00
update() {
2018-05-02 16:51:22 +00:00
if (this.contactSync && this.groupSync) {
this.dispatchEvent(new Event('success'));
this.cleanup();
}
},
2018-07-21 21:51:20 +00:00
onTimeout() {
2018-05-02 16:51:22 +00:00
if (this.contactSync || this.groupSync) {
this.dispatchEvent(new Event('success'));
} else {
this.dispatchEvent(new Event('timeout'));
}
this.cleanup();
},
2018-07-21 21:51:20 +00:00
cleanup() {
2018-05-02 16:51:22 +00:00
clearTimeout(this.timeout);
this.receiver.removeEventListener('contactsync', this.oncontact);
this.receiver.removeEventListener('groupSync', this.ongroup);
delete this.listeners;
},
});
2018-07-21 21:51:20 +00:00
textsecure.SyncRequest = function SyncRequestWrapper(sender, receiver) {
const syncRequest = new SyncRequest(sender, receiver);
2018-05-02 16:51:22 +00:00
this.addEventListener = syncRequest.addEventListener.bind(syncRequest);
this.removeEventListener = syncRequest.removeEventListener.bind(
syncRequest
);
};
2018-05-02 16:51:22 +00:00
textsecure.SyncRequest.prototype = {
constructor: textsecure.SyncRequest,
};
})();