From 7b32067b0769adee55e4cac8d90fab276aeea684 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Wed, 26 Jan 2022 02:49:45 +0000 Subject: [PATCH] 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 --- apps/cory/apps.json | 2 +- apps/cory/apps/app.js | 23 ++++++++++++++-------- core/core.js | 11 +++++++++-- src/ssb.import.c | 45 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 11 deletions(-) diff --git a/apps/cory/apps.json b/apps/cory/apps.json index 93880c48..4e9ac099 100644 --- a/apps/cory/apps.json +++ b/apps/cory/apps.json @@ -1 +1 @@ -{"type":"tildefriends-app","files":{"app.js":"&5hVx7GCBbUh81CFIPkWJCyDZ9Pp1KBne6BiLbKklwCQ=.sha256"}} \ No newline at end of file +{"type":"tildefriends-app","files":{"app.js":"&C82YfV2CQsreGCcY0FHR4owqVOaN10yvVW7OZxFC8sE=.sha256"}} \ No newline at end of file diff --git a/apps/cory/apps/app.js b/apps/cory/apps/app.js index fdb5b735..bd856fd0 100644 --- a/apps/cory/apps/app.js +++ b/apps/cory/apps/app.js @@ -1,21 +1,28 @@ async function main() { var apps = await core.apps(); + var core_apps = await core.apps('core'); var doc = `

Apps

+

Core Apps

+ ` app.setDocument(doc); diff --git a/core/core.js b/core/core.js index f90bf6c2..b1b2da82 100644 --- a/core/core.js +++ b/core/core.js @@ -192,11 +192,18 @@ async function getProcessBlob(blobId, key, options) { }, 'getUser': getUser.bind(null, process, process), 'user': getUser(process, process), - 'apps': function() { + 'apps': function(name) { if (process.credentials && process.credentials.session && 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 { var names = JSON.parse(db.get('apps')); return Object.fromEntries(names.map(name => [name, db.get('path:' + name)])); diff --git a/src/ssb.import.c b/src/ssb.import.c index 8f6c5183..edb11914 100644 --- a/src/ssb.import.c +++ b/src/ssb.import.c @@ -3,6 +3,7 @@ #include "ssb.db.h" #include "ssb.h" +#include #include #include #include @@ -28,6 +29,49 @@ static void _tf_ssb_import_file_close(uv_fs_t* req) 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) { 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) { 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); }