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

Commit

Permalink
Don't show the prompt to enable desktop notifications immediately aft…
Browse files Browse the repository at this point in the history
…er registration (#8274)

* Fix MatrixClientPeg.userRegisteredWithinLastHours so that it works

* Try fixing end-to-end test + add case for New search beta

* Remove end-to-end test case for Search beta toast as it only shows up after 5 minutes

* Revert to localStorage based solution + non-inverted logic + test including time advancement
  • Loading branch information
hughns authored Apr 13, 2022
1 parent d151365 commit 26b771b
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 14 deletions.
9 changes: 7 additions & 2 deletions src/Lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -875,8 +875,9 @@ async function clearStorage(opts?: { deleteEverything?: boolean }): Promise<void
Analytics.disable();

if (window.localStorage) {
// try to save any 3pid invites from being obliterated
// try to save any 3pid invites from being obliterated and registration time
const pendingInvites = ThreepidInviteStore.instance.getWireInvites();
const registrationTime = window.localStorage.getItem("mx_registration_time");

window.localStorage.clear();

Expand All @@ -886,13 +887,17 @@ async function clearStorage(opts?: { deleteEverything?: boolean }): Promise<void
logger.error("idbDelete failed for account:mx_access_token", e);
}

// now restore those invites
// now restore those invites and registration time
if (!opts?.deleteEverything) {
pendingInvites.forEach(i => {
const roomId = i.roomId;
delete i.roomId; // delete to avoid confusing the store
ThreepidInviteStore.instance.storeInvite(roomId, i);
});

if (registrationTime) {
window.localStorage.setItem("mx_registration_time", registrationTime);
}
}
}

Expand Down
13 changes: 9 additions & 4 deletions src/MatrixClientPeg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class MatrixClientPegClass implements IMatrixClientPeg {
};

private matrixClient: MatrixClient = null;
private justRegisteredUserId: string;
private justRegisteredUserId: string | null = null;

// the credentials used to init the current client object.
// used if we tear it down & recreate it with a different store
Expand All @@ -136,7 +136,7 @@ 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()));
Expand All @@ -151,9 +151,14 @@ class MatrixClientPegClass implements IMatrixClientPeg {
}

public userRegisteredWithinLastHours(hours: number): boolean {
if (hours <= 0) {
return false;
}

try {
const date = new Date(window.localStorage.getItem("mx_registration_time"));
return ((new Date().getTime() - date.getTime()) / 36e5) <= hours;
const registrationTime = parseInt(window.localStorage.getItem("mx_registration_time"));
const diff = Date.now() - registrationTime;
return (diff / 36e5) <= hours;
} catch (e) {
return false;
}
Expand Down
58 changes: 58 additions & 0 deletions test/MatrixClientPeg-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
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 { advanceDateAndTime, stubClient } from "./test-utils";
import { MatrixClientPeg as peg } from "../src/MatrixClientPeg";

describe("MatrixClientPeg", () => {
afterEach(() => {
localStorage.clear();
advanceDateAndTime(0);
});

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);
advanceDateAndTime(1 * 60 * 60 * 1000);
expect(peg.userRegisteredWithinLastHours(0)).toBe(false);
expect(peg.userRegisteredWithinLastHours(1)).toBe(false);
expect(peg.userRegisteredWithinLastHours(24)).toBe(true);
advanceDateAndTime(24 * 60 * 60 * 1000);
expect(peg.userRegisteredWithinLastHours(0)).toBe(false);
expect(peg.userRegisteredWithinLastHours(1)).toBe(false);
expect(peg.userRegisteredWithinLastHours(24)).toBe(false);
});

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);
advanceDateAndTime(1 * 60 * 60 * 1000);
expect(peg.userRegisteredWithinLastHours(0)).toBe(false);
expect(peg.userRegisteredWithinLastHours(1)).toBe(false);
expect(peg.userRegisteredWithinLastHours(24)).toBe(false);
});
});
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

0 comments on commit 26b771b

Please sign in to comment.