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

Commit

Permalink
Revert to localStorage based solution + non-inverted logic + test inc…
Browse files Browse the repository at this point in the history
…luding time advancement
  • Loading branch information
hughns committed Apr 13, 2022
1 parent f110e25 commit 4f9d176
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 35 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
20 changes: 10 additions & 10 deletions src/MatrixClientPeg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ class MatrixClientPegClass implements IMatrixClientPeg {

private matrixClient: MatrixClient = null;
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 @@ -141,8 +139,7 @@ class MatrixClientPegClass implements IMatrixClientPeg {
public setJustRegisteredUserId(uid: string | null): void {
this.justRegisteredUserId = uid;
if (uid) {
this.registrationTime = Date.now();
this.registrationTimeUser = uid;
window.localStorage.setItem("mx_registration_time", String(new Date().getTime()));
}
}

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

public userRegisteredWithinLastHours(hours: number): boolean {
if (
!this.registrationTime ||
hours <= 0 ||
this.registrationTimeUser !== this.matrixClient?.credentials.userId
) {
if (hours <= 0) {
return false;
}

try {
const registrationTime = parseInt(window.localStorage.getItem("mx_registration_time"));
const diff = Date.now() - registrationTime;
return (diff / 36e5) <= hours;
} catch (e) {
return false;
}
return ((Date.now() - this.registrationTime) / 36e5) <= hours;
}

public replaceUsingCreds(creds: IMatrixClientCreds): void {
Expand Down
35 changes: 12 additions & 23 deletions test/MatrixClientPeg-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

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

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

it("setJustRegisteredUserId", () => {
Expand All @@ -32,6 +32,14 @@ describe("MatrixClientPeg", () => {
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)", () => {
Expand All @@ -42,28 +50,9 @@ describe("MatrixClientPeg", () => {
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);
advanceDateAndTime(1 * 60 * 60 * 1000);
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);
});
});

0 comments on commit 4f9d176

Please sign in to comment.