2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2020 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 @typescript-eslint/no-explicit-any */
|
|
|
|
/* eslint-disable max-classes-per-file */
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { EventHandler } from './EventTarget';
|
|
|
|
import EventTarget from './EventTarget';
|
2020-04-13 17:37:29 +00:00
|
|
|
import MessageReceiver from './MessageReceiver';
|
2023-07-26 17:49:27 +00:00
|
|
|
import type { ContactSyncEvent } from './messageReceiverEvents';
|
2020-04-13 17:37:29 +00:00
|
|
|
import MessageSender from './SendMessage';
|
2022-09-15 19:17:15 +00:00
|
|
|
import { assertDev } from '../util/assert';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../logging/log';
|
2022-01-14 21:34:52 +00:00
|
|
|
import { singleProtoJobQueue } from '../jobs/singleProtoJobQueue';
|
|
|
|
import * as Errors from '../types/errors';
|
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
|
|
|
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-04-08 17:09:54 +00:00
|
|
|
timeoutMillis: number;
|
|
|
|
|
2022-06-13 21:39:35 +00:00
|
|
|
constructor(private receiver: MessageReceiver, timeoutMillis?: number) {
|
2020-04-13 17:37:29 +00:00
|
|
|
super();
|
|
|
|
|
2022-06-13 21:39:35 +00:00
|
|
|
if (!(receiver instanceof MessageReceiver)) {
|
2020-04-13 17:37:29 +00:00
|
|
|
throw new Error(
|
2022-06-13 21:39:35 +00:00
|
|
|
'Tried to construct a SyncRequest without MessageReceiver'
|
2020-04-13 17:37:29 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
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) {
|
2022-09-15 19:17:15 +00:00
|
|
|
assertDev(
|
|
|
|
false,
|
|
|
|
'SyncRequestInner: started more than once. Doing nothing'
|
|
|
|
);
|
2021-04-08 16:24:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.started = true;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-01-14 21:34:52 +00:00
|
|
|
log.info(
|
|
|
|
'SyncRequest created. Sending config, block, contact, and group requests...'
|
|
|
|
);
|
|
|
|
try {
|
|
|
|
await Promise.all([
|
2022-06-13 21:39:35 +00:00
|
|
|
singleProtoJobQueue.add(
|
|
|
|
MessageSender.getRequestConfigurationSyncMessage()
|
|
|
|
),
|
|
|
|
singleProtoJobQueue.add(MessageSender.getRequestBlockSyncMessage()),
|
|
|
|
singleProtoJobQueue.add(MessageSender.getRequestContactSyncMessage()),
|
2022-01-14 21:34:52 +00:00
|
|
|
]);
|
|
|
|
} catch (error: unknown) {
|
|
|
|
log.error(
|
|
|
|
'SyncRequest: Failed to add request jobs',
|
|
|
|
Errors.toLogFormat(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
|
|
|
update() {
|
2023-07-26 17:49:27 +00:00
|
|
|
if (this.contactSync) {
|
2020-04-13 17:37:29 +00:00
|
|
|
this.dispatchEvent(new Event('success'));
|
|
|
|
this.cleanup();
|
|
|
|
}
|
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
onTimeout() {
|
2023-07-26 17:49:27 +00:00
|
|
|
if (this.contactSync) {
|
2020-04-13 17:37:29 +00:00
|
|
|
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);
|
|
|
|
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
|
|
|
|
2022-06-13 21:39:35 +00:00
|
|
|
constructor(receiver: MessageReceiver, timeoutMillis?: number) {
|
|
|
|
const inner = new SyncRequestInner(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 {
|
2022-12-21 18:41:48 +00:00
|
|
|
void this.inner.start();
|
2021-04-08 16:24:21 +00:00
|
|
|
}
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|