Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Preserve Ensu migration recovery paths

Manav Rathie325b939e01b
diff --git a/rust/apps/ensu/src-tauri/src/commands/chat_db.rs b/rust/apps/ensu/src-tauri/src/commands/chat_db.rs
index a07f44e86d..060ee4d606 100644
--- a/rust/apps/ensu/src-tauri/src/commands/chat_db.rs
+++ b/rust/apps/ensu/src-tauri/src/commands/chat_db.rs
@@ -393,7 +393,7 @@ pub async fn chat_db_open(
state: State<'_, ChatDbState>,
app: AppHandle,
input: ChatDbOpenInput,
-) -> Result<(), ApiError> {
+) -> Result<bool, ApiError> {
let inner = state.inner.clone();
async_runtime::spawn_blocking(move || {
let root = app_data_dir(&app)?;
@@ -405,8 +405,10 @@ pub async fn chat_db_open(
.iter()
.filter_map(|value| crypto::decode_b64(value).ok())
.collect::<Vec<_>>();
- chat_db_migration::prepare(&root, &path, &attachments, key.clone(), recovery_keys)?;
- open_chat_db(&inner, path, key)
+ let migrated =
+ chat_db_migration::prepare(&root, &path, &attachments, key.clone(), recovery_keys)?;
+ open_chat_db(&inner, path, key)?;
+ Ok(migrated)
})
.await
.map_err(|_| chat_db_thread_error())?
diff --git a/rust/apps/ensu/src-tauri/src/commands/chat_db_migration.rs b/rust/apps/ensu/src-tauri/src/commands/chat_db_migration.rs
index 6ed763f2ad..8bee852573 100644
--- a/rust/apps/ensu/src-tauri/src/commands/chat_db_migration.rs
+++ b/rust/apps/ensu/src-tauri/src/commands/chat_db_migration.rs
@@ -29,23 +29,27 @@ pub(super) fn prepare(
target_attachments: &Path,
target_key: Vec<u8>,
recovery_keys: Vec<Vec<u8>>,
-) -> Result<(), ApiError> {
+) -> Result<bool, ApiError> {
let source_db = root.join(SOURCE_DB);
if exists(&source_db)? {
let Ok(source_key) = select_source_key(&source_db, &recovery_keys) else {
- return Ok(());
+ return Ok(false);
};
- import(
+ if import(
&source_db,
&root.join(SOURCE_ATTACHMENTS),
target_db,
target_attachments,
source_key,
target_key,
- )?;
+ )
+ .is_err()
+ {
+ return Ok(false);
+ }
}
let _ = cleanup(root);
- Ok(())
+ Ok(true)
}
fn select_source_key(path: &Path, keys: &[Vec<u8>]) -> Result<Vec<u8>, ApiError> {
@@ -279,7 +283,7 @@ mod tests {
let target_session = target.create_session("Current chat").unwrap();
drop(target);
- prepare(
+ let migrated = prepare(
&root,
&target_db,
&target_attachments,
@@ -287,6 +291,7 @@ mod tests {
vec![vec![12; 32]],
)
.unwrap();
+ assert!(!migrated);
let target = ChatDb::open_sqlite_with_defaults(target_db, target_key).unwrap();
assert_eq!(
diff --git a/web/apps/ensu/src/pages/chat.tsx b/web/apps/ensu/src/pages/chat.tsx
index b654f92475..7aef838617 100644
--- a/web/apps/ensu/src/pages/chat.tsx
+++ b/web/apps/ensu/src/pages/chat.tsx
@@ -768,9 +768,13 @@ const Page: React.FC = () => {
setChatKey(await getOrCreateLocalChatKey());
} catch (error) {
log.error("Failed to initialize local chat key", error);
- onGenericError(error);
+ showMiniDialog({
+ title: "Encryption error",
+ message:
+ "We could not initialize encryption. Please refresh the page.",
+ });
}
- }, [onGenericError]);
+ }, [showMiniDialog]);
useEffect(() => {
chatKeyInitCancelledRef.current = false;
diff --git a/web/apps/ensu/src/services/chat/compatibility.ts b/web/apps/ensu/src/services/chat/compatibility.ts
index 6aac8faa27..4b794d9dec 100644
--- a/web/apps/ensu/src/services/chat/compatibility.ts
+++ b/web/apps/ensu/src/services/chat/compatibility.ts
@@ -229,20 +229,30 @@ const hasLocalStorageStore = () => !!localStorage.getItem(LOCAL_STORAGE_STORE);
const writeBrowserStore = async (source: BrowserStore) => {
const db = await chatDb();
+ const [sessions, messages, attachmentKeys] = await Promise.all([
+ db.getAll("sessions"),
+ db.getAll("messages"),
+ db.getAllKeys("attachmentBytes"),
+ ]);
+ const sessionIDs = new Set(sessions.map(({ sessionUuid }) => sessionUuid));
+ const messageIDs = new Set(messages.map(({ messageUuid }) => messageUuid));
+ const attachmentIDs = new Set(attachmentKeys);
const tx = db.transaction(
["sessions", "messages", "attachmentBytes"],
"readwrite",
);
await Promise.all([
- ...source.sessions.map((session) =>
- tx.objectStore("sessions").put(session),
- ),
- ...source.messages.map((message) =>
- tx.objectStore("messages").put(message),
- ),
- ...source.attachmentBytes.map((attachment) =>
- tx.objectStore("attachmentBytes").put(attachment),
- ),
+ ...source.sessions
+ .filter(({ sessionUuid }) => !sessionIDs.has(sessionUuid))
+ .map((session) => tx.objectStore("sessions").put(session)),
+ ...source.messages
+ .filter(({ messageUuid }) => !messageIDs.has(messageUuid))
+ .map((message) => tx.objectStore("messages").put(message)),
+ ...source.attachmentBytes
+ .filter(({ id }) => !attachmentIDs.has(id))
+ .map((attachment) =>
+ tx.objectStore("attachmentBytes").put(attachment),
+ ),
tx.done,
]);
};
@@ -283,7 +293,7 @@ const readOldIndexedDb = async () => {
created = true;
},
});
- if (created) {
+ if (created || !source.objectStoreNames.contains("sessions")) {
source.close();
await deleteDB(OLD_INDEXED_DB);
return undefined;
@@ -491,10 +501,11 @@ export const openChatStoreWithCompatibility = async (chatKey: string) => {
chatKey,
...(migrationPending ? _migrationSourceKeys : []),
]);
- await invokeChat("chat_db_open", {
+ const migrated = await invokeChat<boolean>("chat_db_open", {
input: { keyB64: chatKey, recoveryKeysB64: sourceKeys },
});
if (migrationPending) {
+ if (!migrated) return;
if (hasLocalStorageStore()) {
try {
await importLocalStorageToNative(sourceKeys);
@@ -510,7 +521,7 @@ export const openChatStoreWithCompatibility = async (chatKey: string) => {
await finalizeLocalChatMigration();
}
} else {
- await removeCurrentBrowserTombstones();
await importOldBrowserStores();
+ await removeCurrentBrowserTombstones();
}
};
diff --git a/web/apps/ensu/src/services/chat/store.ts b/web/apps/ensu/src/services/chat/store.ts
index e9c1f57672..a0d2faf4b6 100644
--- a/web/apps/ensu/src/services/chat/store.ts
+++ b/web/apps/ensu/src/services/chat/store.ts
@@ -85,6 +85,7 @@ interface ChatDbLike {
key: string,
) => Promise<ChatDbSchema[K] | undefined>;
getAll: <K extends ChatStoreName>(name: K) => Promise<ChatDbSchema[K][]>;
+ getAllKeys: (name: ChatStoreName) => Promise<string[]>;
put: <K extends ChatStoreName>(
name: K,
value: ChatDbSchema[K],
@@ -192,6 +193,7 @@ const createIndexedDbChatDb = (
const entries = (await db.getAll(name)) as ChatDbSchema[typeof name][];
return entries.map((entry) => cloneStoreEntry(name, entry));
},
+ getAllKeys: async (name) => db.getAllKeys(name),
put: async (name, value) => {
await db.put(name, cloneStoreEntry(name, value));
},