forked from RedHatInsights/notifications-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RHCLOUD-29776] Introduce recipients limit per email (RedHatInsights#…
…2377) Co-authored-by: Gwenneg Lepage <[email protected]>
- Loading branch information
Showing
12 changed files
with
232 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,22 +2,30 @@ | |
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.redhat.cloud.notifications.connector.email.config.EmailConnectorConfig; | ||
import com.redhat.cloud.notifications.connector.email.constants.ExchangeProperty; | ||
import com.redhat.cloud.notifications.connector.email.model.settings.User; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
import org.apache.camel.Exchange; | ||
import org.apache.camel.Processor; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.stream.Collectors; | ||
|
||
import static java.util.stream.Collectors.toSet; | ||
|
||
@ApplicationScoped | ||
public class RecipientsResolverResponseProcessor implements Processor { | ||
|
||
@Inject | ||
ObjectMapper objectMapper; | ||
|
||
@Inject | ||
EmailConnectorConfig emailConnectorConfig; | ||
|
||
/** | ||
* Processes the response from the recipients-resolver service. Grabs the users from the | ||
* response. | ||
|
@@ -28,8 +36,20 @@ public class RecipientsResolverResponseProcessor implements Processor { | |
@Override | ||
public void process(final Exchange exchange) throws JsonProcessingException { | ||
final String body = exchange.getMessage().getBody(String.class); | ||
final List<User> recipientsList = Arrays.asList(this.objectMapper.readValue(body, User[].class)); | ||
final Set<String> recipientsList = Arrays.asList(this.objectMapper.readValue(body, User[].class)) | ||
.stream().map(User::getEmail).collect(toSet()); | ||
|
||
Set<String> emails = exchange.getProperty(ExchangeProperty.EMAIL_RECIPIENTS, Set.class); | ||
recipientsList.addAll(emails); | ||
|
||
// We have to remove one from the limit, because a default recipient (like [email protected]) will be automatically added | ||
exchange.setProperty(ExchangeProperty.FILTERED_USERS, partition(recipientsList, emailConnectorConfig.getMaxRecipientsPerEmail() - 1)); | ||
} | ||
|
||
exchange.setProperty(ExchangeProperty.FILTERED_USERS, new HashSet<>(recipientsList)); | ||
private static Set<List<String>> partition(Set<String> collection, int n) { | ||
AtomicInteger counter = new AtomicInteger(); | ||
return collection.stream() | ||
.collect(Collectors.groupingBy(it -> counter.getAndIncrement() / n)) | ||
.values().stream().collect(Collectors.toSet()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 0 additions & 63 deletions
63
...ail/src/test/java/com/redhat/cloud/notifications/connector/email/EmptyRecipientsTest.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.