From 0150a9c314825674c7191e3957746946939eb797 Mon Sep 17 00:00:00 2001 From: Lantt Date: Tue, 26 Apr 2022 11:19:54 +0800 Subject: [PATCH 1/2] fix: remove duplicate profile in linked profile when retore --- .../mask/background/database/persona/web.ts | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/packages/mask/background/database/persona/web.ts b/packages/mask/background/database/persona/web.ts index 36b4678c6964..a0d8bf90e6e0 100644 --- a/packages/mask/background/database/persona/web.ts +++ b/packages/mask/background/database/persona/web.ts @@ -448,18 +448,35 @@ const fuse = new Fuse([] as ProfileRecord[], { */ export async function updateProfileDB( updating: Partial & Pick, - t: ProfileTransaction<'readwrite'>, + t: FullPersonaDBTransaction<'readwrite'>, ): Promise { const old = await t.objectStore('profiles').get(updating.identifier.toText()) if (!old) throw new Error('Updating a non exists record') + if (old.linkedPersona && updating.linkedPersona && old.linkedPersona !== updating.linkedPersona) { + const ide = Identifier.fromString(old.identifier, ProfileIdentifier).unwrap() + const oldLinkedPersona = await queryPersonaByProfileDB(ide, t) + + if (oldLinkedPersona) { + oldLinkedPersona.linkedProfiles.delete(ide) + await updatePersonaDB( + oldLinkedPersona, + { + linkedProfiles: 'replace', + explicitUndefinedField: 'ignore', + }, + t, + ) + } + } + const nextRecord: ProfileRecordDB = profileToDB({ ...profileOutDB(old), ...updating, }) await t.objectStore('profiles').put(nextRecord) } -export async function createOrUpdateProfileDB(rec: ProfileRecord, t: ProfileTransaction<'readwrite'>) { +export async function createOrUpdateProfileDB(rec: ProfileRecord, t: FullPersonaDBTransaction<'readwrite'>) { if (await queryProfileDB(rec.identifier, t)) return updateProfileDB(rec, t) else return createProfileDB(rec, t) } From 90eb80e7bbf2dec084841adffee8e64960285986 Mon Sep 17 00:00:00 2001 From: Lantt Date: Tue, 26 Apr 2022 17:34:13 +0800 Subject: [PATCH 2/2] fix: remove duplicate profile in linked profile when retore --- .../mask/background/database/persona/web.ts | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/mask/background/database/persona/web.ts b/packages/mask/background/database/persona/web.ts index a0d8bf90e6e0..93a11f2c566a 100644 --- a/packages/mask/background/database/persona/web.ts +++ b/packages/mask/background/database/persona/web.ts @@ -454,11 +454,11 @@ export async function updateProfileDB( if (!old) throw new Error('Updating a non exists record') if (old.linkedPersona && updating.linkedPersona && old.linkedPersona !== updating.linkedPersona) { - const ide = Identifier.fromString(old.identifier, ProfileIdentifier).unwrap() - const oldLinkedPersona = await queryPersonaByProfileDB(ide, t) + const oldIdentifier = Identifier.fromString(old.identifier, ProfileIdentifier).unwrap() + const oldLinkedPersona = await queryPersonaByProfileDB(oldIdentifier, t) if (oldLinkedPersona) { - oldLinkedPersona.linkedProfiles.delete(ide) + oldLinkedPersona.linkedProfiles.delete(oldIdentifier) await updatePersonaDB( oldLinkedPersona, { @@ -470,6 +470,21 @@ export async function updateProfileDB( } } + if (updating.linkedPersona && old.linkedPersona !== updating.linkedPersona) { + const linkedPersona = await queryPersonaDB(updating.linkedPersona, t) + if (linkedPersona) { + linkedPersona.linkedProfiles.set(updating.identifier, { connectionConfirmState: 'confirmed' }) + await updatePersonaDB( + linkedPersona, + { + linkedProfiles: 'replace', + explicitUndefinedField: 'ignore', + }, + t, + ) + } + } + const nextRecord: ProfileRecordDB = profileToDB({ ...profileOutDB(old), ...updating,