122
113
'date': message.get('date'),
123
114
'from': message.get('from'),
124
115
'subject': message.get('subject'),
125
'message-id': message.get('message-id'),
128
def _trim(self, sequence, end):
129
"""Return the sequence with one of the ends trimmed.
131
:param end: if true, remove the last element. otherwise remove
139
def get_messages(self, archive_uuid, order, count, memo, backward=False):
118
def get_messages(self, archive_uuid, order, count, start):
140
119
if order in ("date", "-date"):
141
120
reversed = order[0] == '-'
143
122
raise AssertionError("Unsupported order.")
145
memo = uuid.UUID(memo)
153
# Get up to n+1 messages from the memo: the last item of the
154
# previous batch (because that's where the memo starts) + this
156
123
pairs = self.archive_messages.get(
157
archive_uuid, column_count=count + 1, column_start=start,
158
column_finish=finish, column_reversed=reversed).items()
160
if len(pairs) and memo and pairs[0][0] <= memo:
161
# The memo (from the previous batch) was included in the result.
163
pairs = self._trim(pairs, False ^ backward)
164
elif len(pairs) > count:
165
# There was no memo in the result, so the n+1th element is
166
# unnecessary. Kill it.
167
pairs = self._trim(pairs, True ^ backward)
170
return (None, [], None)
172
assert 0 < len(pairs) <= count
174
# We've narrowed down the message references. Fetch the messages.
124
archive_uuid, column_count=count + 1,
125
column_start=start, column_reversed=reversed).items()
175
126
ids = [v for k, v in pairs]
176
127
messages = self.messages.multiget(
177
ids, columns=['date', 'from', 'subject', 'message-id'])
128
ids, columns=['date', 'from', 'subject'])
129
actual_count = len(pairs)
130
if len(pairs) > count:
131
assert len(pairs) == count + 1
133
next_memo = str(pairs[count][0])
181
[self._format_message(messages[id]) for id in ids],
137
[self._format_message(messages[id]) for id in ids[:actual_count]],