Progress toward viewing user profile pages.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3737 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2022-01-07 01:52:47 +00:00
parent 49ffd1055e
commit ae84f69025
6 changed files with 82 additions and 47 deletions

View File

@@ -3,6 +3,8 @@
const k_posts_max = 20;
const k_votes_max = 20;
var g_selected = null;
var g_following_cache = {};
var g_followers_cache = {};
var g_following_deep_cache = {};
@@ -171,6 +173,13 @@ async function getRecentPostIds(db, id, ids, limit) {
}
f.rowid = row_id_max;
f.recent = [].concat(recent, f.recent);
var have = {};
f.recent = f.recent.filter(function(x) {
if (!have[x.id]) {
have[x.id] = true;
return true;
}
});
f.recent.sort((x, y) => y.timestamp - x.timestamp);
f.recent = f.recent.slice(0, limit);
var j = JSON.stringify(f);
@@ -231,7 +240,7 @@ async function getPosts(db, ids) {
}
async function ready() {
return refresh();
return refresh(g_selected);
}
ssb.addEventListener('broadcasts', async function() {
@@ -243,51 +252,55 @@ core.register('onConnectionsChanged', async function() {
await app.postMessage({connections: connections});
});
async function refresh() {
async function refresh(selected) {
g_following_cache = {};
g_followers_cache = {};
g_following_deep_cache = {};
await app.postMessage({clear: true});
var whoami = await ssb.whoami();
var db = await database("ssb");
var all_followed = await followingDeep(db, [whoami], 2);
if (selected) {
g_selected = selected;
} else {
g_selected = all_followed;
}
await Promise.all([
app.postMessage({whoami: whoami}),
app.postMessage({broadcasts: await ssb.getBroadcasts()}),
app.postMessage({connections: await ssb.connections()}),
app.postMessage({apps: await core.apps()}),
followingDeep(db, [whoami], 2).then(function(f) {
return getRecentPostIds(db, whoami, [].concat([whoami], f), k_posts_max).then(async function(ids) {
var posts = await getPosts(db, ids);
var roots = posts.map(function(x) {
try {
return JSON.parse(x.content).root;
} catch {
return null;
}
});
roots = roots.filter(function(root) {
return root && posts.every(post => post.id != root);
});
var all_posts = [].concat(posts, await getPosts(db, roots));
await Promise.all(all_posts.map(x => app.postMessage({message: x})));
return Promise.all([].concat(f.map(function(id) {
return [
getVotes(db, id).then(function(votes) {
return app.postMessage({votes: votes});
}),
getAbout(db, id).then(function(user) {
return app.postMessage({user: {user: id, about: user}});
}),
following(db, id).then(function(following) {
return app.postMessage({following: {id: id, users: following}});
}),
followers(db, id).then(function(followers) {
return app.postMessage({followers: {id: id, users: followers}});
}),
];
})));
ssb.getBroadcasts().then(broadcasts => app.postMessage({broadcasts: broadcasts})),
ssb.connections().then(connections => app.postMessage({connections: connections})),
core.apps().then(apps => app.postMessage({apps: apps})),
getRecentPostIds(db, whoami, g_selected, k_posts_max).then(async function(ids) {
var posts = await getPosts(db, ids);
var roots = posts.map(function(x) {
try {
return JSON.parse(x.content).root;
} catch {
return null;
}
});
roots = roots.filter(function(root) {
return root && posts.every(post => post.id != root);
});
var all_posts = [].concat(posts, await getPosts(db, roots));
return Promise.all(all_posts.map(x => app.postMessage({message: x})));
}),
Promise.all([].concat(all_followed.map(function(id) {
return [
getVotes(db, id).then(function(votes) {
return app.postMessage({votes: votes});
}),
getAbout(db, id).then(function(user) {
return app.postMessage({user: {user: id, about: user}});
}),
following(db, id).then(function(following) {
return app.postMessage({following: {id: id, users: following}});
}),
followers(db, id).then(function(followers) {
return app.postMessage({followers: {id: id, users: followers}});
}),
];
}))),
]);
}
@@ -304,6 +317,7 @@ ssb.addEventListener('message', async function(id) {
});
core.register('message', async function(m) {
print(JSON.stringify(m));
if (m.message == 'ready') {
await ready();
} else if (m.message) {
@@ -312,7 +326,13 @@ core.register('message', async function(m) {
} else if (m.message.appendMessage) {
await ssb.appendMessage(m.message.appendMessage);
} else if (m.message.refresh) {
await refresh();
await refresh(g_selected);
}
} else if (m.event == 'hashChange') {
if (m.hash.length > 1) {
refresh([m.hash.substring(1)]);
} else {
refresh();
}
} else if (m.event == 'focus' || m.event == 'blur') {
/* Shh. */