core: ssb.getActiveIdentity JS => C.
This commit is contained in:
105
src/api.js.c
105
src/api.js.c
@@ -321,6 +321,106 @@ static JSValue _tf_api_core_permissionsForUser(JSContext* context, JSValueConst
|
||||
return result;
|
||||
}
|
||||
|
||||
typedef struct _active_identity_work_t
|
||||
{
|
||||
JSContext* context;
|
||||
const char* name;
|
||||
const char* package_owner;
|
||||
const char* package_name;
|
||||
char identity[k_id_base64_len];
|
||||
int result;
|
||||
JSValue promise[2];
|
||||
} active_identity_work_t;
|
||||
|
||||
static void _tf_ssb_getActiveIdentity_visit(const char* identity, void* user_data)
|
||||
{
|
||||
active_identity_work_t* request = user_data;
|
||||
if (!*request->identity)
|
||||
{
|
||||
snprintf(request->identity, sizeof(request->identity), "@%s", identity);
|
||||
}
|
||||
}
|
||||
|
||||
static void _tf_ssb_getActiveIdentity_work(tf_ssb_t* ssb, void* user_data)
|
||||
{
|
||||
active_identity_work_t* request = user_data;
|
||||
sqlite3* db = tf_ssb_acquire_db_reader(ssb);
|
||||
tf_ssb_db_identity_get_active(db, request->name, request->package_owner, request->package_name, request->identity, sizeof(request->identity));
|
||||
tf_ssb_release_db_reader(ssb, db);
|
||||
|
||||
if (!*request->identity)
|
||||
{
|
||||
tf_ssb_db_identity_visit(ssb, request->name, _tf_ssb_getActiveIdentity_visit, request);
|
||||
}
|
||||
|
||||
if (!*request->identity && tf_ssb_db_user_has_permission(ssb, NULL, request->name, "administration"))
|
||||
{
|
||||
tf_ssb_whoami(ssb, request->identity, sizeof(request->identity));
|
||||
}
|
||||
}
|
||||
|
||||
static void _tf_ssb_getActiveIdentity_after_work(tf_ssb_t* ssb, int status, void* user_data)
|
||||
{
|
||||
active_identity_work_t* request = user_data;
|
||||
JSContext* context = request->context;
|
||||
if (request->result == 0)
|
||||
{
|
||||
JSValue identity = JS_NewString(context, request->identity);
|
||||
JSValue error = JS_Call(context, request->promise[0], JS_UNDEFINED, 1, &identity);
|
||||
JS_FreeValue(context, identity);
|
||||
tf_util_report_error(context, error);
|
||||
JS_FreeValue(context, error);
|
||||
}
|
||||
else
|
||||
{
|
||||
JSValue error = JS_Call(context, request->promise[1], JS_UNDEFINED, 0, NULL);
|
||||
tf_util_report_error(context, error);
|
||||
JS_FreeValue(context, error);
|
||||
}
|
||||
JS_FreeValue(context, request->promise[0]);
|
||||
JS_FreeValue(context, request->promise[1]);
|
||||
tf_free((void*)request->name);
|
||||
tf_free((void*)request->package_owner);
|
||||
tf_free((void*)request->package_name);
|
||||
tf_free(request);
|
||||
}
|
||||
|
||||
static JSValue _tf_ssb_getActiveIdentity(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* data)
|
||||
{
|
||||
tf_task_t* task = tf_task_get(context);
|
||||
tf_ssb_t* ssb = tf_task_get_ssb(task);
|
||||
JSValue process = data[0];
|
||||
JSValue credentials = JS_IsObject(process) ? JS_GetPropertyStr(context, process, "credentials") : JS_UNDEFINED;
|
||||
JSValue session = JS_IsObject(credentials) ? JS_GetPropertyStr(context, credentials, "session") : JS_UNDEFINED;
|
||||
JSValue name_value = JS_IsObject(session) ? JS_GetPropertyStr(context, session, "name") : JS_UNDEFINED;
|
||||
JSValue package_owner_value = JS_GetPropertyStr(context, process, "packageOwner");
|
||||
JSValue package_name_value = JS_GetPropertyStr(context, process, "packageName");
|
||||
|
||||
const char* name = JS_ToCString(context, name_value);
|
||||
const char* package_owner = JS_ToCString(context, package_owner_value);
|
||||
const char* package_name = JS_ToCString(context, package_name_value);
|
||||
active_identity_work_t* work = tf_malloc(sizeof(active_identity_work_t));
|
||||
*work = (active_identity_work_t) {
|
||||
.context = context,
|
||||
.name = tf_strdup(name),
|
||||
.package_owner = tf_strdup(package_owner),
|
||||
.package_name = tf_strdup(package_name),
|
||||
};
|
||||
JSValue result = JS_NewPromiseCapability(context, work->promise);
|
||||
JS_FreeCString(context, name);
|
||||
JS_FreeCString(context, package_owner);
|
||||
JS_FreeCString(context, package_name);
|
||||
|
||||
JS_FreeValue(context, package_owner_value);
|
||||
JS_FreeValue(context, package_name_value);
|
||||
JS_FreeValue(context, name_value);
|
||||
JS_FreeValue(context, session);
|
||||
JS_FreeValue(context, credentials);
|
||||
|
||||
tf_ssb_run_work(ssb, _tf_ssb_getActiveIdentity_work, _tf_ssb_getActiveIdentity_after_work, work);
|
||||
return result;
|
||||
}
|
||||
|
||||
static JSValue _tf_api_register_imports(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
||||
{
|
||||
JSValue imports = argv[0];
|
||||
@@ -341,6 +441,11 @@ static JSValue _tf_api_register_imports(JSContext* context, JSValueConst this_va
|
||||
|
||||
JS_SetPropertyStr(context, core, "url", JS_GetPropertyStr(context, process, "url"));
|
||||
|
||||
JSValue ssb = JS_GetPropertyStr(context, imports, "ssb");
|
||||
;
|
||||
JS_SetPropertyStr(context, ssb, "getActiveIdentity", JS_NewCFunctionData(context, _tf_ssb_getActiveIdentity, 3, 0, 1, &process));
|
||||
JS_FreeValue(context, ssb);
|
||||
|
||||
JS_FreeValue(context, core);
|
||||
return JS_UNDEFINED;
|
||||
}
|
||||
|
87
src/ssb.js.c
87
src/ssb.js.c
@@ -530,92 +530,6 @@ static JSValue _tf_ssb_getAllIdentities(JSContext* context, JSValueConst this_va
|
||||
return result;
|
||||
}
|
||||
|
||||
typedef struct _active_identity_work_t
|
||||
{
|
||||
JSContext* context;
|
||||
const char* name;
|
||||
const char* package_owner;
|
||||
const char* package_name;
|
||||
char identity[k_id_base64_len];
|
||||
int result;
|
||||
JSValue promise[2];
|
||||
} active_identity_work_t;
|
||||
|
||||
static void _tf_ssb_getActiveIdentity_visit(const char* identity, void* user_data)
|
||||
{
|
||||
active_identity_work_t* request = user_data;
|
||||
if (!*request->identity)
|
||||
{
|
||||
snprintf(request->identity, sizeof(request->identity), "@%s", identity);
|
||||
}
|
||||
}
|
||||
|
||||
static void _tf_ssb_getActiveIdentity_work(tf_ssb_t* ssb, void* user_data)
|
||||
{
|
||||
active_identity_work_t* request = user_data;
|
||||
sqlite3* db = tf_ssb_acquire_db_reader(ssb);
|
||||
tf_ssb_db_identity_get_active(db, request->name, request->package_owner, request->package_name, request->identity, sizeof(request->identity));
|
||||
tf_ssb_release_db_reader(ssb, db);
|
||||
|
||||
if (!*request->identity)
|
||||
{
|
||||
tf_ssb_db_identity_visit(ssb, request->name, _tf_ssb_getActiveIdentity_visit, request);
|
||||
}
|
||||
|
||||
if (!*request->identity && tf_ssb_db_user_has_permission(ssb, NULL, request->name, "administration"))
|
||||
{
|
||||
tf_ssb_whoami(ssb, request->identity, sizeof(request->identity));
|
||||
}
|
||||
}
|
||||
|
||||
static void _tf_ssb_getActiveIdentity_after_work(tf_ssb_t* ssb, int status, void* user_data)
|
||||
{
|
||||
active_identity_work_t* request = user_data;
|
||||
JSContext* context = request->context;
|
||||
if (request->result == 0)
|
||||
{
|
||||
JSValue identity = JS_NewString(context, request->identity);
|
||||
JSValue error = JS_Call(context, request->promise[0], JS_UNDEFINED, 1, &identity);
|
||||
JS_FreeValue(context, identity);
|
||||
tf_util_report_error(context, error);
|
||||
JS_FreeValue(context, error);
|
||||
}
|
||||
else
|
||||
{
|
||||
JSValue error = JS_Call(context, request->promise[1], JS_UNDEFINED, 0, NULL);
|
||||
tf_util_report_error(context, error);
|
||||
JS_FreeValue(context, error);
|
||||
}
|
||||
JS_FreeValue(context, request->promise[0]);
|
||||
JS_FreeValue(context, request->promise[1]);
|
||||
tf_free((void*)request->name);
|
||||
tf_free((void*)request->package_owner);
|
||||
tf_free((void*)request->package_name);
|
||||
tf_free(request);
|
||||
}
|
||||
|
||||
static JSValue _tf_ssb_getActiveIdentity(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
||||
{
|
||||
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
||||
const char* name = JS_ToCString(context, argv[0]);
|
||||
const char* package_owner = JS_ToCString(context, argv[1]);
|
||||
const char* package_name = JS_ToCString(context, argv[2]);
|
||||
active_identity_work_t* work = tf_malloc(sizeof(active_identity_work_t));
|
||||
*work = (active_identity_work_t) {
|
||||
.context = context,
|
||||
.name = tf_strdup(name),
|
||||
.package_owner = tf_strdup(package_owner),
|
||||
.package_name = tf_strdup(package_name),
|
||||
};
|
||||
JSValue result = JS_NewPromiseCapability(context, work->promise);
|
||||
JS_FreeCString(context, name);
|
||||
JS_FreeCString(context, package_owner);
|
||||
JS_FreeCString(context, package_name);
|
||||
|
||||
tf_ssb_run_work(ssb, _tf_ssb_getActiveIdentity_work, _tf_ssb_getActiveIdentity_after_work, work);
|
||||
return result;
|
||||
}
|
||||
|
||||
typedef struct _identity_info_work_t
|
||||
{
|
||||
JSContext* context;
|
||||
@@ -2390,7 +2304,6 @@ void tf_ssb_register(JSContext* context, tf_ssb_t* ssb)
|
||||
/* Does not require an identity. */
|
||||
JS_SetPropertyStr(context, object, "getServerIdentity", JS_NewCFunction(context, _tf_ssb_getServerIdentity, "getServerIdentity", 0));
|
||||
JS_SetPropertyStr(context, object, "getAllIdentities", JS_NewCFunction(context, _tf_ssb_getAllIdentities, "getAllIdentities", 0));
|
||||
JS_SetPropertyStr(context, object, "getActiveIdentity", JS_NewCFunction(context, _tf_ssb_getActiveIdentity, "getActiveIdentity", 3));
|
||||
JS_SetPropertyStr(context, object, "blobGet", JS_NewCFunction(context, _tf_ssb_blobGet, "blobGet", 1));
|
||||
JS_SetPropertyStr(context, object, "connections", JS_NewCFunction(context, _tf_ssb_connections, "connections", 0));
|
||||
JS_SetPropertyStr(context, object, "storedConnections", JS_NewCFunction(context, _tf_ssb_storedConnections, "storedConnections", 0));
|
||||
|
Reference in New Issue
Block a user