Skip to content

Commit

Permalink
Remove individual emails support in connector-email (RedHatInsights#2310
Browse files Browse the repository at this point in the history
)
  • Loading branch information
gwenneg authored Nov 13, 2023
1 parent 5177d83 commit c70df92
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 380 deletions.
5 changes: 0 additions & 5 deletions .rhcicd/clowdapp-connector-email.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ objects:
value: ${NOTIFICATIONS_CONNECTOR_SEDA_CONCURRENT_CONSUMERS}
- name: NOTIFICATIONS_CONNECTOR_SEDA_QUEUE_SIZE
value: ${NOTIFICATIONS_CONNECTOR_SEDA_QUEUE_SIZE}
- name: NOTIFICATIONS_CONNECTOR_SINGLE_EMAIL_PER_USER_ENABLED
value: ${NOTIFICATIONS_CONNECTOR_SINGLE_EMAIL_PER_USER_ENABLED}
- name: NOTIFICATIONS_CONNECTOR_USER_PROVIDER_BOP_API_TOKEN
valueFrom:
secretKeyRef:
Expand Down Expand Up @@ -237,9 +235,6 @@ parameters:
- name: NOTIFICATIONS_CONNECTOR_SEDA_QUEUE_SIZE
description: Maximum capacity of the SEDA queue
value: "1"
- name: NOTIFICATIONS_CONNECTOR_SINGLE_EMAIL_PER_USER_ENABLED
description: Is sending a single email per user enabled?
value: "false"
- name: NOTIFICATIONS_CONNECTOR_USER_PROVIDER_CACHE_EXPIRE_AFTER_WRITE
description: Expiration delay in seconds of cached users
value: "600"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public void configureRoute() throws Exception {

// Once the split has finished, we can send the exchange to the BOP
// route.
.to(direct(Routes.SEND_EMAIL_BOP_CHOICE));
.to(direct(Routes.SEND_EMAIL_BOP));

/*
* Fetches the users from recipients resolver.
Expand Down Expand Up @@ -375,21 +375,6 @@ public void configureRoute() throws Exception {
.end()
.process(this.recipientsFilter);

from(direct(Routes.SEND_EMAIL_BOP_CHOICE))
.routeId(Routes.SEND_EMAIL_BOP_CHOICE)
.choice()
.when(shouldSkipEmail())
// TODO Lower this log level to DEBUG later.
.log(INFO, getClass().getName(), "Skipped Email notification because the recipients list was empty [orgId=${exchangeProperty." + ORG_ID + "}, historyId=${exchangeProperty." + ID + "}]")
.when(constant(this.emailConnectorConfig.isSingleEmailPerUserEnabled()))
.to(direct(Routes.SEND_EMAIL_BOP_SINGLE_PER_USER))
.log(INFO, getClass().getName(), "Sent single recipient Email notification [orgId=${exchangeProperty." + ORG_ID + "}, historyId=${exchangeProperty." + ID + "}]")
.otherwise()
.to(direct(Routes.SEND_EMAIL_BOP))
.log(INFO, getClass().getName(), "Sent Email notification [orgId=${exchangeProperty." + ORG_ID + "}, historyId=${exchangeProperty." + ID + "}]")
.end()
.to(direct(SUCCESS));

/*
* Prepares the payload accepted by BOP and sends the request to
* the service.
Expand All @@ -398,23 +383,18 @@ public void configureRoute() throws Exception {

from(direct(Routes.SEND_EMAIL_BOP))
.routeId(Routes.SEND_EMAIL_BOP)
// Clear all the headers that may come from the previous route.
.removeHeaders("*")
.process(this.BOPRequestPreparer)
.to(bopEndpoint);

/*
* Temporary route in order to be able to send an email per user,
* instead of one per multiple users.
*/
from(direct(Routes.SEND_EMAIL_BOP_SINGLE_PER_USER))
.routeId(Routes.SEND_EMAIL_BOP_SINGLE_PER_USER)
// Clear all the headers that may come from the previous route.
.removeHeaders("*")
.split(simpleF("${exchangeProperty.%s}", FILTERED_USERS))
.setProperty(ExchangeProperty.SINGLE_EMAIL_PER_USER, constant(true))
.to(direct(Routes.SEND_EMAIL_BOP))
.end();
.choice()
.when(shouldSkipEmail())
// TODO Lower this log level to DEBUG later.
.log(INFO, getClass().getName(), "Skipped Email notification because the recipients list was empty [orgId=${exchangeProperty." + ORG_ID + "}, historyId=${exchangeProperty." + ID + "}]")
.otherwise()
// Clear all the headers that may come from the previous route.
.removeHeaders("*")
.process(this.BOPRequestPreparer)
.to(bopEndpoint)
.log(INFO, getClass().getName(), "Sent Email notification [orgId=${exchangeProperty." + ORG_ID + "}, historyId=${exchangeProperty." + ID + "}]")
.end()
.to(direct(SUCCESS));
}

private Predicate shouldSkipEmail() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ public class EmailConnectorConfig extends ConnectorConfig {
private static final String RBAC_ELEMENTS_PAGE = "notifications.connector.user-provider.rbac.elements-per-page";
private static final String RBAC_URL = "notifications.connector.user-provider.rbac.url";
private static final String SKIP_BOP_USERS_RESOLUTION = "notifications.connector.bop.skip-users-resolution";
@Deprecated(forRemoval = true)
public static final String SINGLE_EMAIL_PER_USER = "notifications.connector.single-email-per-user.enabled";

// The following two keys are public in order to make it easier for
// overriding them in the tests.
Expand All @@ -54,9 +52,6 @@ public class EmailConnectorConfig extends ConnectorConfig {
@ConfigProperty(name = BOP_URL)
String bopURL;

@ConfigProperty(name = SINGLE_EMAIL_PER_USER, defaultValue = "true")
boolean singleEmailPerUserEnabled;

@ConfigProperty(name = FETCH_USERS_RBAC_ENABLED, defaultValue = "true")
boolean fetchUsersWithRBAC;

Expand Down Expand Up @@ -126,7 +121,6 @@ public void log() {
additionalEntries.put(RBAC_APPLICATION_KEY, this.rbacApplicationKey);
additionalEntries.put(RBAC_ELEMENTS_PAGE, this.rbacElementsPerPage);
additionalEntries.put(RBAC_URL, this.rbacURL);
additionalEntries.put(SINGLE_EMAIL_PER_USER, this.singleEmailPerUserEnabled);
additionalEntries.put(USER_PROVIDER_CACHE_EXPIRE_AFTER_WRITE, this.userProviderCacheExpireAfterWrite);
additionalEntries.put(SKIP_BOP_USERS_RESOLUTION, skipBopUsersResolution);
additionalEntries.put(NOTIFICATIONS_RECIPIENTS_RESOLVER_MODULE_ENABLED, recipientsResolverModuleEnabled);
Expand Down Expand Up @@ -233,9 +227,6 @@ public String getRbacURL() {
return this.rbacURL;
}

public boolean isSingleEmailPerUserEnabled() {
return this.singleEmailPerUserEnabled;
}

public int getUserProviderCacheExpireAfterWrite() {
return userProviderCacheExpireAfterWrite;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ public class ExchangeProperty {
* endpoints and the received event.
*/
public static final String RECIPIENT_SETTINGS = "recipient_settings";
/**
* A flag to determine if we need to send the email individually per user.
*/
public static final String SINGLE_EMAIL_PER_USER = "single_email_per_user";
/**
* Holds the list of usernames who subscribed to the event type
* that triggered the notification.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ public class Routes {
public static final String FETCH_USERS_RBAC = "fetch-users-rbac";
public static final String SEND_EMAIL_BOP = "send-email-bop";
@Deprecated(forRemoval = true)
public static final String SEND_EMAIL_BOP_CHOICE = "send-email-bop-choice";
@Deprecated(forRemoval = true)
public static final String SEND_EMAIL_BOP_SINGLE_PER_USER = "send-email-single-per-user";
@Deprecated(forRemoval = true)
public static final String RESOLVE_USERS_WITHOUT_RECIPIENTS_RESOLVER_CLOWDAPP = "fetch-users-without-recipients-resolver-clowdapp";

public static final String RESOLVE_USERS_WITH_RECIPIENTS_RESOLVER_CLOWDAPP = "fetch-users-with-recipients-resolver-clowdapp";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,20 @@
import com.redhat.cloud.notifications.connector.email.model.bop.Emails;
import com.redhat.cloud.notifications.connector.email.model.bop.SendEmailsRequest;
import com.redhat.cloud.notifications.connector.email.model.settings.User;
import io.quarkus.logging.Log;
import io.vertx.core.json.JsonObject;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.component.http.HttpMethods;
import org.eclipse.microprofile.config.inject.ConfigProperty;

import java.util.Arrays;
import java.util.Collections;
import java.util.Set;

import static java.util.stream.Collectors.toSet;

@ApplicationScoped
public class BOPRequestPreparer implements Processor {

@ConfigProperty(name = "env.name", defaultValue = "unknown")
String environment;

@Inject
EmailConnectorConfig emailConnectorConfig;

Expand All @@ -38,32 +31,15 @@ public class BOPRequestPreparer implements Processor {
public void process(final Exchange exchange) {
final String subject = exchange.getProperty(ExchangeProperty.RENDERED_SUBJECT, String.class);
final String body = exchange.getProperty(ExchangeProperty.RENDERED_BODY, String.class);
final Set<String> recipients;

// We still need to support sending individual emails per user for a
// while. However, that will go away soon, so we can consider the
// following code block very much deprecated.
final Boolean singleEmailPerUser = exchange.getProperty(ExchangeProperty.SINGLE_EMAIL_PER_USER, Boolean.class);
if (singleEmailPerUser != null && singleEmailPerUser) {
User recipient = exchange.getMessage().getBody(User.class);
if (emailConnectorConfig.isSkipBopUsersResolution()) {
recipients = Collections.singleton(recipient.getEmail());
} else {
recipients = Collections.singleton(recipient.getUsername());
}
final Set<String> recipients;
final Set<User> users = exchange.getProperty(ExchangeProperty.FILTERED_USERS, Set.class);
if (emailConnectorConfig.isSkipBopUsersResolution()) {
recipients = users.stream().map(User::getEmail).collect(toSet());
Set<String> emails = exchange.getProperty(ExchangeProperty.EMAIL_RECIPIENTS, Set.class);
recipients.addAll(emails);
} else {
final Set<User> users = exchange.getProperty(ExchangeProperty.FILTERED_USERS, Set.class);
if (emailConnectorConfig.isSkipBopUsersResolution()) {
recipients = users.stream().map(User::getEmail).collect(toSet());
Set<String> emails = exchange.getProperty(ExchangeProperty.EMAIL_RECIPIENTS, Set.class);
recipients.addAll(emails);
} else {
recipients = users.stream().map(User::getUsername).collect(toSet());
}
}

if ("stage".equals(environment)) {
Log.infof("Recipients: %s", Arrays.toString(recipients.toArray()));
recipients = users.stream().map(User::getUsername).collect(toSet());
}

final Email email = new Email(
Expand Down

This file was deleted.

Loading

0 comments on commit c70df92

Please sign in to comment.