chibenwa commented on code in PR #2405:
URL: https://github.com/apache/james-project/pull/2405#discussion_r1758815891


##########
server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/SubAddressing.java:
##########
@@ -19,33 +19,86 @@
 
 package org.apache.james.transport.mailets;
 
-import com.github.fge.lambdas.Throwing;
-import com.google.common.collect.ImmutableList;
+import static 
org.apache.james.mailbox.MessageManager.MailboxMetaData.RecentMode.IGNORE;
+
+import java.util.Optional;
+
 import jakarta.inject.Inject;
+import jakarta.inject.Named;
 import jakarta.mail.MessagingException;
+
 import org.apache.james.core.MailAddress;
+import org.apache.james.core.Username;
+import org.apache.james.mailbox.MailboxManager;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MessageManager;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.exception.MailboxNotFoundException;
+import org.apache.james.mailbox.model.MailboxACL;
+import org.apache.james.mailbox.model.MailboxPath;
 import org.apache.james.user.api.UsersRepository;
+import org.apache.james.user.api.UsersRepositoryException;
 import org.apache.mailet.Mail;
 import org.apache.mailet.StorageDirective;
 import org.apache.mailet.base.GenericMailet;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.github.fge.lambdas.Throwing;
+import com.google.common.collect.ImmutableList;
+
+
 
 public class SubAddressing extends GenericMailet {
     private final UsersRepository usersRepository;
+    private final MailboxManager mailboxManager;
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(SubAddressing.class);
 
     @Inject
-    public SubAddressing(UsersRepository usersRepository) {
+    public SubAddressing(UsersRepository usersRepository, 
@Named("mailboxmanager") MailboxManager mailboxManager) {
         this.usersRepository = usersRepository;
+        this.mailboxManager = mailboxManager;
     }
 
     @Override
     public void service(Mail mail) throws MessagingException {
         mail.getRecipients().forEach(recipient ->
-            recipient.getLocalPartDetails("+")
-                .ifPresent(
-                    Throwing.consumer(details ->
-                    
StorageDirective.builder().targetFolders(ImmutableList.of(details)).build()
-                        
.encodeAsAttributes(usersRepository.getUsername(recipient))
-                        .forEach(mail::setAttribute))
-                ));
+            
recipient.getLocalPartDetails(UsersRepository.LOCALPART_DETAIL_DELIMITER)
+                .ifPresent(Throwing.consumer(targetFolder -> 
postIfHasRight(mail, recipient, targetFolder))));

Review Comment:
   postIfHasRight is a brittle method name: it embeds 2 meaning, a condition 
and an action.
   
   Likely we can avoid that by leveraging functional semantic:
   
   ```
               
recipient.getLocalPartDetails(UsersRepository.LOCALPART_DETAIL_DELIMITER)
                   .filter(targetFolder -> hasPostRight(recipient, 
targetFolder))
                   .ifPresentOrElse(Throwing.consumer(targetFolder -> 
post(mail, recipient, targetFolder)),
                       () ->  LOG.info("{} tried to address {}'s subfolder `{}` 
but did not have the right to",
                                mail.getMaybeSender().toString(), recipient, 
targetFolder);));
   ```



-- 
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]

Reply via email to