|
| 1 | +package com.faforever.api.email; |
| 2 | + |
| 3 | +import com.faforever.api.config.FafApiProperties; |
| 4 | +import lombok.Getter; |
| 5 | +import lombok.RequiredArgsConstructor; |
| 6 | +import lombok.extern.slf4j.Slf4j; |
| 7 | +import org.springframework.beans.factory.InitializingBean; |
| 8 | +import org.springframework.stereotype.Component; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.nio.file.Files; |
| 12 | +import java.nio.file.Path; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.Set; |
| 15 | +import java.util.stream.Collectors; |
| 16 | + |
| 17 | +import static java.text.MessageFormat.format; |
| 18 | + |
| 19 | +@Component |
| 20 | +@RequiredArgsConstructor |
| 21 | +@Slf4j |
| 22 | +public class MailBodyBuilder implements InitializingBean { |
| 23 | + |
| 24 | + @Getter |
| 25 | + public enum Template { |
| 26 | + ACCOUNT_ACTIVATION("username", "activationUrl"), |
| 27 | + WELCOME_TO_FAF("username"), |
| 28 | + PASSWORD_RESET("username", "passwordResetUrl"); |
| 29 | + |
| 30 | + private final Set<String> variables; |
| 31 | + |
| 32 | + Template(String... variables) { |
| 33 | + this.variables = Set.of(variables); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + private final FafApiProperties properties; |
| 38 | + |
| 39 | + private Path getTemplateFilePath(Template template) { |
| 40 | + final var path = switch (template) { |
| 41 | + case ACCOUNT_ACTIVATION -> properties.getRegistration().getActivationMailTemplatePath(); |
| 42 | + case WELCOME_TO_FAF -> properties.getRegistration().getWelcomeMailTemplatePath(); |
| 43 | + case PASSWORD_RESET -> properties.getPasswordReset().getMailTemplatePath(); |
| 44 | + }; |
| 45 | + |
| 46 | + return Path.of(path); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void afterPropertiesSet(){ |
| 51 | + boolean templateError = false; |
| 52 | + |
| 53 | + for (final var template : Template.values()) { |
| 54 | + var path = getTemplateFilePath(template); |
| 55 | + |
| 56 | + if (Files.exists(path)) { |
| 57 | + log.debug("Template {} has template file present at {}", template, path); |
| 58 | + } else { |
| 59 | + templateError = true; |
| 60 | + log.error("Template {} is missing file at configurate destination: {}", template, path); |
| 61 | + } |
| 62 | + |
| 63 | + try { |
| 64 | + loadAndValidateTemplate(template); |
| 65 | + } catch (Exception e) { |
| 66 | + log.error("Template {} has invalid template file at {}. Error: {}", template, path, e.getMessage()); |
| 67 | + templateError = true; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + if (templateError) { |
| 72 | + throw new IllegalStateException("At least one template file is not available or inconsistent."); |
| 73 | + } |
| 74 | + |
| 75 | + log.info("All template files present."); |
| 76 | + } |
| 77 | + |
| 78 | + private String loadAndValidateTemplate(Template template) throws IOException { |
| 79 | + final var templateBody = Files.readString(getTemplateFilePath(template)); |
| 80 | + |
| 81 | + final var missingVariables = template.variables.stream() |
| 82 | + .map(variable -> "{{" + variable + "}}") |
| 83 | + .filter(variable -> !templateBody.contains(variable)) |
| 84 | + .collect(Collectors.joining(", ")); |
| 85 | + |
| 86 | + if (missingVariables.length() > 0) { |
| 87 | + throw new IllegalStateException(format("Template file for {0} is missing variables: {1}", template, missingVariables)); |
| 88 | + } |
| 89 | + |
| 90 | + return templateBody; |
| 91 | + } |
| 92 | + |
| 93 | + private void validateVariables(Template template, Set<String> variables) { |
| 94 | + final var missingVariables = template.variables.stream() |
| 95 | + .filter(variable -> !variables.contains(variable)) |
| 96 | + .collect(Collectors.joining(", ")); |
| 97 | + |
| 98 | + final var unknownVariables = variables.stream() |
| 99 | + .filter(variable -> !template.variables.contains(variable)) |
| 100 | + .collect(Collectors.joining(", ")); |
| 101 | + |
| 102 | + if (unknownVariables.length() > 0) { |
| 103 | + log.warn("Unknown variable(s) handed over for template {}: {}", template, unknownVariables); |
| 104 | + } |
| 105 | + |
| 106 | + if (missingVariables.length() > 0) { |
| 107 | + throw new IllegalArgumentException("Variable(s) not assigned: " + missingVariables); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private String populate(Template template, Map<String, String> variables) throws IOException { |
| 112 | + validateVariables(template, variables.keySet()); |
| 113 | + |
| 114 | + var templateBody = loadAndValidateTemplate(template); |
| 115 | + |
| 116 | + log.trace("Raw template body: {}", templateBody); |
| 117 | + |
| 118 | + for (var entry : variables.entrySet()) { |
| 119 | + final var variable = "{{" + entry.getKey() + "}}"; |
| 120 | + final var value = entry.getValue(); |
| 121 | + |
| 122 | + log.trace("Replacing {} with {}", variable, value); |
| 123 | + templateBody = templateBody.replace(variable, value); |
| 124 | + } |
| 125 | + |
| 126 | + log.trace("Replaced template body: {}", templateBody); |
| 127 | + |
| 128 | + return templateBody; |
| 129 | + } |
| 130 | + |
| 131 | + public String buildAccountActivationBody(String username, String activationUrl) throws IOException { |
| 132 | + return populate(Template.ACCOUNT_ACTIVATION, Map.of( |
| 133 | + "username", username, |
| 134 | + "activationUrl", activationUrl |
| 135 | + )); |
| 136 | + } |
| 137 | + |
| 138 | + public String buildPasswordResetBody(String username, String passwordResetUrl) throws IOException { |
| 139 | + return populate(Template.PASSWORD_RESET, Map.of( |
| 140 | + "username", username, |
| 141 | + "passwordResetUrl", passwordResetUrl |
| 142 | + )); |
| 143 | + } |
| 144 | +} |
0 commit comments