1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright 2023 The Matrix.org Foundation C.I.C.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use async_rx::StreamExt as _;
use async_stream::stream;
use futures_core::Stream;
use futures_util::{StreamExt as _, pin_mut};
use matrix_sdk::event_cache::PaginationStatus;
use tracing::instrument;
use super::Error;
use crate::timeline::{
PaginationError::{self, NotSupported},
controller::TimelineFocusKind,
};
impl super::Timeline {
/// Add more events to the start of the timeline.
///
/// Returns whether we hit the start of the timeline.
#[instrument(skip_all, fields(room_id = ?self.room().room_id()))]
pub async fn paginate_backwards(&self, mut num_events: u16) -> Result<bool, Error> {
match self.controller.focus() {
TimelineFocusKind::Live { .. } => {
match self.controller.live_lazy_paginate_backwards(num_events).await {
Some(needed_num_events) => {
num_events = needed_num_events.try_into().expect(
"failed to cast `needed_num_events` (`usize`) into `num_events` (`usize`)",
);
}
None => {
// We could adjust the skip count to a lower value, while passing the
// requested number of events. We *may* have reached the start of the
// timeline, but since we're fulfilling the caller's request, assume it's
// not the case and return false here. A subsequent call will go to the
// `Some()` arm of this match, and cause a call to the event cache's
// pagination.
return Ok(false);
}
}
Ok(self.live_paginate_backwards(num_events).await?)
}
TimelineFocusKind::Event { focused_event_id, thread_mode, .. } => Ok(self
.event_cache
.get_event_focused_cache(focused_event_id.clone(), (*thread_mode).into())
.await?
.ok_or(PaginationError::MissingCache)?
.paginate_backwards(num_events)
.await?
.hit_end_of_timeline),
TimelineFocusKind::Thread { root_event_id } => Ok(self
.event_cache
.thread_pagination(root_event_id.to_owned())
.await
.map_err(PaginationError::EventCache)?
.run_backwards_once(num_events)
.await
.map(|outcome| outcome.reached_start)?),
TimelineFocusKind::PinnedEvents => Err(Error::PaginationError(NotSupported)),
}
}
/// Add more events to the end of the timeline.
///
/// Returns whether we hit the end of the timeline.
#[instrument(skip_all, fields(room_id = ?self.room().room_id()))]
pub async fn paginate_forwards(&self, num_events: u16) -> Result<bool, Error> {
match self.controller.focus() {
TimelineFocusKind::Live { .. } => Ok(true),
TimelineFocusKind::Event { focused_event_id, thread_mode, .. } => Ok(self
.event_cache
.get_event_focused_cache(focused_event_id.clone(), (*thread_mode).into())
.await?
.ok_or(PaginationError::MissingCache)?
.paginate_forwards(num_events)
.await?
.hit_end_of_timeline),
TimelineFocusKind::Thread { .. } | TimelineFocusKind::PinnedEvents => {
Err(Error::PaginationError(NotSupported))
}
}
}
/// Paginate backwards in live mode.
///
/// This can only be called when the timeline is in live mode, not focused
/// on a specific event.
///
/// Returns whether we hit the start of the timeline.
async fn live_paginate_backwards(&self, batch_size: u16) -> Result<bool, Error> {
loop {
match self.event_cache.pagination().run_backwards_once(batch_size).await {
Ok(outcome) => {
if outcome.reached_start {
self.controller.insert_timeline_start_if_missing().await;
return Ok(true);
}
if !outcome.events.is_empty() {
return Ok(false);
}
// Fallthrough: as a special contract, restart pagination,
// if it returned 0 events.
}
// Propagate errors as such.
Err(err) => return Err(err.into()),
}
}
}
/// Subscribe to the back-pagination status of a live timeline.
///
/// This will return `None` if the timeline is in the focused mode.
///
/// Note: this may send multiple Paginating/Idle sequences during a single
/// call to [`Self::paginate_backwards()`].
pub async fn live_back_pagination_status(
&self,
) -> Option<(PaginationStatus, impl Stream<Item = PaginationStatus> + use<>)> {
if !self.controller.is_live() {
return None;
}
let pagination = self.event_cache.pagination();
let mut status = pagination.status();
let current_value = self.controller.map_pagination_status(status.next_now()).await;
let controller = self.controller.clone();
let stream = Box::pin(stream! {
let status_stream = status.dedup();
pin_mut!(status_stream);
while let Some(state) = status_stream.next().await {
let state = controller.map_pagination_status(state).await;
match state {
PaginationStatus::Idle { hit_timeline_start } => {
if hit_timeline_start {
controller.insert_timeline_start_if_missing().await;
}
}
PaginationStatus::Paginating => {}
}
yield state;
}
});
Some((current_value, stream))
}
}