43
36
return threads.values()
46
def get_body_text(message):
47
"""Return the first plain/text messaage part."""
48
if not message.is_multipart():
49
return message.get_payload()
50
for part in email.iterators.typed_subpart_iterator(message, 'multipart'):
51
subparts = part.get_payload()
52
for subpart in subparts:
53
if subpart.get_content_type() == 'text/plain':
54
return subpart.get_payload().strip()
58
def make_json_message(message_id, raw_message, hidden=False):
59
message = email.message_from_string(raw_message)
60
headers = dict(message.items())
39
def make_json_message(message_id, body='body', headers=None, hidden=False):
42
headers['Message-Id'] = message_id
62
44
'message_id': message_id,
63
45
'headers': headers,
64
# This is broken because the in-reply-to must be encoded.
65
# X-Message-ID-Hash is calculated from the Base 32.
66
'thread_id': headers.get('in-reply-to', message_id),
67
'date': headers.get('date'),
68
'subject': headers.get('subject'),
69
'author': headers.get('from'),
46
'thread_id': message_id,
47
'date': headers.get('date', '2005-01-01'),
48
'subject': headers.get('subject', 'subject'),
49
'author': headers.get('author', 'author'),
72
'replies': headers.get('in-reply-to'),
73
'body': get_body_text(message),
52
'replies': headers.get('in-reply-to', None),
86
66
def is_multipart(message):
87
67
return isinstance(message['body'], list)
89
def put_archive(self, archive_id, raw_archive=None):
90
# XXX sinzui 2012-02-29: this needs to raise an error
91
# if the th archive_id is invalid, or the raw archive is not mbox.
92
if archive_id in self.message_archives:
93
raise ArchiveIdExists()
94
self.message_archives[archive_id] = []
96
def put_message(self, archive_id, message_id, raw_message):
97
# XXX sinzui 2012-02-29: this needs to raise an error
98
# if the th archive_id is invalid, message_id is not base32
99
# or the raw message is not an email.
100
if archive_id not in self.message_archives:
101
raise ArchiveIdNotFound()
103
raise ValueError('raw_message is not a message.')
104
json_message = make_json_message(message_id, raw_message)
69
def put_message(self, archive_id, message_id, message):
70
# Make a json message.
105
71
messages = self.message_archives[archive_id]
72
json_message = make_json_message(message_id, body=message)
106
73
messages.append(json_message)
108
75
def get_messages(self, archive_id, query_string):
203
def hide_message(self, archive_id, message_id, query_string):
204
"""Change the visbility of a message in an archive.
170
def hide_message(self, archive_id, query_string):
171
"""Return matching messages.
206
173
:param archive_id: The archive to retrieve from.
207
174
:param query_string: Contains 'parameters', which is a JSON-format
210
177
query = parse_qs(query_string)
211
178
parameters = simplejson.loads(query['parameters'][0])
179
message_id = parameters['message_id']
212
180
hidden = parameters['hidden']
213
181
messages = self.message_archives[archive_id]
214
182
for message in messages: