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