Skip to content

Commit 566e611

Browse files
committed
chore: improve & fix post filtering
1 parent a8473f6 commit 566e611

File tree

2 files changed

+38
-23
lines changed

2 files changed

+38
-23
lines changed

crates/rostra-client-db/src/social.rs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ pub struct EventPaginationCursor {
2121
pub event_id: ShortEventId,
2222
}
2323

24+
impl EventPaginationCursor {
25+
pub const ZERO: Self = Self {
26+
ts: Timestamp::ZERO,
27+
event_id: ShortEventId::ZERO,
28+
};
29+
pub const MAX: Self = Self {
30+
ts: Timestamp::MAX,
31+
event_id: ShortEventId::MAX,
32+
};
33+
}
2434
#[derive(Clone)]
2535
pub struct SocialPostRecord<C> {
2636
pub ts: Timestamp,
@@ -105,7 +115,10 @@ impl Database {
105115
upper_bound: Option<EventPaginationCursor>,
106116
limit: usize,
107117
filter_fn: impl Fn(&SocialPostRecord<SocialPost>) -> bool + Send + 'static,
108-
) -> Vec<SocialPostRecord<content_kind::SocialPost>> {
118+
) -> (
119+
Vec<SocialPostRecord<content_kind::SocialPost>>,
120+
EventPaginationCursor,
121+
) {
109122
let upper_bound = upper_bound
110123
.map(|b| (b.ts, b.event_id))
111124
.unwrap_or((Timestamp::MAX, ShortEventId::MAX));
@@ -116,6 +129,7 @@ impl Database {
116129
let events_content_table = tx.open_table(&events_content::TABLE)?;
117130

118131
let mut ret = vec![];
132+
let mut last = EventPaginationCursor { ts: Timestamp::ZERO, event_id: ShortEventId::ZERO };
119133

120134
for event in social_posts_by_time_table
121135
.range(&(Timestamp::ZERO, ShortEventId::ZERO)..&upper_bound)?
@@ -127,6 +141,7 @@ impl Database {
127141
let (k, _) = event?;
128142
let (ts, event_id) = k.value();
129143

144+
last = EventPaginationCursor { ts, event_id};
130145

131146
let Some(content_state) =
132147
Database::get_event_content_tx(event_id, &events_content_table)?
@@ -168,7 +183,7 @@ impl Database {
168183
ret.push(social_post_record);
169184
}
170185

171-
Ok(ret)
186+
Ok((ret, last))
172187
})
173188
.await
174189
.expect("Storage error")
@@ -179,7 +194,10 @@ impl Database {
179194
lower_bound: Option<EventPaginationCursor>,
180195
limit: usize,
181196
filter_fn: impl Fn(&SocialPostRecord<SocialPost>) -> bool + Send + 'static,
182-
) -> Vec<SocialPostRecord<content_kind::SocialPost>> {
197+
) -> (
198+
Vec<SocialPostRecord<content_kind::SocialPost>>,
199+
EventPaginationCursor,
200+
) {
183201
let lower_bound = lower_bound
184202
.map(|b| (b.ts, b.event_id))
185203
.unwrap_or((Timestamp::ZERO, ShortEventId::ZERO));
@@ -190,6 +208,7 @@ impl Database {
190208
let events_content_table = tx.open_table(&events_content::TABLE)?;
191209

192210
let mut ret = vec![];
211+
let mut last = EventPaginationCursor { ts: Timestamp::MAX, event_id: ShortEventId::MAX };
193212

194213
for event in social_posts_by_time_table
195214
.range(&lower_bound..&(Timestamp::MAX, ShortEventId::MAX))?
@@ -201,6 +220,12 @@ impl Database {
201220
let (k, _) = event?;
202221
let (ts, event_id) = k.value();
203222

223+
// Since we don't have exclusive lower bound range, we need to skip it manually
224+
if (ts, event_id) == lower_bound {
225+
continue;
226+
}
227+
228+
last = EventPaginationCursor { ts, event_id};
204229

205230
let Some(content_state) =
206231
Database::get_event_content_tx(event_id, &events_content_table)?
@@ -243,7 +268,7 @@ impl Database {
243268

244269
}
245270

246-
Ok(ret)
271+
Ok((ret, last))
247272
})
248273
.await
249274
.expect("Storage error")

crates/rostra-web-ui/src/routes/timeline.rs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,9 @@ impl UiState {
274274
TimelineMode::Notifications => {
275275
if let Some(latest_event) = client
276276
.db()
277-
.paginate_social_posts_with_filter(None, 1, |_| true)
277+
.paginate_social_posts_rev_with_filter(None, 1, |_| true)
278278
.await
279+
.0
279280
.into_iter()
280281
.next()
281282
{
@@ -289,12 +290,13 @@ impl UiState {
289290
TimelineMode::Followees | TimelineMode::Network => {
290291
let pending_len = client
291292
.db()
292-
.paginate_social_posts_rev_with_filter(
293+
.paginate_social_posts_with_filter(
293294
cookies.get_last_seen(),
294295
10,
295296
TimelineMode::Notifications.to_filter_fn(client).await,
296297
)
297298
.await
299+
.0
298300
.len();
299301

300302
Ok(Some(pending_len))
@@ -374,11 +376,10 @@ impl UiState {
374376

375377
let filter_fn = mode.to_filter_fn(&client_ref).await;
376378

377-
let post_page = client_ref
379+
let (filtered_posts, last_seen) = client_ref
378380
.db()
379-
.paginate_social_posts_rev(pagination, mode.db_page_size())
381+
.paginate_social_posts_rev_with_filter(pagination, 30, filter_fn)
380382
.await;
381-
let filtered_posts: Vec<_> = post_page.iter().filter(|post| (filter_fn)(post)).collect();
382383

383384
let parents = self
384385
.client(session.id())
@@ -458,13 +459,13 @@ impl UiState {
458459
.call().await?)
459460
}
460461
}
461-
@if let Some(last) = post_page.last() {
462+
@if last_seen != EventPaginationCursor::ZERO {
462463
div ."o-mainBarTimeline__rest -empty"
463464
hx-get=(
464465
format!("{}?ts={}&event_id={}",
465466
mode.to_path(),
466-
last.ts,
467-
last.event_id)
467+
last_seen.ts,
468+
last_seen.event_id)
468469
)
469470
hx-select=".o-mainBarTimeline__item, .o-mainBarTimeline__rest, script.mathjax"
470471
hx-trigger="intersect once, threshold:0.5"
@@ -671,17 +672,6 @@ pub(crate) enum TimelineMode {
671672
}
672673

673674
impl TimelineMode {
674-
/// How many records in a page to query, to have something reasonable to
675-
/// show
676-
fn db_page_size(self) -> usize {
677-
match self {
678-
TimelineMode::Followees => 100,
679-
TimelineMode::Network => 50,
680-
TimelineMode::Notifications => 200,
681-
TimelineMode::Profile(_) => 200,
682-
}
683-
}
684-
685675
fn to_path(self) -> String {
686676
match self {
687677
TimelineMode::Followees => "/ui/followees".to_string(),

0 commit comments

Comments
 (0)