From 1c0964753befd7fc34ec8a48270766951b2a90c7 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Sat, 7 Dec 2024 14:25:19 -0500 Subject: [PATCH] ssb: Correctness around loading messages by time range. --- apps/ssb.json | 2 +- apps/ssb/tf-tab-news-feed.js | 61 +++++++++++++++++++++++++++++------- 2 files changed, 50 insertions(+), 13 deletions(-) diff --git a/apps/ssb.json b/apps/ssb.json index aef14ef2..aafaba3a 100644 --- a/apps/ssb.json +++ b/apps/ssb.json @@ -1,5 +1,5 @@ { "type": "tildefriends-app", "emoji": "🐌", - "previous": "&3Y03Nv5WhnlghnQ1Wqu9z2460JFYumMEUfGDr/0OWfs=.sha256" + "previous": "&LjDy/+vpRFc6dXPOk/+kjlppVMLmKaB6TIZS3WHqoQ8=.sha256" } diff --git a/apps/ssb/tf-tab-news-feed.js b/apps/ssb/tf-tab-news-feed.js index 1f2cbd67..30b98dba 100644 --- a/apps/ssb/tf-tab-news-feed.js +++ b/apps/ssb/tf-tab-news-feed.js @@ -51,7 +51,7 @@ class TfTabNewsFeedElement extends LitElement { JOIN json_each(?2) AS following ON messages.author = following.value WHERE messages.author != ?1 AND - messages.timestamp > ?3 AND + messages.timestamp >= ?3 AND messages.timestamp < ?4 ORDER BY timestamp DESC limit 20 `, @@ -69,16 +69,18 @@ class TfTabNewsFeedElement extends LitElement { WITH mine AS (SELECT rowid, id, previous, author, sequence, timestamp, hash, json(content) AS content, signature FROM messages WHERE messages.author = ? - ORDER BY sequence DESC - LIMIT 20) + ORDER BY sequence DESC) SELECT messages.rowid, messages.id, messages.previous, messages.author, messages.sequence, messages.timestamp, messages.hash, json(messages.content) AS content, messages.signature FROM mine JOIN messages_refs ON mine.id = messages_refs.ref JOIN messages ON messages_refs.message = messages.id UNION SELECT * FROM mine + WHERE + mine.timestamp >= ?2 AND + mine.timestamp < ?3 `, - [this.hash.substring(1)] + [this.hash.substring(1), start_time, end_time] ); return r; } else if (this.hash.startsWith('#%')) { @@ -106,7 +108,7 @@ class TfTabNewsFeedElement extends LitElement { FROM messages JOIN json_each(?) AS following ON messages.author = following.value WHERE - messages.timestamp > ? AND + messages.timestamp >= ? AND messages.timestamp < ? AND messages.content ->> 'channel' = ? ORDER BY messages.timestamp DESC) @@ -126,7 +128,7 @@ class TfTabNewsFeedElement extends LitElement { JOIN json_each(?1) AS following ON messages.author = following.value JOIN json_tree(messages.content, '$.mentions') AS mention ON mention.value = '#' || ?4 WHERE - messages.timestamp > ?2 AND + messages.timestamp >= ?2 AND messages.timestamp < ?3 UNION SELECT news.* FROM news @@ -152,7 +154,7 @@ class TfTabNewsFeedElement extends LitElement { WITH news AS (SELECT messages.rowid, messages.id, messages.previous, messages.author, messages.sequence, messages.timestamp, messages.hash, json(messages.content) AS content, messages.signature FROM messages JOIN json_each(?) AS following ON messages.author = following.value - WHERE messages.timestamp > ? AND messages.timestamp < ? + WHERE messages.timestamp >= ? AND messages.timestamp < ? ORDER BY messages.timestamp DESC) SELECT messages.rowid, messages.id, messages.previous, messages.author, messages.sequence, messages.timestamp, messages.hash, json(messages.content) AS content, messages.signature FROM news @@ -178,6 +180,19 @@ class TfTabNewsFeedElement extends LitElement { } } + update_time_range_from_messages(messages) { + this.time_range = [ + messages.reduce( + (accumulator, current) => Math.min(accumulator, current.timestamp), + this.time_range[0] + ), + messages.reduce( + (accumulator, current) => Math.max(accumulator, current.timestamp), + this.time_range[1] + ), + ]; + } + async load_more() { this.loading++; this.loading_canceled = false; @@ -187,7 +202,12 @@ class TfTabNewsFeedElement extends LitElement { let last_start_time = this.start_time; this.start_time = last_start_time - 7 * 24 * 60 * 60 * 1000; more = await this.fetch_messages(this.start_time, last_start_time); - this.time_range = [this.start_time, this.time_range[1]]; + this.update_time_range_from_messages( + more.filter( + (x) => + x.timestamp >= this.start_time && x.timestamp < last_start_time + ) + ); } this.messages = await this.decrypt([...more, ...this.messages]); } finally { @@ -232,7 +252,11 @@ class TfTabNewsFeedElement extends LitElement { try { messages = await this.fetch_messages(this.time_range[1], end_time); messages = await this.decrypt(messages); - this.time_range = [this.time_range[0], end_time]; + this.update_time_range_from_messages( + messages.filter( + (x) => x.timestamp >= this.time_range[1] && x.timestamp < end_time + ) + ); } finally { this.loading--; } @@ -256,6 +280,13 @@ class TfTabNewsFeedElement extends LitElement { this.time_range[0], this.time_range[1] ); + this.update_time_range_from_messages( + messages.filter( + (x) => + x.timestamp >= this.time_range[0] && + x.timestamp < this.time_range[1] + ) + ); messages = await this.decrypt(messages); if (!messages.length) { let more = []; @@ -263,8 +294,12 @@ class TfTabNewsFeedElement extends LitElement { let last_start_time = start_time; start_time = last_start_time - 7 * 24 * 60 * 60 * 1000; more = await this.fetch_messages(start_time, last_start_time); + this.update_time_range_from_messages( + more.filter( + (x) => x.timestamp >= start_time && x.timestamp < last_start_time + ) + ); } - this.time_range = [start_time, this.time_range[1]]; messages = await this.decrypt([...more, ...this.messages]); } } finally { @@ -305,8 +340,10 @@ class TfTabNewsFeedElement extends LitElement { this.load_messages(); } let more; - if (this.hash == '#@' || - (!this.hash.startsWith('#@') && !this.hash.startsWith('#%'))) { + if ( + this.hash == '#@' || + (!this.hash.startsWith('#@') && !this.hash.startsWith('#%')) + ) { more = html`