Skip to content

Commit

Permalink
feat: improve serialization and API
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-virkus committed Feb 13, 2023
1 parent 7a87aa7 commit 4edb56d
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 27 deletions.
1 change: 0 additions & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ linter:
#- flutter_style_todos # Flutter todos are to verbose for our requirements.
- hash_and_equals
- implementation_imports
- invariant_booleans
- iterable_contains_unrelated_type
# - join_return_with_assignment # leads to less readable code IMHO
- library_names
Expand Down
20 changes: 20 additions & 0 deletions build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
targets:
$default:
builders:
json_serializable:
options:
# Options configure how source code is generated for every
# `@JsonSerializable`-annotated class in the package.
# any_map: false
# checked: false
# constructor: ""
# create_factory: true
# create_field_map: false
# create_per_field_to_json: false
# create_to_json: true
# disallow_unrecognized_keys: false
explicit_to_json: true
# field_rename: none
# generic_argument_factories: false
# ignore_unannotated: false
# include_if_null: true
7 changes: 5 additions & 2 deletions lib/src/imap/message_sequence.dart
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,11 @@ class MessageSequence {
/// optionally skipping the first [skip] entries.
/// When the [pageNumber] is 1 and the [pageSize] is equals or bigger
/// than the [length] of this sequence, this sequence is returned.
MessageSequence subsequenceFromPage(int pageNumber, int pageSize,
{int skip = 0}) {
MessageSequence subsequenceFromPage(
int pageNumber,
int pageSize, {
int skip = 0,
}) {
if (pageNumber == 1 && pageSize >= length) {
return this;
}
Expand Down
74 changes: 67 additions & 7 deletions lib/src/mail/mail_account.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class MailAccount {
this.userName = '',
this.outgoingClientDomain = 'enough.de',
this.supportsPlusAliases = false,
this.aliases,
this.aliases = const [],
this.attributes = const {},
});

Expand All @@ -46,7 +46,7 @@ class MailAccount {
String outgoingClientDomain = 'enough.de',
String? loginName,
bool supportsPlusAliases = false,
List<MailAddress>? aliases,
List<MailAddress> aliases = const [],
}) =>
MailAccount.fromDiscoveredSettingsWithAuth(
name: name,
Expand Down Expand Up @@ -77,7 +77,7 @@ class MailAccount {
String outgoingClientDomain = 'enough.de',
MailAuthentication? outgoingAuth,
bool supportsPlusAliases = false,
List<MailAddress>? aliases,
List<MailAddress> aliases = const [],
}) {
final incoming = MailServerConfig(
authentication: auth,
Expand Down Expand Up @@ -133,7 +133,7 @@ class MailAccount {
SocketType incomingSocketType = SocketType.ssl,
SocketType outgoingSocketType = SocketType.ssl,
bool supportsPlusAliases = false,
List<MailAddress>? aliases,
List<MailAddress> aliases = const [],
}) =>
MailAccount.fromManualSettingsWithAuth(
name: name,
Expand Down Expand Up @@ -185,7 +185,7 @@ class MailAccount {
SocketType incomingSocketType = SocketType.ssl,
SocketType outgoingSocketType = SocketType.ssl,
bool supportsPlusAliases = false,
List<MailAddress>? aliases,
List<MailAddress> aliases = const [],
}) {
final incoming = MailServerConfig(
authentication: auth,
Expand Down Expand Up @@ -246,7 +246,7 @@ class MailAccount {
MailAddress get fromAddress => MailAddress(userName, email);

/// Optional list of associated aliases
final List<MailAddress>? aliases;
final List<MailAddress> aliases;

/// Optional indicator if the mail service supports + based aliases
///
Expand Down Expand Up @@ -279,7 +279,7 @@ class MailAccount {
other.incoming == incoming &&
other.outgoing == outgoing &&
other.supportsPlusAliases == supportsPlusAliases &&
other.aliases?.length == aliases?.length &&
other.aliases.length == aliases.length &&
other.attributes.length == attributes.length;

@override
Expand All @@ -290,6 +290,8 @@ class MailAccount {

/// Creates a new [MailAccount] with the given settings or by copying
/// the current settings.
///
/// Compare [copyWithAttribute], [copyWithAlias]
MailAccount copyWith({
String? name,
String? email,
Expand All @@ -312,6 +314,64 @@ class MailAccount {
supportsPlusAliases: supportsPlusAliases ?? this.supportsPlusAliases,
attributes: attributes ?? this.attributes,
);

/// Copies this account with the attribute [name] and [value]
///
/// Compare [copyWith], [copyWithAlias]
MailAccount copyWithAttribute(String name, dynamic value) {
final attributes =
this.attributes.isEmpty ? <String, dynamic>{} : this.attributes;
attributes[name] = value;

return MailAccount(
name: name,
email: email,
userName: userName,
incoming: incoming,
outgoing: outgoing,
aliases: aliases,
outgoingClientDomain: outgoingClientDomain,
supportsPlusAliases: supportsPlusAliases,
attributes: attributes,
);
}

/// Copies this account with the additional [alias]
///
/// Compare [copyWith], [copyWithAttribute]
MailAccount copyWithAlias(MailAddress alias) {
final aliases = this.aliases.isEmpty ? <MailAddress>[] : this.aliases
..add(alias);

return MailAccount(
name: name,
email: email,
userName: userName,
incoming: incoming,
outgoing: outgoing,
aliases: aliases,
outgoingClientDomain: outgoingClientDomain,
supportsPlusAliases: supportsPlusAliases,
attributes: attributes,
);
}

/// Convenience method to update the incoming and outgoing authentication
/// user name for identifying the user towards the mail service.
MailAccount copyWithAuthenticationUserName(String authenticationUserName) {
var incomingAuth = incoming.authentication;
if (incomingAuth is UserNameBasedAuthentication) {
incomingAuth = incomingAuth.copyWithUserName(authenticationUserName);
}
var outgoingAuth = outgoing.authentication;
if (outgoingAuth is UserNameBasedAuthentication) {
outgoingAuth = outgoingAuth.copyWithUserName(authenticationUserName);
}
return copyWith(
incoming: incoming.copyWith(authentication: incomingAuth),
outgoing: outgoing.copyWith(authentication: outgoingAuth),
);
}
}

/// Configuration of a specific mail service like IMAP, POP3 or SMTP
Expand Down
18 changes: 10 additions & 8 deletions lib/src/mail/mail_account.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 25 additions & 3 deletions lib/src/mail/mail_authentication.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ abstract class UserNameBasedAuthentication extends MailAuthentication {

/// The user name
final String userName;

/// Copies this authentication with the new [userName]
UserNameBasedAuthentication copyWithUserName(String userName);
}

/// Provides a simple username-password authentication
Expand Down Expand Up @@ -111,6 +114,14 @@ class PlainAuthentication extends UserNameBasedAuthentication {

@override
int get hashCode => userName.hashCode | password.hashCode;

@override
UserNameBasedAuthentication copyWithUserName(String userName) =>
PlainAuthentication(userName, password);

/// Copies this authentication with the given values
PlainAuthentication copyWith({String? userName, String? password}) =>
PlainAuthentication(userName ?? this.userName, password ?? this.password);
}

/// Contains an OAuth compliant token
Expand Down Expand Up @@ -230,8 +241,12 @@ class OauthAuthentication extends UserNameBasedAuthentication {
final OauthToken token;

@override
Future<void> authenticate(ServerConfig serverConfig,
{ImapClient? imap, PopClient? pop, SmtpClient? smtp}) async {
Future<void> authenticate(
ServerConfig serverConfig, {
ImapClient? imap,
PopClient? pop,
SmtpClient? smtp,
}) async {
final userName = this.userName;
final accessToken = token.accessToken;
switch (serverConfig.type) {
Expand Down Expand Up @@ -260,6 +275,13 @@ class OauthAuthentication extends UserNameBasedAuthentication {
int get hashCode => userName.hashCode | token.hashCode;

/// Copies this [OauthAuthentication] with the given [token]
OauthAuthentication copyWith(OauthToken token) =>
OauthAuthentication copyWith({String? userName, OauthToken? token}) =>
OauthAuthentication(
userName ?? this.userName,
token ?? this.token,
);

@override
UserNameBasedAuthentication copyWithUserName(String userName) =>
OauthAuthentication(userName, token);
}
2 changes: 1 addition & 1 deletion lib/src/mail/mail_authentication.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions lib/src/mail/mail_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,13 @@ class MailClient {
final newToken =
auth.token.copyWith(refreshed.accessToken, refreshed.expiresIn);
final incoming = account.incoming.copyWith(
authentication: auth.copyWith(newToken),
authentication: auth.copyWith(token: newToken),
);
var outgoing = account.outgoing;
final outAuth = outgoing.authentication;
if (outAuth is OauthAuthentication) {
outgoing = outgoing.copyWith(
authentication: outAuth.copyWith(newToken),
authentication: outAuth.copyWith(token: newToken),
);
}
_account = _account.copyWith(
Expand Down
4 changes: 4 additions & 0 deletions lib/src/mail_address.dart
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ class MailAddress {
return null;
}

/// Copies this mail address with the given values
MailAddress copyWith({String? personalName, String? email}) =>
MailAddress(personalName ?? this.personalName, email ?? this.email);

@override
int get hashCode => email.hashCode + (personalName?.hashCode ?? 0);

Expand Down
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ dependencies:
encrypt: ^5.0.0
enough_convert: ^1.5.0
event_bus: ^2.0.0
intl: ^0.17.0
json_annotation: ^4.6.0
intl: ^0.18.0
json_annotation: ^4.8.0
pointycastle: ^3.6.0
xml: '>=6.0.0 <7.0.0'


dev_dependencies:
build_runner: ^2.2.0
build_runner: ^2.3.0
json_serializable: ^6.3.0
lints: ^2.0.1
test: ^1.20.1
Expand Down

0 comments on commit 4edb56d

Please sign in to comment.