@@ -530,25 +530,66 @@ DmMessage dmMessage({
530
530
}) as Map <String , dynamic >);
531
531
}
532
532
533
- /// A GetMessagesResult the server might return on an `anchor=newest` request.
534
- GetMessagesResult newestGetMessagesResult ({
535
- required bool foundOldest,
533
+ /// A GetMessagesResult the server might return for
534
+ /// a request that sent the given [anchor] .
535
+ ///
536
+ /// The request's anchor controls the response's [GetMessagesResult.anchor] ,
537
+ /// affects the default for [foundAnchor] ,
538
+ /// and in some cases forces the value of [foundOldest] or [foundNewest] .
539
+ GetMessagesResult getMessagesResult ({
540
+ required Anchor anchor,
541
+ bool ? foundAnchor,
542
+ bool ? foundOldest,
543
+ bool ? foundNewest,
536
544
bool historyLimited = false ,
537
545
required List <Message > messages,
538
546
}) {
539
- return GetMessagesResult (
540
- // These anchor, foundAnchor, and foundNewest values are what the server
541
- // appears to always return when the request had `anchor=newest`.
542
- anchor: 10000000000000000 , // that's 16 zeros
543
- foundAnchor: false ,
544
- foundNewest: true ,
547
+ final resultAnchor = switch (anchor) {
548
+ AnchorCode .oldest => 0 ,
549
+ NumericAnchor (: final messageId) => messageId,
550
+ AnchorCode .firstUnread =>
551
+ throw ArgumentError ("firstUnread not accepted in this helper; try NumericAnchor" ),
552
+ AnchorCode .newest => 10_000_000_000_000_000 , // that's 16 zeros
553
+ };
545
554
555
+ switch (anchor) {
556
+ case AnchorCode .oldest || AnchorCode .newest:
557
+ assert (foundAnchor == null );
558
+ foundAnchor = false ;
559
+ case AnchorCode .firstUnread || NumericAnchor ():
560
+ foundAnchor ?? = true ;
561
+ }
562
+
563
+ if (anchor == AnchorCode .oldest) {
564
+ assert (foundOldest == null );
565
+ foundOldest = true ;
566
+ } else if (anchor == AnchorCode .newest) {
567
+ assert (foundNewest == null );
568
+ foundNewest = true ;
569
+ }
570
+ if (foundOldest == null || foundNewest == null ) throw ArgumentError ();
571
+
572
+ return GetMessagesResult (
573
+ anchor: resultAnchor,
574
+ foundAnchor: foundAnchor,
546
575
foundOldest: foundOldest,
576
+ foundNewest: foundNewest,
547
577
historyLimited: historyLimited,
548
578
messages: messages,
549
579
);
550
580
}
551
581
582
+ /// A GetMessagesResult the server might return on an `anchor=newest` request,
583
+ /// or `anchor=first_unread` when there are no unreads.
584
+ GetMessagesResult newestGetMessagesResult ({
585
+ required bool foundOldest,
586
+ bool historyLimited = false ,
587
+ required List <Message > messages,
588
+ }) {
589
+ return getMessagesResult (anchor: AnchorCode .newest, foundOldest: foundOldest,
590
+ historyLimited: historyLimited, messages: messages);
591
+ }
592
+
552
593
/// A GetMessagesResult the server might return on an initial request
553
594
/// when the anchor is in the middle of history (e.g., a /near/ link).
554
595
GetMessagesResult nearGetMessagesResult ({
0 commit comments