Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.
Merged
78 changes: 78 additions & 0 deletions src/DraftCleaner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Copyright 2024 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 { logger } from "matrix-js-sdk/src/logger";

import { MatrixClientPeg } from "./MatrixClientPeg";
import { EDITOR_STATE_STORAGE_PREFIX } from "./components/views/rooms/SendMessageComposer";

// The key used to persist the the timestamp we last cleaned up drafts
export const DRAFT_LAST_CLEANUP_KEY = "mx_draft_cleanup";
// The period of time we wait between cleaning drafts
export const DRAFT_CLEANUP_PERIOD = 1000 * 60 * 60 * 24 * 30;

/**
* Checks if `DRAFT_CLEANUP_PERIOD` has expired, if so, deletes any stord editor drafts that exist for rooms that are not in the known list.
*/
export function cleanUpDraftsIfRequired(): void {
if (!shouldCleanupDrafts()) {
return;
}
logger.debug(`Cleaning up editor drafts...`);
cleaupDrafts();
try {
localStorage.setItem(DRAFT_LAST_CLEANUP_KEY, String(Date.now()));
} catch (error) {
logger.error("Failed to persist draft cleanup key", error);
}
}

/**
*
* @returns {bool} True if the timestamp has not been persisted or the `DRAFT_CLEANUP_PERIOD` has expired.
*/
function shouldCleanupDrafts(): boolean {
try {
const lastCleanupTimestamp = localStorage.getItem(DRAFT_LAST_CLEANUP_KEY);
if (!lastCleanupTimestamp) {
return true;
}
const parsedTimestamp = Number.parseInt(lastCleanupTimestamp || "", 10);
if (!Number.isInteger(parsedTimestamp)) {
return true;
}
return Date.now() > parsedTimestamp + DRAFT_CLEANUP_PERIOD;
} catch (error) {
return true;
}
}

/**
* Clear all drafts for the CIDER editor if the room does not exist in the known rooms.
*/
function cleaupDrafts(): void {
for (let i = 0; i < localStorage.length; i++) {
const keyName = localStorage.key(i);
if (!keyName?.startsWith(EDITOR_STATE_STORAGE_PREFIX)) continue;
// Remove the prefix and the optional event id suffix to leave the room id
const roomId = keyName.slice(EDITOR_STATE_STORAGE_PREFIX.length).split("_$")[0];
const room = MatrixClientPeg.safeGet().getRoom(roomId);
if (!room) {
logger.debug(`Removing draft for unknown room with key ${keyName}`);
localStorage.removeItem(keyName);
}
}
}
3 changes: 3 additions & 0 deletions src/components/structures/MatrixChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ import { checkSessionLockFree, getSessionLock } from "../../utils/SessionLock";
import { SessionLockStolenView } from "./auth/SessionLockStolenView";
import { ConfirmSessionLockTheftView } from "./auth/ConfirmSessionLockTheftView";
import { LoginSplashView } from "./auth/LoginSplashView";
import { cleanUpDraftsIfRequired } from "../../DraftCleaner";

// legacy export
export { default as Views } from "../../Views";
Expand Down Expand Up @@ -1542,6 +1543,8 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
showNotificationsToast(false);
}

cleanUpDraftsIfRequired();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this point in the code is executed at the point we load the sync accumulator also, rather than just after the first real sync


dis.fire(Action.FocusSendMessageComposer);
this.setState({
ready: true,
Expand Down
5 changes: 4 additions & 1 deletion src/components/views/rooms/SendMessageComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ import { IDiff } from "../../../editor/diff";
import { getBlobSafeMimeType } from "../../../utils/blobs";
import { EMOJI_REGEX } from "../../../HtmlUtils";

// The prefix used when persisting editor drafts to localstorage.
export const EDITOR_STATE_STORAGE_PREFIX = "mx_cider_state_";

/**
* Build the mentions information based on the editor model (and any related events):
*
Expand Down Expand Up @@ -605,7 +608,7 @@ export class SendMessageComposer extends React.Component<ISendMessageComposerPro
}

private get editorStateKey(): string {
let key = `mx_cider_state_${this.props.room.roomId}`;
let key = EDITOR_STATE_STORAGE_PREFIX + this.props.room.roomId;
if (this.props.relation?.rel_type === THREAD_RELATION_TYPE.name) {
key += `_${this.props.relation.event_id}`;
}
Expand Down
32 changes: 32 additions & 0 deletions test/components/structures/MatrixChat-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import { SettingLevel } from "../../../src/settings/SettingLevel";
import { MatrixClientPeg as peg } from "../../../src/MatrixClientPeg";
import DMRoomMap from "../../../src/utils/DMRoomMap";
import { ReleaseAnnouncementStore } from "../../../src/stores/ReleaseAnnouncementStore";
import { DRAFT_LAST_CLEANUP_KEY } from "../../../src/DraftCleaner";

jest.mock("matrix-js-sdk/src/oidc/authorize", () => ({
completeAuthorizationCodeGrant: jest.fn(),
Expand Down Expand Up @@ -598,6 +599,37 @@ describe("<MatrixChat />", () => {
expect(screen.getByText(`Welcome ${userId}`)).toBeInTheDocument();
});

describe("clean up drafts", () => {
const roomId = "!room:server.org";
const unknownRoomId = "!room2:server.org";
const room = new Room(roomId, mockClient, userId);
const timestamp = 2345678901234;
beforeEach(() => {
localStorage.setItem(`mx_cider_state_${unknownRoomId}`, "fake_content");
localStorage.setItem(`mx_cider_state_${roomId}`, "fake_content");
mockClient.getRoom.mockImplementation((id) => [room].find((room) => room.roomId === id) || null);
});
afterEach(() => {
jest.restoreAllMocks();
});
it("should clean up drafts", async () => {
Date.now = jest.fn(() => timestamp);
localStorage.setItem(`mx_cider_state_${roomId}`, "fake_content");
localStorage.setItem(`mx_cider_state_${unknownRoomId}`, "fake_content");
await getComponentAndWaitForReady();
expect(localStorage.getItem(`mx_cider_state_${roomId}`)).not.toBeNull();
expect(localStorage.getItem(`mx_cider_state_${unknownRoomId}`)).toBeNull();
});

it("should not clean up drafts before expiry", async () => {
// Set the last cleanup to the recent past
localStorage.setItem(`mx_cider_state_${unknownRoomId}`, "fake_content");
localStorage.setItem(DRAFT_LAST_CLEANUP_KEY, String(timestamp - 100));
await getComponentAndWaitForReady();
expect(localStorage.getItem(`mx_cider_state_${unknownRoomId}`)).not.toBeNull();
});
});

describe("onAction()", () => {
beforeEach(() => {
jest.spyOn(defaultDispatcher, "dispatch").mockClear();
Expand Down