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:
2022-01-26 02:49:45 +00:00
parent e1167b6854
commit 7b32067b07
4 changed files with 70 additions and 11 deletions

View File

@ -3,6 +3,7 @@
#include "ssb.db.h"
#include "ssb.h"
#include <quickjs.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
@ -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);
}