diff --git a/core/src/main/java/ch/dissem/bitmessage/utils/ConversationService.java b/core/src/main/java/ch/dissem/bitmessage/utils/ConversationService.java index a4d8a44..f261a08 100644 --- a/core/src/main/java/ch/dissem/bitmessage/utils/ConversationService.java +++ b/core/src/main/java/ch/dissem/bitmessage/utils/ConversationService.java @@ -22,18 +22,35 @@ import ch.dissem.bitmessage.ports.MessageRepository; import java.util.*; import java.util.Collections; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static java.util.regex.Pattern.CASE_INSENSITIVE; /** - * Helper service to work with conversations + * Service that helps with conversations. */ public class ConversationService { private final MessageRepository messageRepository; + private final Pattern SUBJECT_PREFIX = Pattern.compile("^(re|fwd?):", CASE_INSENSITIVE); + public ConversationService(MessageRepository messageRepository) { this.messageRepository = messageRepository; } + /** + * Retrieve the whole conversation from one single message. If the message isn't part + * of a conversation, a singleton list containing the given message is returned. Otherwise + * it's the same as {@link #getConversation(UUID)} + * + * @param message + * @return a list of messages that belong to the same conversation. + */ public List getConversation(Plaintext message) { + if (message.getConversationId() == null) { + return Collections.singletonList(message); + } return getConversation(message.getConversationId()); } @@ -77,6 +94,19 @@ public class ConversationService { return result; } + public String getSubject(List<Plaintext> conversation) { + if (conversation.isEmpty()) { + return null; + } + // TODO: this has room for improvement + String subject = conversation.get(0).getSubject(); + Matcher matcher = SUBJECT_PREFIX.matcher(subject); + if (matcher.find()) { + return subject.substring(matcher.end()).trim(); + } + return subject.trim(); + } + private int lastParentPosition(Plaintext child, LinkedList<Plaintext> messages) { Iterator<Plaintext> plaintextIterator = messages.descendingIterator(); int i = 0; diff --git a/repositories/src/main/resources/db/migration/V4.0__Create_table_message_parent.sql b/repositories/src/main/resources/db/migration/V4.0__Create_table_message_parent.sql index 8b5f0ca..aa0a120 100644 --- a/repositories/src/main/resources/db/migration/V4.0__Create_table_message_parent.sql +++ b/repositories/src/main/resources/db/migration/V4.0__Create_table_message_parent.sql @@ -1,10 +1,10 @@ -ALTER TABLE Message ADD COLUMN conversation UUID NOT NULL DEFAULT RANDOM_UUID(); +ALTER TABLE Message ADD COLUMN conversation UUID; CREATE TABLE Message_Parent ( parent BINARY(64) NOT NULL, child BINARY(64) NOT NULL, pos INT NOT NULL, - conversation UUID, + conversation UUID NOT NULL, PRIMARY KEY (parent, child), FOREIGN KEY (child) REFERENCES Message (iv)