pub struct Timeline {
pub count: u32,
pub next_cursor: Option<String>,
pub loaded: bool,
/* private fields */
}Expand description
Helper struct to navigate collections of direct messages by tracking the status of Twitter’s cursor references.
The API of the Direct Message Timeline differs from the Tweet Timeline, in that Twitter
returns a “cursor” ID instead of paging through results by asking for messages before or after
a certain ID. It’s not a strict CursorIter, though, in that there is no “previous cursor”
ID given by Twitter; messages are loaded one-way, from newest to oldest.
To start using a Timeline, call list to set one up. Before starting, you can call
with_page_size to set how many messages to ask for at once. Then use start and next_page
to load messages one page at a time.
let timeline = egg_mode::direct::list(&token).with_page_size(50);
let mut messages = timeline.start().await.unwrap();
while timeline.next_cursor.is_some() {
let next_page = timeline.next_page().await.unwrap();
messages.extend(next_page.response);
}An adapter is provided which converts a Timeline into a futures::stream::Stream which
yields one message at a time and lazily loads each page as needed. As the stream’s Item is a
Result which can express the error caused by loading the next page, it also implements
futures::stream::TryStream as well. The previous example can also be expressed like this:
use egg_mode::Response;
use egg_mode::direct::DirectMessage;
use futures::stream::TryStreamExt;
let timeline = egg_mode::direct::list(&token).with_page_size(50);
let messages = timeline.into_stream()
.try_collect::<Vec<Response<DirectMessage>>>()
.await
.unwrap();In addition, an adapter is available which loads all available messages and sorts them into
“conversations” between the authenticated user and other users. The into_conversations
adapter loads all available messages and returns a DMConversations map after sorting them.
Fields§
§count: u32The number of messages to request in a single page. The default is 20; the maximum is 50.
next_cursor: Option<String>The string ID that can be used to load the next page of results. A value of None
indicates that either no messages have been loaded yet, or that the most recently loaded
page is the last page of messages available.
loaded: boolWhether this Timeline has been called yet.
Implementations§
Source§impl Timeline
impl Timeline
Sourcepub fn with_page_size(self, count: u32) -> Self
pub fn with_page_size(self, count: u32) -> Self
Builder function to set the page size. The default value for the page size is 20; the maximum allowed is 50.
Sourcepub fn start(
&mut self,
) -> impl Future<Output = Result<Response<Vec<DirectMessage>>, Error>> + '_
pub fn start( &mut self, ) -> impl Future<Output = Result<Response<Vec<DirectMessage>>, Error>> + '_
Clear the saved cursor information on this timeline, then return the most recent set of messages.
Sourcepub fn next_page(
&mut self,
) -> impl Future<Output = Result<Response<Vec<DirectMessage>>, Error>> + '_
pub fn next_page( &mut self, ) -> impl Future<Output = Result<Response<Vec<DirectMessage>>, Error>> + '_
Loads the next page of messages, setting the next_cursor to the one received from
Twitter.
Sourcepub fn into_stream(
self,
) -> impl Stream<Item = Result<Response<DirectMessage>, Error>>
pub fn into_stream( self, ) -> impl Stream<Item = Result<Response<DirectMessage>, Error>>
Converts this Timeline into a Stream of direct messages, which automatically loads the
next page as needed.
Sourcepub async fn into_conversations(self) -> Result<DMConversations, Error>
pub async fn into_conversations(self) -> Result<DMConversations, Error>
Loads all the direct messages from this Timeline and sorts them into a DMConversations
map.
This adapter is a convenient way to sort all of a user’s messages (from the last 30 days) into a familiar user-interface pattern of a list of conversations between the authenticated user and a specific other user. This function first pulls all the available messages, then sorts them into a set of threads by matching them against which user the authenticated user is messaging.
If there are more messages available than can be loaded without hitting the rate limit (15
calls to the list endpoint per 15 minutes), then this function will stop once it receives
a rate-limit error and sort the messages it received.