forked from cory/tildefriends
Cory McWilliams
461e7b7d5a
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4634 ed5197a5-7fde-0310-b194-c3ffbd925b24
95 lines
2.5 KiB
JavaScript
95 lines
2.5 KiB
JavaScript
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];
|
|
} |