Disabling 'compress' and adding lots of debug help
[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 import util.go_utils as gobject_utils
18
19
20 _moduleLogger = logging.getLogger(__name__)
21
22
23 class Conversations(object):
24
25         OLDEST_COMPATIBLE_FORMAT_VERSION = misc_utils.parse_version("0.8.0")
26         OLDEST_MESSAGE_WINDOW = datetime.timedelta(days=60)
27
28         def __init__(self, getter, asyncPool):
29                 self._get_raw_conversations = getter
30                 self._asyncPool = asyncPool
31                 self._conversations = {}
32                 self._loadedFromCache = False
33                 self._hasDoneUpdate = False
34
35                 self.updateSignalHandler = coroutines.CoTee()
36
37         @property
38         def _name(self):
39                 return repr(self._get_raw_conversations.__name__)
40
41         def load(self, path):
42                 _moduleLogger.debug("%s Loading cache" % (self._name, ))
43                 assert not self._conversations
44                 try:
45                         with open(path, "rb") as f:
46                                 fileVersion, fileBuild, convs = pickle.load(f)
47                 except (pickle.PickleError, IOError, EOFError, ValueError):
48                         _moduleLogger.exception("While loading for %s" % self._name)
49                         return
50
51                 if misc_utils.compare_versions(
52                         self.OLDEST_COMPATIBLE_FORMAT_VERSION,
53                         misc_utils.parse_version(fileVersion),
54                 ) <= 0:
55                         _moduleLogger.info("%s Loaded cache" % (self._name, ))
56                         self._conversations = convs
57                         self._loadedFromCache = True
58                         for key, mergedConv in self._conversations.iteritems():
59                                 _moduleLogger.debug("%s \tLoaded %s" % (self._name, key))
60                                 for conv in mergedConv.conversations:
61                                         _moduleLogger.debug("%s \t\tLoaded %s (%r) %r %r %r" % (self._name, conv.id, conv.time, conv.isRead, conv.isArchived, len(conv.messages)))
62                 else:
63                         _moduleLogger.debug(
64                                 "%s Skipping cache due to version mismatch (%s-%s)" % (
65                                         self._name, fileVersion, fileBuild
66                                 )
67                         )
68
69         def save(self, path):
70                 try:
71                         _moduleLogger.info("%s Saving cache" % (self._name, ))
72                         #for conv in self._conversations.itervalues():
73                         #       conv.compress(self.OLDEST_MESSAGE_WINDOW)
74
75                         for key, mergedConv in self._conversations.iteritems():
76                                 _moduleLogger.debug("%s \tSaving %s" % (self._name, key))
77                                 for conv in mergedConv.conversations:
78                                         _moduleLogger.debug("%s \t\tSaving %s (%r) %r %r %r" % (self._name, conv.id, conv.time, conv.isRead, conv.isArchived, len(conv.messages)))
79
80                         dataToDump = (constants.__version__, constants.__build__, self._conversations)
81                         with open(path, "wb") as f:
82                                 pickle.dump(dataToDump, f, pickle.HIGHEST_PROTOCOL)
83                 except (pickle.PickleError, IOError):
84                         _moduleLogger.exception("While saving for %s" % self._name)
85                 _moduleLogger.info("%s Cache saved" % (self._name, ))
86
87         def update(self, force=False):
88                 if not force and self._conversations:
89                         return
90
91                 le = gobject_utils.AsyncLinearExecution(self._asyncPool, self._update)
92                 le.start()
93
94         @misc_utils.log_exception(_moduleLogger)
95         def _update(self):
96                 conversationResult = yield (
97                         self._get_raw_conversations,
98                         (),
99                         {},
100                 )
101
102                 oldConversationIds = set(self._conversations.iterkeys())
103
104                 updateConversationIds = set()
105                 conversations = list(conversationResult)
106                 conversations.sort()
107                 for conversation in conversations:
108                         key = misc_utils.normalize_number(conversation.number)
109                         try:
110                                 mergedConversations = self._conversations[key]
111                         except KeyError:
112                                 mergedConversations = MergedConversations()
113                                 self._conversations[key] = mergedConversations
114
115                         if self._loadedFromCache or self._hasDoneUpdate:
116                                 markAllAsRead = False
117                         else:
118                                 markAllAsRead = True
119                         try:
120                                 mergedConversations.append_conversation(conversation, markAllAsRead)
121                                 isConversationUpdated = True
122                         except RuntimeError, e:
123                                 if False:
124                                         _moduleLogger.debug("%s Skipping conversation for %r because '%s'" % (self._name, key, e))
125                                 isConversationUpdated = False
126
127                         if isConversationUpdated:
128                                 updateConversationIds.add(key)
129
130                 for key in updateConversationIds:
131                         mergedConv = self._conversations[key]
132                         _moduleLogger.debug("%s \tUpdated %s" % (self._name, key))
133                         for conv in mergedConv.conversations:
134                                 _moduleLogger.debug("%s \t\tUpdated %s (%r) %r %r %r" % (self._name, conv.id, conv.time, conv.isRead, conv.isArchived, len(conv.messages)))
135
136                 if updateConversationIds:
137                         message = (self, updateConversationIds, )
138                         self.updateSignalHandler.stage.send(message)
139                 self._hasDoneUpdate = True
140
141         def get_conversations(self):
142                 return self._conversations.iterkeys()
143
144         def get_conversation(self, key):
145                 return self._conversations[key]
146
147         def clear_conversation(self, key):
148                 try:
149                         del self._conversations[key]
150                 except KeyError:
151                         _moduleLogger.info("%s Conversation never existed for %r" % (self._name, key, ))
152
153         def clear_all(self):
154                 self._conversations.clear()
155
156
157 class MergedConversations(object):
158
159         def __init__(self):
160                 self._conversations = []
161
162         def append_conversation(self, newConversation, markAllAsRead):
163                 self._validate(newConversation)
164                 for similarConversation in self._find_related_conversation(newConversation.id):
165                         self._update_previous_related_conversation(similarConversation, newConversation)
166                         self._remove_repeats(similarConversation, newConversation)
167
168                 # HACK: Because GV marks all messages as read when you reply it has
169                 # the following race:
170                 # 1. Get all messages
171                 # 2. Contact sends a text
172                 # 3. User sends a text marking contacts text as read
173                 # 4. Get all messages not returning text from step 2
174                 # This isn't a problem for voicemails but we don't know(?( enough.
175                 # So we hack around this by:
176                 # * We cache to disk the history of messages sent/received
177                 # * On first run we mark all server messages as read due to no cache
178                 # * If not first load or from cache (disk or in-memory) then it must be unread
179                 if markAllAsRead:
180                         newConversation.isRead = True
181                 else:
182                         newConversation.isRead = False
183
184                 if newConversation.messages:
185                         # must not have had all items removed due to duplicates
186                         self._conversations.append(newConversation)
187
188         def to_dict(self):
189                 selfDict = {}
190                 selfDict["conversations"] = [conv.to_dict() for conv in self._conversations]
191                 return selfDict
192
193         @property
194         def conversations(self):
195                 return self._conversations
196
197         def compress(self, timedelta):
198                 now = datetime.datetime.now()
199                 oldNumConvs = len(self._conversations)
200                 oldConvs = self._conversations
201                 self._conversations = [
202                         conv
203                         for conv in self._conversations
204                         if (now - conv.time) < timedelta
205                 ]
206                 newNumConvs = len(self._conversations)
207                 if oldNumConvs != newNumConvs:
208                         _moduleLogger.debug("Compressed conversations from %s to %s" % (oldNumConvs, newNumConvs))
209                 else:
210                         _moduleLogger.debug("Did not compress, %s" % (newNumConvs))
211
212         def _validate(self, newConversation):
213                 if not self._conversations:
214                         return
215
216                 for constantField in ("number", ):
217                         assert getattr(self._conversations[0], constantField) == getattr(newConversation, constantField), "Constant field changed, soemthing is seriously messed up: %r v %r" % (
218                                 getattr(self._conversations[0], constantField),
219                                 getattr(newConversation, constantField),
220                         )
221
222                 if newConversation.time <= self._conversations[-1].time:
223                         raise RuntimeError("Conversations got out of order")
224
225         def _find_related_conversation(self, convId):
226                 similarConversations = (
227                         conversation
228                         for conversation in self._conversations
229                         if conversation.id == convId
230                 )
231                 return similarConversations
232
233         def _update_previous_related_conversation(self, relatedConversation, newConversation):
234                 for commonField in ("isSpam", "isTrash", "isArchived"):
235                         newValue = getattr(newConversation, commonField)
236                         setattr(relatedConversation, commonField, newValue)
237
238         def _remove_repeats(self, relatedConversation, newConversation):
239                 newConversationMessages = newConversation.messages
240                 newConversation.messages = [
241                         newMessage
242                         for newMessage in newConversationMessages
243                         if newMessage not in relatedConversation.messages
244                 ]
245                 _moduleLogger.debug("Found %d new messages in conversation %s (%d/%d)" % (
246                         len(newConversationMessages) - len(newConversation.messages),
247                         newConversation.id,
248                         len(newConversation.messages),
249                         len(newConversationMessages),
250                 ))
251                 assert 0 < len(newConversation.messages), "Everything shouldn't have been removed"
252
253
254 def filter_out_read(conversations):
255         return (
256                 conversation
257                 for conversation in conversations
258                 if not conversation.isRead and not conversation.isArchived
259         )
260
261
262 def is_message_from_self(message):
263         return message.whoFrom == "Me:"
264
265
266 def filter_out_self(conversations):
267         return (
268                 newConversation
269                 for newConversation in conversations
270                 if len(newConversation.messages) and any(
271                         not is_message_from_self(message)
272                         for message in newConversation.messages
273                 )
274         )
275
276
277 class FilterOutReported(object):
278
279         NULL_TIMESTAMP = datetime.datetime(1, 1, 1)
280
281         def __init__(self):
282                 self._lastMessageTimestamp = self.NULL_TIMESTAMP
283
284         def get_last_timestamp(self):
285                 return self._lastMessageTimestamp
286
287         def __call__(self, conversations):
288                 filteredConversations = [
289                         conversation
290                         for conversation in conversations
291                         if self._lastMessageTimestamp < conversation.time
292                 ]
293                 if filteredConversations and self._lastMessageTimestamp < filteredConversations[0].time:
294                         self._lastMessageTimestamp = filteredConversations[0].time
295                 return filteredConversations