diff --git a/apps/ssb.json b/apps/ssb.json index 305d161f..1b567cf9 100644 --- a/apps/ssb.json +++ b/apps/ssb.json @@ -1,5 +1,5 @@ { "type": "tildefriends-app", "emoji": "🦀", - "previous": "&ViNc/3DepKybORDYRqBjsXbzX/wzy43nVI6UFy6vNBI=.sha256" + "previous": "&bivD/AIKN+ZOBUrq6+Z049v1AJ5PXV4M1Uqot+iHg5M=.sha256" } diff --git a/apps/ssb/tf-app.js b/apps/ssb/tf-app.js index 4ae70c4b..a5871d01 100644 --- a/apps/ssb/tf-app.js +++ b/apps/ssb/tf-app.js @@ -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) {