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 Conversations(object):
25 OLDEST_COMPATIBLE_FORMAT_VERSION = misc_utils.parse_version("0.8.0")
26 OLDEST_MESSAGE_WINDOW = datetime.timedelta(days=60)
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
35 self.updateSignalHandler = coroutines.CoTee()
39 return repr(self._get_raw_conversations.__name__)
42 _moduleLogger.debug("%s Loading cache" % (self._name, ))
43 assert not self._conversations
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)
51 if misc_utils.compare_versions(
52 self.OLDEST_COMPATIBLE_FORMAT_VERSION,
53 misc_utils.parse_version(fileVersion),
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)))
64 "%s Skipping cache due to version mismatch (%s-%s)" % (
65 self._name, fileVersion, fileBuild
71 _moduleLogger.info("%s Saving cache" % (self._name, ))
72 #for conv in self._conversations.itervalues():
73 # conv.compress(self.OLDEST_MESSAGE_WINDOW)
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)))
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, ))
87 def update(self, force=False):
88 if not force and self._conversations:
91 le = gobject_utils.AsyncLinearExecution(self._asyncPool, self._update)
94 @misc_utils.log_exception(_moduleLogger)
96 conversationResult = yield (
97 self._get_raw_conversations,
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
120 mergedConversations.append_conversation(conversation, markAllAsRead)
121 isConversationUpdated = True
122 except RuntimeError, e:
124 _moduleLogger.debug("%s Skipping conversation for %r because '%s'" % (self._name, key, e))
125 isConversationUpdated = False
127 if isConversationUpdated:
128 updateConversationIds.add(key)
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)))
136 if updateConversationIds:
137 message = (self, updateConversationIds, )
138 self.updateSignalHandler.stage.send(message)
139 self._hasDoneUpdate = True
141 def get_conversations(self):
142 return self._conversations.iterkeys()
144 def get_conversation(self, key):
145 return self._conversations[key]
147 def clear_conversation(self, key):
149 del self._conversations[key]
151 _moduleLogger.info("%s Conversation never existed for %r" % (self._name, key, ))
154 self._conversations.clear()
157 class MergedConversations(object):
160 self._conversations = []
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)
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
180 newConversation.isRead = True
182 newConversation.isRead = False
184 if newConversation.messages:
185 # must not have had all items removed due to duplicates
186 self._conversations.append(newConversation)
190 selfDict["conversations"] = [conv.to_dict() for conv in self._conversations]
194 def conversations(self):
195 return self._conversations
197 def compress(self, timedelta):
198 now = datetime.datetime.now()
199 oldNumConvs = len(self._conversations)
200 oldConvs = self._conversations
201 self._conversations = [
203 for conv in self._conversations
204 if (now - conv.time) < timedelta
206 newNumConvs = len(self._conversations)
207 if oldNumConvs != newNumConvs:
208 _moduleLogger.debug("Compressed conversations from %s to %s" % (oldNumConvs, newNumConvs))
210 _moduleLogger.debug("Did not compress, %s" % (newNumConvs))
212 def _validate(self, newConversation):
213 if not self._conversations:
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),
222 if newConversation.time <= self._conversations[-1].time:
223 raise RuntimeError("Conversations got out of order")
225 def _find_related_conversation(self, convId):
226 similarConversations = (
228 for conversation in self._conversations
229 if conversation.id == convId
231 return similarConversations
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)
238 def _remove_repeats(self, relatedConversation, newConversation):
239 newConversationMessages = newConversation.messages
240 newConversation.messages = [
242 for newMessage in newConversationMessages
243 if newMessage not in relatedConversation.messages
245 _moduleLogger.debug("Found %d new messages in conversation %s (%d/%d)" % (
246 len(newConversationMessages) - len(newConversation.messages),
248 len(newConversation.messages),
249 len(newConversationMessages),
251 assert 0 < len(newConversation.messages), "Everything shouldn't have been removed"
254 def filter_out_read(conversations):
257 for conversation in conversations
258 if not conversation.isRead and not conversation.isArchived
262 def is_message_from_self(message):
263 return message.whoFrom == "Me:"
266 def filter_out_self(conversations):
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
277 class FilterOutReported(object):
279 NULL_TIMESTAMP = datetime.datetime(1, 1, 1)
282 self._lastMessageTimestamp = self.NULL_TIMESTAMP
284 def get_last_timestamp(self):
285 return self._lastMessageTimestamp
287 def __call__(self, conversations):
288 filteredConversations = [
290 for conversation in conversations
291 if self._lastMessageTimestamp < conversation.time
293 if filteredConversations and self._lastMessageTimestamp < filteredConversations[0].time:
294 self._lastMessageTimestamp = filteredConversations[0].time
295 return filteredConversations