forked from cory/tildefriends
Work in progress wiki simplification, I hope.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4603 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
102
apps/wiki/app.js
102
apps/wiki/app.js
@ -1,6 +1,7 @@
|
||||
import * as tfrpc from '/tfrpc.js';
|
||||
|
||||
let g_hash;
|
||||
let g_collection_notifies = {};
|
||||
|
||||
tfrpc.register(async function getOwnerIdentities() {
|
||||
return ssb.getOwnerIdentities();
|
||||
@ -45,10 +46,21 @@ tfrpc.register(async function get_blob(id) {
|
||||
return utf8Decode(await ssb.blobGet(id));
|
||||
});
|
||||
|
||||
ssb.addEventListener('message', async function(id) {
|
||||
let message;
|
||||
await ssb.sqlAsync('SELECT * FROM messages WHERE id = ?', [id], function(row) { message = row; });
|
||||
await tfrpc.rpc.notify_new_message(message);
|
||||
let g_new_message_resolve;
|
||||
let g_new_message_promise = new Promise(function(resolve, reject) {
|
||||
g_new_message_resolve = resolve;
|
||||
});
|
||||
|
||||
function new_message() {
|
||||
return g_new_message_promise;
|
||||
}
|
||||
|
||||
ssb.addEventListener('message', function(id) {
|
||||
let resolve = g_new_message_resolve();
|
||||
g_new_message_promise = new Promise(function(resolve, reject) {
|
||||
g_new_message_resolve = resolve;
|
||||
});
|
||||
resolve();
|
||||
});
|
||||
|
||||
core.register('message', async function message_handler(message) {
|
||||
@ -76,22 +88,78 @@ tfrpc.register(async function encrypt(id, recipients, content) {
|
||||
return await ssb.privateMessageEncrypt(id, recipients, content);
|
||||
});
|
||||
|
||||
async function get_collections(kind) {
|
||||
let me = await ssb.getIdentities();
|
||||
let them = await ssb.following(me, 2);
|
||||
let collections = {};
|
||||
print('querying');
|
||||
for (let row of await query(`
|
||||
SELECT author, content, timestamp
|
||||
FROM messages JOIN json_each(?) AS id ON messages.author = id.value
|
||||
WHERE json_extract(content, '$.type') = 'collection' AND json_extract(content, '$.kind') = ?
|
||||
`, [JSON.stringify(them), kind])) {
|
||||
print(row);
|
||||
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 tfrpc.rpc.try_decrypt(whoami, content);
|
||||
if (x) {
|
||||
content = JSON.parse(x);
|
||||
content.draft = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!x) {
|
||||
return;
|
||||
}
|
||||
if (content.type !== type ||
|
||||
(parent && content.parent !== parent)) {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
content.draft = false;
|
||||
}
|
||||
print('done');
|
||||
return collections;
|
||||
if (content?.key) {
|
||||
if (content?.tombstone) {
|
||||
delete collection[content.key];
|
||||
} else if (collection[content.key]) {
|
||||
collection[content.key] = Object.assign(collection[content.key], content);
|
||||
}
|
||||
} else {
|
||||
collection[message.id] = Object.assign(content, {id: message.id});
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
tfrpc.register(async function collection(ids, kind, parent, max_rowid, data) {
|
||||
let whoami = await ssb.getIdentities();
|
||||
data = data ?? {};
|
||||
let rowid = 0;
|
||||
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;
|
||||
});
|
||||
}
|
||||
|
||||
let modified = 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
|
||||
content LIKE '"%')
|
||||
`, [JSON.stringify(ids), max_rowid ?? -1, rowid, kind, parent], function(row) {
|
||||
if (process_message(whoami, data, row, kind, parent)) {
|
||||
modified = true;
|
||||
}
|
||||
});
|
||||
if (modified) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return [rowid, data];
|
||||
});
|
||||
|
||||
async function main() {
|
||||
await app.setDocument(utf8Decode(await getFile('index.html')));
|
||||
}
|
||||
|
Reference in New Issue
Block a user