Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Don't show the prompt to enable desktop notifications immediately after registration #8274

Merged
merged 5 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/MatrixClientPeg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ class MatrixClientPegClass implements IMatrixClientPeg {
};

private matrixClient: MatrixClient = null;
private justRegisteredUserId: string;
private justRegisteredUserId: string | null = null;
private registrationTime?: number;
private registrationTimeUser?: string;

// the credentials used to init the current client object.
// used if we tear it down & recreate it with a different store
Expand All @@ -136,10 +138,11 @@ class MatrixClientPegClass implements IMatrixClientPeg {
MatrixActionCreators.stop();
}

public setJustRegisteredUserId(uid: string): void {
public setJustRegisteredUserId(uid: string | null): void {
this.justRegisteredUserId = uid;
if (uid) {
window.localStorage.setItem("mx_registration_time", String(new Date().getTime()));
this.registrationTime = Date.now();
this.registrationTimeUser = uid;
}
}

Expand All @@ -151,12 +154,14 @@ class MatrixClientPegClass implements IMatrixClientPeg {
}

public userRegisteredWithinLastHours(hours: number): boolean {
try {
const date = new Date(window.localStorage.getItem("mx_registration_time"));
return ((new Date().getTime() - date.getTime()) / 36e5) <= hours;
} catch (e) {
if (
!this.registrationTime ||
hours <= 0 ||
this.registrationTimeUser !== this.matrixClient?.credentials.userId
) {
return false;
}
return ((Date.now() - this.registrationTime) / 36e5) <= hours;
}

public replaceUsingCreds(creds: IMatrixClientCreds): void {
Expand Down
69 changes: 69 additions & 0 deletions test/MatrixClientPeg-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Copyright 2022 The Matrix.org Foundation C.I.C.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { stubClient } from "./test-utils";
import { MatrixClientPeg as peg } from "../src/MatrixClientPeg";

describe("MatrixClientPeg", () => {
afterEach(() => {
(peg as any).registrationTime = undefined;
(peg as any).registrationTimeUser = undefined;
});

it("setJustRegisteredUserId", () => {
stubClient();
(peg as any).matrixClient = peg.get();
peg.setJustRegisteredUserId("@userId:matrix.rog");
expect(peg.get().credentials.userId).toBe("@userId:matrix.rog");
expect(peg.currentUserIsJustRegistered()).toBe(true);
expect(peg.userRegisteredWithinLastHours(0)).toBe(false);
expect(peg.userRegisteredWithinLastHours(1)).toBe(true);
expect(peg.userRegisteredWithinLastHours(24)).toBe(true);
});

it("setJustRegisteredUserId(null)", () => {
stubClient();
(peg as any).matrixClient = peg.get();
peg.setJustRegisteredUserId(null);
expect(peg.currentUserIsJustRegistered()).toBe(false);
expect(peg.userRegisteredWithinLastHours(0)).toBe(false);
expect(peg.userRegisteredWithinLastHours(1)).toBe(false);
expect(peg.userRegisteredWithinLastHours(24)).toBe(false);
});

it("multiple users", () => {
stubClient();
(peg as any).matrixClient = peg.get();
peg.setJustRegisteredUserId("@userId:matrix.rog");
expect(peg.get().credentials.userId).toBe("@userId:matrix.rog");
expect(peg.currentUserIsJustRegistered()).toBe(true);
expect(peg.userRegisteredWithinLastHours(0)).toBe(false);
expect(peg.userRegisteredWithinLastHours(1)).toBe(true);
expect(peg.userRegisteredWithinLastHours(24)).toBe(true);

peg.setJustRegisteredUserId("@userId2:matrix.rog");
expect(peg.currentUserIsJustRegistered()).toBe(false);
expect(peg.userRegisteredWithinLastHours(0)).toBe(false);
expect(peg.userRegisteredWithinLastHours(1)).toBe(false);
expect(peg.userRegisteredWithinLastHours(24)).toBe(false);

peg.get().credentials.userId = "@userId2:matrix.rog";
expect(peg.currentUserIsJustRegistered()).toBe(true);
expect(peg.userRegisteredWithinLastHours(0)).toBe(false);
expect(peg.userRegisteredWithinLastHours(1)).toBe(true);
expect(peg.userRegisteredWithinLastHours(24)).toBe(true);
});
});
8 changes: 0 additions & 8 deletions test/end-to-end-tests/src/scenarios/toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ export async function toastScenarios(alice: ElementSession, bob: ElementSession)
console.log(" checking and clearing toasts:");

alice.log.startGroup(`clears toasts`);
alice.log.step(`reject desktop notifications toast`);
await rejectToast(alice, "Notifications");
alice.log.done();

alice.log.step(`accepts analytics toast`);
await acceptToast(alice, "Help improve Element");
alice.log.done();
Expand All @@ -35,10 +31,6 @@ export async function toastScenarios(alice: ElementSession, bob: ElementSession)
alice.log.endGroup();

bob.log.startGroup(`clears toasts`);
bob.log.step(`reject desktop notifications toast`);
await rejectToast(bob, "Notifications");
bob.log.done();

bob.log.step(`reject analytics toast`);
await rejectToast(bob, "Help improve Element");
bob.log.done();
Expand Down