Skip to content

Commit 774f60e

Browse files
committed
msglist: Set haveNewest from response, like haveOldest
This is NFC with a correctly-behaved server: we set `anchor=newest`, so the server always sets `found_newest` to true. Conversely, this will be helpful as we generalize `fetchInitial` to work with other anchor values; we'll use the `found_newest` value given by the server, without trying to predict it from the anchor. The server behavior that makes this effectively NFC isn't quite explicit in the API docs. Those say: found_newest: boolean Whether the server promises that the messages list includes the very newest messages matching the narrow (used by clients that paginate their requests to decide whether there may be more messages to fetch). https://zulip.com/api/get-messages#response But with `anchor=newest`, the response does need to include the very newest messages in the narrow -- that's the meaning of that `anchor` value. So the server is in fact promising the list includes those, and `found_newest` is therefore required to be true. (And indeed in practice the server does set `found_newest` to true when `anchor=newest`; it has specific logic to do so.)
1 parent e900b2d commit 774f60e

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

lib/model/message_list.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ class MessageListView with ChangeNotifier, _MessageSequence {
540540
// Now [middleMessage] is the last message (the one just added).
541541
}
542542
_haveOldest = result.foundOldest;
543-
_haveNewest = true; // TODO(#82)
543+
_haveNewest = result.foundNewest;
544544
_setStatus(FetchingStatus.idle, was: FetchingStatus.fetchInitial);
545545
}
546546

test/example_data.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,26 @@ GetMessagesResult newestGetMessagesResult({
549549
);
550550
}
551551

552+
/// A GetMessagesResult the server might return on an initial request
553+
/// when the anchor is in the middle of history (e.g., a /near/ link).
554+
GetMessagesResult nearGetMessagesResult({
555+
required int anchor,
556+
bool foundAnchor = true,
557+
required bool foundOldest,
558+
required bool foundNewest,
559+
bool historyLimited = false,
560+
required List<Message> messages,
561+
}) {
562+
return GetMessagesResult(
563+
anchor: anchor,
564+
foundAnchor: foundAnchor,
565+
foundOldest: foundOldest,
566+
foundNewest: foundNewest,
567+
historyLimited: historyLimited,
568+
messages: messages,
569+
);
570+
}
571+
552572
/// A GetMessagesResult the server might return when we request older messages.
553573
GetMessagesResult olderGetMessagesResult({
554574
required int anchor,

test/model/message_list_test.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import 'recent_senders_test.dart' as recent_senders_test;
2626
import 'test_store.dart';
2727

2828
const newestResult = eg.newestGetMessagesResult;
29+
const nearResult = eg.nearGetMessagesResult;
2930
const olderResult = eg.olderGetMessagesResult;
3031

3132
void main() {
@@ -185,6 +186,24 @@ void main() {
185186
..haveNewest.isTrue();
186187
});
187188

189+
test('early in history', () async {
190+
// For now, this gets a response that isn't realistic for the
191+
// request it sends, to simulate when we start sending requests
192+
// that would make this response realistic.
193+
// TODO(#82): send appropriate fetch request
194+
await prepare();
195+
connection.prepare(json: nearResult(
196+
anchor: 1000, foundOldest: true, foundNewest: false,
197+
messages: List.generate(111, (i) => eg.streamMessage(id: 990 + i)),
198+
).toJson());
199+
await model.fetchInitial();
200+
checkNotifiedOnce();
201+
check(model)
202+
..messages.length.equals(111)
203+
..haveOldest.isTrue()
204+
..haveNewest.isFalse();
205+
});
206+
188207
test('no messages found', () async {
189208
await prepare();
190209
connection.prepare(json: newestResult(

0 commit comments

Comments
 (0)