3030import jakarta .mail .internet .AddressException ;
3131import jakarta .mail .internet .InternetAddress ;
3232
33- import org .apache .commons .lang3 .tuple .Pair ;
3433import org .apache .james .core .MailAddress ;
3534import org .apache .james .mime4j .dom .address .Mailbox ;
3635import org .apache .james .mime4j .field .address .LenientAddressParser ;
@@ -131,26 +130,24 @@ private void manageVacation(MailAddress recipient, Mail processedMail, ZonedDate
131130 return ;
132131 }
133132
134- AccountId accountId = AccountId .fromString (recipient .toString ());
135-
136- Mono <Vacation > vacation = vacationService .retrieveVacation (accountId );
137- Mono <Boolean > alreadySent = vacationService .isNotificationRegistered (
138- AccountId .fromString (recipient .toString ()),
139- RecipientId .fromMailAddress (processedMail .getMaybeSender ().get ()));
140- Pair <Vacation , Boolean > pair = Flux .combineLatest (vacation , alreadySent , Pair ::of )
141- .blockFirst ();
133+ RecipientId replyRecipient = RecipientId .fromMailAddress (processedMail .getMaybeSender ().get ());
134+ VacationInformation vacationInformation = retrieveVacationInformation (recipient , replyRecipient );
142135
143- sendNotificationIfRequired (recipient , processedMail , processingDate , pair .getKey (), pair .getValue ());
136+ boolean shouldSendNotification = vacationInformation .vacation .isActiveAtDate (processingDate ) && !vacationInformation .alreadySent ;
137+ if (shouldSendNotification ) {
138+ sendNotification (processedMail , vacationInformation );
139+ }
144140 }
145141
146- private void sendNotificationIfRequired (MailAddress recipient , Mail processedMail , ZonedDateTime processingDate , Vacation vacation , Boolean alreadySent ) {
147- if (shouldSendNotification (vacation , processingDate , alreadySent )) {
148- sendNotification (recipient , processedMail , vacation );
149- }
142+ record VacationInformation (Vacation vacation , MailAddress recipient , AccountId accountId , RecipientId replyRecipient , Boolean alreadySent ) {
143+
150144 }
151145
152- private boolean shouldSendNotification (Vacation vacation , ZonedDateTime processingDate , boolean alreadySent ) {
153- return vacation .isActiveAtDate (processingDate ) && !alreadySent ;
146+ private VacationInformation retrieveVacationInformation (MailAddress recipient , RecipientId replyRecipient ) {
147+ AccountId accountId = AccountId .fromString (recipient .toString ());
148+ Mono <Vacation > vacation = vacationService .retrieveVacation (accountId );
149+ Mono <Boolean > alreadySent = vacationService .isNotificationRegistered (accountId , replyRecipient );
150+ return Flux .combineLatest (vacation , alreadySent , (a , b ) -> new VacationInformation (a , recipient , accountId , replyRecipient , b )).blockFirst ();
154151 }
155152
156153 private boolean isNoReplySender (Mail processedMail ) {
@@ -168,30 +165,27 @@ private boolean isSentToSelf(Optional<MailAddress> maybeSender, MailAddress reci
168165 .orElse (false );
169166 }
170167
171- private void sendNotification (MailAddress recipient , Mail processedMail , Vacation vacation ) {
168+ private void sendNotification (Mail processedMail , VacationInformation vacationInformation ) {
172169 try {
173170 VacationReply vacationReply = VacationReply .builder (processedMail )
174- .receivedMailRecipient (recipient )
175- .vacation (vacation )
171+ .replyRecipient (vacationInformation .replyRecipient .getMailAddress ())
172+ .receivedMailRecipient (vacationInformation .recipient ())
173+ .vacation (vacationInformation .vacation )
176174 .build (mimeMessageBodyGenerator );
177175
178- sendNotification (vacationReply , recipient );
176+ getMailetContext ().sendMail (getSender (vacationInformation .recipient ),
177+ vacationReply .getRecipients (),
178+ vacationReply .getMimeMessage ());
179179
180- vacationService .registerNotification (AccountId . fromString ( recipient . toString () ),
181- RecipientId . fromMailAddress ( processedMail . getMaybeSender (). get () ),
182- vacation .getToDate ())
180+ vacationService .registerNotification (vacationInformation . accountId ( ),
181+ vacationInformation . replyRecipient ( ),
182+ vacationInformation . vacation () .getToDate ())
183183 .block ();
184184 } catch (MessagingException e ) {
185- LOGGER .warn ("Failed to send JMAP vacation notification from {} to {}" , recipient , processedMail . getMaybeSender (), e );
185+ LOGGER .warn ("Failed to send JMAP vacation notification from {} to {}" , vacationInformation . recipient (), vacationInformation . replyRecipient (). getAsString (), e );
186186 }
187187 }
188188
189- private void sendNotification (VacationReply vacationReply , MailAddress recipient ) throws MessagingException {
190- getMailetContext ().sendMail (getSender (recipient ),
191- vacationReply .getRecipients (),
192- vacationReply .getMimeMessage ());
193- }
194-
195189 private MailAddress getSender (MailAddress recipient ) {
196190 if (!useUserAsMailFrom ) {
197191 return MailAddress .nullSender ();
0 commit comments