Oh, votes are slowing everything down. Batch them and simplify.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3648 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2021-01-20 02:20:40 +00:00
parent 03cf347394
commit 87747c0b6b
6 changed files with 35 additions and 36 deletions

View File

@ -1 +1 @@
{"type":"tildefriends-app","files":{"app.js":"&qvp1qQcymJp9pWxnZXA9/VXFt6FRGj9JCAxNCt/afsU=.sha256","index.html":"&sXphZUl6JQm0Cd4UOVqPZu13PDhqQqFk/EOmKneaEhY=.sha256","vue-material.js":"&K5cdLqXYCENPak/TCINHQhyJhpS4G9DlZHGwoh/LF2g=.sha256","tf-user.js":"&V9Ol799hM6i1PZDbAS91CMhgx7aENDTJnT+KrwK4Lh4=.sha256","tf-message.js":"&e811GqoyT6JhNrgXA1rCB/Y7ggtbOz2xcZP5+rQrrws=.sha256","tf.js":"&lYgDJTZaYTvss6Ykzmr3o6KBc6hPFiEzPn6DR3R5Peo=.sha256"}} {"type":"tildefriends-app","files":{"app.js":"&KQoBrrw9o/ngsLCeS+1yTNWBWzzjm1tPQTJZ+LQaL98=.sha256","index.html":"&UdGmQAbbLedvG7wpsDZRcpZl05jwUe61GDLACoHANYg=.sha256","vue-material.js":"&K5cdLqXYCENPak/TCINHQhyJhpS4G9DlZHGwoh/LF2g=.sha256","tf-user.js":"&V9Ol799hM6i1PZDbAS91CMhgx7aENDTJnT+KrwK4Lh4=.sha256","tf-message.js":"&VNC8ptzGXWAVl9LBBZfrk3YmweA8PQ02d7/wZxWXoAA=.sha256","tf.js":"&Vcdlau6e9yBimrokJzoSPvRIS5BNWXcrVO+cO9TUKBg=.sha256"}}

View File

@ -1,7 +1,7 @@
"use strict"; "use strict";
const k_posts_max = 20; const k_posts_max = 20;
const k_votes_max = 100; const k_votes_max = 20;
async function following(db, id) { async function following(db, id) {
var o = await db.get(id + ":following"); var o = await db.get(id + ":following");
@ -188,7 +188,7 @@ async function getRecentPostIds(db, id, ids, limit) {
async function getVotes(db, id) { async function getVotes(db, id) {
var o = await db.get(id + ":votes"); var o = await db.get(id + ":votes");
const k_version = 3; const k_version = 5;
var votes = []; var votes = [];
var f = o ? JSON.parse(o) : o; var f = o ? JSON.parse(o) : o;
if (!f || f.version != k_version) { if (!f || f.version != k_version) {
@ -231,12 +231,7 @@ async function getPosts(db, ids) {
await ssb.sqlStream( await ssb.sqlStream(
"SELECT rowid, * FROM messages WHERE id IN (" + ids.map(x => "?").join(", ") + ")", "SELECT rowid, * FROM messages WHERE id IN (" + ids.map(x => "?").join(", ") + ")",
ids, ids,
function(row) { row => posts.push(row));
try {
posts.push(row);
} catch {
}
});
} }
return posts; return posts;
} }
@ -278,21 +273,19 @@ async function refresh() {
return root && posts.every(post => post.id != root); return root && posts.every(post => post.id != root);
}); });
return [].concat(posts, await getPosts(db, roots)); return [].concat(posts, await getPosts(db, roots));
}).then(async function(posts) { }).then(function(posts) {
posts.forEach(async function(post) { return Promise.all(posts.map(x => app.postMessage({message: x})));
await app.postMessage({message: post});
});
}); });
f.forEach(async function(id) { Promise.all(f.map(function(id) {
await Promise.all([ return Promise.all([
getVotes(db, id).then(async function(votes) { getVotes(db, id).then(function(votes) {
return Promise.all(votes.map(vote => app.postMessage({vote: vote}))); return app.postMessage({votes: votes});
}), }),
getAbout(db, id).then(async function(user) { getAbout(db, id).then(function(user) {
return app.postMessage({user: {user: id, about: user}}); return app.postMessage({user: {user: id, about: user}});
}), }),
]); ]);
}); }));
}), }),
sendUser(db, whoami), sendUser(db, whoami),
]); ]);

View File

@ -73,7 +73,13 @@
<md-button class="md-raised md-primary" v-on:click="post_message()">Submit Post</md-button> <md-button class="md-raised md-primary" v-on:click="post_message()">Submit Post</md-button>
</md-card-actions> </md-card-actions>
</md-card> </md-card>
<tf-message v-for="message in messages" v-if="!content_json(message).root || !messages.some(m => m.id == content_json(message).root)" v-bind:message="message" v-bind:messages="messages" v-bind:key="message.id"></tf-message> <tf-message
v-for="message in messages"
v-if="!content_json(message).root || !messages.some(m => m.id == content_json(message).root)"
v-bind:message="message"
v-bind:messages="messages"
v-bind:key="message.id"
v-bind:votes="votes"></tf-message>
</md-app-content> </md-app-content>
</md-app> </md-app>
</div> </div>

View File

@ -1,5 +1,5 @@
Vue.component('tf-message', { Vue.component('tf-message', {
props: ['message', 'messages'], props: ['message', 'messages', 'votes'],
data: function() { return { showRaw: false } }, data: function() { return { showRaw: false } },
computed: { computed: {
content_json: function() { content_json: function() {
@ -17,15 +17,10 @@ Vue.component('tf-message', {
} catch {} } catch {}
}); });
}, },
votes: function() { votes2: function() {
return [];
var id = this.message.id; var id = this.message.id;
return this.votes.filter(function (x) { var votes = this.votes[id] || [];
try { return votes.reduce(function (accum, value) {
var j = JSON.parse(x.content);
return j.type == 'vote' && j.vote.link == id;
} catch {}
}).reduce(function (accum, value) {
var expression = JSON.parse(value.content).vote.expression; var expression = JSON.parse(value.content).vote.expression;
if (!accum[expression]) { if (!accum[expression]) {
accum[expression] = []; accum[expression] = [];
@ -79,9 +74,9 @@ Vue.component('tf-message', {
<div v-else-if="content_json && content_json.type == 'contact'"><tf-user :id="message.author"></tf-user> {{content_json.following ? '==&gt;' : '=/=&gt;'}} <tf-user :id="content_json.contact"></tf-user></div> <div v-else-if="content_json && content_json.type == 'contact'"><tf-user :id="message.author"></tf-user> {{content_json.following ? '==&gt;' : '=/=&gt;'}} <tf-user :id="content_json.contact"></tf-user></div>
<div v-else>{{message.content}}</div> <div v-else>{{message.content}}</div>
</div> </div>
<tf-message v-for="sub_message in sub_messages" v-bind:message="sub_message" v-bind:messages="messages" v-bind:key="sub_message.id"></tf-message> <tf-message v-for="sub_message in sub_messages" v-bind:message="sub_message" v-bind:messages="messages" v-bind:votes="votes" v-bind:key="sub_message.id"></tf-message>
<md-chip v-for="vote in Object.keys(votes)" v-bind:key="vote"> <md-chip v-for="vote in Object.keys(votes2)" v-bind:key="vote">
{{vote + (votes[vote].length > 1 ? ' (' + votes[vote].length + ')' : '')}} {{vote + (votes2[vote].length > 1 ? ' (' + votes2[vote].length + ')' : '')}}
</md-chip> </md-chip>
</md-app-content> </md-app-content>
</md-app>`, </md-app>`,

View File

@ -10,7 +10,7 @@ var g_data = {
show_user_dialog: null, show_user_dialog: null,
connect: null, connect: null,
pubs: [], pubs: [],
votes: [], votes: {},
apps: {}, apps: {},
share_app: null, share_app: null,
}; };
@ -38,6 +38,14 @@ window.addEventListener('message', function(event) {
g_data.pubs = event.data.pubs; g_data.pubs = event.data.pubs;
} else if (key == 'apps') { } else if (key == 'apps') {
g_data.apps = event.data.apps; g_data.apps = event.data.apps;
} else if (key == 'votes') {
event.data.votes.forEach(function(vote) {
var link = JSON.parse(vote.content).vote.link;
if (!g_data.votes[link]) {
Vue.set(g_data.votes, link, []);
}
g_data.votes[link].push(vote);
});
} else if (key == 'clear') { } else if (key == 'clear') {
Object.keys(g_data_initial).forEach(function(key) { Object.keys(g_data_initial).forEach(function(key) {
Vue.set(g_data, key, JSON.parse(JSON.stringify(g_data_initial[key]))); Vue.set(g_data, key, JSON.parse(JSON.stringify(g_data_initial[key])));

View File

@ -231,7 +231,6 @@ function save() {
type: "tildefriends-app", type: "tildefriends-app",
files: Object.fromEntries(Object.keys(gFiles).map(x => [x, gFiles[x].id || gApp.files[x]])), files: Object.fromEntries(Object.keys(gFiles).map(x => [x, gFiles[x].id || gApp.files[x]])),
}; };
console.log(app);
Object.values(gFiles).forEach(function(file) { delete file.id; }); Object.values(gFiles).forEach(function(file) { delete file.id; });
gApp = JSON.parse(JSON.stringify(app)); gApp = JSON.parse(JSON.stringify(app));
@ -285,7 +284,6 @@ function save() {
var anySkipped = false; var anySkipped = false;
Object.values(gFiles).forEach(function(file) { Object.values(gFiles).forEach(function(file) {
if (file.doc.isClean(file.generation)) { if (file.doc.isClean(file.generation)) {
console.log("Not saving clean file.");
anySkipped = true; anySkipped = true;
return; return;
} }
@ -320,7 +318,6 @@ function save() {
always(); always();
}); });
console.log("Saving file");
file.request.open("POST", "/save", true); file.request.open("POST", "/save", true);
file.request.setRequestHeader("Content-Type", "text/plain"); file.request.setRequestHeader("Content-Type", "text/plain");
file.request.send(file.doc.getValue()); file.request.send(file.doc.getValue());