aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-01-05 11:44:40 +0300
committerGravatar Vitaly Takmazov2018-01-05 11:44:40 +0300
commit4f2f63dbda970c06e63f444e11e29da483961843 (patch)
treef14238dc6debe5f8fa9a4907df536b5fa70dce2e
parentd5a737bd5b1155cdbb80ab3d2927113c0d385bd6 (diff)
email: parse In-Reply-To
-rw-r--r--juick-api/src/main/java/com/juick/api/EmailManager.java3
-rw-r--r--juick-api/src/main/java/com/juick/api/controllers/Post.java11
-rw-r--r--juick-api/src/test/java/com/juick/api/tests/MessagesTests.java10
3 files changed, 21 insertions, 3 deletions
diff --git a/juick-api/src/main/java/com/juick/api/EmailManager.java b/juick-api/src/main/java/com/juick/api/EmailManager.java
index df15d9de..a01eac78 100644
--- a/juick-api/src/main/java/com/juick/api/EmailManager.java
+++ b/juick-api/src/main/java/com/juick/api/EmailManager.java
@@ -2,7 +2,6 @@ package com.juick.api;
import com.juick.Message;
import com.juick.server.component.MessageEvent;
-import com.juick.server.util.HttpBadRequestException;
import com.juick.service.EmailService;
import com.juick.service.MessagesService;
import com.juick.service.SubscriptionService;
@@ -15,12 +14,10 @@ import org.springframework.stereotype.Component;
import javax.inject.Inject;
import javax.mail.MessagingException;
-import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
-
import java.util.Properties;
import static com.juick.formatters.PlainTextFormatter.formatPost;
diff --git a/juick-api/src/main/java/com/juick/api/controllers/Post.java b/juick-api/src/main/java/com/juick/api/controllers/Post.java
index 33d83d2c..032b549b 100644
--- a/juick-api/src/main/java/com/juick/api/controllers/Post.java
+++ b/juick-api/src/main/java/com/juick/api/controllers/Post.java
@@ -52,6 +52,7 @@ import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.Properties;
+import java.util.Scanner;
import java.util.UUID;
/**
@@ -210,6 +211,7 @@ public class Post {
String from = msg.getFrom().length > 1 ? ((InternetAddress) msg.getSender()).getAddress()
: ((InternetAddress) msg.getFrom()[0]).getAddress();
logger.info("got msg from {}", from);
+
User visitor = userService.getUserByEmail(from);
if (!visitor.isAnonymous()) {
MimeMessageParser parser = new MimeMessageParser(msg);
@@ -248,6 +250,15 @@ public class Post {
logger.info("attachment error: {}", e);
}
});
+ String[] inReplyToHeaders = msg.getHeader("In-Reply-To");
+ if (inReplyToHeaders != null && inReplyToHeaders.length > 0) {
+ Scanner inReplyToScanner = new Scanner(inReplyToHeaders[0].trim()).useDelimiter("\\.|@|<");
+ int mid = Integer.parseInt(inReplyToScanner.next());
+ int rid = Integer.parseInt(inReplyToScanner.next());
+ logger.info("Message is reply to #%d/%d", mid, rid);
+ body[0] = rid > 0 ? String.format("#%d/%d %s", mid, rid, body[0])
+ : String.format("#%d %s", mid, body[0]);
+ }
rocks.xmpp.core.stanza.model.Message xmsg = new rocks.xmpp.core.stanza.model.Message();
xmsg.setType(rocks.xmpp.core.stanza.model.Message.Type.CHAT);
xmsg.setFrom(Jid.of(String.valueOf(visitor.getUid()), "uid.juick.com", "mail"));
diff --git a/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java b/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java
index 0022c966..a270a120 100644
--- a/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java
+++ b/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java
@@ -52,6 +52,7 @@ import javax.inject.Inject;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
+import java.util.Scanner;
import java.util.stream.IntStream;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -310,4 +311,13 @@ public class MessagesTests extends AbstractJUnit4SpringContextTests {
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(0)));
}
+ @Test
+ public void inReplyToScannerTest() {
+ String header = "<123456.56@juick.com>";
+ Scanner headerScanner = new Scanner(header).useDelimiter("\\.|@|<");
+ int mid = Integer.parseInt(headerScanner.next());
+ int rid = Integer.parseInt(headerScanner.next());
+ assertThat(mid, equalTo(123456));
+ assertThat(rid, equalTo(56));
+ }
}