mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(ses): apply the ListContacts filter (#4981)
* feat(ses): apply the ListContacts filter ListContacts now honours Filter.FilteredStatus and Filter.TopicFilter the way SES v2 matches them, with the validation messages and precedence SES answers. The filter is applied before the page is cut, so filtered pages are exact, and a token stays a position in the list, as on SES, rather than being bound to the filter. * test(ses): name the filter unit test SesContactFilterServiceTest
This commit is contained in:
1 parent
a68f8eab3a
commit
ef9ed50742
8 files changed
+545
-8
No files matched your search
@@ -34,12 +34,15 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2Json.coerceBoolean;
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2Json.coerceBooleanOrFalse;
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2Json.intMemberOrAbsent;
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2Json.parseTagsArray;
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2Json.putTimestamp;
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2Json.remapV1Exception;
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2Json.requireJsonObject;
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2Json.stringMemberOrAbsent;
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2Json.structureMemberOrAbsent;
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2Json.typedStringMemberOrAbsent;
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2Json.unexpectedStartError;
|
||||
|
||||
/**
|
||||
@@ -237,9 +240,6 @@ public class SesContactController {
|
||||
@Path("/contact-lists/{contactListName}/contacts/list")
|
||||
public Response listContacts(@Context HttpHeaders headers,
|
||||
@PathParam("contactListName") String contactListName, String body) {
|
||||
// AWS uses POST .../contacts/list with Filter/PageSize/NextToken in the body; Floci pages
|
||||
// the contacts (filtering not yet implemented) and rejects a malformed or non-object body
|
||||
// like the other v2 endpoints.
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
try {
|
||||
JsonNode request = (body == null || body.isBlank())
|
||||
@@ -249,7 +249,7 @@ public class SesContactController {
|
||||
Integer pageSize = intMemberOrAbsent(request, "PageSize");
|
||||
String nextToken = stringMemberOrAbsent(request, "NextToken");
|
||||
SesContactService.ContactPage listed = contactService.listContacts(contactListName, region,
|
||||
SesListPaging.V2_LIST_CONTACTS, pageSize, nextToken);
|
||||
SesListPaging.V2_LIST_CONTACTS, pageSize, nextToken, parseContactFilter(request));
|
||||
ObjectNode result = objectMapper.createObjectNode();
|
||||
ArrayNode arr = result.putArray("Contacts");
|
||||
for (Contact c : listed.contacts().items()) {
|
||||
@@ -264,6 +264,21 @@ public class SesContactController {
|
||||
}
|
||||
}
|
||||
|
||||
private static SesContactService.ContactFilter parseContactFilter(JsonNode request) {
|
||||
JsonNode filterNode = structureMemberOrAbsent(request, "Filter");
|
||||
if (filterNode.isMissingNode() || filterNode.isNull()) {
|
||||
return null;
|
||||
}
|
||||
JsonNode topicNode = structureMemberOrAbsent(filterNode, "TopicFilter");
|
||||
SesContactService.TopicFilter topicFilter = null;
|
||||
if (!topicNode.isMissingNode() && !topicNode.isNull()) {
|
||||
topicFilter = new SesContactService.TopicFilter(typedStringMemberOrAbsent(topicNode, "TopicName"),
|
||||
coerceBooleanOrFalse(topicNode.path("UseDefaultIfPreferenceUnavailable")));
|
||||
}
|
||||
return new SesContactService.ContactFilter(typedStringMemberOrAbsent(filterNode, "FilteredStatus"),
|
||||
topicFilter);
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/contact-lists/{contactListName}/contacts/{emailAddress}")
|
||||
public Response getContact(@Context HttpHeaders headers,
|
||||
|
||||
@@ -361,19 +361,76 @@ public class SesContactService {
|
||||
public record ContactPage(PaginatedResult<Contact> contacts, ContactList list) {
|
||||
}
|
||||
|
||||
/** SES reads the page size and the token before it looks the list up. */
|
||||
/** A ListContacts {@code Filter}; {@code topicFilter} is null when the request has none. */
|
||||
public record ContactFilter(String filteredStatus, TopicFilter topicFilter) {
|
||||
}
|
||||
|
||||
public record TopicFilter(String topicName, boolean useDefaultIfPreferenceUnavailable) {
|
||||
}
|
||||
|
||||
/**
|
||||
* SES checks the status enum, then the page size, then the filter's shape, then the token, the
|
||||
* list and last the topic. SES cuts the page before it filters, so its filtered pages come back
|
||||
* short or empty; Floci filters first and returns exact pages. The token is a position in the
|
||||
* list, not bound to the filter, so SES and Floci both take one across filters.
|
||||
*/
|
||||
public ContactPage listContacts(String listName, String region, SesListPaging paging, Integer pageSize,
|
||||
String nextToken) {
|
||||
String nextToken, ContactFilter filter) {
|
||||
if (filter != null && filter.filteredStatus() != null
|
||||
&& !SUBSCRIPTION_STATUSES.contains(filter.filteredStatus())) {
|
||||
throw validationError("filter.filteredStatus",
|
||||
"Member must satisfy enum value set: [OPT_OUT, OPT_IN]");
|
||||
}
|
||||
// Validated here as well as in page() so the size error wins over the filter's shape.
|
||||
paging.pageSize(pageSize);
|
||||
if (filter != null) {
|
||||
if (filter.filteredStatus() == null) {
|
||||
throw new AwsException("BadRequestException", "Invalid FilteredStatus <null>", 400);
|
||||
}
|
||||
if (filter.topicFilter() != null && (filter.topicFilter().topicName() == null
|
||||
|| filter.topicFilter().topicName().isBlank())) {
|
||||
throw new AwsException("BadRequestException", "TopicName can't be blank in TopicFilter.", 400);
|
||||
}
|
||||
}
|
||||
ContactList list = contactListStore.get(contactListKey(region, listName)).orElse(null);
|
||||
List<Contact> contacts = list == null ? List.of() : contactsOf(listName, region);
|
||||
String topicName = filter == null || filter.topicFilter() == null ? null : filter.topicFilter().topicName();
|
||||
boolean topicFound = topicName == null || (list != null && defaultTopicStatus(list, topicName) != null);
|
||||
List<Contact> contacts = list == null || !topicFound ? List.of()
|
||||
: contactsOf(listName, region).stream().filter(c -> matchesFilter(c, list, filter)).toList();
|
||||
PaginatedResult<Contact> page = paging.page(region, contacts, Contact::getEmailAddress, pageSize,
|
||||
nextToken);
|
||||
if (list == null) {
|
||||
throw contactListNotFound(listName);
|
||||
}
|
||||
if (!topicFound) {
|
||||
throw new AwsException("NotFoundException",
|
||||
"List: " + listName + " doesn't contain Topic: " + topicName, 404);
|
||||
}
|
||||
return new ContactPage(page, list);
|
||||
}
|
||||
|
||||
// UnsubscribeAll is OPT_OUT for every topic. Without a topic it is the only thing a status
|
||||
// matches; with one, an explicit preference decides, and the topic default only when asked for.
|
||||
private boolean matchesFilter(Contact contact, ContactList list, ContactFilter filter) {
|
||||
if (filter == null) {
|
||||
return true;
|
||||
}
|
||||
TopicFilter topicFilter = filter.topicFilter();
|
||||
String status;
|
||||
if (contact.isUnsubscribeAll()) {
|
||||
status = "OPT_OUT";
|
||||
} else if (topicFilter == null) {
|
||||
status = "OPT_IN";
|
||||
} else if (explicitTopicStatus(contact, topicFilter.topicName()) != null) {
|
||||
status = explicitTopicStatus(contact, topicFilter.topicName());
|
||||
} else if (topicFilter.useDefaultIfPreferenceUnavailable()) {
|
||||
status = defaultTopicStatus(list, topicFilter.topicName());
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return filter.filteredStatus().equals(status);
|
||||
}
|
||||
|
||||
public ContactsWithList listContacts(String listName, String region) {
|
||||
ContactList list = getContactList(listName, region);
|
||||
return new ContactsWithList(contactsOf(listName, region), list);
|
||||
|
||||
@@ -145,6 +145,42 @@ final class SesV2Json {
|
||||
throw unexpectedStartError(node);
|
||||
}
|
||||
|
||||
/**
|
||||
* A structure member as the AWS deserializer reads it: absent or null is returned as is, an
|
||||
* array is "Start of list found where not expected" and any other non-object "Expected null".
|
||||
*/
|
||||
static JsonNode structureMemberOrAbsent(JsonNode parent, String field) {
|
||||
JsonNode node = parent.path(field);
|
||||
if (node.isMissingNode() || node.isNull() || node.isObject()) {
|
||||
return node;
|
||||
}
|
||||
if (node.isArray()) {
|
||||
throw unexpectedStartError(node);
|
||||
}
|
||||
throw new AwsException("SerializationException", "Expected null", 400);
|
||||
}
|
||||
|
||||
/** A string member with the AWS deserializer's message for each wrong JSON type. */
|
||||
static String typedStringMemberOrAbsent(JsonNode parent, String field) {
|
||||
JsonNode node = parent.path(field);
|
||||
if (node.isMissingNode() || node.isNull()) {
|
||||
return null;
|
||||
}
|
||||
if (node.isTextual()) {
|
||||
return node.textValue();
|
||||
}
|
||||
if (node.isNumber()) {
|
||||
throw new AwsException("SerializationException",
|
||||
"NUMBER_VALUE can not be converted to a String", 400);
|
||||
}
|
||||
if (node.isBoolean()) {
|
||||
throw new AwsException("SerializationException",
|
||||
(node.booleanValue() ? "TRUE_VALUE" : "FALSE_VALUE")
|
||||
+ " can not be converted to a String", 400);
|
||||
}
|
||||
throw unexpectedStartError(node);
|
||||
}
|
||||
|
||||
static AwsException unexpectedStartError(JsonNode node) {
|
||||
if (node.isArray()) {
|
||||
return new AwsException("SerializationException",
|
||||
|
||||
Reference in new issue
Block a user