2018-10-18 01:01:21 +00:00
|
|
|
/* 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 || {};
|
2016-01-14 21:40:26 +00:00
|
|
|
|
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'
|
|
|
|
);
|
2016-01-14 21:40:26 +00:00
|
|
|
}
|
2018-05-02 16:51:22 +00:00
|
|
|
this.receiver = receiver;
|
|
|
|
|
|
|
|
this.oncontact = this.onContactSyncComplete.bind(this);
|
|
|
|
receiver.addEventListener('contactsync', this.oncontact);
|
2016-01-14 21:40:26 +00:00
|
|
|
|
2018-05-02 16:51:22 +00:00
|
|
|
this.ongroup = this.onGroupSyncComplete.bind(this);
|
|
|
|
receiver.addEventListener('groupsync', this.ongroup);
|
2016-01-14 21:40:26 +00:00
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
const ourNumber = textsecure.storage.user.getNumber();
|
|
|
|
const { wrap, sendOptions } = ConversationController.prepareForSend(
|
2018-11-07 19:20:43 +00:00
|
|
|
ourNumber,
|
|
|
|
{ syncMessage: true }
|
2018-10-18 01:01:21 +00:00
|
|
|
);
|
2018-11-28 19:54:34 +00:00
|
|
|
|
|
|
|
window.log.info('SyncRequest created. Sending config sync request...');
|
|
|
|
wrap(sender.sendRequestConfigurationSyncMessage(sendOptions));
|
|
|
|
|
|
|
|
window.log.info('SyncRequest now sending contact sync message...');
|
2018-10-18 01:01:21 +00:00
|
|
|
wrap(sender.sendRequestContactSyncMessage(sendOptions))
|
2018-07-21 21:51:20 +00:00
|
|
|
.then(() => {
|
2018-07-21 19:00:08 +00:00
|
|
|
window.log.info('SyncRequest now sending group sync messsage...');
|
2018-10-18 01:01:21 +00:00
|
|
|
return wrap(sender.sendRequestGroupSyncMessage(sendOptions));
|
2018-05-02 16:51:22 +00:00
|
|
|
})
|
2018-07-21 21:51:20 +00:00
|
|
|
.catch(error => {
|
2018-07-21 19:00:08 +00:00
|
|
|
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);
|
|
|
|
}
|
2016-01-14 21:40:26 +00:00
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
});
|
2016-01-14 21:40:26 +00:00
|
|
|
|
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
|
|
|
|
);
|
|
|
|
};
|
2016-01-14 21:40:26 +00:00
|
|
|
|
2018-05-02 16:51:22 +00:00
|
|
|
textsecure.SyncRequest.prototype = {
|
|
|
|
constructor: textsecure.SyncRequest,
|
|
|
|
};
|
|
|
|
})();
|