Skip to content

Commit

Permalink
RHCLOUD-28489 Filter email recipient settings
Browse files Browse the repository at this point in the history
  • Loading branch information
gwenneg committed Sep 22, 2023
1 parent 0f997e9 commit afdcd3a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public class EmailProcessor extends SystemEndpointTypeProcessor {

@Override
public void process(final Event event, final List<Endpoint> endpoints) {
final Set<RecipientSettings> recipientSettings = this.extractAndTransformRecipientSettings(event, endpoints);

// Generate an aggregation if the event supports it.
this.emailSubscriptionTypeProcessor.generateAggregationWhereDue(event);
Expand All @@ -52,6 +51,22 @@ public void process(final Event event, final List<Endpoint> endpoints) {
return;
}

// Get the set of user IDs that should receive an email notification for
// the given event.
final List<String> subscribers = this.getSubscribers(event);

final Set<RecipientSettings> recipientSettings = this.extractAndTransformRecipientSettings(event, endpoints);

// When the user preferences are not ignored and there are no subscribers to the event,
// there's no need to further process the recipient settings because no email will be sent from them.
recipientSettings.removeIf(settings -> !settings.isIgnoreUserPreferences() && subscribers.isEmpty());

// If we removed all recipient settings, it means no email will be sent from the event and we can exit this method.
if (recipientSettings.isEmpty()) {
Log.debugf("[event_uuid: %s] The event was skipped because there were no subscribers for it and user preferences are not ignored", event.getId());
return;
}

// Render the subject and the body of the email.
final InstantEmailTemplate instantEmailTemplate = instantEmailTemplateMaybe.get();

Expand All @@ -64,14 +79,6 @@ public void process(final Event event, final List<Endpoint> endpoints) {
final String subject = this.templateService.renderTemplate(event.getEventWrapper().getEvent(), subjectTemplate);
final String body = this.templateService.renderTemplate(event.getEventWrapper().getEvent(), bodyTemplate);

// Get the set of user IDs that should receive an email notification for
// the given event.
final List<String> subscribers = this.getSubscribers(event);
if (subscribers.isEmpty()) {
Log.debugf("[event_uuid: %s] The event was skipped because there were no subscribers for it", event.getId());
return;
}

// Prepare all the data to be sent to the connector.
final EmailNotification emailNotification = new EmailNotification(
body,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ public void process(Event event, List<Endpoint> endpoints) {
* @param event the event to be included, or not, in the aggregation.
*/
public void generateAggregationWhereDue(final Event event) {
// TODO: Check if we should be using the eventType or application's id instead of the name
final EventType eventType = event.getEventType();
final String bundleName = eventType.getApplication().getBundle().getName();
final String applicationName = eventType.getApplication().getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ private Set<User> recipientUsers(String orgId, RecipientSettings request, Set<St
If the subscription type is opt-in, the user preferences should NOT be ignored and the subscribers Set is empty,
then we don't need to retrieve the users from RBAC/IT/BOP because we'll return an empty Set anyway.
*/
// TODO Report this change in the email connector ASAP.
if (isOptIn && !request.isIgnoreUserPreferences() && subscribers.isEmpty()) {
usersCount.set(0);
return Collections.emptySet();
Expand Down

0 comments on commit afdcd3a

Please sign in to comment.