38
36
return threads.values()
41
def get_body_text(message):
42
"""Return the first plain/text messaage part."""
43
if not message.is_multipart():
44
return message.get_payload()
45
for part in email.iterators.typed_subpart_iterator(message, 'multipart'):
46
subparts = part.get_payload()
47
for subpart in subparts:
48
if subpart.get_content_type() == 'text/plain':
49
return subpart.get_payload().strip()
53
def make_json_message(message_id, raw_message, hidden=False):
54
message = email.message_from_string(raw_message)
55
headers = dict(message.items())
57
'message_id': message_id,
59
# This is broken because the in-reply-to must be encoded.
60
# X-Message-ID-Hash is calculated from the Base 32.
61
'thread_id': headers.get('in-reply-to', message_id),
62
'date': headers.get('date'),
63
'subject': headers.get('subject'),
64
'author': headers.get('from'),
67
'replies': headers.get('in-reply-to'),
68
'body': get_body_text(message),
74
40
"""A memory-backed message store."""
76
def __init__(self, message_archives):
42
def __init__(self, messages):
78
self.message_archives = message_archives
44
self.messages = messages
81
47
def is_multipart(message):
82
48
return isinstance(message['body'], list)
84
def put_message(self, archive_id, message_id, raw_message):
85
# XXX sinzui 2012-02-29: this needs to raise an error
86
# if the th archive_id is invalid, message_id is not base32
87
# or the raw message is not an email.
88
if archive_id not in self.message_archives:
89
raise ArchiveIdNotFound()
91
raise ValueError('raw_message is not a message.')
92
json_message = make_json_message(message_id, raw_message)
93
messages = self.message_archives[archive_id]
94
messages.append(json_message)
96
50
def get_messages(self, archive_id, query_string):
97
51
"""Return matching messages.
191
def hide_message(self, archive_id, message_id, query_string):
192
"""Change the visbility of a message in an archive.
145
def hide_message(self, archive_id, query_string):
146
"""Return matching messages.
194
148
:param archive_id: The archive to retrieve from.
195
149
:param query_string: Contains 'parameters', which is a JSON-format
198
152
query = parse_qs(query_string)
199
153
parameters = simplejson.loads(query['parameters'][0])
154
message_id = parameters['message_id']
200
155
hidden = parameters['hidden']
201
messages = self.message_archives[archive_id]
156
messages = self.messages[archive_id]
202
157
for message in messages:
203
158
if message['message_id'] == message_id:
204
159
message['hidden'] = hidden