Limiting the size of the cache
[theonering] / src / gvoice / conversations.py
1 #!/usr/bin/python
2
3 from __future__ import with_statement
4
5 import datetime
6 import logging
7
8 try:
9         import cPickle
10         pickle = cPickle
11 except ImportError:
12         import pickle
13
14 import constants
15 import util.coroutines as coroutines
16 import util.misc as misc_utils
17
18
19 _moduleLogger = logging.getLogger(__name__)
20
21
22 class Conversations(object):
23
24         OLDEST_COMPATIBLE_FORMAT_VERSION = misc_utils.parse_version("0.8.0")
25         OLDEST_MESSAGE_WINDOW = datetime.timedelta(days=60)
26
27         def __init__(self, getter):
28                 self._get_raw_conversations = getter
29                 self._conversations = {}
30
31                 self.updateSignalHandler = coroutines.CoTee()
32
33         @property
34         def _name(self):
35                 return repr(self._get_raw_conversations.__name__)
36
37         def load(self, path):
38                 assert not self._conversations
39                 try:
40                         with open(path, "rb") as f:
41                                 fileVersion, fileBuild, convs = pickle.load(f)
42                 except (pickle.PickleError, IOError, EOFError, ValueError):
43                         _moduleLogger.exception("While loading for %s" % self._name)
44                         return
45
46                 if misc_utils.compare_versions(
47                         self.OLDEST_COMPATIBLE_FORMAT_VERSION,
48                         misc_utils.parse_version(fileVersion),
49                 ) <= 0:
50                         self._conversations = convs
51                 else:
52                         _moduleLogger.debug(
53                                 "%s Skipping cache due to version mismatch (%s-%s)" % (
54                                         self._name, fileVersion, fileBuild
55                                 )
56                         )
57
58         def save(self, path):
59                 try:
60                         for conv in self._conversations.itervalues():
61                                 conv.compress(self.OLDEST_MESSAGE_WINDOW)
62                         dataToDump = (constants.__version__, constants.__build__, self._conversations)
63                         with open(path, "wb") as f:
64                                 pickle.dump(dataToDump, f, pickle.HIGHEST_PROTOCOL)
65                 except (pickle.PickleError, IOError):
66                         _moduleLogger.exception("While saving for %s" % self._name)
67
68         def update(self, force=False):
69                 if not force and self._conversations:
70                         return
71
72                 oldConversationIds = set(self._conversations.iterkeys())
73
74                 updateConversationIds = set()
75                 conversations = list(self._get_raw_conversations())
76                 conversations.sort()
77                 for conversation in conversations:
78                         key = misc_utils.normalize_number(conversation.number)
79                         try:
80                                 mergedConversations = self._conversations[key]
81                         except KeyError:
82                                 mergedConversations = MergedConversations()
83                                 self._conversations[key] = mergedConversations
84
85                         try:
86                                 mergedConversations.append_conversation(conversation)
87                                 isConversationUpdated = True
88                         except RuntimeError, e:
89                                 if False:
90                                         _moduleLogger.debug("%s Skipping conversation for %r because '%s'" % (self._name, key, e))
91                                 isConversationUpdated = False
92
93                         if isConversationUpdated:
94                                 updateConversationIds.add(key)
95
96                 if updateConversationIds:
97                         message = (self, updateConversationIds, )
98                         self.updateSignalHandler.stage.send(message)
99
100         def get_conversations(self):
101                 return self._conversations.iterkeys()
102
103         def get_conversation(self, key):
104                 return self._conversations[key]
105
106         def clear_conversation(self, key):
107                 try:
108                         del self._conversations[key]
109                 except KeyError:
110                         _moduleLogger.info("%s Conversation never existed for %r" % (self._name, key, ))
111
112         def clear_all(self):
113                 self._conversations.clear()
114
115
116 class MergedConversations(object):
117
118         def __init__(self):
119                 self._conversations = []
120
121         def append_conversation(self, newConversation):
122                 self._validate(newConversation)
123                 similarExist = False
124                 for similarConversation in self._find_related_conversation(newConversation.id):
125                         self._update_previous_related_conversation(similarConversation, newConversation)
126                         self._remove_repeats(similarConversation, newConversation)
127                         similarExist = True
128                 if similarExist:
129                         # Hack to reduce a race window with GV marking messages as read
130                         # because it thinks we replied when really we replied to the
131                         # previous message.  Clients of this code are expected to handle
132                         # this gracefully.  Other race conditions may exist but clients are
133                         # responsible for them
134                         if newConversation.messages:
135                                 newConversation.isRead = False
136                         else:
137                                 newConversation.isRead = True
138                 self._conversations.append(newConversation)
139
140         def to_dict(self):
141                 selfDict = {}
142                 selfDict["conversations"] = [conv.to_dict() for conv in self._conversations]
143                 return selfDict
144
145         @property
146         def conversations(self):
147                 return self._conversations
148
149         def compress(self, timedelta):
150                 now = datetime.datetime.now()
151                 oldNumConvs = len(self._conversations)
152                 oldConvs = self._conversations
153                 self._conversations = [
154                         conv
155                         for conv in self._conversations
156                         if (now - conv.time) < timedelta
157                 ]
158                 newNumConvs = len(self._conversations)
159                 if oldNumConvs != newNumConvs:
160                         _moduleLogger.debug("Compressed conversations from %s to %s" % (oldNumConvs, newNumConvs))
161                 else:
162                         _moduleLogger.debug("Did not compress, %s" % (newNumConvs))
163
164         def _validate(self, newConversation):
165                 if not self._conversations:
166                         return
167
168                 for constantField in ("number", ):
169                         assert getattr(self._conversations[0], constantField) == getattr(newConversation, constantField), "Constant field changed, soemthing is seriously messed up: %r v %r" % (
170                                 getattr(self._conversations[0], constantField),
171                                 getattr(newConversation, constantField),
172                         )
173
174                 if newConversation.time <= self._conversations[-1].time:
175                         raise RuntimeError("Conversations got out of order")
176
177         def _find_related_conversation(self, convId):
178                 similarConversations = (
179                         conversation
180                         for conversation in self._conversations
181                         if conversation.id == convId
182                 )
183                 return similarConversations
184
185         def _update_previous_related_conversation(self, relatedConversation, newConversation):
186                 for commonField in ("isSpam", "isTrash", "isArchived"):
187                         newValue = getattr(newConversation, commonField)
188                         setattr(relatedConversation, commonField, newValue)
189
190         def _remove_repeats(self, relatedConversation, newConversation):
191                 newConversationMessages = newConversation.messages
192                 newConversation.messages = [
193                         newMessage
194                         for newMessage in newConversationMessages
195                         if newMessage not in relatedConversation.messages
196                 ]
197                 _moduleLogger.debug("Found %d new messages in conversation %s (%d/%d)" % (
198                         len(newConversationMessages) - len(newConversation.messages),
199                         newConversation.id,
200                         len(newConversation.messages),
201                         len(newConversationMessages),
202                 ))
203                 assert 0 < len(newConversation.messages), "Everything shouldn't have been removed"
204
205
206 def filter_out_read(conversations):
207         return (
208                 conversation
209                 for conversation in conversations
210                 if not conversation.isRead and not conversation.isArchived
211         )
212
213
214 def is_message_from_self(message):
215         return message.whoFrom == "Me:"
216
217
218 def filter_out_self(conversations):
219         return (
220                 newConversation
221                 for newConversation in conversations
222                 if len(newConversation.messages) and any(
223                         not is_message_from_self(message)
224                         for message in newConversation.messages
225                 )
226         )
227
228
229 class FilterOutReported(object):
230
231         NULL_TIMESTAMP = datetime.datetime(1, 1, 1)
232
233         def __init__(self):
234                 self._lastMessageTimestamp = self.NULL_TIMESTAMP
235
236         def get_last_timestamp(self):
237                 return self._lastMessageTimestamp
238
239         def __call__(self, conversations):
240                 filteredConversations = [
241                         conversation
242                         for conversation in conversations
243                         if self._lastMessageTimestamp < conversation.time
244                 ]
245                 if filteredConversations and self._lastMessageTimestamp < filteredConversations[0].time:
246                         self._lastMessageTimestamp = filteredConversations[0].time
247                 return filteredConversations