X-Git-Url: http://git.maemo.org/git/?p=theonering;a=blobdiff_plain;f=src%2Fgvoice%2Fconversations.py;h=8baf8b8f91b008a84a7a264918ffb05f551226f4;hp=3e0040c83661477bb28ed138d6a13a8bec386901;hb=3d5f80ff4e1607d37b92566619f1c3d6180fd000;hpb=ce4c05e738f65ca74316c59651bf63fb67329e06 diff --git a/src/gvoice/conversations.py b/src/gvoice/conversations.py index 3e0040c..8baf8b8 100644 --- a/src/gvoice/conversations.py +++ b/src/gvoice/conversations.py @@ -1,11 +1,14 @@ #!/usr/bin/python +# @bug Its inconsistent as to whether messages from contacts come as from the +# contact or just a raw number. It seems GV is inconsistent about populating the contact id, so we might have to pull from the addressbook +# @bug False positives on startup. Luckily the object path for the channel is +# unique, so can use that to cache some of the data out to file import logging import util.coroutines as coroutines - -import backend +import util.misc as util_misc _moduleLogger = logging.getLogger("gvoice.conversations") @@ -13,12 +16,11 @@ _moduleLogger = logging.getLogger("gvoice.conversations") class Conversations(object): - def __init__(self, backend): - self._backend = backend + def __init__(self, getter): + self._get_raw_conversations = getter self._conversations = {} self.updateSignalHandler = coroutines.CoTee() - self.update() def update(self, force=False): if not force and self._conversations: @@ -27,23 +29,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._get_raw_conversations()) + conversations.sort() + for conversation in conversations: + key = conversation.contactId, util_misc.strip_number(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) @@ -59,60 +61,68 @@ class Conversations(object): return self._conversations[key] def clear_conversation(self, key): - del self._conversations[key] + try: + del self._conversations[key] + except KeyError: + _moduleLogger.info("Conversation never existed for %r" % (key,)) def clear_all(self): self._conversations.clear() -class Conversation(object): +class MergedConversations(object): - def __init__(self, backend, data): - self._backend = backend - self._data = dict((key, value) for (key, value) in data.iteritems()) + def __init__(self): + self._conversations = [] - # confirm we have a list - self._data["messageParts"] = list( - self._append_time(message, self._data["time"]) - for message in self._data["messageParts"] - ) + 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) - def __getitem__(self, key): - return self._data[key] + @property + def conversations(self): + return self._conversations - def merge_conversation(self, moreData): - """ - @returns True if there was content to merge (new messages arrived - rather than being a duplicate) + def _validate(self, newConversation): + if not self._conversations: + return - @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 - - for preferredMoreField in ("id", "name", "time", "relTime", "prettyNumber", "location"): - preferredFieldValue = moreData[preferredMoreField] - if preferredFieldValue: - self._data[preferredMoreField] = preferredFieldValue - - messageAppended = False - - # @todo Handle No Transcription voicemails - 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 - messageParts.sort() - - return messageAppended - - @staticmethod - def _append_time(message, exactWhen): - whoFrom, message, when = message - return exactWhen, whoFrom, message, when + 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.debug("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"