2021-11-30 16:29:57 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import type { StorageInterface } from '../types/Storage.d';
|
|
|
|
import type { WebAPIType } from '../textsecure/WebAPI';
|
|
|
|
import { LatestQueue } from '../util/LatestQueue';
|
|
|
|
import { waitForOnline } from '../util/waitForOnline';
|
|
|
|
|
|
|
|
// This is only exported for testing.
|
|
|
|
export class AreWeASubscriberService {
|
|
|
|
private readonly queue = new LatestQueue();
|
|
|
|
|
|
|
|
update(
|
|
|
|
storage: Pick<StorageInterface, 'get' | 'put' | 'onready'>,
|
2024-03-18 21:48:00 +00:00
|
|
|
server: Pick<WebAPIType, 'getHasSubscription' | 'isOnline'>
|
2021-11-30 16:29:57 +00:00
|
|
|
): void {
|
|
|
|
this.queue.add(async () => {
|
|
|
|
await new Promise<void>(resolve => storage.onready(resolve));
|
|
|
|
|
|
|
|
const subscriberId = storage.get('subscriberId');
|
|
|
|
if (!subscriberId || !subscriberId.byteLength) {
|
2022-12-21 18:41:48 +00:00
|
|
|
await storage.put('areWeASubscriber', false);
|
2021-11-30 16:29:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-18 21:48:00 +00:00
|
|
|
await waitForOnline({ server });
|
2021-11-30 16:29:57 +00:00
|
|
|
|
2022-12-21 18:41:48 +00:00
|
|
|
await storage.put(
|
2021-11-30 16:29:57 +00:00
|
|
|
'areWeASubscriber',
|
|
|
|
await server.getHasSubscription(subscriberId)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const areWeASubscriberService = new AreWeASubscriberService();
|