I am too good at breaking syncing.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3684 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2021-11-06 02:10:13 +00:00
parent 308e24c9fa
commit 68cf3efcde
2 changed files with 26 additions and 15 deletions

View File

@ -45,7 +45,7 @@ async function followingDeep(db, seed_ids, depth) {
var f = await Promise.all(seed_ids.map(x => following(db, x))); var f = await Promise.all(seed_ids.map(x => following(db, x)));
var ids = [].concat(...f); var ids = [].concat(...f);
var x = await followingDeep(db, [...new Set(ids)].sort(), depth - 1); var x = await followingDeep(db, [...new Set(ids)].sort(), depth - 1);
x = [].concat(...x, ...seed_ids); x = [...new Set([].concat(...x, ...seed_ids))].sort();
return x; return x;
} }
@ -65,8 +65,8 @@ function get_latest_sequence_for_author(author) {
ssb.registerConnectionsChanged(function(change, connection) { ssb.registerConnectionsChanged(function(change, connection) {
if (change == 'add') { if (change == 'add') {
var sequence = get_latest_sequence_for_author(connection.id); var sequence = get_latest_sequence_for_author(connection.id);
connection.send_json({'name': ['createHistoryStream'], 'type': 'source', 'args': [{'id': connection.id, 'seq': sequence, 'live': true}]}, function(message) { connection.send_json({'name': ['createHistoryStream'], 'type': 'source', 'args': [{'id': connection.id, 'seq': sequence, 'live': true, keys: false}]}, function(message) {
ssb.storeMessage(message.message.value); ssb.storeMessage(message.message.value ? message.message.value : message.message);
}); });
connection.send_json({'name': ['blobs', 'createWants'], 'type': 'source', 'args': []}, function(message) { connection.send_json({'name': ['blobs', 'createWants'], 'type': 'source', 'args': []}, function(message) {
Object.keys(message.message).forEach(function(id) { Object.keys(message.message).forEach(function(id) {
@ -95,8 +95,8 @@ ssb.registerConnectionsChanged(function(change, connection) {
followingDeep(g_database, [ssb.whoami()], 2).then(function(ids) { followingDeep(g_database, [ssb.whoami()], 2).then(function(ids) {
for (let id of ids) { for (let id of ids) {
var sequence = get_latest_sequence_for_author(id); var sequence = get_latest_sequence_for_author(id);
connection.send_json({'name': ['createHistoryStream'], 'type': 'source', 'args': [{'id': id, 'seq': sequence}]}, function(message) { connection.send_json({'name': ['createHistoryStream'], 'type': 'source', 'args': [{'id': id, 'seq': sequence, 'live': true, 'keys': false}]}, function(message) {
ssb.storeMessage(message.message.value); ssb.storeMessage(message.message.value ? message.message.value : message.message);
}); });
} }
}); });
@ -143,13 +143,27 @@ ssb.registerRpc(['blobs', 'get'], function(request) {
ssb.registerRpc(['createHistoryStream'], function(request) { ssb.registerRpc(['createHistoryStream'], function(request) {
var id = request.args[0].id; var id = request.args[0].id;
var seq = request.args[0].seq; var seq = request.args[0].seq;
var keys = request.args[0].keys || request.args[0].keys === undefined;
ssb.sqlStream( ssb.sqlStream(
'SELECT previous, id, sequence, timestamp, hash, content, signature FROM messages WHERE author = ?1 AND sequence >= ?2 ORDER BY sequence', 'SELECT previous, id, sequence, timestamp, hash, content, signature FROM messages WHERE author = ?1 AND sequence >= ?2 ORDER BY sequence',
[id, seq ?? 0], [id, seq ?? 0],
function(row) { function(row) {
var message = { if (keys) {
key: row.id + '.' + row.hash, var message = {
value: { key: row.id,
value: {
previous: row.previous,
author: id,
sequence: row.sequence,
timestamp: row.timestamp,
hash: row.hash,
content: JSON.parse(row.content),
signature: row.signature,
},
timestamp: row.timestamp,
};
} else {
var message = {
previous: row.previous, previous: row.previous,
author: id, author: id,
sequence: row.sequence, sequence: row.sequence,
@ -157,9 +171,8 @@ ssb.registerRpc(['createHistoryStream'], function(request) {
hash: row.hash, hash: row.hash,
content: JSON.parse(row.content), content: JSON.parse(row.content),
signature: row.signature, signature: row.signature,
}, };
timestamp: row.timestamp, }
};
request.send_json(message); request.send_json(message);
}); });
}); });

View File

@ -484,7 +484,7 @@ bool tf_ssb_verify_and_strip_signature(JSContext* context, JSValue val, char* ou
verified = r == 0; verified = r == 0;
if (!verified) if (!verified)
{ {
printf("crypto_sign_verify_detached fail\n"); printf("crypto_sign_verify_detached fail (r=%d)\n", r);
} }
} }
else else
@ -1115,12 +1115,10 @@ void tf_ssb_append_message(tf_ssb_t* ssb, JSValue message)
len = 0; len = 0;
json = JS_ToCStringLen(context, &len, jsonval); json = JS_ToCStringLen(context, &len, jsonval);
printf("appending message %.*s\n", (int)len, json);
JS_FreeCString(context, json); JS_FreeCString(context, json);
JS_FreeValue(context, jsonval); JS_FreeValue(context, jsonval);
char id[sodium_base64_ENCODED_LEN(crypto_hash_sha256_BYTES, sodium_base64_VARIANT_ORIGINAL) + 1]; char id[sodium_base64_ENCODED_LEN(crypto_hash_sha256_BYTES, sodium_base64_VARIANT_ORIGINAL) + 7 + 1];
tf_ssb_calculate_message_id(ssb->context, root, id, sizeof(id)); tf_ssb_calculate_message_id(ssb->context, root, id, sizeof(id));
if (valid && if (valid &&
!tf_ssb_db_store_message(ssb, ssb->context, id, root, signature_base64)) !tf_ssb_db_store_message(ssb, ssb->context, id, root, signature_base64))