james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [1/4] james-project git commit: JAMES-2340 Incoming mails in Inbox should be considered as ham
Date Wed, 04 Apr 2018 02:57:10 GMT
Repository: james-project
Updated Branches:
  refs/heads/master 12bbac5c5 -> 04ec4777a


JAMES-2340 Incoming mails in Inbox should be considered as ham


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/61f90721
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/61f90721
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/61f90721

Branch: refs/heads/master
Commit: 61f907211116106659d30b75999c78b20b16c8dd
Parents: 612138a
Author: Antoine Duprat <aduprat@linagora.com>
Authored: Tue Apr 3 13:02:21 2018 +0200
Committer: Antoine Duprat <aduprat@linagora.com>
Committed: Tue Apr 3 13:03:41 2018 +0200

----------------------------------------------------------------------
 .../spamassassin/SpamAssassinListener.java      | 14 +++++++
 .../spamassassin/SpamAssassinListenerTest.java  | 44 +++++++++++++++++++-
 2 files changed, 57 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/61f90721/mailbox/plugin/spamassassin/src/main/java/org/apache/james/mailbox/spamassassin/SpamAssassinListener.java
----------------------------------------------------------------------
diff --git a/mailbox/plugin/spamassassin/src/main/java/org/apache/james/mailbox/spamassassin/SpamAssassinListener.java
b/mailbox/plugin/spamassassin/src/main/java/org/apache/james/mailbox/spamassassin/SpamAssassinListener.java
index 9572051..7160625 100644
--- a/mailbox/plugin/spamassassin/src/main/java/org/apache/james/mailbox/spamassassin/SpamAssassinListener.java
+++ b/mailbox/plugin/spamassassin/src/main/java/org/apache/james/mailbox/spamassassin/SpamAssassinListener.java
@@ -19,6 +19,7 @@
 package org.apache.james.mailbox.spamassassin;
 
 import java.io.InputStream;
+import java.util.List;
 
 import javax.inject.Inject;
 
@@ -28,8 +29,10 @@ import org.apache.james.mailbox.exception.MailboxException;
 import org.apache.james.mailbox.model.MailboxId;
 import org.apache.james.mailbox.model.MailboxPath;
 import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
+import org.apache.james.mailbox.store.event.EventFactory;
 import org.apache.james.mailbox.store.event.MessageMoveEvent;
 import org.apache.james.mailbox.store.event.SpamEventListener;
+import org.apache.james.mailbox.store.mail.model.MailboxMessage;
 import org.apache.james.mailbox.store.mail.model.Message;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -76,6 +79,17 @@ public class SpamAssassinListener implements SpamEventListener {
                 spamAssassin.learnHam(messages, messageMoveEvent.getSession().getUser().getUserName());
             }
         }
+        if (event instanceof EventFactory.AddedImpl) {
+            EventFactory.AddedImpl addedEvent = (EventFactory.AddedImpl) event;
+            if (addedEvent.getMailboxPath().isInbox()) {
+                List<InputStream> contents = addedEvent.getAvailableMessages()
+                    .values()
+                    .stream()
+                    .map(Throwing.function(MailboxMessage::getFullContent))
+                    .collect(Guavate.toImmutableList());
+                spamAssassin.learnHam(contents, addedEvent.getSession().getUser().getUserName());
+            }
+        }
     }
 
     public ImmutableList<InputStream> retrieveMessages(MessageMoveEvent messageMoveEvent)
{

http://git-wip-us.apache.org/repos/asf/james-project/blob/61f90721/mailbox/plugin/spamassassin/src/test/java/org/apache/james/mailbox/spamassassin/SpamAssassinListenerTest.java
----------------------------------------------------------------------
diff --git a/mailbox/plugin/spamassassin/src/test/java/org/apache/james/mailbox/spamassassin/SpamAssassinListenerTest.java
b/mailbox/plugin/spamassassin/src/test/java/org/apache/james/mailbox/spamassassin/SpamAssassinListenerTest.java
index 4ec7213..f7b3ba3 100644
--- a/mailbox/plugin/spamassassin/src/test/java/org/apache/james/mailbox/spamassassin/SpamAssassinListenerTest.java
+++ b/mailbox/plugin/spamassassin/src/test/java/org/apache/james/mailbox/spamassassin/SpamAssassinListenerTest.java
@@ -22,6 +22,7 @@ import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
 
 import java.nio.charset.StandardCharsets;
 import java.util.Date;
@@ -29,6 +30,7 @@ import java.util.Date;
 import javax.mail.Flags;
 import javax.mail.util.SharedByteArrayInputStream;
 
+import org.apache.james.mailbox.DefaultMailboxes;
 import org.apache.james.mailbox.MessageUid;
 import org.apache.james.mailbox.exception.MailboxException;
 import org.apache.james.mailbox.inmemory.InMemoryMailboxSessionMapperFactory;
@@ -37,6 +39,9 @@ import org.apache.james.mailbox.model.MailboxId;
 import org.apache.james.mailbox.model.MailboxPath;
 import org.apache.james.mailbox.model.MessageMoves;
 import org.apache.james.mailbox.model.TestMessageId;
+import org.apache.james.mailbox.store.SimpleMessageMetaData;
+import org.apache.james.mailbox.store.event.EventFactory;
+import org.apache.james.mailbox.store.event.EventFactory.AddedImpl;
 import org.apache.james.mailbox.store.event.MessageMoveEvent;
 import org.apache.james.mailbox.store.mail.MailboxMapper;
 import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder;
@@ -48,6 +53,7 @@ import org.junit.Test;
 
 import com.github.fge.lambdas.Throwing;
 import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSortedMap;
 
 public class SpamAssassinListenerTest {
 
@@ -56,6 +62,9 @@ public class SpamAssassinListenerTest {
     public static final int UID_VALIDITY = 43;
     private SpamAssassin spamAssassin;
     private SpamAssassinListener listener;
+    private SimpleMailbox inbox;
+    private MailboxId inboxId;
+    private SimpleMailbox mailbox1;
     private MailboxId mailboxId1;
     private MailboxId mailboxId2;
     private MailboxId spamMailboxId;
@@ -68,7 +77,10 @@ public class SpamAssassinListenerTest {
         spamAssassin = mock(SpamAssassin.class);
         InMemoryMailboxSessionMapperFactory mapperFactory = new InMemoryMailboxSessionMapperFactory();
         mailboxMapper = mapperFactory.getMailboxMapper(MAILBOX_SESSION);
-        mailboxId1 = mailboxMapper.save(new SimpleMailbox(MailboxPath.forUser(USER, "mailbox1"),
UID_VALIDITY));
+        inbox = new SimpleMailbox(MailboxPath.forUser(USER, DefaultMailboxes.INBOX), UID_VALIDITY);
+        inboxId = mailboxMapper.save(inbox);
+        mailbox1 = new SimpleMailbox(MailboxPath.forUser(USER, "mailbox1"), UID_VALIDITY);
+        mailboxId1 = mailboxMapper.save(mailbox1);
         mailboxId2 = mailboxMapper.save(new SimpleMailbox(MailboxPath.forUser(USER, "mailbox2"),
UID_VALIDITY));
         spamMailboxId = mailboxMapper.save(new SimpleMailbox(MailboxPath.forUser(USER, "Spam"),
UID_VALIDITY));
         spamCapitalMailboxId = mailboxMapper.save(new SimpleMailbox(MailboxPath.forUser(USER,
"SPAM"), UID_VALIDITY));
@@ -222,6 +234,36 @@ public class SpamAssassinListenerTest {
         verify(spamAssassin).learnHam(any(), any());
     }
 
+    @Test
+    public void eventShouldCallSpamAssassinHamLearningWhenTheMessageIsAddedInInbox() {
+        SimpleMailboxMessage message = createMessage(inboxId);
+        EventFactory eventFactory = new EventFactory();
+        AddedImpl addedEvent = eventFactory.new AddedImpl(
+                MAILBOX_SESSION,
+                inbox,
+                ImmutableSortedMap.of(MessageUid.of(45), new SimpleMessageMetaData(message)),
+                ImmutableMap.of(MessageUid.of(45), message));
+
+        listener.event(addedEvent);
+
+        verify(spamAssassin).learnHam(any(), any());
+    }
+
+    @Test
+    public void eventShouldNotCallSpamAssassinHamLearningWhenTheMessageIsAddedInAMailboxOtherThanInbox()
{
+        SimpleMailboxMessage message = createMessage(mailboxId1);
+        EventFactory eventFactory = new EventFactory();
+        AddedImpl addedEvent = eventFactory.new AddedImpl(
+                MAILBOX_SESSION,
+                mailbox1,
+                ImmutableSortedMap.of(MessageUid.of(45), new SimpleMessageMetaData(message)),
+                ImmutableMap.of(MessageUid.of(45), message));
+
+        listener.event(addedEvent);
+
+        verifyNoMoreInteractions(spamAssassin);
+    }
+
     private SimpleMailboxMessage createMessage(MailboxId mailboxId) {
         int size = 45;
         int bodyStartOctet = 25;


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message