X-Git-Url: http://git.maemo.org/git/?p=theonering;a=blobdiff_plain;f=src%2Fgvoice%2Fconversations.py;h=7435da86da3fd3fbb8215507fb295ae170156ffe;hp=fad4b407e4b2f07d20f1038660f5cb79c7278d3c;hb=1bd375495719117939fa0ac384835001485eeef7;hpb=fc96b200d858e66bbf02037b5002a07cd71c2654 diff --git a/src/gvoice/conversations.py b/src/gvoice/conversations.py index fad4b40..7435da8 100644 --- a/src/gvoice/conversations.py +++ b/src/gvoice/conversations.py @@ -5,8 +5,6 @@ import logging import util.coroutines as coroutines -import backend - _moduleLogger = logging.getLogger("gvoice.conversations") @@ -18,7 +16,6 @@ class Conversations(object): self._conversations = {} self.updateSignalHandler = coroutines.CoTee() - self.update() def update(self, force=False): if not force and self._conversations: @@ -27,23 +24,23 @@ class Conversations(object): oldConversationIds = set(self._conversations.iterkeys()) updateConversationIds = set() - messages = self._backend.get_messages() - sortedMessages = backend.sort_messages(messages) - for messageData in sortedMessages: - key = messageData["contactId"], messageData["number"] + conversations = list(self._backend.get_conversations()) + conversations.sort() + for conversation in conversations: + key = conversation.contactId, conversation.number try: - conversation = self._conversations[key] - isNewConversation = False + mergedConversations = self._conversations[key] except KeyError: - conversation = Conversation(self._backend, messageData) - self._conversations[key] = conversation - isNewConversation = True + mergedConversations = MergedConversations() + self._conversations[key] = mergedConversations - if isNewConversation: - # @todo see if this has issues with a user marking a item as unread/unarchive? + try: + mergedConversations.append_conversation(conversation) isConversationUpdated = True - else: - isConversationUpdated = conversation.merge_conversation(messageData) + except RuntimeError, e: + if False: + _moduleLogger.info("Skipping conversation for %r because '%s'" % (key, e)) + isConversationUpdated = False if isConversationUpdated: updateConversationIds.add(key) @@ -58,53 +55,69 @@ class Conversations(object): def get_conversation(self, key): return self._conversations[key] + def clear_conversation(self, key): + try: + del self._conversations[key] + except KeyError: + _moduleLogger.info("Conversation never existed for %r" % (key,)) -class Conversation(object): - - def __init__(self, backend, data): - self._backend = backend - self._data = dict((key, value) for (key, value) in data.iteritems()) - - # confirm we have a list - self._data["messageParts"] = list( - self._append_time(message, self._data["time"]) - for message in self._data["messageParts"] - ) - - def __getitem__(self, key): - return self._data[key] - - def merge_conversation(self, moreData): - """ - @returns True if there was content to merge (new messages arrived - rather than being a duplicate) + def clear_all(self): + self._conversations.clear() - @warning This assumes merges are done in chronological order - """ - for constantField in ("contactId", "number"): - assert self._data[constantField] == moreData[constantField], "Constant field changed, soemthing is seriously messed up: %r v %r" % (self._data, moreData) - if moreData["time"] < self._data["time"]: - # If its older, assuming it has nothing new to report - return False +class MergedConversations(object): - for preferredMoreField in ("id", "name", "time", "relTime", "prettyNumber", "location"): - preferredFieldValue = moreData[preferredMoreField] - if preferredFieldValue: - self._data[preferredMoreField] = preferredFieldValue + def __init__(self): + self._conversations = [] - messageAppended = False + def append_conversation(self, newConversation): + self._validate(newConversation) + for similarConversation in self._find_related_conversation(newConversation.id): + self._update_previous_related_conversation(similarConversation, newConversation) + self._remove_repeats(similarConversation, newConversation) + self._conversations.append(newConversation) - messageParts = self._data["messageParts"] - for message in moreData["messageParts"]: - messageWithTimestamp = self._append_time(message, moreData["time"]) - if messageWithTimestamp not in messageParts: - messageParts.append(messageWithTimestamp) - messageAppended = True + @property + def conversations(self): + return self._conversations - return messageAppended + def _validate(self, newConversation): + if not self._conversations: + return - @staticmethod - def _append_time(message, exactWhen): - whoFrom, message, when = message - return whoFrom, message, when, exactWhen + for constantField in ("contactId", "number"): + assert getattr(self._conversations[0], constantField) == getattr(newConversation, constantField), "Constant field changed, soemthing is seriously messed up: %r v %r" % ( + getattr(self._conversations[0], constantField), + getattr(newConversation, constantField), + ) + + if newConversation.time <= self._conversations[-1].time: + raise RuntimeError("Conversations got out of order") + + def _find_related_conversation(self, convId): + similarConversations = ( + conversation + for conversation in self._conversations + if conversation.id == convId + ) + return similarConversations + + def _update_previous_related_conversation(self, relatedConversation, newConversation): + for commonField in ("isRead", "isSpam", "isTrash", "isArchived"): + newValue = getattr(newConversation, commonField) + setattr(relatedConversation, commonField, newValue) + + def _remove_repeats(self, relatedConversation, newConversation): + newConversationMessages = newConversation.messages + newConversation.messages = [ + newMessage + for newMessage in newConversationMessages + if newMessage not in relatedConversation.messages + ] + _moduleLogger.info("Found %d new messages in conversation %s (%d/%d)" % ( + len(newConversationMessages) - len(newConversation.messages), + newConversation.id, + len(newConversation.messages), + len(newConversationMessages), + )) + assert 0 < len(newConversation.messages), "Everything shouldn't have been removed"