@@ -589,25 +589,66 @@ DmMessage dmMessage({
589
589
}) as Map <String , dynamic >);
590
590
}
591
591
592
- /// A GetMessagesResult the server might return on an `anchor=newest` request.
593
- GetMessagesResult newestGetMessagesResult ({
594
- required bool foundOldest,
592
+ /// A GetMessagesResult the server might return for
593
+ /// a request that sent the given [anchor] .
594
+ ///
595
+ /// The request's anchor controls the response's [GetMessagesResult.anchor] ,
596
+ /// affects the default for [foundAnchor] ,
597
+ /// and in some cases forces the value of [foundOldest] or [foundNewest] .
598
+ GetMessagesResult getMessagesResult ({
599
+ required Anchor anchor,
600
+ bool ? foundAnchor,
601
+ bool ? foundOldest,
602
+ bool ? foundNewest,
595
603
bool historyLimited = false ,
596
604
required List <Message > messages,
597
605
}) {
598
- return GetMessagesResult (
599
- // These anchor, foundAnchor, and foundNewest values are what the server
600
- // appears to always return when the request had `anchor=newest`.
601
- anchor: 10000000000000000 , // that's 16 zeros
602
- foundAnchor: false ,
603
- foundNewest: true ,
606
+ final resultAnchor = switch (anchor) {
607
+ AnchorCode .oldest => 0 ,
608
+ NumericAnchor (: final messageId) => messageId,
609
+ AnchorCode .firstUnread =>
610
+ throw ArgumentError ("firstUnread not accepted in this helper; try NumericAnchor" ),
611
+ AnchorCode .newest => 10_000_000_000_000_000 , // that's 16 zeros
612
+ };
604
613
614
+ switch (anchor) {
615
+ case AnchorCode .oldest || AnchorCode .newest:
616
+ assert (foundAnchor == null );
617
+ foundAnchor = false ;
618
+ case AnchorCode .firstUnread || NumericAnchor ():
619
+ foundAnchor ?? = true ;
620
+ }
621
+
622
+ if (anchor == AnchorCode .oldest) {
623
+ assert (foundOldest == null );
624
+ foundOldest = true ;
625
+ } else if (anchor == AnchorCode .newest) {
626
+ assert (foundNewest == null );
627
+ foundNewest = true ;
628
+ }
629
+ if (foundOldest == null || foundNewest == null ) throw ArgumentError ();
630
+
631
+ return GetMessagesResult (
632
+ anchor: resultAnchor,
633
+ foundAnchor: foundAnchor,
605
634
foundOldest: foundOldest,
635
+ foundNewest: foundNewest,
606
636
historyLimited: historyLimited,
607
637
messages: messages,
608
638
);
609
639
}
610
640
641
+ /// A GetMessagesResult the server might return on an `anchor=newest` request,
642
+ /// or `anchor=first_unread` when there are no unreads.
643
+ GetMessagesResult newestGetMessagesResult ({
644
+ required bool foundOldest,
645
+ bool historyLimited = false ,
646
+ required List <Message > messages,
647
+ }) {
648
+ return getMessagesResult (anchor: AnchorCode .newest, foundOldest: foundOldest,
649
+ historyLimited: historyLimited, messages: messages);
650
+ }
651
+
611
652
/// A GetMessagesResult the server might return on an initial request
612
653
/// when the anchor is in the middle of history (e.g., a /near/ link).
613
654
GetMessagesResult nearGetMessagesResult ({
0 commit comments