b7d56def82
* Starting to work through lint errors * libsignal-protocol: Update changes for primary repo compatibility * Step 1: task_with_timeout rename * Step 2: Apply the changes to TaskWithTimeout.ts * Step 1: All to-be-converted libtextsecure/*.js files moved * Step 2: No Typescript errors! * Get libtextsecure tests passing again * TSLint errors down to 1 * Compilation succeeds, no lint errors or test failures * WebSocketResources - update import for case-sensitive filesystems * Fixes for lint-deps * Remove unnecessary @ts-ignore * Fix inability to message your own contact after link * Add log message for the end of migration 20 * lint fix
100 lines
3 KiB
TypeScript
100 lines
3 KiB
TypeScript
import EventTarget from './EventTarget';
|
|
import MessageReceiver from './MessageReceiver';
|
|
import MessageSender from './SendMessage';
|
|
|
|
class SyncRequestInner extends EventTarget {
|
|
receiver: MessageReceiver;
|
|
contactSync?: boolean;
|
|
groupSync?: boolean;
|
|
timeout: any;
|
|
oncontact: Function;
|
|
ongroup: Function;
|
|
|
|
constructor(sender: MessageSender, receiver: MessageReceiver) {
|
|
super();
|
|
|
|
if (
|
|
!(sender instanceof MessageSender) ||
|
|
!(receiver instanceof MessageReceiver)
|
|
) {
|
|
throw new Error(
|
|
'Tried to construct a SyncRequest without MessageSender and MessageReceiver'
|
|
);
|
|
}
|
|
this.receiver = receiver;
|
|
|
|
this.oncontact = this.onContactSyncComplete.bind(this);
|
|
receiver.addEventListener('contactsync', this.oncontact);
|
|
|
|
this.ongroup = this.onGroupSyncComplete.bind(this);
|
|
receiver.addEventListener('groupsync', this.ongroup);
|
|
|
|
const ourNumber = window.textsecure.storage.user.getNumber();
|
|
const { wrap, sendOptions } = window.ConversationController.prepareForSend(
|
|
ourNumber,
|
|
{
|
|
syncMessage: true,
|
|
}
|
|
);
|
|
|
|
window.log.info('SyncRequest created. Sending config sync request...');
|
|
// tslint:disable
|
|
wrap(sender.sendRequestConfigurationSyncMessage(sendOptions));
|
|
|
|
window.log.info('SyncRequest now sending block sync request...');
|
|
wrap(sender.sendRequestBlockSyncMessage(sendOptions));
|
|
|
|
window.log.info('SyncRequest now sending contact sync message...');
|
|
wrap(sender.sendRequestContactSyncMessage(sendOptions))
|
|
.then(() => {
|
|
window.log.info('SyncRequest now sending group sync messsage...');
|
|
return wrap(sender.sendRequestGroupSyncMessage(sendOptions));
|
|
})
|
|
.catch((error: Error) => {
|
|
window.log.error(
|
|
'SyncRequest error:',
|
|
error && error.stack ? error.stack : error
|
|
);
|
|
});
|
|
this.timeout = setTimeout(this.onTimeout.bind(this), 60000);
|
|
}
|
|
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);
|
|
this.receiver.removeEventListener('contactsync', this.oncontact);
|
|
this.receiver.removeEventListener('groupSync', this.ongroup);
|
|
delete this.listeners;
|
|
}
|
|
}
|
|
|
|
export default class SyncRequest {
|
|
constructor(sender: MessageSender, receiver: MessageReceiver) {
|
|
const inner = new SyncRequestInner(sender, receiver);
|
|
this.addEventListener = inner.addEventListener.bind(inner);
|
|
this.removeEventListener = inner.removeEventListener.bind(inner);
|
|
}
|
|
|
|
addEventListener: (name: string, handler: Function) => void;
|
|
removeEventListener: (name: string, handler: Function) => void;
|
|
}
|