Arsnael commented on code in PR #2817:
URL: https://github.com/apache/james-project/pull/2817#discussion_r2373850277
##########
server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/service/UserMailboxesService.java:
##########
@@ -213,4 +222,81 @@ private Stream<MailboxMetaData>
listUserMailboxes(MailboxSession mailboxSession)
.toStream();
}
+ public Mono<Result> runRulesOnMailbox(Username username, MailboxName
mailboxName, Rules rules, RunRulesOnMailboxTask.Context context) {
+ MailboxSession mailboxSession =
mailboxManager.createSystemSession(username);
+ RuleMatcher ruleMatcher = new RuleMatcher(rules.getRules());
+
+ return
Mono.from(mailboxManager.getMailboxReactive(MailboxPath.forUser(username,
mailboxName.asString()), mailboxSession))
+ .flatMapMany(messageManager ->
Flux.from(messageManager.getMessagesReactive(MessageRange.all(),
FetchGroup.HEADERS, mailboxSession))
+ .concatMap(Throwing.function(messageResult ->
runRulesOnMessage(ruleMatcher, messageResult, mailboxSession, context))))
+ .onErrorResume(e -> {
+ LOGGER.error("Error when applying rules to mailbox. Mailbox {}
for user {}", mailboxName.asString(), username, e);
+ context.incrementFails();
+ return Mono.just(Result.PARTIAL);
+ })
+ .reduce(Task::combine)
+ .switchIfEmpty(Mono.just(Result.COMPLETED))
+ .doFinally(any ->
mailboxManager.endProcessingRequest(mailboxSession));
+ }
+
+ private Flux<Result> runRulesOnMessage(RuleMatcher ruleMatcher,
MessageResult messageResult, MailboxSession mailboxSession,
RunRulesOnMailboxTask.Context context) throws MailboxException {
+ return Flux.fromStream(ruleMatcher.findApplicableRules(messageResult))
+ .map(Rule::getAction)
+ .concatMap(action -> applyActionOnMessage(messageResult, action,
mailboxSession, context));
+ }
+
+ private Mono<Result> applyActionOnMessage(MessageResult messageResult,
Rule.Action action, MailboxSession mailboxSession,
RunRulesOnMailboxTask.Context context) {
+ actionOnMessagePreconditions(action);
+ return appendInMailboxes(messageResult, action, mailboxSession,
context);
+ }
+
+ private void actionOnMessagePreconditions(Rule.Action action) { // TODO
move up to the task route?
+ if (action.isMarkAsSeen() || action.isMarkAsImportant() ||
action.isReject()
+ || action.getForward().isPresent() ||
!action.getWithKeywords().isEmpty()) {
+ throw new NotImplementedException("Only action on moving messages
is supported for now");
+ }
+
+ if (action.getAppendInMailboxes().getMailboxIds().isEmpty()) {
+ throw new IllegalArgumentException("Move action should not be
empty");
+ }
+ }
+
+ private Mono<Result> appendInMailboxes(MessageResult messageResult,
Rule.Action action, MailboxSession mailboxSession,
RunRulesOnMailboxTask.Context context) {
+ ImmutableList<String> mailboxIds =
action.getAppendInMailboxes().getMailboxIds();
+
+ if (mailboxIds.isEmpty()) {
+ return Mono.just(Result.COMPLETED);
+ }
+
+ if (mailboxIds.contains(messageResult.getMailboxId().serialize())) {
+ return copyInMailboxes(messageResult.getUid(),
messageResult.getMailboxId(), mailboxIds, mailboxSession, context);
+ } else {
+ return moveInMailboxes(messageResult, mailboxIds, mailboxSession,
context);
+ }
Review Comment:
Completely missed and forgot about that method... would defo make the logic
easier, thanks
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]