This repository was archived by the owner on Sep 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 811
Clean up editor drafts for unknown rooms #12850
Merged
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a928bb9
Clean up editor drafts for unknown rooms and add tests.
langleyd 7fa877e
Merge branch 'develop' into langleyd/cleanup_drafts
langleyd e12f5d0
lint
langleyd 3b97224
Merge branch 'langleyd/cleanup_drafts' of https://github.com/matrix-o…
langleyd eb605f0
Call cleanUpDraftsIfRequired when we know a live update has completed.
langleyd e8a3b34
Merge branch 'develop' into langleyd/cleanup_drafts
langleyd 71d8517
Fix test for new call site of draft cleaning
langleyd 564103d
Merge branch 'develop' into langleyd/cleanup_drafts
langleyd 549c67f
fix test
langleyd 918c40d
Merge branch 'langleyd/cleanup_drafts' of https://github.com/matrix-o…
langleyd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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