diff --git a/src/prerna/io/connector/mail/AbstractReplyMailReactor.java b/src/prerna/io/connector/mail/AbstractReplyMailReactor.java index 64cbc8785e..cb96627917 100644 --- a/src/prerna/io/connector/mail/AbstractReplyMailReactor.java +++ b/src/prerna/io/connector/mail/AbstractReplyMailReactor.java @@ -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. @@ -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 attachments) { + List 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 attachments) { + this(id, body, html, replyAll, recipients, attachments, null); } } @@ -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 output = new LinkedHashMap<>(); @@ -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: diff --git a/src/prerna/io/connector/mail/AbstractSaveDraftReactor.java b/src/prerna/io/connector/mail/AbstractSaveDraftReactor.java index d9b29b89b1..b319263a0e 100644 --- a/src/prerna/io/connector/mail/AbstractSaveDraftReactor.java +++ b/src/prerna/io/connector/mail/AbstractSaveDraftReactor.java @@ -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)); } /** diff --git a/src/prerna/io/connector/ms/outlook/MicrosoftOutlookMailHelper.java b/src/prerna/io/connector/ms/outlook/MicrosoftOutlookMailHelper.java index 9ff341b85f..8fe1fb7835 100644 --- a/src/prerna/io/connector/ms/outlook/MicrosoftOutlookMailHelper.java +++ b/src/prerna/io/connector/ms/outlook/MicrosoftOutlookMailHelper.java @@ -601,6 +601,50 @@ public Map 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 setSubject(String accessToken, Map 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 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 fillHtmlDraft(String accessToken, Map draft, String html) { return fillHtmlDraft(accessToken, draft, html, null, null); } diff --git a/src/prerna/io/connector/ms/outlook/MicrosoftOutlookReplyMailReactor.java b/src/prerna/io/connector/ms/outlook/MicrosoftOutlookReplyMailReactor.java index 1e1939c4ad..559d10b41d 100644 --- a/src/prerna/io/connector/ms/outlook/MicrosoftOutlookReplyMailReactor.java +++ b/src/prerna/io/connector/ms/outlook/MicrosoftOutlookReplyMailReactor.java @@ -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()); diff --git a/src/prerna/io/connector/ms/outlook/MicrosoftOutlookSaveDraftReactor.java b/src/prerna/io/connector/ms/outlook/MicrosoftOutlookSaveDraftReactor.java index cd92271b0c..eb02a3e5ac 100644 --- a/src/prerna/io/connector/ms/outlook/MicrosoftOutlookSaveDraftReactor.java +++ b/src/prerna/io/connector/ms/outlook/MicrosoftOutlookSaveDraftReactor.java @@ -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)); } diff --git a/test/prerna/io/connector/ms/outlook/MicrosoftFormattedDraftUnitTests.java b/test/prerna/io/connector/ms/outlook/MicrosoftFormattedDraftUnitTests.java index 42126a4fe8..a550a814de 100644 --- a/test/prerna/io/connector/ms/outlook/MicrosoftFormattedDraftUnitTests.java +++ b/test/prerna/io/connector/ms/outlook/MicrosoftFormattedDraftUnitTests.java @@ -172,6 +172,52 @@ void aReplyWhoseFilesCannotBeAddedLeavesNoDraftBehind(@TempDir Path folder) thro } } + @Test + void aReplyWithASubjectGivesItsDraftThatSubject() throws Exception { + List calls = new ArrayList<>(); + try (MockedStatic login = mockStatic(MicrosoftLoginUtils.class); + MockedStatic 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.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 calls = new ArrayList<>(); + try (MockedStatic login = mockStatic(MicrosoftLoginUtils.class); + MockedStatic 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 calls = new ArrayList<>();