ConversationService additions, fixed migration script

This commit is contained in:
Christian Basler 2017-03-30 16:30:46 +02:00
parent d9090eb70c
commit 3ab3d7a0ca
2 changed files with 33 additions and 3 deletions

View File

@ -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<Plaintext> 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;

View File

@ -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)