ssb: Bring back the date. Whoops.

This commit is contained in:
2025-04-05 22:05:26 -04:00
parent 8be354fc49
commit 657f25e22b
6 changed files with 73 additions and 35 deletions

View File

@ -2,7 +2,7 @@ let g_hash;
async function query(sql, params) {
let results = [];
await ssb.sqlAsync(sql, params, function(row) {
await ssb.sqlAsync(sql, params, function (row) {
results.push(row);
});
return results;
@ -35,7 +35,10 @@ async function resolve(id) {
}
async function get_names(identities) {
return Object.fromEntries((await query(`
return Object.fromEntries(
(
await query(
`
SELECT author, name FROM (
SELECT
messages.author,
@ -47,23 +50,35 @@ async function get_names(identities) {
json_extract(messages.content, '$.type') = 'about' AND
content ->> 'about' = messages.author AND name IS NOT NULL)
WHERE author_rank = 1
`, [JSON.stringify(identities)])).map(x => [x.author, x.name]));
`,
[JSON.stringify(identities)]
)
).map((x) => [x.author, x.name])
);
}
async function render(hash) {
g_hash = hash;
if (!hash) {
let sites = (await query(`
let sites = await query(
`
SELECT site.author, site.id
FROM messages site
WHERE site.content ->> 'type' = 'web-init'
`, []));
let names = await get_names(sites.map(x => x.author));
`,
[]
);
let names = await get_names(sites.map((x) => x.author));
if (hash === g_hash) {
await app.setDocument(`<ul style="background-color: #ddd">${sites.map(x => `<li><a target="_top" href="#${encodeURIComponent(x.id)}">${names[x.author] ?? x.author} - ${x.id}</a></li>`).join('\n')}</ul>`);
await app.setDocument(
`<ul style="background-color: #ddd">${sites.map((x) => `<li><a target="_top" href="#${encodeURIComponent(x.id)}">${names[x.author] ?? x.author} - ${x.id}</a></li>`).join('\n')}</ul>`
);
}
} else {
let site_id = hash.charAt(0) == '#' ? decodeURIComponent(hash.substring(1)) : decodeURIComponent(hash);
let site_id =
hash.charAt(0) == '#'
? decodeURIComponent(hash.substring(1))
: decodeURIComponent(hash);
await app.setDocument(`<html style="margin: 0; padding: 0; width: 100vw; height: 100vh; margin: 0; padding: 0">
<body style="display: flex; flex-direction: column; width: 100vw; height: 100vh">
<iframe src="${encodeURIComponent(site_id)}/index.html" style="flex: 1 1; border: 0; background-color: #fff"></iframe>
@ -82,4 +97,4 @@ async function main() {
render(null);
}
main();
main();

View File

@ -1,6 +1,6 @@
async function query(sql, params) {
let results = [];
await ssb.sqlAsync(sql, params, function(row) {
await ssb.sqlAsync(sql, params, function (row) {
results.push(row);
});
return results;
@ -19,17 +19,22 @@ function guess_content_type(name) {
}
async function main() {
let path = request.path.replaceAll(/(%[0-9a-fA-F]{2})/g, x => String.fromCharCode(parseInt(x.substring(1), 16)));
let path = request.path.replaceAll(/(%[0-9a-fA-F]{2})/g, (x) =>
String.fromCharCode(parseInt(x.substring(1), 16))
);
let match = path.match(/^(%.{44}\.sha256)(?:\/)?(.*)$/);
let content_type = guess_content_type(request.path);
let root = await query(`
let root = await query(
`
SELECT root.content ->> 'root' AS root
FROM messages site
JOIN messages root
ON site.id = ? AND root.author = site.author AND root.content ->> 'site' = site.id
ORDER BY root.sequence DESC LIMIT 1
`, [match[1]]);
`,
[match[1]]
);
let root_id = root[0]['root'];
let last_id = root_id;
let blob = await ssb.blobGet(root_id);
@ -40,8 +45,7 @@ async function main() {
blob = await ssb.blobGet(dir?.links[part]);
content_type = guess_content_type(part);
}
} catch {
}
} catch {}
respond({
status_code: 200,
@ -50,10 +54,10 @@ async function main() {
});
}
main().catch(function(e) {
main().catch(function (e) {
respond({
status_code: 200,
data: `${e.message}\n${e.stack}`,
content_type: 'text/plain',
});
});
});