From 35b7eb511abaae9515b0840cb2406eeba6d66fa5 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Wed, 12 Jan 2022 02:55:29 +0000 Subject: [PATCH] Support selecting a thread and showing related messages (by a rather brute force search). Sort child messages in the reverse order of top level messages, because that's how we roll. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3754 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- apps/cory/index.json | 2 +- apps/cory/index/app.js | 50 ++++++++++++++++++++++++++++++++++- apps/cory/index/tf-message.js | 6 +++++ apps/cory/index/tf.js | 4 +-- 4 files changed, 58 insertions(+), 4 deletions(-) diff --git a/apps/cory/index.json b/apps/cory/index.json index 026d6b6d..37d54edb 100644 --- a/apps/cory/index.json +++ b/apps/cory/index.json @@ -1 +1 @@ -{"type":"tildefriends-app","files":{"app.js":"&+Q+L0IpjsqRhedfyBVkrZASqXtLArzn+/SwycrDaL60=.sha256","index.html":"&/CE4CuVKI56h5tfAHF0kzCXbLfhVjWzsM8aZAB9HpEM=.sha256","vue-material.js":"&K5cdLqXYCENPak/TCINHQhyJhpS4G9DlZHGwoh/LF2g=.sha256","tf-user.js":"&DdJwZYEo7AqFyutYMvEjykoVXxdHVog0UXye6Sbo0TU=.sha256","tf-message.js":"&/shEzHxwgMAgtiJE5QzpWayMvwHt/G0dhwgujwQHtn0=.sha256","tf.js":"&k1spHFz1z0DroPr/95QBOjR3RVeCJi61TK4i2Xr37uE=.sha256","commonmark.min.js":"&5x6ek3tFrKTZX6hXNNyFsjmhvrjmWpUkwuuaiyVV1Us=.sha256","vue.js":"&g1wvA+yHl1sVC+eufTsg9If7ZeVyMTBU+h0tks7ZNzE=.sha256","vue-material-theme-default-dark.css":"&RP2nr+2CR18BpHHw5ST9a5GJUCOG9n0G2kuGkcQioWE=.sha256","vue-material.min.css":"&kGbUM2QgFSyHZRzqQb0b+0S3EVIlZ0AXpdiAVjIhou8=.sha256","roboto.css":"&jJv43Om673mQO5JK0jj7714s5E+5Yrf82H6LcDx7wUs=.sha256","material-icons.css":"&a28PdcVvgq/DxyIvJAx/e+ZOEtOuHnr3kjLWKyzH11M=.sha256"}} \ No newline at end of file +{"type":"tildefriends-app","files":{"app.js":"&qrR7s0k3vd/aGg03JurUTRMzWyPIEMwGiKAEYNpHpT8=.sha256","index.html":"&/CE4CuVKI56h5tfAHF0kzCXbLfhVjWzsM8aZAB9HpEM=.sha256","vue-material.js":"&K5cdLqXYCENPak/TCINHQhyJhpS4G9DlZHGwoh/LF2g=.sha256","tf-user.js":"&DdJwZYEo7AqFyutYMvEjykoVXxdHVog0UXye6Sbo0TU=.sha256","tf-message.js":"&A/R6o9GsaDTEyoAIS8EE1ATCfugBlhdS+jdMz7vsfsM=.sha256","tf.js":"&gJUMwBsssB+Suy/NTUjcB3JeloFLvV18i6egqvThaVY=.sha256","commonmark.min.js":"&5x6ek3tFrKTZX6hXNNyFsjmhvrjmWpUkwuuaiyVV1Us=.sha256","vue.js":"&g1wvA+yHl1sVC+eufTsg9If7ZeVyMTBU+h0tks7ZNzE=.sha256","vue-material-theme-default-dark.css":"&RP2nr+2CR18BpHHw5ST9a5GJUCOG9n0G2kuGkcQioWE=.sha256","vue-material.min.css":"&kGbUM2QgFSyHZRzqQb0b+0S3EVIlZ0AXpdiAVjIhou8=.sha256","roboto.css":"&jJv43Om673mQO5JK0jj7714s5E+5Yrf82H6LcDx7wUs=.sha256","material-icons.css":"&a28PdcVvgq/DxyIvJAx/e+ZOEtOuHnr3kjLWKyzH11M=.sha256"}} \ No newline at end of file diff --git a/apps/cory/index/app.js b/apps/cory/index/app.js index 2354c357..48eb5172 100644 --- a/apps/cory/index/app.js +++ b/apps/cory/index/app.js @@ -209,6 +209,48 @@ async function getRecentPostIds(db, id, ids, limit) { return f.recent.map(x => x.id); } +async function getRelatedPostIds(db, message, ids, limit) { + const k_batch_max = 16; + var recent = []; + var row_id_max = 0; + await ssb.sqlStream( + "SELECT MAX(rowid) as rowid FROM messages", + [], + function(row) { + row_id_max = row.rowid; + }); + var id = message.id; + try { + id = JSON.parse(message.content).root || id; + } catch { + } + for (var i = 0; i < ids.length; i += k_batch_max) { + var ids_batch = ids.slice(i, Math.min(i + k_batch_max, ids.length)); + await ssb.sqlStream( + "SELECT "+ + " rowid, "+ + " id, "+ + " timestamp "+ + "FROM messages "+ + "WHERE "+ + " timestamp >= ? AND "+ + " rowid <= ? AND "+ + " author IN (" + ids_batch.map(x => '?').join(", ") + ") AND "+ + " json_extract(content, '$.type') = 'post' AND "+ + " json_extract(content, '$.root') = ? "+ + "ORDER BY timestamp DESC LIMIT ?", + [].concat([message.timestamp, row_id_max], ids_batch, [id, limit]), + function(row) { + if (row.id) { + recent.push({id: row.id, timestamp: row.timestamp}); + } + }); + } + recent.sort((x, y) => y.timestamp - x.timestamp); + recent = recent.slice(0, limit); + return recent.map(x => x.id); +} + async function getVotes(db, id) { var o = await db.get(id + ":votes"); const k_version = 7; @@ -291,7 +333,13 @@ async function refresh(selected) { ssb.connections().then(connections => app.postMessage({connections: connections})), core.apps().then(apps => app.postMessage({apps: apps})), ]); - var ids = await getRecentPostIds(db, whoami, g_selected, k_posts_max); + var ids; + if (selected && selected.length == 1 && selected[0].startsWith('%')) { + var m = await getPosts(db, selected); + ids = m.length ? await getRelatedPostIds(db, m[0], all_followed, k_posts_max) : []; + } else { + ids = await getRecentPostIds(db, whoami, g_selected, k_posts_max); + } var posts = await getPosts(db, ids); var roots = posts.map(function(x) { try { diff --git a/apps/cory/index/tf-message.js b/apps/cory/index/tf-message.js index 77744afa..03db16c6 100644 --- a/apps/cory/index/tf-message.js +++ b/apps/cory/index/tf-message.js @@ -46,10 +46,16 @@ Vue.component('tf-message', { '*'); } }, + show_message: function() { + window.parent.postMessage({action: 'setHash', hash: this.message.id}, '*'); + }, }, template: `

+ + percent +

diff --git a/apps/cory/index/tf.js b/apps/cory/index/tf.js index b3abb7ce..e8208fc5 100644 --- a/apps/cory/index/tf.js +++ b/apps/cory/index/tf.js @@ -44,7 +44,7 @@ function processMessages() { for (let message of g_data.messages) { if (root == message.id) { message.children.push(new_message); - message.children.sort((x, y) => y.timestamp - x.timestamp); + message.children.sort((x, y) => x.timestamp - y.timestamp); found = true; } } @@ -53,7 +53,7 @@ function processMessages() { for (let message of g_data.messages) { if (JSON.parse(message.content).root == new_message.id) { new_message.children.push(message); - new_message.children.sort((x, y) => y.timestamp - x.timestamp); + new_message.children.sort((x, y) => x.timestamp - y.timestamp); removed[message.id] = true; } }