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