2021-12-28 10:25:04 -05:00
|
|
|
"use strict";
|
2021-09-06 13:50:38 -04:00
|
|
|
var g_wants_requests = {};
|
2021-11-03 19:23:20 -04:00
|
|
|
var g_database = new Database('core');
|
2021-12-27 14:52:42 -05:00
|
|
|
const k_use_create_history_stream = false;
|
2021-12-28 10:25:04 -05:00
|
|
|
const k_blobs_concurrent_target = 8;
|
2021-09-06 13:50:38 -04:00
|
|
|
|
2021-12-27 14:52:42 -05:00
|
|
|
function following(db, id) {
|
|
|
|
var o = db.get(id + ":following");
|
2021-09-08 20:15:57 -04:00
|
|
|
const k_version = 5;
|
|
|
|
var f = o ? JSON.parse(o) : o;
|
|
|
|
if (!f || f.version != k_version) {
|
|
|
|
f = {users: [], sequence: 0, version: k_version};
|
|
|
|
}
|
|
|
|
f.users = new Set(f.users);
|
2021-12-27 14:52:42 -05:00
|
|
|
ssb.sqlStream(
|
2021-09-08 20:15:57 -04:00
|
|
|
"SELECT "+
|
|
|
|
" sequence, "+
|
|
|
|
" json_extract(content, '$.contact') AS contact, "+
|
|
|
|
" json_extract(content, '$.following') AS following "+
|
|
|
|
"FROM messages "+
|
|
|
|
"WHERE "+
|
|
|
|
" author = ?1 AND "+
|
|
|
|
" sequence > ?2 AND "+
|
|
|
|
" json_extract(content, '$.type') = 'contact' "+
|
|
|
|
"UNION SELECT MAX(sequence) AS sequence, NULL, NULL FROM messages WHERE author = ?1 "+
|
|
|
|
"ORDER BY sequence",
|
|
|
|
[id, f.sequence],
|
|
|
|
function(row) {
|
|
|
|
if (row.following) {
|
|
|
|
f.users.add(row.contact);
|
|
|
|
} else {
|
|
|
|
f.users.delete(row.contact);
|
|
|
|
}
|
|
|
|
f.sequence = row.sequence;
|
|
|
|
});
|
|
|
|
f.users = Array.from(f.users);
|
|
|
|
var j = JSON.stringify(f);
|
|
|
|
if (o != j) {
|
2021-12-27 14:52:42 -05:00
|
|
|
db.set(id + ":following", j);
|
2021-09-08 20:15:57 -04:00
|
|
|
}
|
|
|
|
return f.users;
|
|
|
|
}
|
|
|
|
|
2021-12-27 14:52:42 -05:00
|
|
|
function followingDeep(db, seed_ids, depth) {
|
2021-09-08 20:15:57 -04:00
|
|
|
if (depth <= 0) {
|
|
|
|
return seed_ids;
|
|
|
|
}
|
2021-12-27 14:52:42 -05:00
|
|
|
var f = seed_ids.map(x => following(db, x));
|
2021-09-08 20:15:57 -04:00
|
|
|
var ids = [].concat(...f);
|
2021-12-27 14:52:42 -05:00
|
|
|
var x = followingDeep(db, [...new Set(ids)].sort(), depth - 1);
|
2021-11-05 22:10:13 -04:00
|
|
|
x = [...new Set([].concat(...x, ...seed_ids))].sort();
|
2021-09-08 20:15:57 -04:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2021-10-31 15:39:16 -04:00
|
|
|
function get_latest_sequence_for_author(author) {
|
2021-10-30 17:07:01 -04:00
|
|
|
var sequence = 0;
|
2021-10-31 15:39:16 -04:00
|
|
|
ssb.sqlStream(
|
2021-10-30 17:07:01 -04:00
|
|
|
'SELECT MAX(sequence) AS sequence FROM messages WHERE author = ?1',
|
|
|
|
[author],
|
|
|
|
function(row) {
|
2021-10-31 15:39:16 -04:00
|
|
|
if (row.sequence) {
|
2021-12-27 14:52:42 -05:00
|
|
|
sequence = row.sequence;
|
2021-10-31 15:39:16 -04:00
|
|
|
}
|
2021-10-30 17:07:01 -04:00
|
|
|
});
|
|
|
|
return sequence;
|
|
|
|
}
|
|
|
|
|
2021-12-22 14:57:34 -05:00
|
|
|
function storeMessage(message) {
|
|
|
|
var payload = message.message.value ? message.message.value : message.message;
|
|
|
|
if (typeof(payload) == 'object') {
|
|
|
|
ssb.storeMessage(payload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 17:28:58 -05:00
|
|
|
ssb.addEventListener('connections', function(change, connection) {
|
2021-09-06 13:50:38 -04:00
|
|
|
if (change == 'add') {
|
2021-10-31 15:39:16 -04:00
|
|
|
var sequence = get_latest_sequence_for_author(connection.id);
|
2021-12-27 14:52:42 -05:00
|
|
|
if (k_use_create_history_stream) {
|
|
|
|
connection.send_json({'name': ['createHistoryStream'], 'type': 'source', 'args': [{'id': connection.id, 'seq': sequence, 'live': true, 'keys': false}]}, storeMessage);
|
|
|
|
var me = ssb.whoami();
|
|
|
|
followingDeep(g_database, [me], 2).then(function(ids) {
|
|
|
|
for (let id of ids) {
|
|
|
|
if (id == me) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
var sequence = get_latest_sequence_for_author(id);
|
|
|
|
connection.send_json({'name': ['createHistoryStream'], 'type': 'source', 'args': [{'id': id, 'seq': sequence, 'live': true, 'keys': false}]}, storeMessage);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
if (connection.is_client) {
|
|
|
|
connection.send_json({"name": ["ebt", "replicate"], "args": [{"version": 3, "format": "classic"}], "type": "duplex"}, ebtReplicateClient);
|
|
|
|
}
|
|
|
|
}
|
2021-12-28 10:25:04 -05:00
|
|
|
|
|
|
|
connection.active_blob_wants = {};
|
2021-09-06 13:50:38 -04:00
|
|
|
connection.send_json({'name': ['blobs', 'createWants'], 'type': 'source', 'args': []}, function(message) {
|
|
|
|
Object.keys(message.message).forEach(function(id) {
|
|
|
|
if (message.message[id] < 0) {
|
2021-12-28 10:25:04 -05:00
|
|
|
delete connection.active_blob_wants[id];
|
2021-09-06 13:50:38 -04:00
|
|
|
var blob = ssb.blobGet(id);
|
|
|
|
if (blob) {
|
|
|
|
var out_message = {};
|
|
|
|
out_message[id] = blob.byteLength;
|
|
|
|
g_wants_requests[connection.id].send_json(out_message);
|
|
|
|
}
|
|
|
|
} else {
|
2021-10-31 17:15:18 -04:00
|
|
|
var received_bytes = 0;
|
|
|
|
var expected_bytes = message.message[id];
|
|
|
|
var buffer = new Uint8Array(expected_bytes);
|
2021-10-30 17:07:01 -04:00
|
|
|
connection.send_json({'name': ['blobs', 'get'], 'type': 'source', 'args': [id]}, function(message) {
|
2021-12-28 10:25:04 -05:00
|
|
|
if (message.flags & 0x4 /* end */) {
|
|
|
|
delete connection.active_blob_wants[id];
|
|
|
|
} else {
|
|
|
|
buffer.set(new Uint8Array(message.message, 0, message.message.byteLength), received_bytes);
|
|
|
|
received_bytes += message.message.byteLength;
|
|
|
|
if (received_bytes == expected_bytes) {
|
|
|
|
ssb.blobStore(buffer);
|
|
|
|
}
|
2021-10-31 17:15:18 -04:00
|
|
|
}
|
2021-09-06 13:50:38 -04:00
|
|
|
});
|
2021-12-28 10:25:04 -05:00
|
|
|
if (Object.keys(connection.active_blob_wants).length < k_blobs_concurrent_target) {
|
|
|
|
requestMoreBlobs(g_wants_requests[connection.id]);
|
|
|
|
}
|
2021-09-06 13:50:38 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else if (change == 'remove') {
|
2021-11-03 18:15:46 -04:00
|
|
|
print('REMOVE', connection.id);
|
2021-09-06 13:50:38 -04:00
|
|
|
delete g_wants_requests[connection.id];
|
|
|
|
} else {
|
2021-11-03 18:15:46 -04:00
|
|
|
print('CHANGE', change);
|
2021-09-06 13:50:38 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-12-28 10:25:04 -05:00
|
|
|
function blob_want_discovered(request, id) {
|
|
|
|
if (Object.keys(request.connection.active_blob_wants).length > k_blobs_concurrent_target) {
|
|
|
|
return;
|
2021-09-06 13:50:38 -04:00
|
|
|
}
|
2021-12-28 10:25:04 -05:00
|
|
|
var message = {};
|
|
|
|
message[id] = -1;
|
|
|
|
request.send_json(message);
|
|
|
|
request.connection.active_blob_wants[id] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function requestMoreBlobs(request) {
|
2021-09-06 13:50:38 -04:00
|
|
|
ssb.sqlStream(
|
2021-12-28 10:25:04 -05:00
|
|
|
'SELECT id FROM blob_wants LIMIT ' + k_blobs_concurrent_target,
|
2021-09-06 13:50:38 -04:00
|
|
|
[],
|
2021-12-28 10:25:04 -05:00
|
|
|
row => blob_want_discovered(request, row.id));
|
|
|
|
}
|
|
|
|
|
|
|
|
ssb.addRpc(['blobs', 'createWants'], function(request) {
|
|
|
|
g_wants_requests[request.connection.id] = request;
|
|
|
|
ssb.addEventListener('blob_want_added', id => blob_want_discovered(request, id));
|
|
|
|
requestMoreBlobs(request);
|
2021-09-06 13:50:38 -04:00
|
|
|
});
|
|
|
|
|
2021-11-07 17:28:58 -05:00
|
|
|
ssb.addRpc(['blobs', 'has'], function(request) {
|
2021-09-06 13:50:38 -04:00
|
|
|
var found = false;
|
|
|
|
ssb.sqlStream(
|
|
|
|
'SELECT 1 FROM blobs where id = ?1',
|
|
|
|
[request.args[0]],
|
|
|
|
function(row) {
|
|
|
|
found = true;
|
|
|
|
});
|
|
|
|
request.send_json(found);
|
|
|
|
});
|
|
|
|
|
2021-11-07 17:28:58 -05:00
|
|
|
ssb.addRpc(['blobs', 'get'], function(request) {
|
2021-12-28 16:48:03 -05:00
|
|
|
for (let arg of request.args) {
|
|
|
|
var blob;
|
|
|
|
if (arg.key) {
|
|
|
|
blob = ssb.blobGet(arg.key);
|
|
|
|
} else {
|
|
|
|
blob = ssb.blobGet(arg);
|
|
|
|
}
|
|
|
|
const k_send_max = 8192;
|
|
|
|
if (blob.byteLength > k_send_max) {
|
|
|
|
for (var i = 0; i < blob.byteLength; i += k_send_max) {
|
|
|
|
var buffer = new Uint8Array(blob, i, Math.min(blob.byteLength - i, k_send_max));
|
|
|
|
request.send_binary(buffer);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
request.send_binary(blob);
|
|
|
|
}
|
|
|
|
request.send_json_end(true);
|
2021-10-31 15:39:16 -04:00
|
|
|
}
|
2021-12-28 16:48:03 -05:00
|
|
|
request.more(function(request) {});
|
2021-09-06 13:50:38 -04:00
|
|
|
});
|
|
|
|
|
2021-12-22 14:57:34 -05:00
|
|
|
ssb.addRpc(['gossip', 'ping'], function(request) {
|
|
|
|
request.more(function(message) {
|
2021-12-27 14:52:42 -05:00
|
|
|
message.send_json(Date.now());
|
2021-12-22 14:57:34 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-12-27 14:52:42 -05:00
|
|
|
ssb.addRpc(['tunnel', 'isRoom'], function(request) {
|
|
|
|
request.send_json(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
function ebtReplicateSendClock(request, have) {
|
|
|
|
var me = ssb.whoami();
|
|
|
|
var message = {};
|
2022-01-22 15:13:14 -05:00
|
|
|
var last_sent = request.connection.sent_clock || {};
|
2021-12-28 10:25:04 -05:00
|
|
|
var ids = followingDeep(g_database, [me], 2).concat([request.connection.id]);
|
2022-01-22 15:47:10 -05:00
|
|
|
if (!Object.keys(last_sent).length) {
|
2022-01-22 15:13:14 -05:00
|
|
|
for (let id of ids) {
|
|
|
|
message[id] = get_latest_sequence_for_author(id);
|
|
|
|
}
|
2021-12-27 14:52:42 -05:00
|
|
|
}
|
2021-12-28 10:25:04 -05:00
|
|
|
for (let id of Object.keys(have)) {
|
|
|
|
if (message[id] === undefined) {
|
|
|
|
var sequence = get_latest_sequence_for_author(id);
|
|
|
|
message[id] = sequence ? sequence : -1;
|
|
|
|
}
|
|
|
|
}
|
2021-12-27 14:52:42 -05:00
|
|
|
|
|
|
|
var to_send = {}
|
|
|
|
for (let id of ids) {
|
|
|
|
if (last_sent[id] === undefined || message[id] > last_sent[id]) {
|
|
|
|
last_sent[id] = to_send[id] = message[id] === -1 ? -1 : message[id] << 1;
|
|
|
|
}
|
2021-12-28 10:25:04 -05:00
|
|
|
if (Object.keys(to_send).length >= 32) {
|
|
|
|
request.send_json(to_send);
|
|
|
|
to_send = {};
|
|
|
|
}
|
2021-12-27 14:52:42 -05:00
|
|
|
}
|
|
|
|
request.connection.sent_clock = last_sent;
|
|
|
|
|
|
|
|
if (Object.keys(to_send).length) {
|
|
|
|
request.send_json(to_send);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatMessage(row) {
|
|
|
|
var message = {
|
|
|
|
previous: row.previous,
|
|
|
|
author: row.author,
|
|
|
|
sequence: row.sequence,
|
|
|
|
timestamp: row.timestamp,
|
|
|
|
hash: row.hash,
|
|
|
|
content: JSON.parse(row.content),
|
|
|
|
signature: row.signature,
|
|
|
|
};
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
|
|
|
function ebtReplicateRegisterMessageCallback(request) {
|
|
|
|
var me = ssb.whoami();
|
|
|
|
ssb.addEventListener('message', function(message_id) {
|
|
|
|
ssb.sqlStream(
|
|
|
|
'SELECT previous, author, id, sequence, timestamp, hash, content, signature FROM messages WHERE id = ?1 AND author = ?2',
|
|
|
|
[message_id, me],
|
|
|
|
function (row) {
|
|
|
|
request.send_json(formatMessage(row));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function ebtReplicateCommon(request) {
|
|
|
|
var me = ssb.whoami();
|
|
|
|
if (request.message.author) {
|
|
|
|
storeMessage(request);
|
|
|
|
} else {
|
|
|
|
ebtReplicateSendClock(request, request.message);
|
|
|
|
|
|
|
|
for (let id of Object.keys(request.message)) {
|
|
|
|
if (request.message[id] >= 0 && (request.message[id] & 1) == 0) {
|
|
|
|
ssb.sqlStream(
|
|
|
|
'SELECT previous, author, id, sequence, timestamp, hash, content, signature FROM messages WHERE author = ?1 AND sequence >= ?2 ORDER BY sequence',
|
|
|
|
[id, request.message[id] >> 1],
|
|
|
|
function (row) {
|
|
|
|
request.send_json(formatMessage(row));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function ebtReplicateClient(request) {
|
|
|
|
if (!request.connection.message_registered) {
|
|
|
|
ebtReplicateRegisterMessageCallback(request);
|
|
|
|
request.connection.message_registered = true;
|
|
|
|
}
|
|
|
|
ebtReplicateCommon(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
function ebtReplicateServer(request) {
|
|
|
|
ebtReplicateRegisterMessageCallback(request);
|
|
|
|
ebtReplicateSendClock(request, {});
|
|
|
|
request.more(ebtReplicateCommon);
|
|
|
|
}
|
|
|
|
|
|
|
|
ssb.addRpc(['ebt', 'replicate'], ebtReplicateServer);
|
|
|
|
|
2021-11-07 17:28:58 -05:00
|
|
|
ssb.addRpc(['createHistoryStream'], function(request) {
|
2021-09-06 13:50:38 -04:00
|
|
|
var id = request.args[0].id;
|
|
|
|
var seq = request.args[0].seq;
|
2021-11-05 22:10:13 -04:00
|
|
|
var keys = request.args[0].keys || request.args[0].keys === undefined;
|
2021-11-10 19:05:07 -05:00
|
|
|
function sendMessage(row) {
|
|
|
|
if (keys) {
|
|
|
|
var message = {
|
|
|
|
key: row.id,
|
|
|
|
value: {
|
2021-10-27 22:19:57 -04:00
|
|
|
previous: row.previous,
|
2021-11-10 19:05:07 -05:00
|
|
|
author: row.author,
|
2021-10-27 22:19:57 -04:00
|
|
|
sequence: row.sequence,
|
|
|
|
timestamp: row.timestamp,
|
|
|
|
hash: row.hash,
|
|
|
|
content: JSON.parse(row.content),
|
|
|
|
signature: row.signature,
|
2021-11-10 19:05:07 -05:00
|
|
|
},
|
|
|
|
timestamp: row.timestamp,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
var message = {
|
|
|
|
previous: row.previous,
|
|
|
|
author: row.author,
|
|
|
|
sequence: row.sequence,
|
|
|
|
timestamp: row.timestamp,
|
|
|
|
hash: row.hash,
|
|
|
|
content: JSON.parse(row.content),
|
|
|
|
signature: row.signature,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
request.send_json(message);
|
|
|
|
}
|
|
|
|
ssb.sqlStream(
|
|
|
|
'SELECT previous, author, id, sequence, timestamp, hash, content, signature FROM messages WHERE author = ?1 AND sequence >= ?2 ORDER BY sequence',
|
|
|
|
[id, seq ?? 0],
|
|
|
|
sendMessage);
|
2021-12-20 12:00:25 -05:00
|
|
|
ssb.addEventListener('message', function(message_id) {
|
2021-11-10 19:05:07 -05:00
|
|
|
ssb.sqlStream(
|
2021-12-27 14:52:42 -05:00
|
|
|
'SELECT previous, author, id, sequence, timestamp, hash, content, signature FROM messages WHERE id = ?1 AND author = ?2',
|
|
|
|
[message_id, id],
|
2021-12-20 12:00:25 -05:00
|
|
|
function (row) {
|
2021-12-27 14:52:42 -05:00
|
|
|
sendMessage(row);
|
2021-12-20 12:00:25 -05:00
|
|
|
});
|
2021-11-10 19:05:07 -05:00
|
|
|
});
|
2021-09-06 13:50:38 -04:00
|
|
|
});
|