Progress toward generating static wiki pages.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4634 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2023-11-16 01:33:00 +00:00
parent 06ea8d4781
commit 461e7b7d5a
6 changed files with 100 additions and 92 deletions

View File

@ -1,5 +1,5 @@
{ {
"type": "tildefriends-app", "type": "tildefriends-app",
"emoji": "📝", "emoji": "📝",
"previous": "&FINPRELEEbBO4wVoRYZyd5/VIMERFMyk4NjC8wxjelE=.sha256" "previous": "&UMb81rZg54kgoGBmpGBtWcmtuioPfTx6HT3OyQYJ/LU=.sha256"
} }

View File

@ -1,4 +1,5 @@
import * as tfrpc from '/tfrpc.js'; import * as tfrpc from '/tfrpc.js';
import * as utils from './utils.js';
let g_hash; let g_hash;
let g_collection_notifies = {}; let g_collection_notifies = {};
@ -90,95 +91,7 @@ tfrpc.register(async function encrypt(id, recipients, content) {
return await ssb.privateMessageEncrypt(id, recipients, content); return await ssb.privateMessageEncrypt(id, recipients, content);
}); });
async function process_message(whoami, collection, message, kind, parent) { tfrpc.register(utils.collection);
let content = JSON.parse(message.content);
if (typeof content == 'string') {
let x;
for (let id of (whoami || [])) {
x = await ssb.privateMessageDecrypt(id, content);
if (x) {
try {
content = JSON.parse(x);
content.draft = true;
break;
} catch {
return;
}
}
}
if (!x) {
return;
}
if (content.type !== kind ||
(parent && content.parent !== parent)) {
return;
}
} else {
content.draft = false;
}
if (content?.key) {
if (content?.tombstone) {
delete collection[content.key];
} else {
collection[content.key] = Object.assign(collection[content.key] || {}, content);
}
} else {
collection[message.id] = Object.assign(content, {id: message.id});
if (!collection[message.id].editors) {
collection[message.id].editors = [message.author];
}
}
return true;
}
tfrpc.register(async function collection(ids, kind, parent, max_rowid, data, include_private) {
let whoami = await ssb.getIdentities();
data = data ?? {};
let rowid = 0;
let first = true;
print('CHECKING', kind, ids);
await ssb.sqlAsync('SELECT MAX(rowid) AS rowid FROM messages', [], function(row) {
rowid = row.rowid;
});
while (true) {
if (rowid == max_rowid) {
await new_message();
await ssb.sqlAsync('SELECT MAX(rowid) AS rowid FROM messages', [], function(row) {
rowid = row.rowid;
});
first = false;
}
let modified = false;
let rows = [];
print(include_private ? true: false, ids);
await ssb.sqlAsync(`
SELECT messages.id, author, content, timestamp
FROM messages
JOIN json_each(?1) AS id ON messages.author = id.value
WHERE
messages.rowid > ?2 AND
messages.rowid <= ?3 AND
((json_extract(messages.content, '$.type') = ?4 AND
(?5 IS NULL OR json_extract(messages.content, '$.parent') = ?5)) OR
(?6 AND content LIKE '"%'))
ORDER BY timestamp
`, [JSON.stringify(ids), max_rowid ?? -1, rowid, kind, parent, include_private ? true : false], function(row) {
rows.push(row);
});
print('done');
max_rowid = rowid;
for (let row of rows) {
if (await process_message(whoami, data, row, kind, parent)) {
modified = true;
}
}
if (first || modified) {
break;
}
}
return [rowid, data];
});
async function main() { async function main() {
await app.setDocument(utf8Decode(await getFile('index.html'))); await app.setDocument(utf8Decode(await getFile('index.html')));

File diff suppressed because one or more lines are too long

View File

@ -5,7 +5,6 @@
</head> </head>
<body style="color: #fff"> <body style="color: #fff">
<tf-collections-app></tf-collections-app> <tf-collections-app></tf-collections-app>
<script src="commonmark.min.js"></script>
<script>window.litDisableBundleWarning = true;</script> <script>window.litDisableBundleWarning = true;</script>
<script src="tf-collection.js" type="module"></script> <script src="tf-collection.js" type="module"></script>
<script src="tf-id-picker.js" type="module"></script> <script src="tf-id-picker.js" type="module"></script>

View File

@ -1,5 +1,6 @@
import {LitElement, html, unsafeHTML} from './lit-all.min.js'; import {LitElement, html, unsafeHTML} from './lit-all.min.js';
import * as tfrpc from '/static/tfrpc.js'; import * as tfrpc from '/static/tfrpc.js';
import * as commonmark from './commonmark.min.js';
class TfWikiDocElement extends LitElement { class TfWikiDocElement extends LitElement {
static get properties() { static get properties() {

95
apps/wiki/utils.js Normal file
View File

@ -0,0 +1,95 @@
async function process_message(whoami, collection, message, kind, parent) {
let content = JSON.parse(message.content);
if (typeof content == 'string') {
let x;
for (let id of (whoami || [])) {
x = await ssb.privateMessageDecrypt(id, content);
if (x) {
try {
content = JSON.parse(x);
content.draft = true;
break;
} catch {
return;
}
}
}
if (!x) {
return;
}
if (content.type !== kind ||
(parent && content.parent !== parent)) {
return;
}
} else {
content.draft = false;
}
if (content?.key) {
if (content?.tombstone) {
delete collection[content.key];
} else {
collection[content.key] = Object.assign(collection[content.key] || {}, content);
}
} else {
collection[message.id] = Object.assign(content, {id: message.id});
if (!collection[message.id].editors) {
collection[message.id].editors = [message.author];
}
}
return true;
}
export async function collection(ids, kind, parent, max_rowid, data, include_private) {
print('COLLECTION?');
let whoami = await ssb.getIdentities();
print('WHOAMI', whoami);
data = data ?? {};
let rowid = 0;
let first = true;
print('CHECKING', kind, ids);
await ssb.sqlAsync('SELECT MAX(rowid) AS rowid FROM messages', [], function(row) {
rowid = row.rowid;
});
print('one');
while (true) {
if (rowid == max_rowid) {
await new_message();
await ssb.sqlAsync('SELECT MAX(rowid) AS rowid FROM messages', [], function(row) {
rowid = row.rowid;
});
first = false;
}
print('two');
let modified = false;
let rows = [];
print(include_private ? true: false, ids);
print(JSON.stringify([JSON.stringify(ids), max_rowid ?? -1, rowid, kind, parent, include_private ? true : false]));
await ssb.sqlAsync(`
SELECT messages.id, author, content, timestamp
FROM messages
JOIN json_each(?1) AS id ON messages.author = id.value
WHERE
messages.rowid > ?2 AND
messages.rowid <= ?3 AND
((json_extract(messages.content, '$.type') = ?4 AND
(?5 IS NULL OR json_extract(messages.content, '$.parent') = ?5)) OR
(?6 AND content LIKE '"%'))
ORDER BY timestamp
`, [JSON.stringify(ids), max_rowid ?? -1, rowid, kind, parent, include_private ? true : false], function(row) {
print('three');
rows.push(row);
});
print('done');
max_rowid = rowid;
for (let row of rows) {
if (await process_message(whoami, data, row, kind, parent)) {
modified = true;
}
}
if (first || modified) {
break;
}
}
return [rowid, data];
}