Use TupleKey(ObjectKey-AccountId-UserName) to store data to hive
(cherry picked from commit e0ace535d5f3af71eb13598d92524caf2c6d9bbf)
This commit is contained in:
@@ -7,6 +7,7 @@ import 'package:jmap_dart_client/jmap/core/session/session.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/sort/comparator.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/state.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/unsigned_int.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/user_name.dart';
|
||||
import 'package:jmap_dart_client/jmap/mail/email/email.dart';
|
||||
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
|
||||
import 'package:model/email/presentation_email.dart';
|
||||
@@ -36,9 +37,18 @@ abstract class ThreadDataSource {
|
||||
}
|
||||
);
|
||||
|
||||
Future<List<Email>> getAllEmailCache(AccountId accountId, {MailboxId? inMailboxId, Set<Comparator>? sort, FilterMessageOption? filterOption, UnsignedInt? limit});
|
||||
Future<List<Email>> getAllEmailCache(
|
||||
AccountId accountId,
|
||||
UserName userName,
|
||||
{
|
||||
MailboxId? inMailboxId,
|
||||
Set<Comparator>? sort,
|
||||
FilterMessageOption? filterOption,
|
||||
UnsignedInt? limit
|
||||
}
|
||||
);
|
||||
|
||||
Future<void> update(AccountId accountId, {List<Email>? updated, List<Email>? created, List<EmailId>? destroyed});
|
||||
Future<void> update(AccountId accountId, UserName userName, {List<Email>? updated, List<Email>? created, List<EmailId>? destroyed});
|
||||
|
||||
Future<List<EmailId>> emptyTrashFolder(
|
||||
Session session,
|
||||
|
||||
@@ -7,6 +7,7 @@ import 'package:jmap_dart_client/jmap/core/session/session.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/sort/comparator.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/state.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/unsigned_int.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/user_name.dart';
|
||||
import 'package:jmap_dart_client/jmap/mail/email/email.dart';
|
||||
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
|
||||
import 'package:model/email/presentation_email.dart';
|
||||
@@ -53,7 +54,8 @@ class LocalThreadDataSourceImpl extends ThreadDataSource {
|
||||
|
||||
@override
|
||||
Future<List<Email>> getAllEmailCache(
|
||||
AccountId accountId, {
|
||||
AccountId accountId,
|
||||
UserName userName, {
|
||||
MailboxId? inMailboxId,
|
||||
Set<Comparator>? sort,
|
||||
FilterMessageOption? filterOption,
|
||||
@@ -62,6 +64,7 @@ class LocalThreadDataSourceImpl extends ThreadDataSource {
|
||||
return Future.sync(() async {
|
||||
return await _emailCacheManager.getAllEmail(
|
||||
accountId,
|
||||
userName,
|
||||
inMailboxId: inMailboxId,
|
||||
sort: sort,
|
||||
filterOption: filterOption ?? FilterMessageOption.all,
|
||||
@@ -71,7 +74,8 @@ class LocalThreadDataSourceImpl extends ThreadDataSource {
|
||||
|
||||
@override
|
||||
Future<void> update(
|
||||
AccountId accountId, {
|
||||
AccountId accountId,
|
||||
UserName userName, {
|
||||
List<Email>? updated,
|
||||
List<Email>? created,
|
||||
List<EmailId>? destroyed
|
||||
@@ -79,6 +83,7 @@ class LocalThreadDataSourceImpl extends ThreadDataSource {
|
||||
return Future.sync(() async {
|
||||
return await _emailCacheManager.update(
|
||||
accountId,
|
||||
userName,
|
||||
updated: updated,
|
||||
created: created,
|
||||
destroyed: destroyed);
|
||||
|
||||
@@ -7,6 +7,7 @@ import 'package:jmap_dart_client/jmap/core/session/session.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/sort/comparator.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/state.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/unsigned_int.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/user_name.dart';
|
||||
import 'package:jmap_dart_client/jmap/mail/email/email.dart';
|
||||
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
|
||||
import 'package:model/email/presentation_email.dart';
|
||||
@@ -74,12 +75,12 @@ class ThreadDataSourceImpl extends ThreadDataSource {
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<Email>> getAllEmailCache(AccountId accountId, {MailboxId? inMailboxId, Set<Comparator>? sort, FilterMessageOption? filterOption, UnsignedInt? limit}) {
|
||||
Future<List<Email>> getAllEmailCache(AccountId accountId, UserName userName, {MailboxId? inMailboxId, Set<Comparator>? sort, FilterMessageOption? filterOption, UnsignedInt? limit}) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> update(AccountId accountId, {List<Email>? updated, List<Email>? created, List<EmailId>? destroyed}) {
|
||||
Future<void> update(AccountId accountId, UserName userName, {List<Email>? updated, List<Email>? created, List<EmailId>? destroyed}) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
|
||||
import 'package:jmap_dart_client/jmap/account_id.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/user_name.dart';
|
||||
import 'package:jmap_dart_client/jmap/mail/email/email.dart';
|
||||
import 'package:model/extensions/account_id_extensions.dart';
|
||||
import 'package:model/extensions/email_id_extensions.dart';
|
||||
@@ -8,10 +9,10 @@ import 'package:tmail_ui_user/features/thread/data/extensions/email_extension.da
|
||||
import 'package:tmail_ui_user/features/thread/data/model/email_cache.dart';
|
||||
|
||||
extension ListEmailExtension on List<Email> {
|
||||
Map<String, EmailCache> toMapCache(AccountId accountId) {
|
||||
Map<String, EmailCache> toMapCache(AccountId accountId, UserName userName) {
|
||||
return {
|
||||
for (var email in this)
|
||||
TupleKey(email.id!.asString, accountId.asString).toString() : email.toEmailCache()
|
||||
TupleKey(email.id!.asString, accountId.asString, userName.value).encodeKey : email.toEmailCache()
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,12 @@
|
||||
|
||||
import 'package:jmap_dart_client/jmap/account_id.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/user_name.dart';
|
||||
import 'package:jmap_dart_client/jmap/mail/email/email.dart';
|
||||
import 'package:model/extensions/account_id_extensions.dart';
|
||||
import 'package:model/extensions/email_id_extensions.dart';
|
||||
import 'package:tmail_ui_user/features/caching/utils/cache_utils.dart';
|
||||
|
||||
extension ListEmailIdExtension on List<EmailId> {
|
||||
List<String> toCacheKeyList(AccountId accountId) => map((id) => TupleKey(id.asString, accountId.asString).toString()).toList();
|
||||
List<String> toCacheKeyList(AccountId accountId, UserName userName) =>
|
||||
map((id) => TupleKey(id.asString, accountId.asString, userName.value).encodeKey).toList();
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import 'package:jmap_dart_client/jmap/account_id.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/unsigned_int.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/user_name.dart';
|
||||
import 'package:model/model.dart';
|
||||
import 'package:jmap_dart_client/jmap/mail/email/email.dart';
|
||||
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
|
||||
@@ -20,13 +21,14 @@ class EmailCacheManager {
|
||||
EmailCacheManager(this._emailCacheClient);
|
||||
|
||||
Future<List<Email>> getAllEmail(
|
||||
AccountId accountId, {
|
||||
AccountId accountId,
|
||||
UserName userName, {
|
||||
MailboxId? inMailboxId,
|
||||
Set<Comparator>? sort,
|
||||
UnsignedInt? limit,
|
||||
FilterMessageOption filterOption = FilterMessageOption.all
|
||||
}) async {
|
||||
final emailCacheList = await _emailCacheClient.getListByCollectionId(accountId.asString);
|
||||
final emailCacheList = await _emailCacheClient.getListByTupleKey(accountId.asString, userName.value);
|
||||
final emailList = emailCacheList
|
||||
.toEmailList()
|
||||
.where((email) => _filterEmailByMailbox(email, filterOption, inMailboxId))
|
||||
@@ -53,16 +55,17 @@ class EmailCacheManager {
|
||||
}
|
||||
|
||||
Future<void> update(
|
||||
AccountId accountId, {
|
||||
AccountId accountId,
|
||||
UserName userName, {
|
||||
List<Email>? updated,
|
||||
List<Email>? created,
|
||||
List<EmailId>? destroyed
|
||||
}) async {
|
||||
final emailCacheExist = await _emailCacheClient.isExistTable();
|
||||
if (emailCacheExist) {
|
||||
final updatedCacheEmails = updated?.toMapCache(accountId) ?? {};
|
||||
final createdCacheEmails = created?.toMapCache(accountId) ?? {};
|
||||
final destroyedCacheEmails = destroyed?.toCacheKeyList(accountId) ?? [];
|
||||
final updatedCacheEmails = updated?.toMapCache(accountId, userName) ?? {};
|
||||
final createdCacheEmails = created?.toMapCache(accountId, userName) ?? {};
|
||||
final destroyedCacheEmails = destroyed?.toCacheKeyList(accountId, userName) ?? [];
|
||||
|
||||
await Future.wait([
|
||||
_emailCacheClient.updateMultipleItem(updatedCacheEmails),
|
||||
@@ -70,7 +73,7 @@ class EmailCacheManager {
|
||||
_emailCacheClient.deleteMultipleItem(destroyedCacheEmails)
|
||||
]);
|
||||
} else {
|
||||
final createdCacheEmails = created?.toMapCache(accountId) ?? {};
|
||||
final createdCacheEmails = created?.toMapCache(accountId, userName) ?? {};
|
||||
await _emailCacheClient.insertMultipleItem(createdCacheEmails);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import 'package:jmap_dart_client/jmap/core/session/session.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/sort/comparator.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/state.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/unsigned_int.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/user_name.dart';
|
||||
import 'package:jmap_dart_client/jmap/mail/email/email.dart';
|
||||
import 'package:jmap_dart_client/jmap/mail/email/email_filter_condition.dart';
|
||||
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
|
||||
@@ -46,12 +47,13 @@ class ThreadRepositoryImpl extends ThreadRepository {
|
||||
log('ThreadRepositoryImpl::getAllEmail(): filter = ${emailFilter?.mailboxId}');
|
||||
final localEmailResponse = await Future.wait([
|
||||
mapDataSource[DataSourceType.local]!.getAllEmailCache(
|
||||
accountId,
|
||||
inMailboxId: emailFilter?.mailboxId,
|
||||
sort: sort,
|
||||
limit: limit,
|
||||
filterOption: emailFilter?.filterOption),
|
||||
stateDataSource.getState(accountId, StateType.email)
|
||||
accountId,
|
||||
session.username,
|
||||
inMailboxId: emailFilter?.mailboxId,
|
||||
sort: sort,
|
||||
limit: limit,
|
||||
filterOption: emailFilter?.filterOption),
|
||||
stateDataSource.getState(accountId, session.username, StateType.email)
|
||||
]).then((List response) {
|
||||
return EmailsResponse(emailList: response.first, state: response.last);
|
||||
});
|
||||
@@ -82,7 +84,7 @@ class ThreadRepositoryImpl extends ThreadRepository {
|
||||
}
|
||||
|
||||
if (networkEmailResponse != null) {
|
||||
await _updateEmailCache(accountId, newCreated: networkEmailResponse.emailList);
|
||||
await _updateEmailCache(accountId, session.username, newCreated: networkEmailResponse.emailList);
|
||||
}
|
||||
|
||||
if (localEmailResponse.hasState()) {
|
||||
@@ -98,7 +100,7 @@ class ThreadRepositoryImpl extends ThreadRepository {
|
||||
if (networkEmailResponse != null) {
|
||||
log('ThreadRepositoryImpl::getAllEmail(): filter = ${emailFilter?.mailboxId} no local state -> update from network: ${networkEmailResponse.state}');
|
||||
if (networkEmailResponse.state != null) {
|
||||
await _updateState(accountId, networkEmailResponse.state!);
|
||||
await _updateState(accountId, session.username, networkEmailResponse.state!);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -106,11 +108,12 @@ class ThreadRepositoryImpl extends ThreadRepository {
|
||||
final newEmailResponse = await Future.wait([
|
||||
mapDataSource[DataSourceType.local]!.getAllEmailCache(
|
||||
accountId,
|
||||
session.username,
|
||||
inMailboxId: emailFilter?.mailboxId,
|
||||
sort: sort,
|
||||
limit: limit,
|
||||
filterOption: emailFilter?.filterOption),
|
||||
stateDataSource.getState(accountId, StateType.email)
|
||||
stateDataSource.getState(accountId, session.username, StateType.email)
|
||||
]).then((List response) {
|
||||
return EmailsResponse(emailList: response.first, state: response.last);
|
||||
});
|
||||
@@ -140,7 +143,7 @@ class ThreadRepositoryImpl extends ThreadRepository {
|
||||
filter: filter ?? EmailFilterCondition(inMailbox: mailboxId),
|
||||
properties: propertiesCreated,
|
||||
);
|
||||
await _updateEmailCache(accountId, newCreated: networkEmailResponse.emailList);
|
||||
await _updateEmailCache(accountId, session.username, newCreated: networkEmailResponse.emailList);
|
||||
|
||||
return networkEmailResponse;
|
||||
}
|
||||
@@ -179,21 +182,23 @@ class ThreadRepositoryImpl extends ThreadRepository {
|
||||
}
|
||||
|
||||
Future<void> _updateEmailCache(
|
||||
AccountId accountId, {
|
||||
AccountId accountId,
|
||||
UserName userName, {
|
||||
List<Email>? newUpdated,
|
||||
List<Email>? newCreated,
|
||||
List<EmailId>? newDestroyed
|
||||
}) async {
|
||||
await mapDataSource[DataSourceType.local]!.update(
|
||||
accountId,
|
||||
userName,
|
||||
updated: newUpdated,
|
||||
created: newCreated,
|
||||
destroyed: newDestroyed);
|
||||
}
|
||||
|
||||
Future<void> _updateState(AccountId accountId, State newState) async {
|
||||
Future<void> _updateState(AccountId accountId, UserName userName, State newState) async {
|
||||
log('ThreadRepositoryImpl::_updateState(): [MAIL] $newState');
|
||||
await stateDataSource.saveState(accountId, newState.toStateCache(StateType.email));
|
||||
await stateDataSource.saveState(accountId, userName, newState.toStateCache(StateType.email));
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -220,11 +225,12 @@ class ThreadRepositoryImpl extends ThreadRepository {
|
||||
final newEmailResponse = await Future.wait([
|
||||
mapDataSource[DataSourceType.local]!.getAllEmailCache(
|
||||
accountId,
|
||||
session.username,
|
||||
inMailboxId: emailFilter?.mailboxId,
|
||||
sort: sort,
|
||||
filterOption: emailFilter?.filterOption
|
||||
),
|
||||
stateDataSource.getState(accountId, StateType.email)
|
||||
stateDataSource.getState(accountId, session.username, StateType.email)
|
||||
]).then((List response) {
|
||||
return EmailsResponse(emailList: response.first, state: response.last);
|
||||
});
|
||||
@@ -249,7 +255,7 @@ class ThreadRepositoryImpl extends ThreadRepository {
|
||||
@override
|
||||
Stream<EmailsResponse> loadMoreEmails(GetEmailRequest emailRequest) async* {
|
||||
final response = await _getAllEmailsWithoutLastEmailId(emailRequest);
|
||||
await _updateEmailCache(emailRequest.accountId, newCreated: response.emailList);
|
||||
await _updateEmailCache(emailRequest.accountId, emailRequest.session.username, newCreated: response.emailList);
|
||||
yield response;
|
||||
}
|
||||
|
||||
@@ -299,7 +305,7 @@ class ThreadRepositoryImpl extends ThreadRepository {
|
||||
accountId,
|
||||
trashMailboxId,
|
||||
(listEmailIdDeleted) async {
|
||||
await _updateEmailCache(accountId, newDestroyed: listEmailIdDeleted);
|
||||
await _updateEmailCache(accountId, session.username, newDestroyed: listEmailIdDeleted);
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -313,7 +319,7 @@ class ThreadRepositoryImpl extends ThreadRepository {
|
||||
Properties? propertiesUpdated,
|
||||
}
|
||||
) async {
|
||||
final localEmailList = await mapDataSource[DataSourceType.local]!.getAllEmailCache(accountId);
|
||||
final localEmailList = await mapDataSource[DataSourceType.local]!.getAllEmailCache(accountId, session.username);
|
||||
|
||||
EmailChangeResponse? emailChangeResponse;
|
||||
bool hasMoreChanges = true;
|
||||
@@ -351,12 +357,13 @@ class ThreadRepositoryImpl extends ThreadRepository {
|
||||
|
||||
await _updateEmailCache(
|
||||
accountId,
|
||||
session.username,
|
||||
newCreated: emailChangeResponse.created,
|
||||
newUpdated: newEmailUpdated,
|
||||
newDestroyed: emailChangeResponse.destroyed);
|
||||
|
||||
if (emailChangeResponse.newStateEmail != null) {
|
||||
await _updateState(accountId, emailChangeResponse.newStateEmail!);
|
||||
await _updateState(accountId, session.username, emailChangeResponse.newStateEmail!);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user