3 from __future__ import with_statement
15 import util.coroutines as coroutines
16 import util.misc as misc_utils
17 import util.go_utils as gobject_utils
20 _moduleLogger = logging.getLogger(__name__)
23 class ConversationError(RuntimeError):
28 class Conversations(object):
30 OLDEST_COMPATIBLE_FORMAT_VERSION = misc_utils.parse_version("0.8.0")
32 def __init__(self, getter, asyncPool):
33 self._get_raw_conversations = getter
34 self._asyncPool = asyncPool
35 self._conversations = {}
36 self._loadedFromCache = False
37 self._hasDoneUpdate = False
39 self.updateSignalHandler = coroutines.CoTee()
43 return repr(self._get_raw_conversations.__name__)
46 _moduleLogger.debug("%s Loading cache" % (self._name, ))
47 assert not self._conversations
49 with open(path, "rb") as f:
50 fileVersion, fileBuild, convs = pickle.load(f)
51 except (pickle.PickleError, IOError, EOFError, ValueError):
52 _moduleLogger.exception("While loading for %s" % self._name)
55 if convs and misc_utils.compare_versions(
56 self.OLDEST_COMPATIBLE_FORMAT_VERSION,
57 misc_utils.parse_version(fileVersion),
59 _moduleLogger.info("%s Loaded cache" % (self._name, ))
60 self._conversations = convs
61 self._loadedFromCache = True
64 "%s Skipping cache due to version mismatch (%s-%s)" % (
65 self._name, fileVersion, fileBuild
70 _moduleLogger.info("%s Saving cache" % (self._name, ))
71 if not self._conversations:
72 _moduleLogger.info("%s Odd, no conversations to cache. Did we never load the cache?" % (self._name, ))
76 dataToDump = (constants.__version__, constants.__build__, self._conversations)
77 with open(path, "wb") as f:
78 pickle.dump(dataToDump, f, pickle.HIGHEST_PROTOCOL)
79 except (pickle.PickleError, IOError):
80 _moduleLogger.exception("While saving for %s" % self._name)
81 _moduleLogger.info("%s Cache saved" % (self._name, ))
83 def update(self, force=False):
84 if not force and self._conversations:
87 le = gobject_utils.AsyncLinearExecution(self._asyncPool, self._update)
90 @misc_utils.log_exception(_moduleLogger)
93 conversationResult = yield (
94 self._get_raw_conversations,
99 _moduleLogger.exception("%s While updating conversations" % (self._name, ))
102 oldConversationIds = set(self._conversations.iterkeys())
104 updateConversationIds = set()
105 conversations = list(conversationResult)
107 for conversation in conversations:
108 key = misc_utils.normalize_number(conversation.number)
110 mergedConversations = self._conversations[key]
112 mergedConversations = MergedConversations()
113 self._conversations[key] = mergedConversations
115 if self._loadedFromCache or self._hasDoneUpdate:
116 markAllAsRead = False
121 mergedConversations.append_conversation(conversation, markAllAsRead)
122 isConversationUpdated = True
123 except ConversationError, e:
124 isConversationUpdated = False
125 except AssertionError, e:
126 _moduleLogger.debug("%s Skipping conversation for %r because '%s'" % (self._name, key, e))
127 isConversationUpdated = False
128 except RuntimeError, e:
129 _moduleLogger.debug("%s Skipping conversation for %r because '%s'" % (self._name, key, e))
130 isConversationUpdated = False
132 if isConversationUpdated:
133 updateConversationIds.add(key)
135 for key in updateConversationIds:
136 mergedConv = self._conversations[key]
137 _moduleLogger.debug("%s \tUpdated %s" % (self._name, key))
138 for conv in mergedConv.conversations:
139 message = "%s \t\tUpdated %s (%r) %r %r %r" % (
140 self._name, conv.id, conv.time, conv.isRead, conv.isArchived, len(conv.messages)
142 _moduleLogger.debug(message)
144 if updateConversationIds:
145 message = (self, updateConversationIds, )
146 self.updateSignalHandler.stage.send(message)
147 self._hasDoneUpdate = True
149 def get_conversations(self):
150 return self._conversations.iterkeys()
152 def get_conversation(self, key):
153 return self._conversations[key]
155 def clear_conversation(self, key):
157 del self._conversations[key]
159 _moduleLogger.info("%s Conversation never existed for %r" % (self._name, key, ))
162 self._conversations.clear()
165 class MergedConversations(object):
168 self._conversations = []
170 def append_conversation(self, newConversation, markAllAsRead):
171 self._validate(newConversation)
172 for similarConversation in self._find_related_conversation(newConversation.id):
173 self._update_previous_related_conversation(similarConversation, newConversation)
174 self._remove_repeats(similarConversation, newConversation)
176 # HACK: Because GV marks all messages as read when you reply it has
177 # the following race:
178 # 1. Get all messages
179 # 2. Contact sends a text
180 # 3. User sends a text marking contacts text as read
181 # 4. Get all messages not returning text from step 2
182 # This isn't a problem for voicemails but we don't know(?( enough.
183 # So we hack around this by:
184 # * We cache to disk the history of messages sent/received
185 # * On first run we mark all server messages as read due to no cache
186 # * If not first load or from cache (disk or in-memory) then it must be unread
188 newConversation.isRead = True
190 newConversation.isRead = False
192 if newConversation.messages:
193 # must not have had all items removed due to duplicates
194 self._conversations.append(newConversation)
198 selfDict["conversations"] = [conv.to_dict() for conv in self._conversations]
202 def conversations(self):
203 return self._conversations
205 def _validate(self, newConversation):
206 if not self._conversations:
209 for constantField in ("number", ):
210 assert getattr(self._conversations[0], constantField) == getattr(newConversation, constantField), "Constant field changed, soemthing is seriously messed up: %r v %r" % (
211 getattr(self._conversations[0], constantField),
212 getattr(newConversation, constantField),
215 if newConversation.time <= self._conversations[-1].time:
216 raise ConversationError("Conversations got out of order")
218 def _find_related_conversation(self, convId):
219 similarConversations = (
221 for conversation in self._conversations
222 if conversation.id == convId
224 return similarConversations
226 def _update_previous_related_conversation(self, relatedConversation, newConversation):
227 for commonField in ("isSpam", "isTrash", "isArchived"):
228 newValue = getattr(newConversation, commonField)
229 setattr(relatedConversation, commonField, newValue)
231 def _remove_repeats(self, relatedConversation, newConversation):
232 newConversationMessages = newConversation.messages
233 newConversation.messages = [
235 for newMessage in newConversationMessages
236 if newMessage not in relatedConversation.messages
238 _moduleLogger.debug("Found %d new messages in conversation %s (%d/%d)" % (
239 len(newConversationMessages) - len(newConversation.messages),
241 len(newConversation.messages),
242 len(newConversationMessages),
244 assert 0 < len(newConversation.messages), "Everything shouldn't have been removed"
247 def filter_out_read(conversations):
250 for conversation in conversations
251 if not conversation.isRead and not conversation.isArchived
255 def is_message_from_self(message):
256 return message.whoFrom == "Me:"
259 def filter_out_self(conversations):
262 for newConversation in conversations
263 if len(newConversation.messages) and any(
264 not is_message_from_self(message)
265 for message in newConversation.messages
270 class FilterOutReported(object):
272 NULL_TIMESTAMP = datetime.datetime(1, 1, 1)
275 self._lastMessageTimestamp = self.NULL_TIMESTAMP
277 def get_last_timestamp(self):
278 return self._lastMessageTimestamp
280 def __call__(self, conversations):
281 filteredConversations = [
283 for conversation in conversations
284 if self._lastMessageTimestamp < conversation.time
286 if filteredConversations and self._lastMessageTimestamp < filteredConversations[0].time:
287 self._lastMessageTimestamp = filteredConversations[0].time
288 return filteredConversations