You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
session-desktop/libtextsecure/sync_request.js

89 lines
2.6 KiB
JavaScript

/* global Event, textsecure, window, libsession */
/* eslint-disable more/no-then */
// eslint-disable-next-line func-names
(function() {
window.textsecure = window.textsecure || {};
async function SyncRequest() {
// this.receiver = receiver;
window.log.info('SyncRequest created. Sending config sync request...');
const { CONFIGURATION } = textsecure.protobuf.SyncMessage.Request.Type;
const { RequestSyncMessage } = window.libsession.Messages.Outgoing;
const requestConfigurationSyncMessage = new RequestSyncMessage({
timestamp: Date.now(),
reqestType: CONFIGURATION,
});
await libsession
.getMessageQueue()
.sendSyncMessage(requestConfigurationSyncMessage);
window.log.info('SyncRequest now sending contact sync message...');
const { CONTACTS } = textsecure.protobuf.SyncMessage.Request.Type;
const requestContactSyncMessage = new RequestSyncMessage({
timestamp: Date.now(),
reqestType: CONTACTS,
});
await libsession
.getMessageQueue()
.sendSyncMessage(requestContactSyncMessage);
window.log.info('SyncRequest now sending group sync messsage...');
const { GROUPS } = textsecure.protobuf.SyncMessage.Request.Type;
const requestGroupSyncMessage = new RequestSyncMessage({
timestamp: Date.now(),
reqestType: GROUPS,
});
await libsession.getMessageQueue().sendSyncMessage(requestGroupSyncMessage);
this.timeout = setTimeout(this.onTimeout.bind(this), 60000);
}
SyncRequest.prototype = new textsecure.EventTarget();
SyncRequest.prototype.extend({
constructor: SyncRequest,
onContactSyncComplete() {
this.contactSync = true;
this.update();
},
onGroupSyncComplete() {
this.groupSync = true;
this.update();
},
update() {
if (this.contactSync && this.groupSync) {
this.dispatchEvent(new Event('success'));
this.cleanup();
}
},
onTimeout() {
if (this.contactSync || this.groupSync) {
this.dispatchEvent(new Event('success'));
} else {
this.dispatchEvent(new Event('timeout'));
}
this.cleanup();
},
cleanup() {
clearTimeout(this.timeout);
delete this.listeners;
},
});
textsecure.SyncRequest = function SyncRequestWrapper() {
const syncRequest = new SyncRequest();
this.addEventListener = syncRequest.addEventListener.bind(syncRequest);
this.removeEventListener = syncRequest.removeEventListener.bind(
syncRequest
);
};
textsecure.SyncRequest.prototype = {
constructor: textsecure.SyncRequest,
};
})();