Skip to content

Commit

Permalink
🐛 clean up flag for default domain alias
Browse files Browse the repository at this point in the history
  • Loading branch information
ebullient committed Jul 19, 2024
1 parent d7251cd commit 97d87df
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public static class Discord {

public static class ForwardEmail {
/** Is an alias active for this user */
@JsonAlias("active")
boolean configured;
@JsonAlias({ "active", "configured" })
boolean hasDefaultAlias;

/** Additional ForwardEmail aliases. Optional and rare. */
@JsonAlias("alt_alias")
Expand All @@ -52,10 +52,6 @@ public static class ForwardEmail {
public Collection<? extends String> altAlias() {
return altAlias == null ? List.of() : altAlias;
}

public boolean validAddress(String email, String login, String defaultDomain) {
return email.equals(login + "@" + defaultDomain) || altAlias().contains(email);
}
}

@SuppressWarnings("unused")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ public Response updateAliases(Map<String, Set<String>> aliases) {
// API CALL: set/update alias mappings
Map<AliasKey, Alias> aliasMap = emailService.postAliases(sanitized, session.name());

if (!emailConfig.configured && !aliasMap.isEmpty()) {
user = updateConfiguredFlag(user);
if (!emailConfig.hasDefaultAlias
&& aliasMap.keySet().stream().anyMatch(k -> emailService.isDefaultAlias(session.login(), k))) {
user = updateHasDefaultFlag(user);
}

return user.toResponse()
Expand All @@ -110,11 +111,6 @@ public Response updateAliases(Map<String, Set<String>> aliases) {
public Response generatePassword(AliasRequest request) {
try {
CommonhausUser user = getUser();
ForwardEmail emailConfig = user.services().forwardEmail();
if (!emailConfig.configured) {
return Response.status(Response.Status.NOT_FOUND).build();
}

Map<AliasKey, Alias> aliasMap = emailService.fetchAliases(session, user);
AliasKey key = AliasKey.fromCache(request.email());
Alias alias = aliasMap.get(key);
Expand Down Expand Up @@ -147,11 +143,11 @@ protected CommonhausUser getUser() {
return user;
}

CommonhausUser updateConfiguredFlag(CommonhausUser user) {
CommonhausUser updateHasDefaultFlag(CommonhausUser user) {
// eventual consistency. No big deal if this
CommonhausUser result = datastore.setCommonhausUser(new UpdateEvent(user,
(c, u) -> {
u.services().forwardEmail().configured = true;
u.services().forwardEmail().hasDefaultAlias = true;
},
"Fix forward email service active flag",
false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public static class TestAlias {

public boolean has_recipient_verification;
public Set<String> verified_recipients;

}

public static class TestDomain extends Domain {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ protected AliasKey normalizeAlias(String email) {
return AliasKey.createKey(name.toLowerCase(), domain.toLowerCase());
}

public boolean isDefaultAlias(String login, AliasKey email) {
return email.name().equals(login) && email.domain().equals(defaultAliasDomain());
}

public Set<AliasKey> normalizeEmailAddresses(MemberSession session, ForwardEmail forwardEmail) {
List<String> addresses = new ArrayList<>();
addresses.add(session.login());
Expand Down

0 comments on commit 97d87df

Please sign in to comment.