@@ -819,38 +819,41 @@ impl<'a> From<&'a GuildChannel> for ChannelId {
819
819
#[ derive( Clone , Debug ) ]
820
820
#[ cfg( feature = "model" ) ]
821
821
pub struct MessagesIter < H : AsRef < Http > > {
822
- channel_id : ChannelId ,
823
822
http : H ,
823
+ channel_id : ChannelId ,
824
824
buffer : Vec < Message > ,
825
825
before : Option < MessageId > ,
826
826
tried_fetch : bool ,
827
827
}
828
828
829
829
#[ cfg( feature = "model" ) ]
830
830
impl < H : AsRef < Http > > MessagesIter < H > {
831
- fn new ( channel_id : ChannelId , http : H ) -> MessagesIter < H > {
831
+ fn new ( http : H , channel_id : ChannelId ) -> MessagesIter < H > {
832
832
MessagesIter {
833
- channel_id,
834
833
http,
834
+ channel_id,
835
835
buffer : Vec :: new ( ) ,
836
836
before : None ,
837
837
tried_fetch : false ,
838
838
}
839
839
}
840
840
841
- /// Fills the `self.buffer` cache of Messages .
841
+ /// Fills the `self.buffer` cache with [`Message`]s .
842
842
///
843
- /// This drops any messages that were currently in the buffer, so it should
844
- /// only be called when `self.buffer` is empty. Additionally, this updates
843
+ /// This drops any messages that were currently in the buffer. Ideally, it
844
+ /// should only be called when `self.buffer` is empty. Additionally, this updates
845
845
/// `self.before` so that the next call does not return duplicate items.
846
- /// If there are no more messages to be fetched, then this marks
847
- /// `self.before` as None, indicating that no more calls ought to be made.
846
+ ///
847
+ /// If there are no more messages to be fetched, then this sets `self.before`
848
+ /// as `None`, indicating that no more calls ought to be made.
848
849
///
849
850
/// If this method is called with `self.before` as None, the last 100
850
851
/// (or lower) messages sent in the channel are added in the buffer.
851
852
///
852
- /// The messages are sorted such that the newest message is the first
853
+ /// The messages are sorted such that the newest message is the first
853
854
/// element of the buffer and the newest message is the last.
855
+ ///
856
+ /// [`Message`]: crate::model::channel::Message
854
857
async fn refresh ( & mut self ) -> Result < ( ) > {
855
858
// Number of messages to fetch.
856
859
let grab_size = 100 ;
@@ -861,11 +864,13 @@ impl<H: AsRef<Http>> MessagesIter<H> {
861
864
if let Some ( before) = self . before {
862
865
b. before ( before) ;
863
866
}
867
+
864
868
b. limit ( grab_size)
865
869
} ) . await ?;
866
870
867
- self . before = self . buffer . get ( 0 )
868
- . map ( |message| message. id ) ;
871
+ self . buffer . reverse ( ) ;
872
+
873
+ self . before = self . buffer . first ( ) . map ( |m| m. id ) ;
869
874
870
875
self . tried_fetch = true ;
871
876
@@ -908,7 +913,7 @@ impl<H: AsRef<Http>> MessagesIter<H> {
908
913
///
909
914
/// [`messages`]: ../id/struct.ChannelId.html#method.messages
910
915
pub fn stream ( http : impl AsRef < Http > , channel_id : ChannelId ) -> impl Stream < Item =Result < Message > > {
911
- let init_state = MessagesIter :: new ( channel_id , http ) ;
916
+ let init_state = MessagesIter :: new ( http , channel_id ) ;
912
917
913
918
futures:: stream:: unfold ( init_state, |mut state| async {
914
919
if state. buffer . is_empty ( ) && state. before . is_some ( ) || !state. tried_fetch {
0 commit comments