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

View File

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

View File

@ -10,7 +10,7 @@ var g_data = {
show_user_dialog: null,
connect: null,
pubs: [],
votes: [],
votes: {},
apps: {},
share_app: null,
};
@ -38,6 +38,14 @@ window.addEventListener('message', function(event) {
g_data.pubs = event.data.pubs;
} else if (key == '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') {
Object.keys(g_data_initial).forEach(function(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",
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; });
gApp = JSON.parse(JSON.stringify(app));
@ -285,7 +284,6 @@ function save() {
var anySkipped = false;
Object.values(gFiles).forEach(function(file) {
if (file.doc.isClean(file.generation)) {
console.log("Not saving clean file.");
anySkipped = true;
return;
}
@ -320,7 +318,6 @@ function save() {
always();
});
console.log("Saving file");
file.request.open("POST", "/save", true);
file.request.setRequestHeader("Content-Type", "text/plain");
file.request.send(file.doc.getValue());