2021-04-08 16:24:21 +00:00
|
|
|
// Copyright 2020-2021 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-09-24 21:53:21 +00:00
|
|
|
/* eslint-disable more/no-then */
|
|
|
|
/* eslint-disable @typescript-eslint/ban-types */
|
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
|
|
/* eslint-disable max-classes-per-file */
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
import EventTarget, { EventHandler } from './EventTarget';
|
2020-04-13 17:37:29 +00:00
|
|
|
import MessageReceiver from './MessageReceiver';
|
2021-07-09 19:36:10 +00:00
|
|
|
import { ContactSyncEvent, GroupSyncEvent } from './messageReceiverEvents';
|
2020-04-13 17:37:29 +00:00
|
|
|
import MessageSender from './SendMessage';
|
2021-04-08 16:24:21 +00:00
|
|
|
import { assert } from '../util/assert';
|
2021-07-15 23:48:09 +00:00
|
|
|
import { getSendOptions } from '../util/getSendOptions';
|
|
|
|
import { handleMessageSend } from '../util/handleMessageSend';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../logging/log';
|
2020-04-13 17:37:29 +00:00
|
|
|
|
|
|
|
class SyncRequestInner extends EventTarget {
|
2021-04-08 16:24:21 +00:00
|
|
|
private started = false;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
contactSync?: boolean;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
groupSync?: boolean;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
timeout: any;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
oncontact: (event: ContactSyncEvent) => void;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
ongroup: (event: GroupSyncEvent) => void;
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-04-08 17:09:54 +00:00
|
|
|
timeoutMillis: number;
|
|
|
|
|
2021-04-08 16:24:21 +00:00
|
|
|
constructor(
|
|
|
|
private sender: MessageSender,
|
2021-04-08 17:09:54 +00:00
|
|
|
private receiver: MessageReceiver,
|
|
|
|
timeoutMillis?: number
|
2021-04-08 16:24:21 +00:00
|
|
|
) {
|
2020-04-13 17:37:29 +00:00
|
|
|
super();
|
|
|
|
|
|
|
|
if (
|
|
|
|
!(sender instanceof MessageSender) ||
|
|
|
|
!(receiver instanceof MessageReceiver)
|
|
|
|
) {
|
|
|
|
throw new Error(
|
|
|
|
'Tried to construct a SyncRequest without MessageSender and MessageReceiver'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.oncontact = this.onContactSyncComplete.bind(this);
|
2021-07-09 19:36:10 +00:00
|
|
|
receiver.addEventListener('contactSync', this.oncontact);
|
2020-04-13 17:37:29 +00:00
|
|
|
|
|
|
|
this.ongroup = this.onGroupSyncComplete.bind(this);
|
2021-07-09 19:36:10 +00:00
|
|
|
receiver.addEventListener('groupSync', this.ongroup);
|
2021-04-08 17:09:54 +00:00
|
|
|
|
|
|
|
this.timeoutMillis = timeoutMillis || 60000;
|
2021-04-08 16:24:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async start(): Promise<void> {
|
|
|
|
if (this.started) {
|
|
|
|
assert(false, 'SyncRequestInner: started more than once. Doing nothing');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.started = true;
|
|
|
|
|
|
|
|
const { sender } = this;
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
const ourConversation = window.ConversationController.getOurConversationOrThrow();
|
|
|
|
const sendOptions = await getSendOptions(ourConversation.attributes, {
|
2021-04-08 16:24:21 +00:00
|
|
|
syncMessage: true,
|
|
|
|
});
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
if (window.ConversationController.areWePrimaryDevice()) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn('SyncRequest.start: We are primary device; returning early');
|
2021-07-15 23:48:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('SyncRequest created. Sending config sync request...');
|
2021-07-15 23:48:09 +00:00
|
|
|
handleMessageSend(sender.sendRequestConfigurationSyncMessage(sendOptions), {
|
|
|
|
messageIds: [],
|
|
|
|
sendType: 'otherSync',
|
|
|
|
});
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('SyncRequest now sending block sync request...');
|
2021-07-15 23:48:09 +00:00
|
|
|
handleMessageSend(sender.sendRequestBlockSyncMessage(sendOptions), {
|
|
|
|
messageIds: [],
|
|
|
|
sendType: 'otherSync',
|
|
|
|
});
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('SyncRequest now sending contact sync message...');
|
2021-07-15 23:48:09 +00:00
|
|
|
handleMessageSend(sender.sendRequestContactSyncMessage(sendOptions), {
|
|
|
|
messageIds: [],
|
|
|
|
sendType: 'otherSync',
|
|
|
|
})
|
2020-04-13 17:37:29 +00:00
|
|
|
.then(() => {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('SyncRequest now sending group sync message...');
|
2021-07-15 23:48:09 +00:00
|
|
|
return handleMessageSend(
|
|
|
|
sender.sendRequestGroupSyncMessage(sendOptions),
|
|
|
|
{ messageIds: [], sendType: 'otherSync' }
|
|
|
|
);
|
2020-04-13 17:37:29 +00:00
|
|
|
})
|
|
|
|
.catch((error: Error) => {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2020-04-13 17:37:29 +00:00
|
|
|
'SyncRequest error:',
|
|
|
|
error && error.stack ? error.stack : error
|
|
|
|
);
|
|
|
|
});
|
2021-04-08 17:09:54 +00:00
|
|
|
this.timeout = setTimeout(this.onTimeout.bind(this), this.timeoutMillis);
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
onContactSyncComplete() {
|
|
|
|
this.contactSync = true;
|
|
|
|
this.update();
|
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
onGroupSyncComplete() {
|
|
|
|
this.groupSync = true;
|
|
|
|
this.update();
|
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
update() {
|
|
|
|
if (this.contactSync && this.groupSync) {
|
|
|
|
this.dispatchEvent(new Event('success'));
|
|
|
|
this.cleanup();
|
|
|
|
}
|
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
onTimeout() {
|
|
|
|
if (this.contactSync || this.groupSync) {
|
|
|
|
this.dispatchEvent(new Event('success'));
|
|
|
|
} else {
|
|
|
|
this.dispatchEvent(new Event('timeout'));
|
|
|
|
}
|
|
|
|
this.cleanup();
|
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
cleanup() {
|
|
|
|
clearTimeout(this.timeout);
|
|
|
|
this.receiver.removeEventListener('contactsync', this.oncontact);
|
|
|
|
this.receiver.removeEventListener('groupSync', this.ongroup);
|
|
|
|
delete this.listeners;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class SyncRequest {
|
2021-04-08 16:24:21 +00:00
|
|
|
private inner: SyncRequestInner;
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
addEventListener: (
|
|
|
|
name: 'success' | 'timeout',
|
|
|
|
handler: EventHandler
|
|
|
|
) => void;
|
2021-04-08 16:24:21 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
removeEventListener: (
|
|
|
|
name: 'success' | 'timeout',
|
|
|
|
handler: EventHandler
|
|
|
|
) => void;
|
2021-04-08 16:24:21 +00:00
|
|
|
|
2021-04-08 17:09:54 +00:00
|
|
|
constructor(
|
|
|
|
sender: MessageSender,
|
|
|
|
receiver: MessageReceiver,
|
|
|
|
timeoutMillis?: number
|
|
|
|
) {
|
|
|
|
const inner = new SyncRequestInner(sender, receiver, timeoutMillis);
|
2021-04-08 16:24:21 +00:00
|
|
|
this.inner = inner;
|
2020-04-13 17:37:29 +00:00
|
|
|
this.addEventListener = inner.addEventListener.bind(inner);
|
|
|
|
this.removeEventListener = inner.removeEventListener.bind(inner);
|
|
|
|
}
|
|
|
|
|
2021-04-08 16:24:21 +00:00
|
|
|
start(): void {
|
|
|
|
this.inner.start();
|
|
|
|
}
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|