ssb: Make the client a bit less aggressive about determining private messages every load.
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 22m56s

This commit is contained in:
Cory McWilliams 2025-01-03 15:53:19 -05:00
parent 75c71135ba
commit 9da4857066
2 changed files with 43 additions and 13 deletions

View File

@ -1,5 +1,5 @@
{
"type": "tildefriends-app",
"emoji": "🦀",
"previous": "&ViNc/3DepKybORDYRqBjsXbzX/wzy43nVI6UFy6vNBI=.sha256"
"previous": "&bivD/AIKN+ZOBUrq6+Z049v1AJ5PXV4M1Uqot+iHg5M=.sha256"
}

View File

@ -270,35 +270,65 @@ class TfElement extends LitElement {
}
async get_latest_private(following) {
const k_version = 1;
// { "version": 1, "range": [1234, 5678], messages: [ "%1.sha256", "%2.sha256", ... ], latest: rowid }
let cache = JSON.parse(await tfrpc.rpc.databaseGet(`private:${this.whoami}`) ?? '{}');
if (cache.version !== k_version) {
cache = {
version: k_version,
messages: [],
range: [],
};
}
let latest = (
await tfrpc.rpc.query('SELECT MAX(rowid) AS latest FROM messages')
)[0].latest;
const k_chunk_count = 256;
while (latest - k_chunk_count >= 0) {
let ranges = [];
const k_chunk_size = 512;
if (cache.range.length) {
for (let i = cache.range[1]; i < latest; i += k_chunk_size) {
ranges.push([i, Math.min(i + k_chunk_size, latest), true]);
}
for (let i = cache.range[0]; i >= 0; i -= k_chunk_size) {
ranges.push([Math.max(i - k_chunk_size, 0), Math.min(cache.range[0], i + k_chunk_size), false]);
}
} else {
for (let i = 0; i < latest; i += k_chunk_size) {
ranges.push([i, Math.min(i + k_chunk_size, latest), true]);
}
}
console.log(cache);
for (let range of ranges) {
let messages = await tfrpc.rpc.query(
`
SELECT messages.rowid, messages.id, previous, author, sequence, timestamp, hash, json(content) AS content, signature
SELECT messages.rowid, messages.id, json(content) AS content
FROM messages
JOIN json_each(?1) AS following ON messages.author = following.value
WHERE
messages.rowid > ?2 AND
messages.rowid <= ?3 AND
messages.rowid > ?1 AND
messages.rowid <= ?2 AND
json(messages.content) LIKE '"%'
ORDER BY sequence DESC
`,
[
JSON.stringify(following),
latest - k_chunk_count,
latest,
range[0],
range[1],
]
);
messages = (await this.decrypt(messages)).filter((x) => x.decrypted);
if (messages.length) {
return Math.max(...messages.map((x) => x.rowid));
cache.latest = Math.max(cache.latest ?? 0, ...messages.map((x) => x.rowid));
if (range[2]) {
cache.messages = [...cache.messages, ...messages.map(x => x.id)];
} else {
cache.messages = [...messages.map(x => x.id), ...cache.messages];
}
}
latest -= k_chunk_count;
cache.range[0] = Math.min(cache.range[0] ?? range[0], range[0]);
cache.range[1] = Math.max(cache.range[1] ?? range[1], range[1]);
await tfrpc.rpc.databaseSet(`private:${this.whoami}`, JSON.stringify(cache));
}
return -1;
console.log(cache);
return cache.latest;
}
async load_channels_latest(following) {