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

Commit 4f9d176

Browse files
committed
Revert to localStorage based solution + non-inverted logic + test including time advancement
1 parent f110e25 commit 4f9d176

File tree

3 files changed

+29
-35
lines changed

3 files changed

+29
-35
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: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,6 @@ class MatrixClientPegClass implements IMatrixClientPeg {
118118

119119
private matrixClient: MatrixClient = null;
120120
private justRegisteredUserId: string | null = null;
121-
private registrationTime?: number;
122-
private registrationTimeUser?: string;
123121

124122
// the credentials used to init the current client object.
125123
// used if we tear it down & recreate it with a different store
@@ -141,8 +139,7 @@ class MatrixClientPegClass implements IMatrixClientPeg {
141139
public setJustRegisteredUserId(uid: string | null): void {
142140
this.justRegisteredUserId = uid;
143141
if (uid) {
144-
this.registrationTime = Date.now();
145-
this.registrationTimeUser = uid;
142+
window.localStorage.setItem("mx_registration_time", String(new Date().getTime()));
146143
}
147144
}
148145

@@ -154,14 +151,17 @@ class MatrixClientPegClass implements IMatrixClientPeg {
154151
}
155152

156153
public userRegisteredWithinLastHours(hours: number): boolean {
157-
if (
158-
!this.registrationTime ||
159-
hours <= 0 ||
160-
this.registrationTimeUser !== this.matrixClient?.credentials.userId
161-
) {
154+
if (hours <= 0) {
155+
return false;
156+
}
157+
158+
try {
159+
const registrationTime = parseInt(window.localStorage.getItem("mx_registration_time"));
160+
const diff = Date.now() - registrationTime;
161+
return (diff / 36e5) <= hours;
162+
} catch (e) {
162163
return false;
163164
}
164-
return ((Date.now() - this.registrationTime) / 36e5) <= hours;
165165
}
166166

167167
public replaceUsingCreds(creds: IMatrixClientCreds): void {

test/MatrixClientPeg-test.ts

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import { stubClient } from "./test-utils";
17+
import { advanceDateAndTime, stubClient } from "./test-utils";
1818
import { MatrixClientPeg as peg } from "../src/MatrixClientPeg";
1919

2020
describe("MatrixClientPeg", () => {
2121
afterEach(() => {
22-
(peg as any).registrationTime = undefined;
23-
(peg as any).registrationTimeUser = undefined;
22+
localStorage.clear();
23+
advanceDateAndTime(0);
2424
});
2525

2626
it("setJustRegisteredUserId", () => {
@@ -32,6 +32,14 @@ describe("MatrixClientPeg", () => {
3232
expect(peg.userRegisteredWithinLastHours(0)).toBe(false);
3333
expect(peg.userRegisteredWithinLastHours(1)).toBe(true);
3434
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);
3543
});
3644

3745
it("setJustRegisteredUserId(null)", () => {
@@ -42,28 +50,9 @@ describe("MatrixClientPeg", () => {
4250
expect(peg.userRegisteredWithinLastHours(0)).toBe(false);
4351
expect(peg.userRegisteredWithinLastHours(1)).toBe(false);
4452
expect(peg.userRegisteredWithinLastHours(24)).toBe(false);
45-
});
46-
47-
it("multiple users", () => {
48-
stubClient();
49-
(peg as any).matrixClient = peg.get();
50-
peg.setJustRegisteredUserId("@userId:matrix.rog");
51-
expect(peg.get().credentials.userId).toBe("@userId:matrix.rog");
52-
expect(peg.currentUserIsJustRegistered()).toBe(true);
53-
expect(peg.userRegisteredWithinLastHours(0)).toBe(false);
54-
expect(peg.userRegisteredWithinLastHours(1)).toBe(true);
55-
expect(peg.userRegisteredWithinLastHours(24)).toBe(true);
56-
57-
peg.setJustRegisteredUserId("@userId2:matrix.rog");
58-
expect(peg.currentUserIsJustRegistered()).toBe(false);
53+
advanceDateAndTime(1 * 60 * 60 * 1000);
5954
expect(peg.userRegisteredWithinLastHours(0)).toBe(false);
6055
expect(peg.userRegisteredWithinLastHours(1)).toBe(false);
6156
expect(peg.userRegisteredWithinLastHours(24)).toBe(false);
62-
63-
peg.get().credentials.userId = "@userId2:matrix.rog";
64-
expect(peg.currentUserIsJustRegistered()).toBe(true);
65-
expect(peg.userRegisteredWithinLastHours(0)).toBe(false);
66-
expect(peg.userRegisteredWithinLastHours(1)).toBe(true);
67-
expect(peg.userRegisteredWithinLastHours(24)).toBe(true);
6857
});
6958
});

0 commit comments

Comments
 (0)