Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SearchSnippet][Part-2] Create SearchSnippet data handler #3252

Merged
merged 3 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions contact/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -652,10 +652,10 @@ packages:
description:
path: "."
ref: main
resolved-ref: b75666ba1ac351c7e7be7a1a8f95e58a860505fc
resolved-ref: bb4ffba9e3aba2cbb60c1173663abf975b521199
url: "https://github.com/linagora/jmap-dart-client.git"
source: git
version: "0.2.3"
version: "0.3.0"
js:
dependency: transitive
description:
Expand Down
4 changes: 2 additions & 2 deletions email_recovery/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,10 @@ packages:
description:
path: "."
ref: main
resolved-ref: b75666ba1ac351c7e7be7a1a8f95e58a860505fc
resolved-ref: bb4ffba9e3aba2cbb60c1173663abf975b521199
url: "https://github.com/linagora/jmap-dart-client.git"
source: git
version: "0.2.3"
version: "0.3.0"
js:
dependency: transitive
description:
Expand Down
4 changes: 2 additions & 2 deletions fcm/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,10 @@ packages:
description:
path: "."
ref: main
resolved-ref: b75666ba1ac351c7e7be7a1a8f95e58a860505fc
resolved-ref: bb4ffba9e3aba2cbb60c1173663abf975b521199
url: "https://github.com/linagora/jmap-dart-client.git"
source: git
version: "0.2.3"
version: "0.3.0"
js:
dependency: transitive
description:
Expand Down
4 changes: 2 additions & 2 deletions forward/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,10 @@ packages:
description:
path: "."
ref: main
resolved-ref: b75666ba1ac351c7e7be7a1a8f95e58a860505fc
resolved-ref: bb4ffba9e3aba2cbb60c1173663abf975b521199
url: "https://github.com/linagora/jmap-dart-client.git"
source: git
version: "0.2.3"
version: "0.3.0"
js:
dependency: transitive
description:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ class QuickSearchEmailInteractor {
properties: properties);

final presentationEmailList = emailList
.map((email) => email.toPresentationEmail())
.map((email) => email.toPresentationEmail(
searchSnippetSubject: email.searchSnippetSubject,
searchSnippetPreview: email.searchSnippetPreview,
))
.toList();

return Right(QuickSearchEmailSuccess(presentationEmailList));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ class RefreshChangesSearchEmailInteractor {
properties: properties);

final presentationEmailList = emailList
.map((email) => email.toPresentationEmail())
.map((email) => email.toPresentationEmail(
searchSnippetSubject: email.searchSnippetSubject,
searchSnippetPreview: email.searchSnippetPreview,
))
.toList();

yield Right(RefreshChangesSearchEmailSuccess(presentationEmailList));
Expand Down
13 changes: 13 additions & 0 deletions lib/features/thread/data/datasource/thread_datasource.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'package:model/email/presentation_email.dart';
import 'package:tmail_ui_user/features/thread/data/model/email_change_response.dart';
import 'package:tmail_ui_user/features/thread/domain/model/email_response.dart';
import 'package:tmail_ui_user/features/thread/domain/model/filter_message_option.dart';
import 'package:tmail_ui_user/features/thread/domain/model/search_emails_response.dart';

abstract class ThreadDataSource {
Future<EmailsResponse> getAllEmail(
Expand All @@ -28,6 +29,18 @@ abstract class ThreadDataSource {
}
);

Future<SearchEmailsResponse> searchEmails(
Session session,
AccountId accountId,
{
UnsignedInt? limit,
int? position,
Set<Comparator>? sort,
Filter? filter,
Properties? properties
}
);

Future<EmailChangeResponse> getChanges(
Session session,
AccountId accountId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import 'package:tmail_ui_user/features/thread/data/local/email_cache_manager.dar
import 'package:tmail_ui_user/features/thread/data/model/email_change_response.dart';
import 'package:tmail_ui_user/features/thread/domain/model/email_response.dart';
import 'package:tmail_ui_user/features/thread/domain/model/filter_message_option.dart';
import 'package:tmail_ui_user/features/thread/domain/model/search_emails_response.dart';
import 'package:tmail_ui_user/main/exceptions/exception_thrower.dart';

class LocalThreadDataSourceImpl extends ThreadDataSource {
Expand All @@ -40,6 +41,21 @@ class LocalThreadDataSourceImpl extends ThreadDataSource {
throw UnimplementedError();
}

@override
Future<SearchEmailsResponse> searchEmails(
Session session,
AccountId accountId,
{
UnsignedInt? limit,
int? position,
Set<Comparator>? sort,
Filter? filter,
Properties? properties
}
) {
throw UnimplementedError();
}

@override
Future<EmailChangeResponse> getChanges(
Session session,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import 'package:tmail_ui_user/features/thread/data/network/thread_api.dart';
import 'package:tmail_ui_user/features/thread/data/network/thread_isolate_worker.dart';
import 'package:tmail_ui_user/features/thread/domain/model/email_response.dart';
import 'package:tmail_ui_user/features/thread/domain/model/filter_message_option.dart';
import 'package:tmail_ui_user/features/thread/domain/model/search_emails_response.dart';
import 'package:tmail_ui_user/main/exceptions/exception_thrower.dart';

class ThreadDataSourceImpl extends ThreadDataSource {
Expand Down Expand Up @@ -56,6 +57,30 @@ class ThreadDataSourceImpl extends ThreadDataSource {
}).catchError(_exceptionThrower.throwException);
}

@override
Future<SearchEmailsResponse> searchEmails(
Session session,
AccountId accountId,
{
UnsignedInt? limit,
int? position,
Set<Comparator>? sort,
Filter? filter,
Properties? properties,
}
) {
return Future.sync(() async {
return await threadAPI.searchEmails(
session,
accountId,
limit: limit,
position: position,
sort: sort,
filter: filter,
properties: properties);
}).catchError(_exceptionThrower.throwException);
}

@override
Future<EmailChangeResponse> getChanges(
Session session,
Expand Down
97 changes: 97 additions & 0 deletions lib/features/thread/data/network/thread_api.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import 'dart:async';

import 'package:core/utils/app_logger.dart';
import 'package:jmap_dart_client/http/http_client.dart';
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/filter/filter.dart';
import 'package:jmap_dart_client/jmap/core/properties/properties.dart';
import 'package:jmap_dart_client/jmap/core/request/reference_path.dart';
import 'package:jmap_dart_client/jmap/core/request/request_invocation.dart';
import 'package:jmap_dart_client/jmap/core/response/response_object.dart';
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';
Expand All @@ -16,8 +19,12 @@ import 'package:jmap_dart_client/jmap/mail/email/email.dart';
import 'package:jmap_dart_client/jmap/mail/email/get/get_email_method.dart';
import 'package:jmap_dart_client/jmap/mail/email/get/get_email_response.dart';
import 'package:jmap_dart_client/jmap/mail/email/query/query_email_method.dart';
import 'package:jmap_dart_client/jmap/mail/email/search_snippet/search_snippet.dart';
import 'package:jmap_dart_client/jmap/mail/email/search_snippet/search_snippet_get_method.dart';
import 'package:jmap_dart_client/jmap/mail/email/search_snippet/search_snippet_get_response.dart';
import 'package:tmail_ui_user/features/thread/data/model/email_change_response.dart';
import 'package:tmail_ui_user/features/thread/domain/model/email_response.dart';
import 'package:tmail_ui_user/features/thread/domain/model/search_emails_response.dart';
import 'package:tmail_ui_user/main/error/capability_validator.dart';

class ThreadAPI {
Expand Down Expand Up @@ -83,6 +90,96 @@ class ThreadAPI {
return EmailsResponse(emailList: resultList?.list, state: resultList?.state);
}

Future<SearchEmailsResponse> searchEmails(
Session session,
AccountId accountId,
{
UnsignedInt? limit,
int? position,
Set<Comparator>? sort,
Filter? filter,
Properties? properties
}
) async {
final processingInvocation = ProcessingInvocation();

final jmapRequestBuilder = JmapRequestBuilder(httpClient, processingInvocation);

// Email/query
final queryEmailMethod = QueryEmailMethod(accountId);

if (limit != null) queryEmailMethod.addLimit(limit);

if (position != null) queryEmailMethod.addPosition(position);

if (sort != null) queryEmailMethod.addSorts(sort);

if (filter != null) queryEmailMethod.addFilters(filter);

final queryEmailInvocation = jmapRequestBuilder.invocation(queryEmailMethod);

// SearchSnippet/get
final getSearchSnippetMethod = SearchSnippetGetMethod(accountId);
if (filter != null) getSearchSnippetMethod.addFilters(filter);
getSearchSnippetMethod.addReferenceEmailIds(
processingInvocation.createResultReference(
queryEmailInvocation.methodCallId,
ReferencePath.idsPath));
final getSearchSnippetInvocation = jmapRequestBuilder.invocation(
getSearchSnippetMethod);

// Email/get
final getEmailMethod = GetEmailMethod(accountId);

if (properties != null) getEmailMethod.addProperties(properties);

getEmailMethod.addReferenceIds(processingInvocation.createResultReference(
queryEmailInvocation.methodCallId,
ReferencePath.idsPath));

final getEmailInvocation = jmapRequestBuilder.invocation(getEmailMethod);

final capabilities = getEmailMethod.requiredCapabilities
.toCapabilitiesSupportTeamMailboxes(session, accountId);

final result = await (jmapRequestBuilder
..usings(capabilities))
.build()
.execute();

final emailResultList = result.parse<GetEmailResponse>(
getEmailInvocation.methodCallId, GetEmailResponse.deserialize);

if (sort != null && emailResultList != null) {
for (var comparator in sort) {
emailResultList.sortEmails(comparator);
}
}

final searchSnippets = _getSearchSnippetsFromResponse(
result,
getSearchSnippetMethodCallId: getSearchSnippetInvocation.methodCallId,
);
return SearchEmailsResponse(
emailList: emailResultList?.list,
state: emailResultList?.state,
searchSnippets: searchSnippets);
}

List<SearchSnippet>? _getSearchSnippetsFromResponse(
ResponseObject response,
{required MethodCallId getSearchSnippetMethodCallId}
) {
try {
return response.parse<SearchSnippetGetResponse>(
getSearchSnippetMethodCallId,
SearchSnippetGetResponse.fromJson)?.list;
} catch (e) {
logError('ThreadAPI::searchEmails:getSearchSnippetsFromResponse: Exception = $e');
return null;
}
}

Future<EmailChangeResponse> getChanges(
Session session,
AccountId accountId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import 'package:tmail_ui_user/features/thread/domain/model/email_filter.dart';
import 'package:tmail_ui_user/features/thread/domain/model/email_response.dart';
import 'package:tmail_ui_user/features/thread/domain/model/filter_message_option.dart';
import 'package:tmail_ui_user/features/thread/domain/model/get_email_request.dart';
import 'package:tmail_ui_user/features/thread/domain/model/search_email.dart';
import 'package:tmail_ui_user/features/thread/domain/repository/thread_repository.dart';

class ThreadRepositoryImpl extends ThreadRepository {
Expand Down Expand Up @@ -287,7 +288,7 @@ class ThreadRepositoryImpl extends ThreadRepository {
}

@override
Future<List<Email>> searchEmails(
Future<List<SearchEmail>> searchEmails(
Session session,
AccountId accountId,
{
Expand All @@ -298,7 +299,7 @@ class ThreadRepositoryImpl extends ThreadRepository {
Properties? properties
}
) async {
final emailResponse = await mapDataSource[DataSourceType.network]!.getAllEmail(
final searchEmailsResponse = await mapDataSource[DataSourceType.network]!.searchEmails(
session,
accountId,
limit: limit,
Expand All @@ -307,7 +308,7 @@ class ThreadRepositoryImpl extends ThreadRepository {
filter: filter,
properties: properties);

return emailResponse.emailList ?? List.empty();
return searchEmailsResponse.toSearchEmails ?? [];
}

@override
Expand Down
2 changes: 1 addition & 1 deletion lib/features/thread/domain/model/email_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class EmailsResponse with EquatableMixin {
final List<Email>? emailList;
final State? state;

EmailsResponse({
const EmailsResponse({
this.emailList,
this.state
});
Expand Down
Loading
Loading