Skip to content

Commit

Permalink
Add Accept-Language and Content-Language header to email
Browse files Browse the repository at this point in the history
  • Loading branch information
dab246 committed Nov 17, 2023
1 parent a1b7969 commit d494be8
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 91 deletions.
113 changes: 78 additions & 35 deletions lib/jmap/mail/email/email_body_part.dart
Original file line number Diff line number Diff line change
@@ -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<EmailHeader>? 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<String>? language;

@JsonKey(includeIfNull: false)
final String? location;

@JsonKey(includeIfNull: false)
final Set<EmailBodyPart>? subParts;
final Map<IndividualHeaderIdentifier, String?>? acceptLanguageHeader;
final Map<IndividualHeaderIdentifier, String?>? contentLanguageHeader;

EmailBodyPart({
this.partId,
Expand All @@ -66,11 +39,79 @@ class EmailBodyPart with EquatableMixin {
this.language,
this.location,
this.subParts,
this.acceptLanguageHeader,
this.contentLanguageHeader,
});

factory EmailBodyPart.fromJson(Map<String, dynamic> json) => _$EmailBodyPartFromJson(json);

Map<String, dynamic> toJson() => _$EmailBodyPartToJson(this);
factory EmailBodyPart.fromJson(Map<String, dynamic> 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<dynamic>?)
?.map((e) => EmailHeader.fromJson(e as Map<String, dynamic>))
.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<dynamic>?)?.map((e) => e as String).toSet(),
location: json['location'] as String?,
subParts: (json['subParts'] as List<dynamic>?)
?.map((e) => EmailBodyPart.fromJson(e as Map<String, dynamic>))
.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<String, dynamic> toJson() {
final val = <String, dynamic>{};

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<Object?> get props => [
Expand All @@ -85,7 +126,9 @@ class EmailBodyPart with EquatableMixin {
cid,
language,
location,
subParts
subParts,
acceptLanguageHeader,
contentLanguageHeader,
];
}

Expand Down
56 changes: 0 additions & 56 deletions lib/jmap/mail/email/email_body_part.g.dart

This file was deleted.

2 changes: 2 additions & 0 deletions lib/jmap/mail/email/individual_header_identifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit d494be8

Please sign in to comment.