From d494be83f645a660462cc2a25e4c6869bd3547ba Mon Sep 17 00:00:00 2001 From: dab246 Date: Fri, 17 Nov 2023 21:57:40 +0700 Subject: [PATCH] Add `Accept-Language` and `Content-Language` header to email --- lib/jmap/mail/email/email_body_part.dart | 113 ++++++++++++------ lib/jmap/mail/email/email_body_part.g.dart | 56 --------- .../email/individual_header_identifier.dart | 2 + 3 files changed, 80 insertions(+), 91 deletions(-) delete mode 100644 lib/jmap/mail/email/email_body_part.g.dart diff --git a/lib/jmap/mail/email/email_body_part.dart b/lib/jmap/mail/email/email_body_part.dart index 3a9802c..64f3e9a 100644 --- a/lib/jmap/mail/email/email_body_part.dart +++ b/lib/jmap/mail/email/email_body_part.dart @@ -1,57 +1,30 @@ import 'package:equatable/equatable.dart'; import 'package:http_parser/http_parser.dart'; import 'package:jmap_dart_client/http/converter/id_nullable_converter.dart'; +import 'package:jmap_dart_client/http/converter/individual_header_identifier_converter.dart'; import 'package:jmap_dart_client/http/converter/media_type_nullable_converter.dart'; import 'package:jmap_dart_client/http/converter/part_id_nullable_converter.dart'; import 'package:jmap_dart_client/http/converter/unsigned_int_nullable_converter.dart'; import 'package:jmap_dart_client/jmap/core/id.dart'; import 'package:jmap_dart_client/jmap/core/unsigned_int.dart'; import 'package:jmap_dart_client/jmap/mail/email/email_header.dart'; -import 'package:json_annotation/json_annotation.dart'; +import 'package:jmap_dart_client/jmap/mail/email/individual_header_identifier.dart'; -part 'email_body_part.g.dart'; - -@MediaTypeNullableConverter() -@PartIdNullableConverter() -@UnsignedIntNullableConverter() -@IdNullableConverter() -@JsonSerializable() class EmailBodyPart with EquatableMixin { - @JsonKey(includeIfNull: false) final PartId? partId; - - @JsonKey(includeIfNull: false) final Id? blobId; - - @JsonKey(includeIfNull: false) final UnsignedInt? size; - - @JsonKey(includeIfNull: false) final Set? headers; - - @JsonKey(includeIfNull: false) final String? name; - - @JsonKey(includeIfNull: false) final MediaType? type; - - @JsonKey(includeIfNull: false) final String? charset; - - @JsonKey(includeIfNull: false) final String? disposition; - - @JsonKey(includeIfNull: false) final String? cid; - - @JsonKey(includeIfNull: false) final Set? language; - - @JsonKey(includeIfNull: false) final String? location; - - @JsonKey(includeIfNull: false) final Set? subParts; + final Map? acceptLanguageHeader; + final Map? contentLanguageHeader; EmailBodyPart({ this.partId, @@ -66,11 +39,79 @@ class EmailBodyPart with EquatableMixin { this.language, this.location, this.subParts, + this.acceptLanguageHeader, + this.contentLanguageHeader, }); - factory EmailBodyPart.fromJson(Map json) => _$EmailBodyPartFromJson(json); - - Map toJson() => _$EmailBodyPartToJson(this); + factory EmailBodyPart.fromJson(Map json) { + return EmailBodyPart( + partId: const PartIdNullableConverter().fromJson(json['partId'] as String?), + blobId: const IdNullableConverter().fromJson(json['blobId'] as String?), + size: const UnsignedIntNullableConverter().fromJson(json['size'] as int?), + headers: (json['headers'] as List?) + ?.map((e) => EmailHeader.fromJson(e as Map)) + .toSet(), + name: json['name'] as String?, + type: const MediaTypeNullableConverter().fromJson(json['type'] as String?), + charset: json['charset'] as String?, + disposition: json['disposition'] as String?, + cid: json['cid'] as String?, + language: + (json['language'] as List?)?.map((e) => e as String).toSet(), + location: json['location'] as String?, + subParts: (json['subParts'] as List?) + ?.map((e) => EmailBodyPart.fromJson(e as Map)) + .toSet(), + acceptLanguageHeader: IndividualHeaderIdentifierNullableConverter() + .parseEntry( + IndividualHeaderIdentifier.acceptLanguageHeader.value, + json[IndividualHeaderIdentifier.acceptLanguageHeader.value] as String? + ), + contentLanguageHeader: IndividualHeaderIdentifierNullableConverter() + .parseEntry( + IndividualHeaderIdentifier.contentLanguageHeader.value, + json[IndividualHeaderIdentifier.contentLanguageHeader.value] as String? + ), + ); + } + + Map toJson() { + final val = {}; + + void writeNotNull(String key, dynamic value) { + if (value != null) { + val[key] = value; + } + } + + writeNotNull('partId', const PartIdNullableConverter().toJson(partId)); + writeNotNull('blobId', const IdNullableConverter().toJson(blobId)); + writeNotNull('size', const UnsignedIntNullableConverter().toJson(size)); + writeNotNull('headers', headers?.toList()); + writeNotNull('name', name); + writeNotNull('type', const MediaTypeNullableConverter().toJson(type)); + writeNotNull('charset', charset); + writeNotNull('disposition', disposition); + writeNotNull('cid', cid); + writeNotNull('language', language?.toList()); + writeNotNull('location', location); + writeNotNull('subParts', subParts?.toList()); + writeNotNull( + IndividualHeaderIdentifier.acceptLanguageHeader.value, + IndividualHeaderIdentifierNullableConverter().toJson( + acceptLanguageHeader, + IndividualHeaderIdentifier.acceptLanguageHeader + ) + ); + writeNotNull( + IndividualHeaderIdentifier.contentLanguageHeader.value, + IndividualHeaderIdentifierNullableConverter().toJson( + contentLanguageHeader, + IndividualHeaderIdentifier.contentLanguageHeader + ) + ); + return val; + } @override List get props => [ @@ -85,7 +126,9 @@ class EmailBodyPart with EquatableMixin { cid, language, location, - subParts + subParts, + acceptLanguageHeader, + contentLanguageHeader, ]; } diff --git a/lib/jmap/mail/email/email_body_part.g.dart b/lib/jmap/mail/email/email_body_part.g.dart deleted file mode 100644 index 1a33e98..0000000 --- a/lib/jmap/mail/email/email_body_part.g.dart +++ /dev/null @@ -1,56 +0,0 @@ -// GENERATED CODE - DO NOT MODIFY BY HAND - -part of 'email_body_part.dart'; - -// ************************************************************************** -// JsonSerializableGenerator -// ************************************************************************** - -EmailBodyPart _$EmailBodyPartFromJson(Map json) { - return EmailBodyPart( - partId: const PartIdNullableConverter().fromJson(json['partId'] as String?), - blobId: const IdNullableConverter().fromJson(json['blobId'] as String?), - size: const UnsignedIntNullableConverter().fromJson(json['size'] as int?), - headers: (json['headers'] as List?) - ?.map((e) => EmailHeader.fromJson(e as Map)) - .toSet(), - name: json['name'] as String?, - type: const MediaTypeNullableConverter().fromJson(json['type'] as String?), - charset: json['charset'] as String?, - disposition: json['disposition'] as String?, - cid: json['cid'] as String?, - language: - (json['language'] as List?)?.map((e) => e as String).toSet(), - location: json['location'] as String?, - subParts: (json['subParts'] as List?) - ?.map((e) => EmailBodyPart.fromJson(e as Map)) - .toSet(), - ); -} - -Map _$EmailBodyPartToJson(EmailBodyPart instance) { - final val = {}; - - void writeNotNull(String key, dynamic value) { - if (value != null) { - val[key] = value; - } - } - - writeNotNull( - 'partId', const PartIdNullableConverter().toJson(instance.partId)); - writeNotNull('blobId', const IdNullableConverter().toJson(instance.blobId)); - writeNotNull( - 'size', const UnsignedIntNullableConverter().toJson(instance.size)); - writeNotNull('headers', instance.headers?.toList()); - writeNotNull('name', instance.name); - writeNotNull( - 'type', const MediaTypeNullableConverter().toJson(instance.type)); - writeNotNull('charset', instance.charset); - writeNotNull('disposition', instance.disposition); - writeNotNull('cid', instance.cid); - writeNotNull('language', instance.language?.toList()); - writeNotNull('location', instance.location); - writeNotNull('subParts', instance.subParts?.toList()); - return val; -} diff --git a/lib/jmap/mail/email/individual_header_identifier.dart b/lib/jmap/mail/email/individual_header_identifier.dart index b2ed503..3d158fb 100644 --- a/lib/jmap/mail/email/individual_header_identifier.dart +++ b/lib/jmap/mail/email/individual_header_identifier.dart @@ -4,6 +4,8 @@ class IndividualHeaderIdentifier with EquatableMixin { static final headerUserAgent = IndividualHeaderIdentifier('header:User-Agent:asText'); static final headerMdn = IndividualHeaderIdentifier('header:Disposition-Notification-To:asText'); static final headerCalendarEvent = IndividualHeaderIdentifier('header:X-MEETING-UID:asText'); + static final acceptLanguageHeader = IndividualHeaderIdentifier('header:Accept-Language'); + static final contentLanguageHeader = IndividualHeaderIdentifier('header:Content-Language'); final String value;