Skip to content

Commit

Permalink
Merge branch 'dev' into feat/WPB-3072-group-messages-in-the-chat-by-u…
Browse files Browse the repository at this point in the history
…ser-and-timestamp
  • Loading branch information
przemvs committed Jan 18, 2024
2 parents b45a8ec + 5612167 commit af061b7
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 145 deletions.
6 changes: 2 additions & 4 deletions src/script/E2EIdentity/DelayTimer/DelayTimer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import {FIFTEEN_MINUTES, FOUR_HOURS, ONE_HOUR, ONE_MINUTE} from './delay';
import {DelayTimerService} from './DelayTimer'; // Update this with your module's actual path
import {DelayTimerService} from './DelayTimer';

describe('createGracePeriodTimer', () => {
let timer: DelayTimerService | undefined;
Expand All @@ -27,16 +27,14 @@ describe('createGracePeriodTimer', () => {
jest.clearAllMocks();
jest.useFakeTimers();
global.localStorage.clear();
timer = DelayTimerService?.getInstance({
timer = new DelayTimerService({
gracePeriodInMS: 0,
gracePeriodExpiredCallback: jest.fn(),
delayPeriodExpiredCallback: jest.fn(),
});
});

afterEach(() => {
timer?.resetInstance();
timer = undefined;
jest.useRealTimers();
});

Expand Down
37 changes: 2 additions & 35 deletions src/script/E2EIdentity/DelayTimer/DelayTimer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,42 +32,21 @@ interface CreateGracePeriodTimerParams {
delayPeriodExpiredCallback: () => void;
}

class DelayTimerService {
private static instance: DelayTimerService | null = null;
export class DelayTimerService {
private gracePeriodInMS: number;
private gracePeriodExpiredCallback: () => void;
private delayPeriodExpiredCallback: () => void;
private readonly logger = logdown('@wireapp/core/DelayTimer');
private delayPeriodTimerKey: string = 'E2EIdentity_DelayTimer';
private gracePeriodTimerKey: string = 'E2EIdentity_GracePeriodTimer';

private constructor({
gracePeriodInMS,
gracePeriodExpiredCallback,
delayPeriodExpiredCallback,
}: CreateGracePeriodTimerParams) {
constructor({gracePeriodInMS, gracePeriodExpiredCallback, delayPeriodExpiredCallback}: CreateGracePeriodTimerParams) {
this.gracePeriodInMS = gracePeriodInMS;
this.gracePeriodExpiredCallback = gracePeriodExpiredCallback;
this.delayPeriodExpiredCallback = delayPeriodExpiredCallback;
this.initialize();
}

/**
* Get the singleton instance of GracePeriodTimer or create a new one
* For the first time, params are required to create the instance
* @param params The params to create the grace period timer
* @returns The singleton instance of GracePeriodTimer
*/
public static getInstance(params?: CreateGracePeriodTimerParams) {
if (!DelayTimerService.instance) {
if (!params) {
throw new Error('DelayTimerService is not initialized. Please call getInstance with params.');
}
DelayTimerService.instance = new DelayTimerService(params);
}
return DelayTimerService.instance;
}

/**
* @param CreateGracePeriodTimerParams The params to create the grace period timer
*/
Expand Down Expand Up @@ -263,16 +242,6 @@ class DelayTimerService {
: 0;
}

/**
* Reset the instance
*/
public resetInstance() {
DelayTimerStore.clear.all();
this.clearGracePeriodTimer();
this.clearDelayPeriodTimer();
DelayTimerService.instance = null;
}

public isDelayTimerActive() {
return TaskScheduler.hasActiveTask(this.delayPeriodTimerKey);
}
Expand All @@ -283,5 +252,3 @@ class DelayTimerService {
return remainingTime - delayTime > 0;
}
}

export {DelayTimerService};
12 changes: 9 additions & 3 deletions src/script/E2EIdentity/DelayTimer/delay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import {EnrollmentConfig} from '../E2EIdentityEnrollment';
import {MLSStatuses, WireIdentity} from '../E2EIdentityVerification';
import {MLSStatuses, WireIdentity, isFreshMLSSelfClient} from '../E2EIdentityVerification';

/* eslint-disable no-magic-numbers */

Expand Down Expand Up @@ -54,8 +54,14 @@ export function getDelayTime(gracePeriodInMs: number): number {
return 0;
}

export function shouldEnableSoftLock(enrollmentConfig: EnrollmentConfig, identity?: WireIdentity): boolean {
if (!enrollmentConfig.timer.isSnoozeTimeAvailable() || enrollmentConfig.isFreshMLSSelfClient) {
export async function shouldEnableSoftLock(
enrollmentConfig: EnrollmentConfig,
identity?: WireIdentity,
): Promise<boolean> {
if (await isFreshMLSSelfClient()) {
return true;
}
if (!enrollmentConfig.timer.isSnoozeTimeAvailable()) {
// The user has used up the entire grace period or has a fresh new client, he now needs to enroll
return true;
}
Expand Down
17 changes: 11 additions & 6 deletions src/script/E2EIdentity/E2EIdentityEnrollment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ jest.mock('./E2EIdentityVerification', () => ({
hasActiveCertificate: jest.fn().mockResolvedValue(false),
getActiveWireIdentity: jest.fn().mockResolvedValue({certificate: 'certificate data'}),
isE2EIEnabled: jest.fn().mockReturnValue(true),
isFreshMLSSelfClient: jest.fn().mockResolvedValue(false),
}));

// These values should lead to renewalPromptTime being less than the mocked current time
Expand Down Expand Up @@ -135,8 +136,8 @@ describe('E2EIHandler', () => {
jest.spyOn(container.resolve(Core), 'enrollE2EI').mockResolvedValueOnce(true);

const instance = await E2EIHandler.getInstance().initialize(params);
await instance['enroll']();

void instance['enroll']();
await wait(1);
expect(instance['currentStep']).toBe(E2EIHandlerStep.SUCCESS);
});

Expand All @@ -146,7 +147,8 @@ describe('E2EIHandler', () => {
jest.spyOn(container.resolve(UserState), 'self').mockImplementationOnce(() => user);

const instance = await E2EIHandler.getInstance().initialize(params);
await instance['enroll']();
void instance['enroll']();
await wait(1);
expect(instance['currentStep']).toBe(E2EIHandlerStep.ERROR);
});

Expand All @@ -169,7 +171,8 @@ describe('E2EIHandler', () => {

it('should display loading message when enroled', async () => {
const handler = await E2EIHandler.getInstance().initialize(params);
await handler['enroll']();
void handler['enroll']();
await wait(1);
expect(getModalOptions).toHaveBeenCalledWith(
expect.objectContaining({
type: ModalType.LOADING,
Expand All @@ -182,7 +185,8 @@ describe('E2EIHandler', () => {

const handler = await E2EIHandler.getInstance().initialize(params);
handler['showLoadingMessage'] = jest.fn();
await handler['enroll']();
void handler['enroll']();
await wait(1);
expect(getModalOptions).toHaveBeenCalledWith(
expect.objectContaining({
type: ModalType.SUCCESS,
Expand All @@ -195,7 +199,8 @@ describe('E2EIHandler', () => {

const handler = await E2EIHandler.getInstance().initialize(params);
handler['showLoadingMessage'] = jest.fn();
await handler['enroll']();
void handler['enroll']();
await wait(1);
expect(getModalOptions).toHaveBeenCalledWith(
expect.objectContaining({
type: ModalType.ERROR,
Expand Down
Loading

0 comments on commit af061b7

Please sign in to comment.