Remove duplicate apps entries on import.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4156 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2023-01-29 01:58:57 +00:00
parent c692b1b1f8
commit 3c288f7f68

View File

@ -52,10 +52,36 @@ static void _tf_ssb_import_add_app(tf_ssb_t* ssb, const char* user, const char*
JS_FreeValue(context, apps);
apps = JS_NewArray(context);
}
int32_t length = tf_util_get_length(context, apps);
JS_SetPropertyUint32(context, apps, length, JS_NewString(context, app));
JSValue json = JS_JSONStringify(context, apps, JS_NULL, JS_NULL);
JSValue sort = JS_GetPropertyStr(context, apps, "sort");
JS_FreeValue(context, JS_Call(context, sort, apps, 0, NULL));
JS_FreeValue(context, sort);
length++;
JSValue out_apps = JS_NewArray(context);
const char* last_added = NULL;
int write_index = 0;
for (int read_index = 0; read_index < length; read_index++)
{
JSValue read_value = JS_GetPropertyUint32(context, apps, read_index);
const char* read_string = JS_ToCString(context, read_value);
if (read_string && (!last_added || strcmp(read_string, last_added)))
{
JS_SetPropertyUint32(context, out_apps, write_index++, read_value);
}
else
{
JS_FreeValue(context, read_value);
}
JS_FreeCString(context, last_added);
last_added = read_string;
}
JSValue json = JS_JSONStringify(context, out_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)
{
@ -69,6 +95,7 @@ static void _tf_ssb_import_add_app(tf_ssb_t* ssb, const char* user, const char*
JS_FreeCString(context, text);
JS_FreeValue(context, json);
JS_FreeValue(context, out_apps);
JS_FreeValue(context, apps);
}