Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/prerna/io/connector/mail/AbstractReplyMailReactor.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
public abstract class AbstractReplyMailReactor extends AbstractDraftDeliveryReactor {

private static final String[] KEYS = { ID, BODY, REPLY_ALL, AS_DRAFT, HTML, OVERRIDE_RECIPIENTS, TO, CC,
ATTACHMENTS };
SUBJECT, ATTACHMENTS };

/**
* A reply the caller wrote.
Expand All @@ -56,12 +56,21 @@ public abstract class AbstractReplyMailReactor extends AbstractDraftDeliveryReac
* @param recipients exactly who the reply goes to, or null for whoever the
* reply goes to on its own
* @param attachments the files to attach
* @param subject the subject to give the reply, or null to keep the one it
* is answering
*/
public record ReplyMailRequest(String id, String body, boolean html, boolean replyAll, MailRecipients recipients,
List<File> attachments) {
List<File> attachments, String subject) {

public ReplyMailRequest {
attachments = attachments == null ? List.of() : List.copyOf(attachments);
subject = subject == null || subject.isBlank() ? null : subject.trim();
}

/** A reply that keeps the subject of the message it answers. */
public ReplyMailRequest(String id, String body, boolean html, boolean replyAll, MailRecipients recipients,
List<File> attachments) {
this(id, body, html, replyAll, recipients, attachments, null);
}
}

Expand Down Expand Up @@ -103,7 +112,7 @@ protected final NounMetadata executeAuthenticated() {
"At least one recipient in " + TO + " or " + CC + " is required to send a reply to chosen recipients.");
}
ReplyMailRequest request = new ReplyMailRequest(id, body, readBoolean(HTML, false), replyAll, recipients,
readInsightFiles(ATTACHMENTS));
readInsightFiles(ATTACHMENTS), readString(SUBJECT));
ComposedMail draft = draftReply(this.insight.getUser(), request);

Map<String, Object> output = new LinkedHashMap<>();
Expand Down Expand Up @@ -140,6 +149,8 @@ protected final String describeKey(String key) {
case OVERRIDE_RECIPIENTS:
return "Optional boolean to send to exactly the to and cc lists passed, including empty lists, instead "
+ "of whoever the reply goes to on its own. Defaults to false.";
case SUBJECT:
return "Optional subject for the reply. Omit it to keep the subject of the message being answered.";
case TO:
return "Recipients when overrideRecipients is true, as a list of email addresses.";
case CC:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private AbstractReplyMailReactor.ReplyMailRequest readReply(String id) throws Ex
// to and cc passed with replyTo replace the recipients the reply would have on its own
MailRecipients recipients = to.isEmpty() && cc.isEmpty() ? null : new MailRecipients(to, cc);
return new AbstractReplyMailReactor.ReplyMailRequest(id, body, readBoolean(HTML, false),
readBoolean(REPLY_ALL, false), recipients, readInsightFiles(ATTACHMENTS));
readBoolean(REPLY_ALL, false), recipients, readInsightFiles(ATTACHMENTS), readString(SUBJECT));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,50 @@ public Map<String, Object> replyHtmlDraft(String accessToken, String uid, String
validatedCc);
}

/**
* Give a draft a subject of its own, keeping everything else on it. A draft whose
* subject cannot be changed is deleted, since it could still be sent with the
* wrong one and every retry would leave another.
*
* @param accessToken the token to write with
* @param draft the draft as Graph created it
* @param subject the subject to give it, or null to leave it as it is
* @return the draft, carrying the new subject
*/
public Map<String, Object> setSubject(String accessToken, Map<String, Object> draft, String subject) {
if (subject == null || subject.isBlank() || draft == null) {
return draft;
}
String id = draft.get("id") instanceof String value ? value : null;
if (id == null || id.isBlank()) {
throw new IllegalStateException("The reply draft could not be confirmed. Check Outlook before retrying.");
}
if (subject.equals(draft.get("subject"))) {
return draft;
}
boolean changed = false;
try {
String response = HttpHelperUtility.patchRequestStringBody(userPath(null) + "/messages/" + encode(id),
headers(accessToken), GSON.toJson(Map.of("subject", subject)), ContentType.APPLICATION_JSON, null,
null, null);
throwOnError(response, "change the draft subject");
Map<String, Object> updated = readMap(response);
if (updated == null || !id.equals(updated.get("id"))) {
throw new IllegalStateException(
"The draft subject could not be confirmed. Check Outlook before retrying.");
}
if (!updated.containsKey("webLink") && draft.get("webLink") instanceof String link) {
updated.put("webLink", link);
}
changed = true;
return updated;
} finally {
if (!changed) {
deleteQuietly(accessToken, null, id);
}
}
}

private Map<String, Object> fillHtmlDraft(String accessToken, Map<String, Object> draft, String html) {
return fillHtmlDraft(accessToken, draft, html, null, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ static ComposedMail draftReplyFor(User user, ReplyMailRequest request) throws Ex
} else {
draft = helper.reply(accessToken, null, request.id(), request.body(), request.replyAll(), true);
}
// a subject the caller chose replaces the one the reply was given
draft = helper.setSubject(accessToken, draft, request.subject());
helper.attachToNewDraft(accessToken, draft, files);
return MicrosoftOutlookMessageMapper.toComposedMail(draft, request.body(), request.html(),
request.attachments().stream().map(OutgoingMail::attachmentName).toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ protected ComposedMail saveDraft(User user, OutgoingMail mail) throws Exception
protected ComposedMail saveReply(User user, ReplyMailRequest request) throws Exception {
// Outlook only finds a message in the signed in user's own mailbox, so an id from elsewhere cannot be answered
ReplyMailRequest resolved = new ReplyMailRequest(outlookId(user, request.id()), request.body(), request.html(),
request.replyAll(), request.recipients(), request.attachments());
request.replyAll(), request.recipients(), request.attachments(), request.subject());
// into the folder chosen in Collaboration settings, when there is one
return MicrosoftOutlookDraftFolder.place(user, MicrosoftOutlookReplyMailReactor.draftReplyFor(user, resolved));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,52 @@ void aReplyWhoseFilesCannotBeAddedLeavesNoDraftBehind(@TempDir Path folder) thro
}
}

@Test
void aReplyWithASubjectGivesItsDraftThatSubject() throws Exception {
List<String> calls = new ArrayList<>();
try (MockedStatic<MicrosoftLoginUtils> login = mockStatic(MicrosoftLoginUtils.class);
MockedStatic<HttpHelperUtility> http = mockStatic(HttpHelperUtility.class, invocation -> {
String url = invocation.getArgument(0);
calls.add(invocation.getMethod().getName() + " " + url);
if (url.endsWith("/createReply")) {
return "{\"id\":\"draft-1\",\"subject\":\"RE: Budget\"}";
}
if (invocation.getMethod().getName().equals("patchRequestStringBody")) {
assertTrue(invocation.<String>getArgument(2).contains("\"subject\":\"testing draft\""));
return "{\"id\":\"draft-1\",\"subject\":\"testing draft\"}";
}
return "";
})) {
login.when(() -> MicrosoftLoginUtils.getValidAccessToken(any())).thenReturn("token");
ReplyMailRequest request = new ReplyMailRequest("source", "Here you go", false, false, null, List.of(),
"testing draft");

new MicrosoftOutlookReplyMailReactor().draftReply(mock(User.class), request);

assertTrue(calls.stream().anyMatch(c -> c.matches("patchRequestStringBody .*/messages/draft-1")),
calls::toString);
assertFalse(calls.stream().anyMatch(c -> c.startsWith("deleteRequestStringBody")), calls::toString);
}
}

@Test
void aReplyWithoutASubjectKeepsTheOneItAnswers() throws Exception {
List<String> calls = new ArrayList<>();
try (MockedStatic<MicrosoftLoginUtils> login = mockStatic(MicrosoftLoginUtils.class);
MockedStatic<HttpHelperUtility> http = mockStatic(HttpHelperUtility.class, invocation -> {
String url = invocation.getArgument(0);
calls.add(invocation.getMethod().getName() + " " + url);
return url.endsWith("/createReply") ? "{\"id\":\"draft-1\",\"subject\":\"RE: Budget\"}" : "";
})) {
login.when(() -> MicrosoftLoginUtils.getValidAccessToken(any())).thenReturn("token");
ReplyMailRequest request = new ReplyMailRequest("source", "Here you go", false, false, null, List.of());

new MicrosoftOutlookReplyMailReactor().draftReply(mock(User.class), request);

assertFalse(calls.stream().anyMatch(c -> c.startsWith("patchRequestStringBody")), calls::toString);
}
}

@Test
void newDraftKeepsHtmlAndNeverSends() {
List<String> calls = new ArrayList<>();
Expand Down
Loading