ssb: Experimenting with the news query strategy.
Some checks failed
Build Tilde Friends / Build-All (push) Has been cancelled

This commit is contained in:
Cory McWilliams 2025-01-21 20:53:23 -05:00
parent e2406df367
commit 18cf058af3
2 changed files with 46 additions and 60 deletions

View File

@ -1,5 +1,5 @@
{ {
"type": "tildefriends-app", "type": "tildefriends-app",
"emoji": "🦀", "emoji": "🦀",
"previous": "&ZKixAk7qZ3OfnjvCoyT1MC7qYVpUz0ngaAozr11xihs=.sha256" "previous": "&QQhMV7DY8GfmrAfjLFhOA2VScoGXlSA0MW520Pigrjg=.sha256"
} }

View File

@ -57,8 +57,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 ?3 IS NULL OR (messages.timestamp >= ?3 AND messages.timestamp < ?4)
messages.timestamp < ?4
ORDER BY timestamp DESC limit 20) ORDER BY timestamp 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 mentions FROM mentions
@ -77,22 +76,21 @@ class TfTabNewsFeedElement extends LitElement {
} else if (this.hash.startsWith('#@')) { } else if (this.hash.startsWith('#@')) {
result = await tfrpc.rpc.query( result = await tfrpc.rpc.query(
` `
WITH mine AS (SELECT rowid, id, previous, author, sequence, timestamp, hash, json(content) AS content, signature WITH
FROM messages mine AS (SELECT rowid, id, previous, author, sequence, timestamp, hash, json(content) AS content, signature
WHERE messages.author = ? FROM messages
ORDER BY sequence DESC) WHERE messages.author = ?),
selected AS (
SELECT * FROM mine
WHERE ?2 IS NULL OR (mine.timestamp >= 2 AND mine.timestamp < ?3)
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 selected
JOIN messages_refs ON mine.id = messages_refs.ref JOIN messages_refs ON selected.id = messages_refs.ref
JOIN messages ON messages_refs.message = messages.id JOIN messages ON messages_refs.message = messages.id
WHERE
messages.timestamp >= ?2 AND
messages.timestamp < ?3
UNION UNION
SELECT * FROM mine SELECT * FROM selected
WHERE
mine.timestamp >= ?2 AND
mine.timestamp < ?3
`, `,
[this.hash.substring(1), start_time, end_time] [this.hash.substring(1), start_time, end_time]
); );
@ -117,14 +115,21 @@ class TfTabNewsFeedElement extends LitElement {
promises.push( promises.push(
tfrpc.rpc.query( tfrpc.rpc.query(
` `
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
FROM messages all_news AS (
JOIN json_each(?) AS following ON messages.author = following.value SELECT messages.rowid, messages.id, messages.previous, messages.author, messages.sequence, messages.timestamp, messages.hash, json(messages.content) AS content, messages.signature
WHERE FROM messages
messages.timestamp >= ? AND JOIN json_each(?) AS following ON messages.author = following.value
messages.timestamp < ? AND WHERE messages.content ->> 'channel' = ?
messages.content ->> 'channel' = ? UNION
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 messages_fts(?5)
JOIN messages ON messages.rowid = messages_fts.rowid
JOIN json_each(?1) AS following ON messages.author = following.value
JOIN json_tree(messages.content, '$.mentions') AS mention ON mention.value = '#' || ?4),
news AS (SELECT * FROM all_news
WHERE ?2 IS NULL OR (all_news.timestamp >= ?2 AND all_news.timestamp < ?3)
ORDER BY all_news.timestamp 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 news FROM news
JOIN messages_refs ON news.id = messages_refs.ref JOIN messages_refs ON news.id = messages_refs.ref
@ -135,15 +140,6 @@ class TfTabNewsFeedElement extends LitElement {
JOIN messages_refs ON news.id = messages_refs.message JOIN messages_refs ON news.id = messages_refs.message
JOIN messages ON messages_refs.ref = messages.id JOIN messages ON messages_refs.ref = messages.id
UNION UNION
SELECT messages.rowid, messages.id, messages.previous, messages.author, messages.sequence, messages.timestamp, messages.hash, json(messages.content) AS content, messages.signature
FROM messages_fts(?5)
JOIN messages ON messages.rowid = messages_fts.rowid
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 < ?3
UNION
SELECT news.* FROM news SELECT news.* FROM news
`, `,
[ [
@ -164,10 +160,9 @@ class TfTabNewsFeedElement extends LitElement {
FROM messages FROM messages
JOIN json_each(?1) AS following ON messages.author = following.value JOIN json_each(?1) AS following ON messages.author = following.value
WHERE WHERE
messages.timestamp >= ?2 AND ?2 IS NULL OR (messages.timestamp >= ?2 AND messages.timestamp < ?3) AND
messages.timestamp < ?3 AND
json(messages.content) LIKE '"%' json(messages.content) LIKE '"%'
ORDER BY sequence DESC ORDER BY sequence DESC LIMIT 20
`, `,
[JSON.stringify(this.following), start_time, end_time] [JSON.stringify(this.following), start_time, end_time]
); );
@ -179,11 +174,17 @@ class TfTabNewsFeedElement extends LitElement {
promises.push( promises.push(
tfrpc.rpc.query( tfrpc.rpc.query(
` `
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
FROM messages all_news AS (
JOIN json_each(?) AS following ON messages.author = following.value SELECT messages.rowid, messages.id, messages.previous, messages.author, messages.sequence, messages.timestamp, messages.hash, json(messages.content) AS content, messages.signature
WHERE messages.timestamp >= ? AND messages.timestamp < ? FROM messages
ORDER BY messages.timestamp DESC) JOIN json_each(?) AS following ON messages.author = following.value
WHERE timestamp >= 0 AND timestamp < ?3),
news AS (
SELECT * FROM all_news
WHERE ?2 IS NULL or (all_news.timestamp >= ?2 AND all_news.timestamp < ?3)
ORDER BY timestamp 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 news FROM news
JOIN messages_refs ON news.id = messages_refs.ref JOIN messages_refs ON news.id = messages_refs.ref
@ -313,6 +314,7 @@ class TfTabNewsFeedElement extends LitElement {
} }
async load_messages() { async load_messages() {
let start_time = new Date();
let self = this; let self = this;
this.loading++; this.loading++;
let messages = []; let messages = [];
@ -327,37 +329,21 @@ class TfTabNewsFeedElement extends LitElement {
this.start_time = start_time; this.start_time = start_time;
this.time_range = [this.start_time, now + 24 * 60 * 60 * 1000]; this.time_range = [this.start_time, now + 24 * 60 * 60 * 1000];
messages = await this.fetch_messages( messages = await this.fetch_messages(
this.time_range[0], null,
this.time_range[1] this.time_range[1]
); );
this.update_time_range_from_messages( this.update_time_range_from_messages(
messages.filter( messages.filter(
(x) => (x) => x.timestamp < this.time_range[1]
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) {
let more = [];
while (!more.length && start_time >= 0) {
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
)
);
}
messages = await this.decrypt([...more, ...this.messages]);
}
} finally { } finally {
this.loading--; this.loading--;
} }
this.messages = this.merge_messages(this.messages, messages); this.messages = this.merge_messages(this.messages, messages);
this.time_loading = undefined; this.time_loading = undefined;
console.log(`loading messages done for ${self.whoami}`); console.log(`loading messages done for ${self.whoami} in ${(new Date() - start_time) / 1000}s`);
} }
mark_all_read() { mark_all_read() {