matrix_sdk_ui/timeline/
pagination.rs

1// Copyright 2023 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use async_rx::StreamExt as _;
16use async_stream::stream;
17use futures_core::Stream;
18use futures_util::{pin_mut, StreamExt as _};
19use matrix_sdk::event_cache::{
20    self,
21    paginator::{PaginatorError, PaginatorState},
22    EventCacheError, RoomPagination,
23};
24use tracing::{instrument, warn};
25
26use super::Error;
27
28impl super::Timeline {
29    /// Add more events to the start of the timeline.
30    ///
31    /// Returns whether we hit the start of the timeline.
32    #[instrument(skip_all, fields(room_id = ?self.room().room_id()))]
33    pub async fn paginate_backwards(&self, mut num_events: u16) -> Result<bool, Error> {
34        if self.controller.is_live().await {
35            match self.controller.live_lazy_paginate_backwards(num_events).await {
36                Some(needed_num_events) => {
37                    num_events = needed_num_events.try_into().expect(
38                        "failed to cast `needed_num_events` (`usize`) into `num_events` (`usize`)",
39                    );
40                }
41                None => {
42                    // We could adjust the skip count to a lower value, while passing the requested
43                    // number of events. We *may* have reached the start of the timeline, but since
44                    // we're fulfilling the caller's request, assume it's not the case and return
45                    // false here. A subsequent call will go to the `Some()` arm of this match, and
46                    // cause a call to the event cache's pagination.
47                    return Ok(false);
48                }
49            }
50
51            Ok(self.live_paginate_backwards(num_events).await?)
52        } else {
53            Ok(self.controller.focused_paginate_backwards(num_events).await?)
54        }
55    }
56
57    /// Add more events to the end of the timeline.
58    ///
59    /// Returns whether we hit the end of the timeline.
60    #[instrument(skip_all, fields(room_id = ?self.room().room_id()))]
61    pub async fn paginate_forwards(&self, num_events: u16) -> Result<bool, Error> {
62        if self.controller.is_live().await {
63            Ok(true)
64        } else {
65            Ok(self.controller.focused_paginate_forwards(num_events).await?)
66        }
67    }
68
69    /// Paginate backwards in live mode.
70    ///
71    /// This can only be called when the timeline is in live mode, not focused
72    /// on a specific event.
73    ///
74    /// Returns whether we hit the start of the timeline.
75    async fn live_paginate_backwards(&self, batch_size: u16) -> event_cache::Result<bool> {
76        loop {
77            match self.event_cache.pagination().run_backwards_once(batch_size).await {
78                Ok(outcome) => {
79                    // As an exceptional contract, restart the back-pagination if we received an
80                    // empty chunk.
81                    if outcome.reached_start || !outcome.events.is_empty() {
82                        return Ok(outcome.reached_start);
83                    }
84                }
85
86                Err(EventCacheError::BackpaginationError(
87                    PaginatorError::InvalidPreviousState {
88                        actual: PaginatorState::Paginating, ..
89                    },
90                )) => {
91                    // Treat an already running pagination exceptionally, returning false so that
92                    // the caller retries later.
93                    warn!("Another pagination request is already happening, returning early");
94                    return Ok(false);
95                }
96
97                // Propagate other errors as such.
98                Err(err) => return Err(err),
99            }
100        }
101    }
102
103    /// Subscribe to the back-pagination status of a live timeline.
104    ///
105    /// This will return `None` if the timeline is in the focused mode.
106    ///
107    /// Note: this may send multiple Paginating/Idle sequences during a single
108    /// call to [`Self::paginate_backwards()`].
109    pub async fn live_back_pagination_status(
110        &self,
111    ) -> Option<(LiveBackPaginationStatus, impl Stream<Item = LiveBackPaginationStatus>)> {
112        if !self.controller.is_live().await {
113            return None;
114        }
115
116        let pagination = self.event_cache.pagination();
117
118        let mut status = pagination.status();
119
120        let current_value =
121            LiveBackPaginationStatus::from_paginator_status(&pagination, status.next_now());
122
123        let stream = Box::pin(stream! {
124            let status_stream = status.dedup();
125
126            pin_mut!(status_stream);
127
128            while let Some(state) = status_stream.next().await {
129                yield LiveBackPaginationStatus::from_paginator_status(&pagination, state);
130            }
131        });
132
133        Some((current_value, stream))
134    }
135}
136
137/// Status for the back-pagination on a live timeline.
138#[derive(Debug, PartialEq)]
139#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
140pub enum LiveBackPaginationStatus {
141    /// No back-pagination is happening right now.
142    Idle {
143        /// Have we hit the start of the timeline, i.e. back-paginating wouldn't
144        /// have any effect?
145        hit_start_of_timeline: bool,
146    },
147
148    /// Back-pagination is already running in the background.
149    Paginating,
150}
151
152impl LiveBackPaginationStatus {
153    /// Converts from a [`PaginatorState`] into the live back-pagination status.
154    ///
155    /// Private method instead of `From`/`Into` impl, to avoid making it public
156    /// API.
157    fn from_paginator_status(pagination: &RoomPagination, state: PaginatorState) -> Self {
158        match state {
159            PaginatorState::Initial => Self::Idle { hit_start_of_timeline: false },
160            PaginatorState::FetchingTargetEvent => {
161                panic!("unexpected paginator state for a live backpagination")
162            }
163            PaginatorState::Idle => {
164                Self::Idle { hit_start_of_timeline: pagination.hit_timeline_start() }
165            }
166            PaginatorState::Paginating => Self::Paginating,
167        }
168    }
169}