Make the 'apps' app list core apps, and populate apps lists when importing.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3795 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2022-01-26 02:49:45 +00:00
parent e1167b6854
commit 7b32067b07
4 changed files with 70 additions and 11 deletions

View File

@ -1 +1 @@
{"type":"tildefriends-app","files":{"app.js":"&5hVx7GCBbUh81CFIPkWJCyDZ9Pp1KBne6BiLbKklwCQ=.sha256"}} {"type":"tildefriends-app","files":{"app.js":"&C82YfV2CQsreGCcY0FHR4owqVOaN10yvVW7OZxFC8sE=.sha256"}}

View File

@ -1,21 +1,28 @@
async function main() { async function main() {
var apps = await core.apps(); var apps = await core.apps();
var core_apps = await core.apps('core');
var doc = `<!DOCTYPE html> var doc = `<!DOCTYPE html>
<html> <html>
<body style="background: #888"> <body style="background: #888">
<h1>Apps</h1> <h1>Apps</h1>
<ul id="apps"></ul> <ul id="apps"></ul>
<h1>Core Apps</h1>
<ul id="core_apps"></ul>
</body> </body>
<script> <script>
var apps = ${JSON.stringify(apps)}; function populate_apps(id, name, apps) {
var list = document.getElementById(id);
for (let app of Object.keys(apps)) { for (let app of Object.keys(apps)) {
var li = document.getElementById('apps').appendChild(document.createElement('li')); var li = list.appendChild(document.createElement('li'));
var a = document.createElement('a'); var a = document.createElement('a');
a.innerText = app; a.innerText = app;
a.href = '/~${core.user.credentials.session.name}/' + app + '/'; a.href = '/~' + name + '/' + app + '/';
a.target = '_top'; a.target = '_top';
li.appendChild(a); li.appendChild(a);
} }
}
populate_apps('apps', '${core.user.credentials.session.name}', ${JSON.stringify(apps)});
populate_apps('core_apps', 'core', ${JSON.stringify(core_apps)});
</script> </script>
</html>` </html>`
app.setDocument(doc); app.setDocument(doc);

View File

@ -192,11 +192,18 @@ async function getProcessBlob(blobId, key, options) {
}, },
'getUser': getUser.bind(null, process, process), 'getUser': getUser.bind(null, process, process),
'user': getUser(process, process), 'user': getUser(process, process),
'apps': function() { 'apps': function(name) {
if (process.credentials && if (process.credentials &&
process.credentials.session && process.credentials.session &&
process.credentials.session.name) { process.credentials.session.name) {
var db = new Database(process.credentials.session.name); if (name && name !== process.credentials.session.name && name !== 'core') {
return {};
} else if (!name) {
name = process.credentials.session.name;
}
}
if (name) {
var db = new Database(name);
try { try {
var names = JSON.parse(db.get('apps')); var names = JSON.parse(db.get('apps'));
return Object.fromEntries(names.map(name => [name, db.get('path:' + name)])); return Object.fromEntries(names.map(name => [name, db.get('path:' + name)]));

View File

@ -3,6 +3,7 @@
#include "ssb.db.h" #include "ssb.db.h"
#include "ssb.h" #include "ssb.h"
#include <quickjs.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <strings.h> #include <strings.h>
@ -28,6 +29,49 @@ static void _tf_ssb_import_file_close(uv_fs_t* req)
free(req->data); free(req->data);
} }
static void _tf_ssb_import_add_app(tf_ssb_t* ssb, const char* user, const char* app)
{
sqlite3_stmt* statement;
JSContext* context = tf_ssb_get_context(ssb);
JSValue apps = JS_UNDEFINED;
if (sqlite3_prepare(tf_ssb_get_db(ssb), "SELECT value FROM properties WHERE id = $1 AND key = 'apps'", -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_bind_text(statement, 1, user, -1, NULL) == SQLITE_OK &&
sqlite3_step(statement) == SQLITE_ROW)
{
const char* json = (const char*)sqlite3_column_text(statement, 0);
apps = JS_ParseJSON(context, json, strlen(json), NULL);
}
sqlite3_finalize(statement);
}
if (!JS_IsArray(context, apps))
{
JS_FreeValue(context, apps);
apps = JS_NewArray(context);
}
int32_t length = 0;
JSValue lengthval = JS_GetPropertyStr(context, apps, "length");
JS_ToInt32(context, &length, lengthval);
JS_SetPropertyUint32(context, apps, length, JS_NewString(context, app));
JSValue json = JS_JSONStringify(context, apps, JS_NULL, JS_NULL);
const char* text = JS_ToCString(context, json);
if (sqlite3_prepare(tf_ssb_get_db(ssb), "INSERT OR REPLACE INTO properties (id, key, value) VALUES ($1, 'apps', $2)", -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_bind_text(statement, 1, user, -1, NULL) == SQLITE_OK &&
sqlite3_bind_text(statement, 2, text, -1, NULL) == SQLITE_OK &&
sqlite3_step(statement) == SQLITE_OK)
{
}
sqlite3_finalize(statement);
}
JS_FreeCString(context, text);
JS_FreeValue(context, json);
JS_FreeValue(context, apps);
}
static void _tf_ssb_import_file_read(uv_fs_t* req) static void _tf_ssb_import_file_read(uv_fs_t* req)
{ {
tf_import_file_t* file = req->data; tf_import_file_t* file = req->data;
@ -49,6 +93,7 @@ static void _tf_ssb_import_file_read(uv_fs_t* req)
sqlite3_step(statement) == SQLITE_DONE) sqlite3_step(statement) == SQLITE_DONE)
{ {
printf("Registered %s path:%s as %s.\n", file->user, file->name, id); printf("Registered %s path:%s as %s.\n", file->user, file->name, id);
_tf_ssb_import_add_app(file->ssb, file->user, file->name);
} }
sqlite3_finalize(statement); sqlite3_finalize(statement);
} }