Limiting the size of the cache
[theonering] / src / gvoice / conversations.py
index f29f5e6..5f3d371 100644 (file)
@@ -2,6 +2,7 @@
 
 from __future__ import with_statement
 
+import datetime
 import logging
 
 try:
@@ -12,14 +13,17 @@ except ImportError:
 
 import constants
 import util.coroutines as coroutines
-import util.misc as util_misc
+import util.misc as misc_utils
 
 
-_moduleLogger = logging.getLogger("gvoice.conversations")
+_moduleLogger = logging.getLogger(__name__)
 
 
 class Conversations(object):
 
+       OLDEST_COMPATIBLE_FORMAT_VERSION = misc_utils.parse_version("0.8.0")
+       OLDEST_MESSAGE_WINDOW = datetime.timedelta(days=60)
+
        def __init__(self, getter):
                self._get_raw_conversations = getter
                self._conversations = {}
@@ -35,19 +39,26 @@ class Conversations(object):
                try:
                        with open(path, "rb") as f:
                                fileVersion, fileBuild, convs = pickle.load(f)
-               except (pickle.PickleError, IOError):
+               except (pickle.PickleError, IOError, EOFError, ValueError):
                        _moduleLogger.exception("While loading for %s" % self._name)
                        return
 
-               if fileVersion == constants.__version__ and fileBuild == constants.__build__:
+               if misc_utils.compare_versions(
+                       self.OLDEST_COMPATIBLE_FORMAT_VERSION,
+                       misc_utils.parse_version(fileVersion),
+               ) <= 0:
                        self._conversations = convs
                else:
                        _moduleLogger.debug(
-                               "%s Skipping cache due to version mismatch (%s-%s)" % (self._name, fileVersion, fileBuild)
+                               "%s Skipping cache due to version mismatch (%s-%s)" % (
+                                       self._name, fileVersion, fileBuild
+                               )
                        )
 
        def save(self, path):
                try:
+                       for conv in self._conversations.itervalues():
+                               conv.compress(self.OLDEST_MESSAGE_WINDOW)
                        dataToDump = (constants.__version__, constants.__build__, self._conversations)
                        with open(path, "wb") as f:
                                pickle.dump(dataToDump, f, pickle.HIGHEST_PROTOCOL)
@@ -64,7 +75,7 @@ class Conversations(object):
                conversations = list(self._get_raw_conversations())
                conversations.sort()
                for conversation in conversations:
-                       key = util_misc.normalize_number(conversation.number)
+                       key = misc_utils.normalize_number(conversation.number)
                        try:
                                mergedConversations = self._conversations[key]
                        except KeyError:
@@ -109,9 +120,21 @@ class MergedConversations(object):
 
        def append_conversation(self, newConversation):
                self._validate(newConversation)
+               similarExist = False
                for similarConversation in self._find_related_conversation(newConversation.id):
                        self._update_previous_related_conversation(similarConversation, newConversation)
                        self._remove_repeats(similarConversation, newConversation)
+                       similarExist = True
+               if similarExist:
+                       # Hack to reduce a race window with GV marking messages as read
+                       # because it thinks we replied when really we replied to the
+                       # previous message.  Clients of this code are expected to handle
+                       # this gracefully.  Other race conditions may exist but clients are
+                       # responsible for them
+                       if newConversation.messages:
+                               newConversation.isRead = False
+                       else:
+                               newConversation.isRead = True
                self._conversations.append(newConversation)
 
        def to_dict(self):
@@ -123,6 +146,21 @@ class MergedConversations(object):
        def conversations(self):
                return self._conversations
 
+       def compress(self, timedelta):
+               now = datetime.datetime.now()
+               oldNumConvs = len(self._conversations)
+               oldConvs = self._conversations
+               self._conversations = [
+                       conv
+                       for conv in self._conversations
+                       if (now - conv.time) < timedelta
+               ]
+               newNumConvs = len(self._conversations)
+               if oldNumConvs != newNumConvs:
+                       _moduleLogger.debug("Compressed conversations from %s to %s" % (oldNumConvs, newNumConvs))
+               else:
+                       _moduleLogger.debug("Did not compress, %s" % (newNumConvs))
+
        def _validate(self, newConversation):
                if not self._conversations:
                        return
@@ -163,3 +201,47 @@ class MergedConversations(object):
                        len(newConversationMessages),
                ))
                assert 0 < len(newConversation.messages), "Everything shouldn't have been removed"
+
+
+def filter_out_read(conversations):
+       return (
+               conversation
+               for conversation in conversations
+               if not conversation.isRead and not conversation.isArchived
+       )
+
+
+def is_message_from_self(message):
+       return message.whoFrom == "Me:"
+
+
+def filter_out_self(conversations):
+       return (
+               newConversation
+               for newConversation in conversations
+               if len(newConversation.messages) and any(
+                       not is_message_from_self(message)
+                       for message in newConversation.messages
+               )
+       )
+
+
+class FilterOutReported(object):
+
+       NULL_TIMESTAMP = datetime.datetime(1, 1, 1)
+
+       def __init__(self):
+               self._lastMessageTimestamp = self.NULL_TIMESTAMP
+
+       def get_last_timestamp(self):
+               return self._lastMessageTimestamp
+
+       def __call__(self, conversations):
+               filteredConversations = [
+                       conversation
+                       for conversation in conversations
+                       if self._lastMessageTimestamp < conversation.time
+               ]
+               if filteredConversations and self._lastMessageTimestamp < filteredConversations[0].time:
+                       self._lastMessageTimestamp = filteredConversations[0].time
+               return filteredConversations