From be72db844d6e398e9ab9643c218f6565ff75bec1 Mon Sep 17 00:00:00 2001 From: vishnukvmd Date: Fri, 11 Nov 2022 17:12:05 +0530 Subject: [PATCH] De-duplicate codes before inserting them into the DB --- lib/store/code_store.dart | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/store/code_store.dart b/lib/store/code_store.dart index 8553f9b83..f4c418d5b 100644 --- a/lib/store/code_store.dart +++ b/lib/store/code_store.dart @@ -4,6 +4,7 @@ import 'package:ente_auth/core/event_bus.dart'; import 'package:ente_auth/events/codes_updated_event.dart'; import 'package:ente_auth/models/code.dart'; import 'package:ente_auth/services/authenticator_service.dart'; +import 'package:logging/logging.dart'; class CodeStore { static final CodeStore instance = CodeStore._privateConstructor(); @@ -11,6 +12,7 @@ class CodeStore { CodeStore._privateConstructor(); late AuthenticatorService _authenticatorService; + final _logger = Logger("CodeStore"); Future init() async { _authenticatorService = AuthenticatorService.instance; @@ -30,6 +32,13 @@ class CodeStore { } Future addCode(Code code) async { + final codes = await getAllCodes(); + for (final existingCode in codes) { + if (existingCode == code) { + _logger.info("Found duplicate code, skipping add"); + return; + } + } code.id = await _authenticatorService.addEntry(jsonEncode(code.rawData)); Bus.instance.fire(CodesUpdatedEvent()); }