Move SendEmailUtils to the /ui/server directory

SendEmailUtils is a general utility of the web console, and not specifically "only"
to the Registrar console.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=226187094
This commit is contained in:
guyben 2018-12-19 09:42:22 -08:00 committed by Michael Muller
parent 56b61ad5a2
commit 51f22a15ed
8 changed files with 78 additions and 32 deletions

View file

@ -509,7 +509,7 @@ public final class RegistryConfig {
/**
* The email address that outgoing emails from the app are sent from.
*
* @see google.registry.ui.server.registrar.SendEmailUtils
* @see google.registry.ui.server.SendEmailUtils
*/
@Provides
@Config("gSuiteOutgoingEmailAddress")
@ -520,10 +520,10 @@ public final class RegistryConfig {
/**
* The display name that is used on outgoing emails sent by Nomulus.
*
* @see google.registry.ui.server.registrar.SendEmailUtils
* @see google.registry.ui.server.SendEmailUtils
*/
@Provides
@Config("gSuiteOutoingEmailDisplayName")
@Config("gSuiteOutgoingEmailDisplayName")
public static String provideGSuiteOutgoingEmailDisplayName(RegistryConfigSettings config) {
return config.gSuite.outgoingEmailDisplayName;
}

View file

@ -13,12 +13,16 @@ java_library(
"//java/google/registry/ui/css:registrar_dbg.css.js",
],
deps = [
"//java/google/registry/config",
"//java/google/registry/model",
"//java/google/registry/ui",
"//java/google/registry/ui/forms",
"//java/google/registry/util",
"@com_google_appengine_api_1_0_sdk",
"@com_google_code_findbugs_jsr305",
"@com_google_dagger",
"@com_google_flogger",
"@com_google_flogger_system_backend",
"@com_google_guava",
"@com_google_re2j",
"@io_bazel_rules_closure//closure/templates",

View file

@ -12,13 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.ui.server.registrar;
package google.registry.ui.server;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.Iterables.toArray;
import com.google.common.base.Joiner;
import com.google.common.collect.Streams;
import com.google.common.collect.ImmutableList;
import com.google.common.flogger.FluentLogger;
import google.registry.config.RegistryConfig.Config;
import google.registry.util.SendEmailService;
@ -37,30 +37,40 @@ public class SendEmailUtils {
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private final String gSuiteOutgoingEmailAddress;
private final String gSuiteOutoingEmailDisplayName;
private final String gSuiteOutgoingEmailDisplayName;
private final SendEmailService emailService;
private final ImmutableList<String> registrarChangesNotificationEmailAddresses;
@Inject
public SendEmailUtils(
@Config("gSuiteOutgoingEmailAddress") String gSuiteOutgoingEmailAddress,
@Config("gSuiteOutoingEmailDisplayName") String gSuiteOutoingEmailDisplayName,
@Config("gSuiteOutgoingEmailDisplayName") String gSuiteOutgoingEmailDisplayName,
@Config("registrarChangesNotificationEmailAddresses")
ImmutableList<String> registrarChangesNotificationEmailAddresses,
SendEmailService emailService) {
this.gSuiteOutgoingEmailAddress = gSuiteOutgoingEmailAddress;
this.gSuiteOutoingEmailDisplayName = gSuiteOutoingEmailDisplayName;
this.gSuiteOutgoingEmailDisplayName = gSuiteOutgoingEmailDisplayName;
this.emailService = emailService;
this.registrarChangesNotificationEmailAddresses = registrarChangesNotificationEmailAddresses;
}
/**
* Sends an email from Nomulus to the specified recipient(s). Returns true iff sending was
* successful.
* Sends an email from Nomulus to the registrarChangesNotificationEmailAddresses. Returns true iff
* sending to at least 1 address was successful.
*
* <p>This means that if there are no recepients ({@link #hasRecepients} returns false), this will
* return false even thought no error happened.
*
* <p>This also means that if there are multiple recepients, it will return true even if some (but
* not all) of the recepients had an error.
*/
public boolean sendEmail(Iterable<String> addresses, final String subject, String body) {
public boolean sendEmail(final String subject, String body) {
try {
Message msg = emailService.createMessage();
msg.setFrom(
new InternetAddress(gSuiteOutgoingEmailAddress, gSuiteOutoingEmailDisplayName));
new InternetAddress(gSuiteOutgoingEmailAddress, gSuiteOutgoingEmailDisplayName));
List<InternetAddress> emails =
Streams.stream(addresses)
registrarChangesNotificationEmailAddresses.stream()
.map(
emailAddress -> {
try {
@ -85,9 +95,17 @@ public class SendEmailUtils {
} catch (Throwable t) {
logger.atSevere().withCause(t).log(
"Could not email to addresses %s with subject '%s'.",
Joiner.on(", ").join(addresses), subject);
Joiner.on(", ").join(registrarChangesNotificationEmailAddresses), subject);
return false;
}
return true;
}
/**
* Returns whether there are any recepients set up. {@link #sendEmail} will always return false if
* there are no recepients.
*/
public boolean hasRecepients() {
return !registrarChangesNotificationEmailAddresses.isEmpty();
}
}

View file

@ -33,7 +33,6 @@ import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import com.google.common.collect.Streams;
import com.google.common.flogger.FluentLogger;
import google.registry.config.RegistryConfig.Config;
import google.registry.config.RegistryEnvironment;
import google.registry.model.registrar.Registrar;
import google.registry.model.registrar.RegistrarContact;
@ -51,6 +50,7 @@ import google.registry.security.JsonResponseHelper;
import google.registry.ui.forms.FormException;
import google.registry.ui.forms.FormFieldException;
import google.registry.ui.server.RegistrarFormFields;
import google.registry.ui.server.SendEmailUtils;
import google.registry.util.AppEngineServiceUtils;
import google.registry.util.CollectionUtils;
import google.registry.util.DiffUtils;
@ -92,8 +92,6 @@ public class RegistrarSettingsAction implements Runnable, JsonActionRunner.JsonA
@Inject AuthResult authResult;
@Inject RegistryEnvironment registryEnvironment;
@Inject @Config("registrarChangesNotificationEmailAddresses") ImmutableList<String>
registrarChangesNotificationEmailAddresses;
@Inject RegistrarSettingsAction() {}
private static final Predicate<RegistrarContact> HAS_PHONE =
@ -478,7 +476,7 @@ public class RegistrarSettingsAction implements Runnable, JsonActionRunner.JsonA
ImmutableSet<RegistrarContact> existingContacts,
Registrar updatedRegistrar,
ImmutableSet<RegistrarContact> updatedContacts) {
if (registrarChangesNotificationEmailAddresses.isEmpty()) {
if (!sendEmailUtils.hasRecepients()) {
return;
}
@ -495,7 +493,6 @@ public class RegistrarSettingsAction implements Runnable, JsonActionRunner.JsonA
enqueueRegistrarSheetSync(appEngineServiceUtils.getCurrentVersionHostname("backend"));
String environment = Ascii.toLowerCase(String.valueOf(registryEnvironment));
sendEmailUtils.sendEmail(
registrarChangesNotificationEmailAddresses,
String.format(
"Registrar %s (%s) updated in %s",
existingRegistrar.getRegistrarName(),