From 79ff505963a7c21ebcfd594f22da0c0181f72a47 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Sun, 22 Dec 2024 13:16:56 -0500 Subject: [PATCH] ssb: Fix various channel / unread status / show new messages bugs. --- apps/ssb.json | 2 +- apps/ssb/tf-app.js | 93 +++++++++++++++++++++++------------- apps/ssb/tf-tab-news-feed.js | 11 +++-- apps/ssb/tf-tab-news.js | 2 +- 4 files changed, 71 insertions(+), 37 deletions(-) diff --git a/apps/ssb.json b/apps/ssb.json index 0c7f72940..c6de9bc22 100644 --- a/apps/ssb.json +++ b/apps/ssb.json @@ -1,5 +1,5 @@ { "type": "tildefriends-app", "emoji": "🐌", - "previous": "&0gBRfD+3EaZD2S82zAnXT3hgGuNTnUncOh5vGwZwbSw=.sha256" + "previous": "&jsq+9gvTsDRmi/t0t1wzftsi/IxkBwivsJEUpA7X/lE=.sha256" } diff --git a/apps/ssb/tf-app.js b/apps/ssb/tf-app.js index b18ab4e53..a85bde365 100644 --- a/apps/ssb/tf-app.js +++ b/apps/ssb/tf-app.js @@ -38,6 +38,8 @@ class TfElement extends LitElement { this.channels = []; this.channels_unread = {}; this.channels_latest = {}; + this.loading_channels_latest = 0; + this.loading_channels_latest_scheduled = 0; tfrpc.rpc.getBroadcasts().then((b) => { self.broadcasts = b || []; }); @@ -243,6 +245,7 @@ class TfElement extends LitElement { this.unread = [...this.unread, ...messages]; this.unread = this.unread.slice(this.unread.length - 1024); } + this.schedule_load_channels_latest(); } async _handle_whoami_changed(event) { @@ -295,6 +298,62 @@ class TfElement extends LitElement { return -1; } + async load_channels_latest(following) { + this.loading_channels_latest++; + try { + let start_time = new Date(); + let latest_private = this.get_latest_private(following); + let channels = await tfrpc.rpc.query( + ` + SELECT channels.value AS channel, MAX(messages.rowid) AS rowid FROM messages + JOIN json_each(?1) AS channels ON messages.content ->> 'channel' = channels.value + JOIN json_each(?2) AS following ON messages.author = following.value + WHERE messages.content ->> 'type' = 'post' AND messages.content ->> 'root' IS NULL + GROUP by channel + UNION + SELECT '' AS channel, MAX(messages.rowid) AS rowid FROM messages + JOIN json_each(?2) AS following ON messages.author = following.value + UNION + SELECT '@' AS channel, MAX(messages.rowid) AS rowid FROM messages_fts(?3) + JOIN messages ON messages.rowid = messages_fts.rowid + JOIN json_each(?2) AS following ON messages.author = following.value + `, + [ + JSON.stringify(this.channels), + JSON.stringify(following), + '"' + this.whoami.replace('"', '""') + '"', + ] + ); + this.channels_latest = Object.fromEntries( + channels.map((x) => [x.channel, x.rowid]) + ); + console.log('latest', this.channels_latest); + console.log('unread', this.channels_unread); + console.log('channels took', (new Date() - start_time) / 1000.0); + let self = this; + latest_private.then(function(latest) { + self.channels_latest = Object.assign({}, self.channels_latest, {'🔐': latest}); + console.log('private took', (new Date() - start_time) / 1000.0); + }); + } finally { + this.loading_channels_latest--; + } + } + + _schedule_load_channels_latest_timer() { + --this.loading_channels_latest_scheduled; + this.schedule_load_channels_latest(); + } + + schedule_load_channels_latest() { + if (!this.loading_channels_latest) { + this.load_channels_latest(this.following); + } else if (!this.loading_channels_latest_scheduled) { + this.loading_channels_latest_scheduled++; + setTimeout(this._schedule_load_channels_latest_timer, 5000); + } + } + async load() { let whoami = this.whoami; let following = await tfrpc.rpc.following([whoami], 2); @@ -309,28 +368,7 @@ class TfElement extends LitElement { }; by_count.push({count: v.of, id: id}); } - let channels = tfrpc.rpc.query( - ` - SELECT channels.value AS channel, MAX(messages.rowid) AS rowid FROM messages - JOIN json_each(?1) AS channels ON messages.content ->> 'channel' = channels.value - JOIN json_each(?2) AS following ON messages.author = following.value - WHERE messages.content ->> 'type' = 'post' AND messages.content ->> 'root' IS NULL - GROUP by channel - UNION - SELECT '' AS channel, MAX(messages.rowid) AS rowid FROM messages - JOIN json_each(?2) AS following ON messages.author = following.value - UNION - SELECT '@' AS channel, MAX(messages.rowid) AS rowid FROM messages_fts(?3) - JOIN messages ON messages.rowid = messages_fts.rowid - JOIN json_each(?2) AS following ON messages.author = following.value - `, - [ - JSON.stringify(this.channels), - JSON.stringify(Object.keys(following)), - '"' + this.whoami.replace('"', '""') + '"', - ] - ); - let latest_private = this.get_latest_private(Object.keys(following)); + let channels_latest = this.load_channels_latest(Object.keys(following)); this.channels_unread = JSON.parse( (await tfrpc.rpc.databaseGet('unread')) ?? '{}' ); @@ -344,16 +382,7 @@ class TfElement extends LitElement { 'users' ); start_time = new Date(); - channels = await channels; - console.log('channels took', (new Date() - start_time) / 1000.0); - this.channels_latest = Object.fromEntries( - channels.map((x) => [x.channel, x.rowid]) - ); - let self = this; - latest_private.then(function(latest) { - self.channels_latest = Object.assign({}, self.channels_latest, {'🔐': latest}); - console.log('private took', (new Date() - start_time) / 1000.0); - }); + await channels_latest; this.following = Object.keys(following); this.users = users; console.log(`load finished ${whoami} => ${this.whoami}`); diff --git a/apps/ssb/tf-tab-news-feed.js b/apps/ssb/tf-tab-news-feed.js index 609a89605..631a7cc64 100644 --- a/apps/ssb/tf-tab-news-feed.js +++ b/apps/ssb/tf-tab-news-feed.js @@ -165,7 +165,7 @@ class TfTabNewsFeedElement extends LitElement { `, [JSON.stringify(this.following), start_time, end_time] ); - result = (await this.decrypt(result)).filter(x => x.decrypted); + result = (await this.decrypt(result)).filter((x) => x.decrypted); } else { let promises = []; const k_following_limit = 256; @@ -274,7 +274,10 @@ class TfTabNewsFeedElement extends LitElement { let end_time = now + 24 * 60 * 60 * 1000; let messages = []; try { - messages = await this.fetch_messages(this.time_range[1], end_time); + messages = await this.fetch_messages( + this.time_range[1] - 24 * 60 * 60 * 1000, + end_time + ); messages = await this.decrypt(messages); this.update_time_range_from_messages( messages.filter( @@ -284,7 +287,9 @@ class TfTabNewsFeedElement extends LitElement { } finally { this.loading--; } - this.messages = [...this.messages, ...messages]; + this.messages = Object.values( + Object.fromEntries([...this.messages, ...messages].map((x) => [x.id, x])) + ); console.log('done loading latest messages.'); } diff --git a/apps/ssb/tf-tab-news.js b/apps/ssb/tf-tab-news.js index 9202d5ded..00efec71e 100644 --- a/apps/ssb/tf-tab-news.js +++ b/apps/ssb/tf-tab-news.js @@ -113,7 +113,7 @@ class TfTabNewsElement extends LitElement { if ( this.channels_latest[channel] && (this.channels_unread[channel] === undefined || - this.channels_unread[channel] < this.channels_latest[channel]) + this.channels_unread[channel] <= this.channels_latest[channel]) ) { return '🔵'; }