diff --git a/lib/jmap/identities/identity.dart b/lib/jmap/identities/identity.dart index e2f9587..0696d01 100644 --- a/lib/jmap/identities/identity.dart +++ b/lib/jmap/identities/identity.dart @@ -24,9 +24,6 @@ class Identity with EquatableMixin { @JsonKey(includeIfNull: false) final String? email; - @JsonKey(includeIfNull: false) - final Set? cc; - @JsonKey(includeIfNull: false) final Set? bcc; @@ -48,7 +45,6 @@ class Identity with EquatableMixin { this.description, this.name, this.email, - this.cc, this.bcc, this.replyTo, this.textSignature, diff --git a/lib/jmap/identities/identity.g.dart b/lib/jmap/identities/identity.g.dart index 5c5ad08..9244dfa 100644 --- a/lib/jmap/identities/identity.g.dart +++ b/lib/jmap/identities/identity.g.dart @@ -11,9 +11,6 @@ Identity _$IdentityFromJson(Map json) => Identity( description: json['description'] as String?, name: json['name'] as String?, email: json['email'] as String?, - cc: (json['cc'] as List?) - ?.map((e) => EmailAddress.fromJson(e as Map)) - .toSet(), bcc: (json['bcc'] as List?) ?.map((e) => EmailAddress.fromJson(e as Map)) .toSet(), @@ -40,7 +37,6 @@ Map _$IdentityToJson(Identity instance) { writeNotNull('description', instance.description); writeNotNull('name', instance.name); writeNotNull('email', instance.email); - writeNotNull('cc', instance.cc?.toList()); writeNotNull('bcc', instance.bcc?.toList()); writeNotNull('replyTo', instance.replyTo?.toList()); writeNotNull('textSignature', diff --git a/lib/jmap/identities/set/set_identity_method.dart b/lib/jmap/identities/set/set_identity_method.dart new file mode 100644 index 0000000..b7884c4 --- /dev/null +++ b/lib/jmap/identities/set/set_identity_method.dart @@ -0,0 +1,47 @@ +import 'package:jmap_dart_client/http/converter/account_id_converter.dart'; +import 'package:jmap_dart_client/http/converter/id_converter.dart'; +import 'package:jmap_dart_client/http/converter/set/set_method_properties_converter.dart'; +import 'package:jmap_dart_client/jmap/account_id.dart'; +import 'package:jmap_dart_client/jmap/core/capability/capability_identifier.dart'; +import 'package:jmap_dart_client/jmap/core/method/request/set_method.dart'; +import 'package:jmap_dart_client/jmap/core/request/request_invocation.dart'; +import 'package:jmap_dart_client/jmap/identities/identity.dart'; + +class SetIdentityMethod extends SetMethod { + SetIdentityMethod(AccountId accountId) : super(accountId); + + @override + MethodName get methodName => MethodName('Identity/set'); + + @override + Set get requiredCapabilities => { + CapabilityIdentifier.jmapCore, + CapabilityIdentifier.jmapSubmission + }; + + @override + Map toJson() { + final val = { + 'accountId': const AccountIdConverter().toJson(accountId), + }; + + void writeNotNull(String key, dynamic value) { + if (value != null) { + val[key] = value; + } + } + + writeNotNull('ifInState', ifInState?.value); + writeNotNull('create', create + ?.map((id, create) => SetMethodPropertiesConverter().fromMapIdToJson(id, create.toJson()))); + writeNotNull('update', update + ?.map((id, update) => SetMethodPropertiesConverter().fromMapIdToJson(id, update.toJson()))); + writeNotNull('destroy', destroy + ?.map((destroyId) => IdConverter().toJson(destroyId)).toList()); + + return val; + } + + @override + List get props => [accountId, ifInState, create, update, destroy]; +} \ No newline at end of file diff --git a/lib/jmap/identities/set/set_identity_response.dart b/lib/jmap/identities/set/set_identity_response.dart new file mode 100644 index 0000000..86fc99a --- /dev/null +++ b/lib/jmap/identities/set/set_identity_response.dart @@ -0,0 +1,76 @@ +import 'package:jmap_dart_client/http/converter/account_id_converter.dart'; +import 'package:jmap_dart_client/http/converter/id_converter.dart'; +import 'package:jmap_dart_client/http/converter/state_converter.dart'; +import 'package:jmap_dart_client/http/converter/state_nullable_converter.dart'; +import 'package:jmap_dart_client/jmap/account_id.dart'; +import 'package:jmap_dart_client/jmap/core/error/set_error.dart'; +import 'package:jmap_dart_client/jmap/core/id.dart'; +import 'package:jmap_dart_client/jmap/core/method/response/set_response.dart'; +import 'package:jmap_dart_client/jmap/core/state.dart'; +import 'package:jmap_dart_client/jmap/identities/identity.dart'; + +class SetIdentityResponse extends SetResponse { + SetIdentityResponse( + AccountId accountId, + State newState, { + State? oldState, + Map? created, + Map? updated, + Set? destroyed, + Map? notCreated, + Map? notUpdated, + Map? notDestroyed + }) : super( + accountId, + newState, + oldState: oldState, + created: created, + updated: updated, + destroyed: destroyed, + notCreated: notCreated, + notUpdated: notUpdated, + notDestroyed: notDestroyed + ); + + static SetIdentityResponse deserialize(Map json) { + return SetIdentityResponse( + const AccountIdConverter().fromJson(json['accountId'] as String), + const StateConverter().fromJson(json['newState'] as String), + oldState: StateNullableConverter().fromJson(json['oldState'] as String?), + created: (json['created'] as Map?) + ?.map((key, value) => MapEntry( + IdConverter().fromJson(key), + Identity.fromJson(value as Map))), + updated: (json['updated'] as Map?) + ?.map((key, value) => MapEntry( + IdConverter().fromJson(key), + value != null ? Identity.fromJson(value as Map) : null)), + destroyed: (json['destroyed'] as List?) + ?.map((id) => IdConverter().fromJson(id)).toSet(), + notCreated: (json['notCreated'] as Map?) + ?.map((key, value) => MapEntry( + IdConverter().fromJson(key), + SetError.fromJson(value))), + notUpdated: (json['notUpdated'] as Map?) + ?.map((key, value) => MapEntry( + IdConverter().fromJson(key), + SetError.fromJson(value))), + notDestroyed: (json['notDestroyed'] as Map?) + ?.map((key, value) => MapEntry( + IdConverter().fromJson(key), + SetError.fromJson(value))), + ); + } + + @override + List get props => [ + oldState, + newState, + created, + updated, + destroyed, + notCreated, + notUpdated, + notDestroyed + ]; +} \ No newline at end of file diff --git a/test/jmap/identities/set_identity_method_test.dart b/test/jmap/identities/set_identity_method_test.dart new file mode 100644 index 0000000..323a449 --- /dev/null +++ b/test/jmap/identities/set_identity_method_test.dart @@ -0,0 +1,99 @@ +import 'package:dio/dio.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:http_mock_adapter/http_mock_adapter.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/id.dart'; +import 'package:jmap_dart_client/jmap/identities/identity.dart'; +import 'package:jmap_dart_client/jmap/identities/set/set_identity_method.dart'; +import 'package:jmap_dart_client/jmap/identities/set/set_identity_response.dart'; +import 'package:jmap_dart_client/jmap/jmap_request.dart'; + +void main() { + group('test to json set identity method', () { + final expectedCreated = Identity( + id: IdentityId(Id('bc6d7c78-672a-45e9-b0de-1dfd2699020a')), + ); + + test('set identity method and response parsing', () async { + final baseOption = BaseOptions(method: 'POST'); + final dio = Dio(baseOption) + ..options.baseUrl = 'http://domain.com'; + final dioAdapter = DioAdapter(dio: dio); + dioAdapter.onPost( + '/jmap', + (server) => server.reply(200, { + "sessionState": "2c9f1b12-b35a-43e6-9af2-0106fb53a943", + "methodResponses": [ + [ + "Identity/set", + { + "accountId": "3ce33c876a726662c627746eb9537a1d13c2338193ef27bd051a3ce5c0fe5b12", + "newState": "2c9f1b12-b35a-43e6-9af2-0106fb53a943", + "created": { + "dab246": { + "id": "bc6d7c78-672a-45e9-b0de-1dfd2699020a", + "mayDelete": true + } + } + }, + "c0" + ] + ] + }), + data: { + "using": [ + "urn:ietf:params:jmap:core", + "urn:ietf:params:jmap:submission" + ], + "methodCalls": [ + [ + "Identity/set", + { + "accountId": "3ce33c876a726662c627746eb9537a1d13c2338193ef27bd051a3ce5c0fe5b12", + "create": { + "dab246": { + "name": "User B1", + "email": "userb@qa.open-paas.org", + "textSignature": "", + "htmlSignature": "
Dat T. Vu
Mobile Engineer
LINAGORA VIETNAM
A: 8th Floor (Toong VPBank Tower, No. 5 Dien Bien Phu Str., Ba Dinh Dist., Ha Noi
P: (+84) 366-769-439
E: tdvu@linagora.com
" + } + } + }, + "c0" + ] + ] + }, + headers: { + "accept": "application/json;jmapVersion=rfc-8621", + "content-type": "application/json; charset=utf-8", + "content-length": 512 + } + ); + + final setIdentityMethod = SetIdentityMethod(AccountId(Id('3ce33c876a726662c627746eb9537a1d13c2338193ef27bd051a3ce5c0fe5b12'))) + ..addCreate(Id('dab246'), + Identity( + name: 'User B1', + email: 'userb@qa.open-paas.org', + textSignature: Signature(''), + htmlSignature: Signature('
Dat T. Vu
Mobile Engineer
LINAGORA VIETNAM
A: 8th Floor (Toong VPBank Tower, No. 5 Dien Bien Phu Str., Ba Dinh Dist., Ha Noi
P: (+84) 366-769-439
E: tdvu@linagora.com
') + ) + ); + + final httpClient = HttpClient(dio); + final requestBuilder = JmapRequestBuilder(httpClient, ProcessingInvocation()); + final setIdentityInvocation = requestBuilder.invocation(setIdentityMethod); + final response = await (requestBuilder + ..usings(setIdentityMethod.requiredCapabilities)) + .build() + .execute(); + + final setIdentityResponse = response.parse( + setIdentityInvocation.methodCallId, + SetIdentityResponse.deserialize); + + expect(setIdentityResponse!.created![Id('dab246')]!.id, equals(expectedCreated.id)); + }); + }); +} \ No newline at end of file