forked from cory/tildefriends
Moving a little bit more to this test.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3661 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
62
core/ssb.js
62
core/ssb.js
@ -1,5 +1,67 @@
|
||||
var g_wants_requests = {};
|
||||
|
||||
async function following(db, id) {
|
||||
var o = await db.get(id + ":following");
|
||||
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);
|
||||
await ssb.sqlStream(
|
||||
"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) {
|
||||
await db.set(id + ":following", j);
|
||||
}
|
||||
return f.users;
|
||||
}
|
||||
|
||||
async function followingDeep(db, seed_ids, depth) {
|
||||
if (depth <= 0) {
|
||||
return seed_ids;
|
||||
}
|
||||
var f = await Promise.all(seed_ids.map(x => following(db, x)));
|
||||
var ids = [].concat(...f);
|
||||
var x = await followingDeep(db, [...new Set(ids)].sort(), depth - 1);
|
||||
x = [].concat(...x, ...seed_ids);
|
||||
return x;
|
||||
}
|
||||
|
||||
var g_database = new Database('core');
|
||||
|
||||
async function test_following() {
|
||||
try {
|
||||
debug_print("I AM", await ssb.whoami());
|
||||
var result = await followingDeep(g_database, [await ssb.whoami()], 1);
|
||||
debug_print("following ", JSON.stringify(result));
|
||||
} catch (e) {
|
||||
debug_print("DOH", e, e.stack);
|
||||
}
|
||||
}
|
||||
|
||||
test_following();
|
||||
|
||||
ssb.registerConnectionsChanged(function(change, connection) {
|
||||
if (change == 'add') {
|
||||
connection.send_json({'name': ['createHistoryStream'], 'type': 'source', 'args': [{'id': connection.id, 'seq': 0}]}, function(message) {
|
||||
|
Reference in New Issue
Block a user