Skip to content

Commit 1dba16a

Browse files
committed
Reverse the message buffer before returning messages from it (#1111)
1 parent 8b42790 commit 1dba16a

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/model/channel/channel_id.rs

+17-12
Original file line numberDiff line numberDiff line change
@@ -819,38 +819,41 @@ impl<'a> From<&'a GuildChannel> for ChannelId {
819819
#[derive(Clone, Debug)]
820820
#[cfg(feature = "model")]
821821
pub struct MessagesIter<H: AsRef<Http>> {
822-
channel_id: ChannelId,
823822
http: H,
823+
channel_id: ChannelId,
824824
buffer: Vec<Message>,
825825
before: Option<MessageId>,
826826
tried_fetch: bool,
827827
}
828828

829829
#[cfg(feature = "model")]
830830
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> {
832832
MessagesIter {
833-
channel_id,
834833
http,
834+
channel_id,
835835
buffer: Vec::new(),
836836
before: None,
837837
tried_fetch: false,
838838
}
839839
}
840840

841-
/// Fills the `self.buffer` cache of Messages.
841+
/// Fills the `self.buffer` cache with [`Message`]s.
842842
///
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
845845
/// `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.
848849
///
849850
/// If this method is called with `self.before` as None, the last 100
850851
/// (or lower) messages sent in the channel are added in the buffer.
851852
///
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
853854
/// element of the buffer and the newest message is the last.
855+
///
856+
/// [`Message`]: crate::model::channel::Message
854857
async fn refresh(&mut self) -> Result<()> {
855858
// Number of messages to fetch.
856859
let grab_size = 100;
@@ -861,11 +864,13 @@ impl<H: AsRef<Http>> MessagesIter<H> {
861864
if let Some(before) = self.before {
862865
b.before(before);
863866
}
867+
864868
b.limit(grab_size)
865869
}).await?;
866870

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);
869874

870875
self.tried_fetch = true;
871876

@@ -908,7 +913,7 @@ impl<H: AsRef<Http>> MessagesIter<H> {
908913
///
909914
/// [`messages`]: ../id/struct.ChannelId.html#method.messages
910915
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);
912917

913918
futures::stream::unfold(init_state, |mut state| async {
914919
if state.buffer.is_empty() && state.before.is_some() || !state.tried_fetch {

0 commit comments

Comments
 (0)