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

Commit 26b771b

Browse files
authored
Don't show the prompt to enable desktop notifications immediately after 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
1 parent d151365 commit 26b771b

File tree

4 files changed

+74
-14
lines changed

4 files changed

+74
-14
lines changed

src/Lifecycle.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -875,8 +875,9 @@ async function clearStorage(opts?: { deleteEverything?: boolean }): Promise<void
875875
Analytics.disable();
876876

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

881882
window.localStorage.clear();
882883

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

889-
// now restore those invites
890+
// now restore those invites and registration time
890891
if (!opts?.deleteEverything) {
891892
pendingInvites.forEach(i => {
892893
const roomId = i.roomId;
893894
delete i.roomId; // delete to avoid confusing the store
894895
ThreepidInviteStore.instance.storeInvite(roomId, i);
895896
});
897+
898+
if (registrationTime) {
899+
window.localStorage.setItem("mx_registration_time", registrationTime);
900+
}
896901
}
897902
}
898903

src/MatrixClientPeg.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class MatrixClientPegClass implements IMatrixClientPeg {
117117
};
118118

119119
private matrixClient: MatrixClient = null;
120-
private justRegisteredUserId: string;
120+
private justRegisteredUserId: string | null = null;
121121

122122
// the credentials used to init the current client object.
123123
// used if we tear it down & recreate it with a different store
@@ -136,7 +136,7 @@ class MatrixClientPegClass implements IMatrixClientPeg {
136136
MatrixActionCreators.stop();
137137
}
138138

139-
public setJustRegisteredUserId(uid: string): void {
139+
public setJustRegisteredUserId(uid: string | null): void {
140140
this.justRegisteredUserId = uid;
141141
if (uid) {
142142
window.localStorage.setItem("mx_registration_time", String(new Date().getTime()));
@@ -151,9 +151,14 @@ class MatrixClientPegClass implements IMatrixClientPeg {
151151
}
152152

153153
public userRegisteredWithinLastHours(hours: number): boolean {
154+
if (hours <= 0) {
155+
return false;
156+
}
157+
154158
try {
155-
const date = new Date(window.localStorage.getItem("mx_registration_time"));
156-
return ((new Date().getTime() - date.getTime()) / 36e5) <= hours;
159+
const registrationTime = parseInt(window.localStorage.getItem("mx_registration_time"));
160+
const diff = Date.now() - registrationTime;
161+
return (diff / 36e5) <= hours;
157162
} catch (e) {
158163
return false;
159164
}

test/MatrixClientPeg-test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { advanceDateAndTime, stubClient } from "./test-utils";
18+
import { MatrixClientPeg as peg } from "../src/MatrixClientPeg";
19+
20+
describe("MatrixClientPeg", () => {
21+
afterEach(() => {
22+
localStorage.clear();
23+
advanceDateAndTime(0);
24+
});
25+
26+
it("setJustRegisteredUserId", () => {
27+
stubClient();
28+
(peg as any).matrixClient = peg.get();
29+
peg.setJustRegisteredUserId("@userId:matrix.rog");
30+
expect(peg.get().credentials.userId).toBe("@userId:matrix.rog");
31+
expect(peg.currentUserIsJustRegistered()).toBe(true);
32+
expect(peg.userRegisteredWithinLastHours(0)).toBe(false);
33+
expect(peg.userRegisteredWithinLastHours(1)).toBe(true);
34+
expect(peg.userRegisteredWithinLastHours(24)).toBe(true);
35+
advanceDateAndTime(1 * 60 * 60 * 1000);
36+
expect(peg.userRegisteredWithinLastHours(0)).toBe(false);
37+
expect(peg.userRegisteredWithinLastHours(1)).toBe(false);
38+
expect(peg.userRegisteredWithinLastHours(24)).toBe(true);
39+
advanceDateAndTime(24 * 60 * 60 * 1000);
40+
expect(peg.userRegisteredWithinLastHours(0)).toBe(false);
41+
expect(peg.userRegisteredWithinLastHours(1)).toBe(false);
42+
expect(peg.userRegisteredWithinLastHours(24)).toBe(false);
43+
});
44+
45+
it("setJustRegisteredUserId(null)", () => {
46+
stubClient();
47+
(peg as any).matrixClient = peg.get();
48+
peg.setJustRegisteredUserId(null);
49+
expect(peg.currentUserIsJustRegistered()).toBe(false);
50+
expect(peg.userRegisteredWithinLastHours(0)).toBe(false);
51+
expect(peg.userRegisteredWithinLastHours(1)).toBe(false);
52+
expect(peg.userRegisteredWithinLastHours(24)).toBe(false);
53+
advanceDateAndTime(1 * 60 * 60 * 1000);
54+
expect(peg.userRegisteredWithinLastHours(0)).toBe(false);
55+
expect(peg.userRegisteredWithinLastHours(1)).toBe(false);
56+
expect(peg.userRegisteredWithinLastHours(24)).toBe(false);
57+
});
58+
});

test/end-to-end-tests/src/scenarios/toast.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ export async function toastScenarios(alice: ElementSession, bob: ElementSession)
2121
console.log(" checking and clearing toasts:");
2222

2323
alice.log.startGroup(`clears toasts`);
24-
alice.log.step(`reject desktop notifications toast`);
25-
await rejectToast(alice, "Notifications");
26-
alice.log.done();
27-
2824
alice.log.step(`accepts analytics toast`);
2925
await acceptToast(alice, "Help improve Element");
3026
alice.log.done();
@@ -35,10 +31,6 @@ export async function toastScenarios(alice: ElementSession, bob: ElementSession)
3531
alice.log.endGroup();
3632

3733
bob.log.startGroup(`clears toasts`);
38-
bob.log.step(`reject desktop notifications toast`);
39-
await rejectToast(bob, "Notifications");
40-
bob.log.done();
41-
4234
bob.log.step(`reject analytics toast`);
4335
await rejectToast(bob, "Help improve Element");
4436
bob.log.done();

0 commit comments

Comments
 (0)