pub async fn do_poll(
        &self,
        full_update: bool
    ) -> Result<Poll, Error> {
        fn date_difference_from_now(date: &ServerTime) -> i64 {
            let current_timestamp = time::get_server_time_now().timestamp();
            
            current_timestamp - date.timestamp()
        }
        
        fn last_poll_full_outdated(last_poll_full_update: Option<ServerTime>) -> bool {
            if let Some(last_poll_full_update) = last_poll_full_update {
                date_difference_from_now(&last_poll_full_update) >= 5 * 60
            } else {
                true
            }
        }
        
        let mut offers_since = 0;
        let mut filter = OfferFilter::ActiveOnly;
        
        {
            let mut poll_data = self.poll_data.write().unwrap();

            if let Some(last_poll) = poll_data.last_poll {
                let seconds_since_last_poll = date_difference_from_now(&last_poll);
                    
                if seconds_since_last_poll <= 1 {
                    // We last polled less than a second ago... we shouldn't spam the API
                    return Err(Error::PollCalledTooSoon);
                }            
            }
            
            poll_data.last_poll = Some(time::get_server_time_now());
        
            if full_update || last_poll_full_outdated(poll_data.last_poll_full_update) {
                filter = OfferFilter::All;
                offers_since = 1;
                poll_data.last_poll_full_update = Some(time::get_server_time_now())
            } else if let Some(poll_offers_since) = poll_data.offers_since {
                // It looks like sometimes Steam can be dumb and backdate a modified offer. We need to handle this.
                // Let's add a 30-minute buffer.
                offers_since = poll_offers_since.timestamp() - 1800;
            }
        }

        let historical_cutoff = time::timestamp_to_server_time(offers_since);
        let mut offers = self.api.get_trade_offers(&filter, &Some(historical_cutoff)).await?;
        let mut offers_since: i64 = 0;
        let mut poll: Poll = Vec::new();
        
        if let Some(cancel_duration) = self.cancel_duration {
            let cancel_time = chrono::Utc::now() - cancel_duration;
            let offers_to_cancel = offers
                .iter_mut()
                .filter(|offer| {
                    offer.trade_offer_state == TradeOfferState::Active &&
                    offer.is_our_offer &&
                    offer.time_created < cancel_time
                });
            let cancel_futures = offers_to_cancel
                .map(|offer| async { self.cancel_offer(offer).await })
                .collect::<Vec<_>>();
            
            // cancels all offers older than cancel_time
            // this will also update the state for the offers that were cancelled
            futures::future::join_all(cancel_futures).await;
        }
        
        {
            let mut poll_data = self.poll_data.write().unwrap();
                
            for offer in offers {
                offers_since = cmp::max(offers_since, offer.time_updated.timestamp());

                match poll_data.state_map.get(&offer.tradeofferid) {
                    Some(poll_trade_offer_state) => {
                        if poll_trade_offer_state != &offer.trade_offer_state {
                            let tradeofferid = offer.tradeofferid;
                            let new_state = offer.trade_offer_state.clone();
                            
                            poll.push((offer, Some(poll_trade_offer_state.clone())));
                            
                            poll_data.state_map.insert(tradeofferid, new_state);
                        }
                    },
                    None => {
                        poll_data.state_map.insert(offer.tradeofferid, offer.trade_offer_state.clone());
                        
                        poll.push((offer, None));
                    },
                }
            }
            
            if offers_since > 0 {
                poll_data.offers_since = Some(time::timestamp_to_server_time(offers_since));
            }
        }
        
        let _ = self.save_poll_data().await;
        
        Ok(poll)
    }
/// Performs a poll for changes to offers.
Performs a poll for changes to offers.
Performs a poll for changes to offers .
pub async fn do_poll(
        &self,
        full_update: bool
    ) -> Result<Poll, Error> {
        fn date_difference_from_now(date: &ServerTime) -> i64 {
            let current_timestamp = time::get_server_time_now().timestamp();
            current_timestamp - date.timestamp()
        }
        fn last_poll_full_outdated(last_poll_full_update: Option<ServerTime>) -> bool {
            if let Some(last_poll_full_update) = last_poll_full_update {
                date_difference_from_now(&last_poll_full_update) >= 5 * 60
            } else {
                true
            }
        }
        let mut offers_since = 0;
        let mut filter = OfferFilter::ActiveOnly;
        {
            let mut poll_data = self.poll_data.write().unwrap();
            if let Some(last_poll) = poll_data.last_poll {
                let seconds_since_last_poll = date_difference_from_now(&last_poll);
                if seconds_since_last_poll <= 1 {
                    // We last polled less than a second ago... we shouldn't spam the API
                    return Err(Error::PollCalledTooSoon);
                }            
            }
            poll_data.last_poll = Some(time::get_server_time_now());
            if full_update || last_poll_full_outdated(poll_data.last_poll_full_update) {
                filter = OfferFilter::All;
                offers_since = 1;
                poll_data.last_poll_full_update = Some(time::get_server_time_now())
            } else if let Some(poll_offers_since) = poll_data.offers_since {
                // It looks like sometimes Steam can be dumb and backdate a modified offer. We need to handle this.
                // Let's add a 30-minute buffer.
                offers_since = poll_offers_since.timestamp() - 1800;
            }
        }
        let historical_cutoff = time::timestamp_to_server_time(offers_since);
        let mut offers = self.api.get_trade_offers(&filter, &Some(historical_cutoff)).await?;
        let mut offers_since: i64 = 0;
        let mut poll: Poll = Vec::new();
        if let Some(cancel_duration) = self.cancel_duration {
            let cancel_time = chrono::Utc::now() - cancel_duration;
            let offers_to_cancel = offers
                .iter_mut()
                .filter(|offer| {
                    offer.trade_offer_state == TradeOfferState::Active &&
                    offer.is_our_offer &&
                    offer.time_created < cancel_time
                });
            let cancel_futures = offers_to_cancel
                .map(|offer| async { self.cancel_offer(offer).await })
                .collect::<Vec<_>>();
            // cancels all offers older than cancel_time
            // this will also update the state for the offers that were cancelled
            futures::future::join_all(cancel_futures).await;
        }
        {
            let mut poll_data = self.poll_data.write().unwrap();
            for offer in offers {
                offers_since = cmp::max(offers_since, offer.time_updated.timestamp());
                match poll_data.state_map.get(&offer.tradeofferid) {
                    Some(poll_trade_offer_state) => {
                        if poll_trade_offer_state != &offer.trade_offer_state {
                            let tradeofferid = offer.tradeofferid;
                            let new_state = offer.trade_offer_state.clone();
                            poll.push((offer, Some(poll_trade_offer_state.clone())));
                            poll_data.state_map.insert(tradeofferid, new_state);
                        }
                    },
                    None => {
                        poll_data.state_map.insert(offer.tradeofferid, offer.trade_offer_state.clone());
                        poll.push((offer, None));
                    },
                }
            }
            if offers_since > 0 {
                poll_data.offers_since = Some(time::timestamp_to_server_time(offers_since));
            }
        }
        let _ = self.save_poll_data().await;
        Ok(poll)
    }
pub async fn do_poll ( & self , full_update : bool ) -> Result < Poll , Error > { fn date_difference_from_now ( date : & ServerTime ) -> i64 { let current_timestamp = time :: get_server_time_now ( ) . timestamp ( ) ; current_timestamp - date . timestamp ( ) } fn last_poll_full_outdated ( last_poll_full_update : Option < ServerTime > ) -> bool { if let Some ( last_poll_full_update ) = last_poll_full_update { date_difference_from_now ( & last_poll_full_update ) >= 5 * 60 } else { true } } let mut offers_since = 0 ; let mut filter = OfferFilter :: ActiveOnly ; { let mut poll_data = self . poll_data . write ( ) . unwrap ( ) ; if let Some ( last_poll ) = poll_data . last_poll { let seconds_since_last_poll = date_difference_from_now ( & last_poll ) ; if seconds_since_last_poll <= 1 { // We last polled less than a second ago... we shouldn't spam the API return Err ( Error :: PollCalledTooSoon ) ; } } poll_data . last_poll = Some ( time :: get_server_time_now ( ) ) ; if full_update || last_poll_full_outdated ( poll_data . last_poll_full_update ) { filter = OfferFilter :: All ; offers_since = 1 ; poll_data . last_poll_full_update = Some ( time :: get_server_time_now ( ) ) } else if let Some ( poll_offers_since ) = poll_data . offers_since { // It looks like sometimes Steam can be dumb and backdate a modified offer. We need to handle this. // Let's add a 30-minute buffer. offers_since = poll_offers_since . timestamp ( ) - 1800 ; } } let historical_cutoff = time :: timestamp_to_server_time ( offers_since ) ; let mut offers = self . api . get_trade_offers ( & filter , & Some ( historical_cutoff ) ) . await ? ; let mut offers_since : i64 = 0 ; let mut poll : Poll = Vec :: new ( ) ; if let Some ( cancel_duration ) = self . cancel_duration { let cancel_time = chrono :: Utc :: now ( ) - cancel_duration ; let offers_to_cancel = offers . iter_mut ( ) . filter ( | offer | { offer . trade_offer_state == TradeOfferState :: Active && offer . is_our_offer && offer . time_created < cancel_time } ) ; let cancel_futures = offers_to_cancel . map ( | offer | async { self . cancel_offer ( offer ) . await } ) . collect :: < Vec < _ > > ( ) ; // cancels all offers older than cancel_time // this will also update the state for the offers that were cancelled futures :: future :: join_all ( cancel_futures ) . await ; } { let mut poll_data = self . poll_data . write ( ) . unwrap ( ) ; for offer in offers { offers_since = cmp :: max ( offers_since , offer . time_updated . timestamp ( ) ) ; match poll_data . state_map . get ( & offer . tradeofferid ) { Some ( poll_trade_offer_state ) => { if poll_trade_offer_state != & offer . trade_offer_state { let tradeofferid = offer . tradeofferid ; let new_state = offer . trade_offer_state . clone ( ) ; poll . push ( ( offer , Some ( poll_trade_offer_state . clone ( ) ) ) ) ; poll_data . state_map . insert ( tradeofferid , new_state ) ; } } , None => { poll_data . state_map . insert ( offer . tradeofferid , offer . trade_offer_state . clone ( ) ) ; poll . push ( ( offer , None ) ) ; } , } } if offers_since > 0 { poll_data . offers_since = Some ( time :: timestamp_to_server_time ( offers_since ) ) ; } } let _ = self . save_poll_data ( ) . await ; Ok ( poll ) }
Performs a poll for changes to offers.
Performs a poll for changes to offers .
&self
full_update
bool
&self
full_update
bool