3 from __future__ import with_statement
15 import util.coroutines as coroutines
16 import util.misc as misc_utils
19 _moduleLogger = logging.getLogger(__name__)
22 class Conversations(object):
24 OLDEST_COMPATIBLE_FORMAT_VERSION = misc_utils.parse_version("0.8.0")
25 OLDEST_MESSAGE_WINDOW = datetime.timedelta(days=60)
27 def __init__(self, getter):
28 self._get_raw_conversations = getter
29 self._conversations = {}
31 self.updateSignalHandler = coroutines.CoTee()
35 return repr(self._get_raw_conversations.__name__)
38 assert not self._conversations
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)
46 if misc_utils.compare_versions(
47 self.OLDEST_COMPATIBLE_FORMAT_VERSION,
48 misc_utils.parse_version(fileVersion),
50 self._conversations = convs
53 "%s Skipping cache due to version mismatch (%s-%s)" % (
54 self._name, fileVersion, fileBuild
60 for conv in self._conversations.itervalues():
61 conv.compress(self.OLDEST_MESSAGE_WINDOW)
62 dataToDump = (constants.__version__, constants.__build__, self._conversations)
63 with open(path, "wb") as f:
64 pickle.dump(dataToDump, f, pickle.HIGHEST_PROTOCOL)
65 except (pickle.PickleError, IOError):
66 _moduleLogger.exception("While saving for %s" % self._name)
68 def update(self, force=False):
69 if not force and self._conversations:
72 oldConversationIds = set(self._conversations.iterkeys())
74 updateConversationIds = set()
75 conversations = list(self._get_raw_conversations())
77 for conversation in conversations:
78 key = misc_utils.normalize_number(conversation.number)
80 mergedConversations = self._conversations[key]
82 mergedConversations = MergedConversations()
83 self._conversations[key] = mergedConversations
86 mergedConversations.append_conversation(conversation)
87 isConversationUpdated = True
88 except RuntimeError, e:
90 _moduleLogger.debug("%s Skipping conversation for %r because '%s'" % (self._name, key, e))
91 isConversationUpdated = False
93 if isConversationUpdated:
94 updateConversationIds.add(key)
96 if updateConversationIds:
97 message = (self, updateConversationIds, )
98 self.updateSignalHandler.stage.send(message)
100 def get_conversations(self):
101 return self._conversations.iterkeys()
103 def get_conversation(self, key):
104 return self._conversations[key]
106 def clear_conversation(self, key):
108 del self._conversations[key]
110 _moduleLogger.info("%s Conversation never existed for %r" % (self._name, key, ))
113 self._conversations.clear()
116 class MergedConversations(object):
119 self._conversations = []
121 def append_conversation(self, newConversation):
122 self._validate(newConversation)
124 for similarConversation in self._find_related_conversation(newConversation.id):
125 self._update_previous_related_conversation(similarConversation, newConversation)
126 self._remove_repeats(similarConversation, newConversation)
129 # Hack to reduce a race window with GV marking messages as read
130 # because it thinks we replied when really we replied to the
131 # previous message. Clients of this code are expected to handle
132 # this gracefully. Other race conditions may exist but clients are
133 # responsible for them
134 if newConversation.messages:
135 newConversation.isRead = False
137 newConversation.isRead = True
138 self._conversations.append(newConversation)
142 selfDict["conversations"] = [conv.to_dict() for conv in self._conversations]
146 def conversations(self):
147 return self._conversations
149 def compress(self, timedelta):
150 now = datetime.datetime.now()
151 oldNumConvs = len(self._conversations)
152 oldConvs = self._conversations
153 self._conversations = [
155 for conv in self._conversations
156 if (now - conv.time) < timedelta
158 newNumConvs = len(self._conversations)
159 if oldNumConvs != newNumConvs:
160 _moduleLogger.debug("Compressed conversations from %s to %s" % (oldNumConvs, newNumConvs))
162 _moduleLogger.debug("Did not compress, %s" % (newNumConvs))
164 def _validate(self, newConversation):
165 if not self._conversations:
168 for constantField in ("number", ):
169 assert getattr(self._conversations[0], constantField) == getattr(newConversation, constantField), "Constant field changed, soemthing is seriously messed up: %r v %r" % (
170 getattr(self._conversations[0], constantField),
171 getattr(newConversation, constantField),
174 if newConversation.time <= self._conversations[-1].time:
175 raise RuntimeError("Conversations got out of order")
177 def _find_related_conversation(self, convId):
178 similarConversations = (
180 for conversation in self._conversations
181 if conversation.id == convId
183 return similarConversations
185 def _update_previous_related_conversation(self, relatedConversation, newConversation):
186 for commonField in ("isSpam", "isTrash", "isArchived"):
187 newValue = getattr(newConversation, commonField)
188 setattr(relatedConversation, commonField, newValue)
190 def _remove_repeats(self, relatedConversation, newConversation):
191 newConversationMessages = newConversation.messages
192 newConversation.messages = [
194 for newMessage in newConversationMessages
195 if newMessage not in relatedConversation.messages
197 _moduleLogger.debug("Found %d new messages in conversation %s (%d/%d)" % (
198 len(newConversationMessages) - len(newConversation.messages),
200 len(newConversation.messages),
201 len(newConversationMessages),
203 assert 0 < len(newConversation.messages), "Everything shouldn't have been removed"
206 def filter_out_read(conversations):
209 for conversation in conversations
210 if not conversation.isRead and not conversation.isArchived
214 def is_message_from_self(message):
215 return message.whoFrom == "Me:"
218 def filter_out_self(conversations):
221 for newConversation in conversations
222 if len(newConversation.messages) and any(
223 not is_message_from_self(message)
224 for message in newConversation.messages
229 class FilterOutReported(object):
231 NULL_TIMESTAMP = datetime.datetime(1, 1, 1)
234 self._lastMessageTimestamp = self.NULL_TIMESTAMP
236 def get_last_timestamp(self):
237 return self._lastMessageTimestamp
239 def __call__(self, conversations):
240 filteredConversations = [
242 for conversation in conversations
243 if self._lastMessageTimestamp < conversation.time
245 if filteredConversations and self._lastMessageTimestamp < filteredConversations[0].time:
246 self._lastMessageTimestamp = filteredConversations[0].time
247 return filteredConversations