diff --git a/lib/storage/constants.dart b/lib/storage/constants.dart index 91f21bf44..2c0cf10c7 100644 --- a/lib/storage/constants.dart +++ b/lib/storage/constants.dart @@ -1,5 +1,12 @@ class StorageKeys { - static const String RECEIPTS = 'RECEIPTS'; - static const String REDACTIONS = 'REDACTIONS'; + static const String AUTH = 'auth'; static const String ROOMS = 'rooms'; + static const String USERS = 'users'; + static const String MEDIA = 'MEDIA'; + static const String CRYPTO = 'crypto'; + static const String EVENTS = 'events'; + static const String MESSAGES = 'messages'; + static const String RECEIPTS = 'receipts'; + static const String REACTIONS = 'reactions'; + static const String REDACTIONS = 'redactions'; } diff --git a/lib/store/auth/storage.dart b/lib/store/auth/storage.dart index 27d47d423..c52eb6c9d 100644 --- a/lib/store/auth/storage.dart +++ b/lib/store/auth/storage.dart @@ -2,18 +2,17 @@ import 'dart:convert'; import 'package:sembast/sembast.dart'; import 'package:syphon/global/print.dart'; +import 'package:syphon/storage/constants.dart'; import 'package:syphon/store/auth/state.dart'; -const String AUTH = 'auth'; - Future saveAuth( AuthStore authStore, { Database storage, }) async { - final store = StoreRef(AUTH); + final store = StoreRef(StorageKeys.AUTH); return await storage.transaction((txn) async { - final record = store.record(AUTH); + final record = store.record(StorageKeys.AUTH); await record.put(txn, json.encode(authStore)); }); } @@ -26,9 +25,9 @@ Future saveAuth( */ Future loadAuth({Database storage}) async { try { - final store = StoreRef(AUTH); + final store = StoreRef(StorageKeys.AUTH); - final auth = await store.record(AUTH).get(storage); + final auth = await store.record(StorageKeys.AUTH).get(storage); if (auth == null) { return null; diff --git a/lib/store/crypto/storage.dart b/lib/store/crypto/storage.dart index fb9d8a97f..71954ec6e 100644 --- a/lib/store/crypto/storage.dart +++ b/lib/store/crypto/storage.dart @@ -3,10 +3,9 @@ import 'dart:convert'; import 'package:redux/redux.dart'; import 'package:sembast/sembast.dart'; import 'package:syphon/global/print.dart'; +import 'package:syphon/storage/constants.dart'; import 'package:syphon/store/crypto/state.dart'; -const String CRYPTO = 'crypto'; - /** * Save Crypto Store * @@ -17,10 +16,10 @@ Future saveCrypto( CryptoStore cryptoStore, { Database storage, }) async { - final store = StoreRef(CRYPTO); + final store = StoreRef(StorageKeys.CRYPTO); return await storage.transaction((txn) async { - final record = store.record(CRYPTO); + final record = store.record(StorageKeys.CRYPTO); await record.put(txn, json.encode(cryptoStore)); }); } @@ -33,9 +32,9 @@ Future saveCrypto( */ Future loadCrypto({Database storage}) async { try { - final store = StoreRef(CRYPTO); + final store = StoreRef(StorageKeys.CRYPTO); - final crypto = await store.record(CRYPTO).get(storage); + final crypto = await store.record(StorageKeys.CRYPTO).get(storage); if (crypto == null) { return null; diff --git a/lib/store/events/receipts/storage.dart b/lib/store/events/receipts/storage.dart index cd696e60c..a10215090 100644 --- a/lib/store/events/receipts/storage.dart +++ b/lib/store/events/receipts/storage.dart @@ -56,35 +56,3 @@ Future> loadReceipts( return Map(); } } - -/// -/// Load Receipts -/// -/// Iterates through -/// -Future>> loadReceiptsOLD( - List messageIds, { - Database storage, -}) async { - try { - final store = StoreRef(StorageKeys.RECEIPTS); - - final receipts = Map>(); - - final flatReceipts = await store.find(storage); - - for (RecordSnapshot record in flatReceipts) { - final testing = await json.decode(record.value); - final mapped = Map.from(testing); - final Map converted = mapped.map( - (key, value) => MapEntry(key, ReadReceipt.fromJson(value)), - ); - receipts[record.key] = converted; - } - - return receipts; - } catch (error) { - printError(error); - return null; - } -} diff --git a/lib/store/events/storage.dart b/lib/store/events/storage.dart index 558a9f38f..b98d3e4fe 100644 --- a/lib/store/events/storage.dart +++ b/lib/store/events/storage.dart @@ -3,23 +3,18 @@ import 'dart:convert'; import 'package:sembast/sembast.dart'; import 'package:syphon/global/algos.dart'; import 'package:syphon/global/print.dart'; +import 'package:syphon/storage/constants.dart'; import 'package:syphon/store/events/ephemeral/m.read/model.dart'; import 'package:syphon/store/events/model.dart'; import 'package:syphon/store/events/messages/model.dart'; import 'package:syphon/store/events/reactions/model.dart'; import 'package:syphon/store/events/redaction/model.dart'; -const String EVENTS = 'events'; -const String MESSAGES = 'messages'; -const String RECEIPTS = 'receipts'; -const String REACTIONS = 'reactions'; -const String REDACTIONS = 'redactions'; - Future saveEvents( List events, { Database storage, }) async { - final store = StoreRef(EVENTS); + final store = StoreRef(StorageKeys.EVENTS); return await storage.transaction((txn) async { for (Event event in events) { @@ -34,8 +29,8 @@ Future deleteEvents( Database storage, }) async { final stores = [ - StoreRef(MESSAGES), - StoreRef(REACTIONS), + StoreRef(StorageKeys.MESSAGES), + StoreRef(StorageKeys.REACTIONS), ]; return await Future.wait(stores.map((store) async { @@ -59,7 +54,7 @@ Future saveRedactions( Database storage, }) async { try { - final store = StoreRef(REDACTIONS); + final store = StoreRef(StorageKeys.REDACTIONS); return await storage.transaction((txn) async { for (Redaction redaction in redactions) { @@ -83,7 +78,7 @@ Future saveRedactions( Future> loadRedactions({ Database storage, }) async { - final store = StoreRef(REDACTIONS); + final store = StoreRef(StorageKeys.REDACTIONS); final redactions = Map(); @@ -111,7 +106,7 @@ Future saveReactions( Database storage, }) async { try { - final store = StoreRef(REACTIONS); + final store = StoreRef(StorageKeys.REACTIONS); return await storage.transaction((txn) async { for (Reaction reaction in reactions) { @@ -158,7 +153,7 @@ Future>> loadReactions( Database storage, }) async { try { - final store = StoreRef(REACTIONS); + final store = StoreRef(StorageKeys.REACTIONS); final reactionsMap = Map>(); final reactionsRecords = await store.records(messageIds).getSnapshots(storage); @@ -184,7 +179,7 @@ Future saveMessages( List messages, { Database storage, }) async { - final store = StoreRef(MESSAGES); + final store = StoreRef(StorageKeys.MESSAGES); return await storage.transaction((txn) async { for (Message message in messages) { @@ -195,7 +190,7 @@ Future saveMessages( } Future loadMessage(String eventId, {Database storage}) async { - final store = StoreRef(MESSAGES); + final store = StoreRef(StorageKeys.MESSAGES); final message = await store.record(eventId).get(storage); @@ -217,7 +212,7 @@ Future> loadMessages( final List messages = []; try { - final store = StoreRef(MESSAGES); + final store = StoreRef(StorageKeys.MESSAGES); // TODO: properly paginate through cold storage messages instead of loading all final messageIds = eventIds ?? []; //.skip(offset).take(limit).toList(); diff --git a/lib/store/media/storage.dart b/lib/store/media/storage.dart index 32a47ba5e..a06deeaf1 100644 --- a/lib/store/media/storage.dart +++ b/lib/store/media/storage.dart @@ -4,14 +4,13 @@ import 'dart:typed_data'; import 'package:flutter/foundation.dart'; import 'package:sembast/sembast.dart'; import 'package:syphon/global/print.dart'; - -const String MEDIA = 'MEDIA'; +import 'package:syphon/storage/constants.dart'; Future checkMedia( String mxcUri, { Database storage, }) async { - final store = StoreRef(MEDIA); + final store = StoreRef(StorageKeys.MEDIA); return await store.record(mxcUri).exists(storage); } @@ -21,7 +20,7 @@ Future saveMedia( Uint8List data, { Database storage, }) async { - final store = StoreRef(MEDIA); + final store = StoreRef(StorageKeys.MEDIA); return await storage.transaction((txn) async { final record = store.record(mxcUri); @@ -39,7 +38,7 @@ Future loadMedia({ Database storage, }) async { try { - final store = StoreRef(MEDIA); + final store = StoreRef(StorageKeys.MEDIA); final mediaData = await store.record(mxcUri).get(storage); @@ -64,7 +63,7 @@ Future> loadMediaAll({ }) async { try { final Map media = {}; - final store = StoreRef(MEDIA); + final store = StoreRef(StorageKeys.MEDIA); final mediaDataAll = await store.find(storage); diff --git a/lib/store/user/storage.dart b/lib/store/user/storage.dart index a63725630..d7518338f 100644 --- a/lib/store/user/storage.dart +++ b/lib/store/user/storage.dart @@ -2,6 +2,7 @@ import 'dart:convert'; import 'package:sembast/sembast.dart'; import 'package:syphon/global/print.dart'; +import 'package:syphon/storage/constants.dart'; import 'package:syphon/store/user/model.dart'; Future saveUsers( @@ -9,7 +10,7 @@ Future saveUsers( Database cache, Database storage, }) async { - final store = StoreRef('users'); + final store = StoreRef(StorageKeys.USERS); return await storage.transaction((txn) async { for (User user in users.values) { @@ -33,61 +34,7 @@ Future> loadUsers({ final Map users = {}; try { - final store = StoreRef('users'); - final count = await store.count(storage); - - final finder = Finder( - limit: page, - offset: offset, - ); - - final usersPaginated = await store.find( - storage, - finder: finder, - ); - - if (usersPaginated.isEmpty) { - return users; - } - - for (RecordSnapshot record in usersPaginated) { - users[record.key] = User.fromJson(json.decode(record.value)); - } - - if (offset < count) { - users.addAll(await loadUsers( - offset: offset + page, - storage: storage, - )); - } - - if (users.isEmpty) { - return null; - } - - printInfo('[users] loaded ${users.length}'); - return users; - } catch (error) { - printError(error.toString(), title: 'loadUsers'); - return null; - } -} - -/** - * Load Users (Cold Storage) - * - * Example of useful recursion - */ -Future> loadUsersOld({ - Database cache, - Database storage, - int offset = 0, - int page = 5000, -}) async { - final Map users = {}; - - try { - final store = StoreRef('users'); + final store = StoreRef(StorageKeys.USERS); final count = await store.count(storage); final finder = Finder(