6
from urlparse import parse_qs
8
from grackle.error import (
11
UnsupportedDisplayType,
16
SUPPORTED_DISPLAY_TYPES = set(['all', 'text-only', 'headers-only'])
19
SUPPORTED_ORDERS = set(
20
['date', 'author', 'subject', 'thread_newest', 'thread_oldest',
24
def threaded_messages(messages):
28
for message in messages:
29
if message.get('replies') is None:
30
threads[message['message_id']] = [message]
33
pending.append(message)
34
for message in pending:
35
threads[message['replies']].append(message)
36
return threads.values()
39
def make_json_message(message_id, body='body', headers=None, hidden=False):
42
headers['Message-Id'] = message_id
44
'message_id': message_id,
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'),
52
'replies': headers.get('in-reply-to', None),
59
"""A memory-backed message store."""
61
def __init__(self, message_archives):
63
self.message_archives = message_archives
66
def is_multipart(message):
67
return isinstance(message['body'], list)
69
def put_message(self, archive_id, message_id, message):
70
# Make a json message.
71
messages = self.message_archives[archive_id]
72
json_message = make_json_message(message_id, body=message)
73
messages.append(json_message)
75
def get_messages(self, archive_id, query_string):
76
"""Return matching messages.
78
:param archive_id: The archive to retrieve from.
79
:param query_string: Contains 'parameters', which is a JSON-format
80
string describing parameters.
82
query = parse_qs(query_string)
83
parameters = simplejson.loads(query['parameters'][0])
84
order = parameters.get('order')
85
messages = self.message_archives[archive_id]
87
if order not in SUPPORTED_ORDERS:
88
raise UnsupportedOrder
89
elif order.startswith('thread_'):
90
threaded = threaded_messages(messages)
92
if order == 'thread_subject':
93
threaded.sort(key=lambda t: t[0]['subject'])
94
if order == 'thread_oldest':
95
threaded.sort(key=lambda t: min(m['date'] for m in t))
96
if order == 'thread_newest':
97
threaded.sort(key=lambda t: max(m['date'] for m in t))
98
for thread in threaded:
99
messages.extend(thread)
101
messages.sort(key=lambda m: m[order])
102
display_type = parameters.get('display_type', 'all')
103
if display_type not in SUPPORTED_DISPLAY_TYPES:
104
raise UnsupportedDisplayType
105
if 'date_range' in parameters:
107
start_date, end_date = parameters['date_range'].split('..')
108
if not start_date or not end_date:
109
raise UnparsableDateRange
111
raise UnparsableDateRange
113
for message in messages:
114
if (not parameters['include_hidden'] and message['hidden']):
116
if ('message_ids' in parameters
117
and message['message_id'] not in parameters['message_ids']):
119
if ('date_range' in parameters
120
and (message['date'] < start_date
121
or message['date'] > end_date)):
123
message = dict(message)
124
if 'headers' in parameters:
126
(k, v) for k, v in message['headers'].iteritems()
127
if k in parameters['headers'])
128
message['headers'] = headers
129
if display_type == 'headers-only':
131
elif display_type == 'text-only' and self.is_multipart(message):
133
part.get_payload() for part in message['body']
134
if part.get_content_type() == 'text/plain']
135
message['body'] = '\n\n'.join(text_parts)
136
elif display_type == 'all' and self.is_multipart(message):
137
parts = [str(part.get_payload()) for part in message['body']]
138
message['body'] = '\n\n'.join(parts)
139
max_body = parameters.get('max_body_length')
140
if max_body is not None and display_type != 'headers-only':
141
message['body'] = message['body'][:max_body]
142
new_messages.append(message)
143
messages = new_messages
144
limit = parameters.get('limit', 100)
145
memo = parameters.get('memo')
146
message_id_indices = dict(
147
(m['message_id'], idx) for idx, m in enumerate(messages))
151
start = message_id_indices[memo.encode('rot13')]
153
previous_memo = messages[start - 1]['message_id'].encode('rot13')
156
end = min(start + limit, len(messages))
157
if end < len(messages):
158
next_memo = messages[end]['message_id'].encode('rot13')
161
messages = messages[start:end]
164
'messages': messages,
165
'next_memo': next_memo,
166
'previous_memo': previous_memo
170
def hide_message(self, archive_id, query_string):
171
"""Return matching messages.
173
:param archive_id: The archive to retrieve from.
174
:param query_string: Contains 'parameters', which is a JSON-format
175
string describing parameters.
177
query = parse_qs(query_string)
178
parameters = simplejson.loads(query['parameters'][0])
179
message_id = parameters['message_id']
180
hidden = parameters['hidden']
181
messages = self.message_archives[archive_id]
182
for message in messages:
183
if message['message_id'] == message_id:
184
message['hidden'] = hidden
186
'message_id': message_id,
190
raise MessageIdNotFound