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