ssb: Correctness around loading messages by time range.

This commit is contained in:
Cory McWilliams 2024-12-07 14:25:19 -05:00
parent daa1c7f577
commit 1c0964753b
2 changed files with 50 additions and 13 deletions

View File

@ -1,5 +1,5 @@
{ {
"type": "tildefriends-app", "type": "tildefriends-app",
"emoji": "🐌", "emoji": "🐌",
"previous": "&3Y03Nv5WhnlghnQ1Wqu9z2460JFYumMEUfGDr/0OWfs=.sha256" "previous": "&LjDy/+vpRFc6dXPOk/+kjlppVMLmKaB6TIZS3WHqoQ8=.sha256"
} }

View File

@ -51,7 +51,7 @@ class TfTabNewsFeedElement extends LitElement {
JOIN json_each(?2) AS following ON messages.author = following.value JOIN json_each(?2) AS following ON messages.author = following.value
WHERE WHERE
messages.author != ?1 AND messages.author != ?1 AND
messages.timestamp > ?3 AND messages.timestamp >= ?3 AND
messages.timestamp < ?4 messages.timestamp < ?4
ORDER BY timestamp DESC limit 20 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 WITH mine AS (SELECT rowid, id, previous, author, sequence, timestamp, hash, json(content) AS content, signature
FROM messages FROM messages
WHERE messages.author = ? WHERE messages.author = ?
ORDER BY sequence DESC ORDER BY sequence DESC)
LIMIT 20)
SELECT messages.rowid, messages.id, messages.previous, messages.author, messages.sequence, messages.timestamp, messages.hash, json(messages.content) AS content, messages.signature SELECT messages.rowid, messages.id, messages.previous, messages.author, messages.sequence, messages.timestamp, messages.hash, json(messages.content) AS content, messages.signature
FROM mine FROM mine
JOIN messages_refs ON mine.id = messages_refs.ref JOIN messages_refs ON mine.id = messages_refs.ref
JOIN messages ON messages_refs.message = messages.id JOIN messages ON messages_refs.message = messages.id
UNION UNION
SELECT * FROM mine 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; return r;
} else if (this.hash.startsWith('#%')) { } else if (this.hash.startsWith('#%')) {
@ -106,7 +108,7 @@ class TfTabNewsFeedElement extends LitElement {
FROM messages FROM messages
JOIN json_each(?) AS following ON messages.author = following.value JOIN json_each(?) AS following ON messages.author = following.value
WHERE WHERE
messages.timestamp > ? AND messages.timestamp >= ? AND
messages.timestamp < ? AND messages.timestamp < ? AND
messages.content ->> 'channel' = ? messages.content ->> 'channel' = ?
ORDER BY messages.timestamp DESC) 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_each(?1) AS following ON messages.author = following.value
JOIN json_tree(messages.content, '$.mentions') AS mention ON mention.value = '#' || ?4 JOIN json_tree(messages.content, '$.mentions') AS mention ON mention.value = '#' || ?4
WHERE WHERE
messages.timestamp > ?2 AND messages.timestamp >= ?2 AND
messages.timestamp < ?3 messages.timestamp < ?3
UNION UNION
SELECT news.* FROM news 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 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 FROM messages
JOIN json_each(?) AS following ON messages.author = following.value 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) 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 SELECT messages.rowid, messages.id, messages.previous, messages.author, messages.sequence, messages.timestamp, messages.hash, json(messages.content) AS content, messages.signature
FROM news 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() { async load_more() {
this.loading++; this.loading++;
this.loading_canceled = false; this.loading_canceled = false;
@ -187,7 +202,12 @@ class TfTabNewsFeedElement extends LitElement {
let last_start_time = this.start_time; let last_start_time = this.start_time;
this.start_time = last_start_time - 7 * 24 * 60 * 60 * 1000; this.start_time = last_start_time - 7 * 24 * 60 * 60 * 1000;
more = await this.fetch_messages(this.start_time, last_start_time); 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]); this.messages = await this.decrypt([...more, ...this.messages]);
} finally { } finally {
@ -232,7 +252,11 @@ class TfTabNewsFeedElement extends LitElement {
try { try {
messages = await this.fetch_messages(this.time_range[1], end_time); messages = await this.fetch_messages(this.time_range[1], end_time);
messages = await this.decrypt(messages); 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 { } finally {
this.loading--; this.loading--;
} }
@ -256,6 +280,13 @@ class TfTabNewsFeedElement extends LitElement {
this.time_range[0], this.time_range[0],
this.time_range[1] 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); messages = await this.decrypt(messages);
if (!messages.length) { if (!messages.length) {
let more = []; let more = [];
@ -263,8 +294,12 @@ class TfTabNewsFeedElement extends LitElement {
let last_start_time = start_time; let last_start_time = start_time;
start_time = last_start_time - 7 * 24 * 60 * 60 * 1000; start_time = last_start_time - 7 * 24 * 60 * 60 * 1000;
more = await this.fetch_messages(start_time, last_start_time); 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]); messages = await this.decrypt([...more, ...this.messages]);
} }
} finally { } finally {
@ -305,8 +340,10 @@ class TfTabNewsFeedElement extends LitElement {
this.load_messages(); this.load_messages();
} }
let more; let more;
if (this.hash == '#@' || if (
(!this.hash.startsWith('#@') && !this.hash.startsWith('#%'))) { this.hash == '#@' ||
(!this.hash.startsWith('#@') && !this.hash.startsWith('#%'))
) {
more = html` more = html`
<p> <p>
<button class="w3-button w3-theme-d1" @click=${this.mark_all_read}> <button class="w3-button w3-theme-d1" @click=${this.mark_all_read}>