import * as tfrpc from '/tfrpc.js'; import * as strava from './strava.js'; let g_database; let g_shared_database; tfrpc.register(async function createIdentity() { return ssb.createIdentity(); }); tfrpc.register(async function appendMessage(id, message) { print('APPEND', JSON.stringify(message)); return ssb.appendMessageWithIdentity(id, message); }); tfrpc.register(function url() { return core.url; }); tfrpc.register(async function getUser() { return core.user; }); tfrpc.register(function getIdentities() { return ssb.getIdentities(); }); tfrpc.register(async function databaseGet(key) { return g_database ? g_database.get(key) : undefined; }); tfrpc.register(async function databaseSet(key, value) { return g_database ? g_database.set(key, value) : undefined; }); tfrpc.register(async function databaseRemove(key, value) { return g_database ? g_database.remove(key, value) : undefined; }); tfrpc.register(async function sharedDatabaseGet(key) { return g_shared_database ? g_shared_database.get(key) : undefined; }); tfrpc.register(async function sharedDatabaseSet(key, value) { return g_shared_database ? g_shared_database.set(key, value) : undefined; }); tfrpc.register(async function sharedDatabaseRemove(key, value) { return g_shared_database ? g_shared_database.remove(key, value) : undefined; }); tfrpc.register(async function query(sql, args) { let result = []; await ssb.sqlAsync(sql, args, function callback(row) { result.push(row); }); return result; }); tfrpc.register(async function store_blob(blob) { if (typeof blob == 'string') { blob = utf8Encode(blob); } if (Array.isArray(blob)) { blob = Uint8Array.from(blob); } return await ssb.blobStore(blob); }); tfrpc.register(async function get_blob(id) { return utf8Decode(await ssb.blobGet(id)); }); tfrpc.register(strava.refresh_token); async function main() { g_shared_database = await shared_database('state'); if (core.user.credentials?.session?.name) { g_database = await database('state'); } let attempt; if (core.user.credentials?.session?.name) { let shared_db = await shared_database('state'); attempt = await shared_db.get(core.user.credentials.session.name); } app.setDocument( utf8Decode(getFile('index.html')).replace( '${data}', JSON.stringify({ attempt: attempt, state: core.user?.credentials?.session?.name, }) ) ); } main();