Skip to content

Commit 7793458

Browse files
gnpricechrisbobbe
authored andcommitted
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 6ff889b commit 7793458

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

lib/model/message_list.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ class MessageListView with ChangeNotifier, _MessageSequence {
570570
// Now [middleMessage] is the last message (the one just added).
571571
}
572572
_haveOldest = result.foundOldest;
573-
_haveNewest = true; // TODO(#82)
573+
_haveNewest = result.foundNewest;
574574
_setStatus(FetchingStatus.idle, was: FetchingStatus.fetchInitial);
575575
}
576576

test/example_data.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,26 @@ GetMessagesResult newestGetMessagesResult({
608608
);
609609
}
610610

611+
/// A GetMessagesResult the server might return on an initial request
612+
/// when the anchor is in the middle of history (e.g., a /near/ link).
613+
GetMessagesResult nearGetMessagesResult({
614+
required int anchor,
615+
bool foundAnchor = true,
616+
required bool foundOldest,
617+
required bool foundNewest,
618+
bool historyLimited = false,
619+
required List<Message> messages,
620+
}) {
621+
return GetMessagesResult(
622+
anchor: anchor,
623+
foundAnchor: foundAnchor,
624+
foundOldest: foundOldest,
625+
foundNewest: foundNewest,
626+
historyLimited: historyLimited,
627+
messages: messages,
628+
);
629+
}
630+
611631
/// A GetMessagesResult the server might return when we request older messages.
612632
GetMessagesResult olderGetMessagesResult({
613633
required int anchor,

test/model/message_list_test.dart

Lines changed: 33 additions & 2 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(
@@ -421,8 +440,20 @@ void main() {
421440
});
422441

423442
test('while in mid-history', () async {
424-
}, skip: true, // TODO(#82): not yet possible to exercise this case
425-
);
443+
final stream = eg.stream();
444+
await prepare(narrow: ChannelNarrow(stream.streamId));
445+
connection.prepare(json: nearResult(
446+
anchor: 1000, foundOldest: true, foundNewest: false,
447+
messages: List.generate(30,
448+
(i) => eg.streamMessage(id: 1000 + i, stream: stream))).toJson());
449+
await model.fetchInitial();
450+
checkNotifiedOnce();
451+
452+
check(model).messages.length.equals(30);
453+
await store.addMessage(eg.streamMessage(stream: stream));
454+
checkNotNotified();
455+
check(model).messages.length.equals(30);
456+
});
426457

427458
test('before fetch', () async {
428459
final stream = eg.stream();

0 commit comments

Comments
 (0)