2021-01-02 13:10:00 -05:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const k_posts_max = 20;
|
2021-01-19 21:20:40 -05:00
|
|
|
const k_votes_max = 20;
|
2021-01-02 13:10:00 -05:00
|
|
|
|
|
|
|
async function following(db, id) {
|
|
|
|
var o = await db.get(id + ":following");
|
2021-01-11 21:23:57 -05:00
|
|
|
const k_version = 5;
|
2021-01-02 13:10:00 -05:00
|
|
|
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],
|
2021-01-11 21:23:57 -05:00
|
|
|
function(row) {
|
2021-01-02 13:10:00 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function followers(db, id) {
|
|
|
|
var o = await db.get(id + ":followers");
|
2021-01-11 21:23:57 -05:00
|
|
|
const k_version = 3;
|
2021-01-02 13:10:00 -05:00
|
|
|
var f = o ? JSON.parse(o) : o;
|
|
|
|
if (!f || f.version != k_version) {
|
|
|
|
f = {users: [], rowid: 0, version: k_version};
|
|
|
|
}
|
|
|
|
f.users = new Set(f.users);
|
|
|
|
await ssb.sqlStream(
|
|
|
|
"SELECT "+
|
|
|
|
" rowid, "+
|
|
|
|
" author AS contact, "+
|
|
|
|
" json_extract(content, '$.following') AS following "+
|
|
|
|
"FROM messages "+
|
|
|
|
"WHERE "+
|
|
|
|
" rowid > $1 AND "+
|
|
|
|
" json_extract(content, '$.type') = 'contact' AND "+
|
|
|
|
" json_extract(content, '$.contact') = $2 "+
|
|
|
|
"UNION SELECT MAX(rowid) as rowid, NULL, NULL FROM messages "+
|
|
|
|
"ORDER BY rowid",
|
|
|
|
[f.rowid, id],
|
2021-01-11 21:23:57 -05:00
|
|
|
function(row) {
|
2021-01-02 13:10:00 -05:00
|
|
|
if (row.following) {
|
|
|
|
f.users.add(row.contact);
|
|
|
|
} else {
|
|
|
|
f.users.delete(row.contact);
|
|
|
|
}
|
|
|
|
f.rowid = row.rowid;
|
|
|
|
});
|
|
|
|
f.users = Array.from(f.users);
|
|
|
|
var j = JSON.stringify(f);
|
|
|
|
if (o != j) {
|
|
|
|
await db.set(id + ":followers", j);
|
|
|
|
}
|
|
|
|
return f.users;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function sendUser(db, id) {
|
|
|
|
return Promise.all([
|
|
|
|
following(db, id).then(async function(following) {
|
|
|
|
return app.postMessage({following: {id: id, users: following}});
|
|
|
|
}),
|
|
|
|
followers(db, id).then(async function(followers) {
|
|
|
|
return app.postMessage({followers: {id: id, users: followers}});
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getAbout(db, id) {
|
|
|
|
var o = await db.get(id + ":about");
|
2021-01-02 14:33:58 -05:00
|
|
|
const k_version = 4;
|
2021-01-02 13:10:00 -05:00
|
|
|
var f = o ? JSON.parse(o) : o;
|
|
|
|
if (!f || f.version != k_version) {
|
|
|
|
f = {about: {}, sequence: 0, version: k_version};
|
|
|
|
}
|
|
|
|
await ssb.sqlStream(
|
|
|
|
"SELECT "+
|
|
|
|
" sequence, "+
|
|
|
|
" content "+
|
|
|
|
"FROM messages "+
|
|
|
|
"WHERE "+
|
|
|
|
" sequence > ?1 AND "+
|
|
|
|
" author = ?2 AND "+
|
|
|
|
" json_extract(content, '$.type') = 'about' AND "+
|
|
|
|
" json_extract(content, '$.about') = author "+
|
|
|
|
"UNION SELECT MAX(sequence) as sequence, NULL FROM messages WHERE author = ?2 "+
|
|
|
|
"ORDER BY sequence",
|
|
|
|
[f.sequence, id],
|
2021-01-09 17:19:52 -05:00
|
|
|
function(row) {
|
2021-01-02 13:10:00 -05:00
|
|
|
f.sequence = row.sequence;
|
|
|
|
if (row.content) {
|
|
|
|
var about = {};
|
|
|
|
try {
|
|
|
|
about = JSON.parse(row.content);
|
|
|
|
} catch {
|
|
|
|
}
|
|
|
|
delete about.about;
|
|
|
|
delete about.type;
|
|
|
|
f.about = Object.assign(f.about, about);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
var j = JSON.stringify(f);
|
|
|
|
if (o != j) {
|
|
|
|
await db.set(id + ":about", j);
|
|
|
|
}
|
|
|
|
return f.about;
|
|
|
|
}
|
|
|
|
|
|
|
|
function fnv32a(value)
|
|
|
|
{
|
|
|
|
var result = 0x811c9dc5;
|
|
|
|
for (var i = 0; i < value.length; i++) {
|
|
|
|
result ^= value.charCodeAt(i);
|
|
|
|
result += (result << 1) + (result << 4) + (result << 7) + (result << 8) + (result << 24);
|
|
|
|
}
|
|
|
|
return result >>> 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getRecentPostIds(db, id, ids, limit) {
|
2021-01-09 17:19:52 -05:00
|
|
|
const k_version = 7;
|
2021-01-02 13:10:00 -05:00
|
|
|
var o = await db.get(id + ':recent_posts');
|
|
|
|
var recent = [];
|
|
|
|
var f = o ? JSON.parse(o) : o;
|
|
|
|
var ids_hash = fnv32a(JSON.stringify(ids));
|
|
|
|
if (!f || f.version != k_version || f.ids_hash != ids_hash) {
|
|
|
|
f = {recent: [], rowid: 0, version: k_version, ids_hash: ids_hash};
|
|
|
|
}
|
|
|
|
await ssb.sqlStream(
|
|
|
|
"SELECT "+
|
|
|
|
" rowid, "+
|
|
|
|
" id "+
|
|
|
|
"FROM messages "+
|
|
|
|
"WHERE "+
|
|
|
|
" rowid > ? AND "+
|
|
|
|
" author IN (" + ids.map(x => '?').join(", ") + ") AND "+
|
2021-01-09 17:19:52 -05:00
|
|
|
" json_extract(content, '$.type') IN ('post', 'tildefriends-app') "+
|
2021-01-02 13:10:00 -05:00
|
|
|
"UNION SELECT MAX(rowid) as rowid, NULL FROM messages "+
|
|
|
|
"ORDER BY rowid DESC LIMIT ?",
|
|
|
|
[].concat([f.rowid], ids, [limit + 1]),
|
|
|
|
function(row) {
|
|
|
|
if (row.id) {
|
|
|
|
recent.push(row.id);
|
|
|
|
}
|
|
|
|
if (row.rowid) {
|
|
|
|
f.rowid = row.rowid;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
f.recent = [].concat(recent, f.recent).slice(0, limit);
|
|
|
|
var j = JSON.stringify(f);
|
|
|
|
if (o != j) {
|
|
|
|
await db.set(id + ":recent_posts", j);
|
|
|
|
}
|
|
|
|
return f.recent;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getVotes(db, id) {
|
|
|
|
var o = await db.get(id + ":votes");
|
2021-01-19 21:20:40 -05:00
|
|
|
const k_version = 5;
|
2021-01-02 13:10:00 -05:00
|
|
|
var votes = [];
|
|
|
|
var f = o ? JSON.parse(o) : o;
|
|
|
|
if (!f || f.version != k_version) {
|
|
|
|
f = {votes: [], rowid: 0, version: k_version};
|
|
|
|
}
|
|
|
|
await ssb.sqlStream(
|
|
|
|
"SELECT "+
|
|
|
|
" rowid, "+
|
|
|
|
" author, "+
|
|
|
|
" id, "+
|
|
|
|
" sequence, "+
|
|
|
|
" timestamp, "+
|
|
|
|
" content "+
|
|
|
|
"FROM messages "+
|
|
|
|
"WHERE "+
|
|
|
|
" rowid > ? AND "+
|
|
|
|
" author = ? AND "+
|
|
|
|
" json_extract(content, '$.type') = 'vote' "+
|
|
|
|
"UNION SELECT MAX(rowid) as rowid, NULL, NULL AS id, NULL, NULL, NULL FROM messages "+
|
|
|
|
"ORDER BY rowid DESC LIMIT ?",
|
|
|
|
[f.rowid, id, k_votes_max],
|
2021-01-11 21:23:57 -05:00
|
|
|
function(row) {
|
2021-01-02 13:10:00 -05:00
|
|
|
if (row.id) {
|
|
|
|
votes.push(row);
|
|
|
|
} else {
|
|
|
|
f.rowid = row.rowid;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
f.votes = [].concat(votes.reverse(), f.votes).slice(0, k_votes_max);
|
|
|
|
var j = JSON.stringify(f);
|
|
|
|
if (o != j) {
|
|
|
|
await db.set(id + ":votes", j);
|
|
|
|
}
|
|
|
|
return f.votes;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getPosts(db, ids) {
|
|
|
|
var posts = [];
|
|
|
|
if (ids.length) {
|
|
|
|
await ssb.sqlStream(
|
2021-01-02 14:33:58 -05:00
|
|
|
"SELECT rowid, * FROM messages WHERE id IN (" + ids.map(x => "?").join(", ") + ")",
|
2021-01-02 13:10:00 -05:00
|
|
|
ids,
|
2021-01-19 21:20:40 -05:00
|
|
|
row => posts.push(row));
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
return posts;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function ready() {
|
2021-01-02 13:35:12 -05:00
|
|
|
return refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
core.register('onBroadcastsChanged', async function() {
|
|
|
|
await app.postMessage({broadcasts: await ssb.getBroadcasts()});
|
|
|
|
});
|
|
|
|
|
|
|
|
core.register('onConnectionsChanged', async function() {
|
|
|
|
var connections = await ssb.connections();
|
|
|
|
await app.postMessage({connections: connections});
|
|
|
|
});
|
|
|
|
|
|
|
|
async function refresh() {
|
|
|
|
await app.postMessage({clear: true});
|
2021-01-02 13:10:00 -05:00
|
|
|
var whoami = await ssb.whoami();
|
|
|
|
var db = await database("ssb");
|
|
|
|
await Promise.all([
|
|
|
|
app.postMessage({whoami: whoami}),
|
|
|
|
app.postMessage({broadcasts: await ssb.getBroadcasts()}),
|
|
|
|
app.postMessage({connections: await ssb.connections()}),
|
2021-01-09 17:19:52 -05:00
|
|
|
app.postMessage({apps: await core.apps()}),
|
2021-01-02 13:10:00 -05:00
|
|
|
followingDeep(db, [whoami], 2).then(function(f) {
|
|
|
|
getRecentPostIds(db, whoami, [].concat([whoami], f), k_posts_max).then(async function(ids) {
|
|
|
|
return getPosts(db, ids);
|
|
|
|
}).then(async function(posts) {
|
|
|
|
var roots = posts.map(function(x) {
|
|
|
|
try {
|
|
|
|
return JSON.parse(x.content).root;
|
|
|
|
} catch {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
roots = roots.filter(function(root) {
|
|
|
|
return root && posts.every(post => post.id != root);
|
|
|
|
});
|
|
|
|
return [].concat(posts, await getPosts(db, roots));
|
2021-01-19 21:20:40 -05:00
|
|
|
}).then(function(posts) {
|
|
|
|
return Promise.all(posts.map(x => app.postMessage({message: x})));
|
2021-01-02 13:10:00 -05:00
|
|
|
});
|
2021-01-19 21:20:40 -05:00
|
|
|
Promise.all(f.map(function(id) {
|
|
|
|
return Promise.all([
|
|
|
|
getVotes(db, id).then(function(votes) {
|
|
|
|
return app.postMessage({votes: votes});
|
2021-01-02 13:10:00 -05:00
|
|
|
}),
|
2021-01-19 21:20:40 -05:00
|
|
|
getAbout(db, id).then(function(user) {
|
2021-01-02 13:10:00 -05:00
|
|
|
return app.postMessage({user: {user: id, about: user}});
|
|
|
|
}),
|
|
|
|
]);
|
2021-01-19 21:20:40 -05:00
|
|
|
}));
|
2021-01-02 13:10:00 -05:00
|
|
|
}),
|
|
|
|
sendUser(db, whoami),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
core.register('message', async function(m) {
|
|
|
|
if (m.message == 'ready') {
|
|
|
|
await ready();
|
|
|
|
} else if (m.message) {
|
|
|
|
if (m.message.connect) {
|
|
|
|
await ssb.connect(m.message.connect);
|
|
|
|
} else if (m.message.post) {
|
|
|
|
await ssb.post(m.message.post);
|
|
|
|
} else if (m.message.appendMessage) {
|
|
|
|
await ssb.appendMessage(m.message.appendMessage);
|
2021-01-09 17:19:52 -05:00
|
|
|
} else if (m.message.share_app) {
|
|
|
|
var app = await ssb.blobGet(m.message.share_app.app);
|
|
|
|
app = JSON.parse(utf8Decode(app));
|
|
|
|
app.type = 'tildefriends-app';
|
|
|
|
app.name = m.message.share_app.name;
|
2021-01-11 21:23:57 -05:00
|
|
|
app.text = m.message.share_app.text;
|
2021-01-09 17:19:52 -05:00
|
|
|
await ssb.appendMessage(app);
|
2021-01-02 13:10:00 -05:00
|
|
|
} else if (m.message.user) {
|
|
|
|
await sendUser(await database("ssb"), m.message.user);
|
|
|
|
} else if (m.message.refresh) {
|
|
|
|
await refresh();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
print(JSON.stringify(m));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
async function main() {
|
2021-01-03 15:31:00 -05:00
|
|
|
if (core.user &&
|
|
|
|
core.user.credentials &&
|
|
|
|
core.user.credentials.permissions &&
|
|
|
|
core.user.credentials.permissions.administration) {
|
|
|
|
await app.setDocument(utf8Decode(await getFile("index.html")));
|
|
|
|
} else {
|
|
|
|
await app.setDocument('<div style="color: #f00">Only the administrator can use this app at this time. Login at the top right.</div>');
|
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
main();
|