ssb: The block list can be crudely managed through the admin app.
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 8m54s
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 8m54s
This commit is contained in:
12
src/ssb.db.c
12
src/ssb.db.c
@@ -2936,3 +2936,15 @@ bool tf_ssb_db_is_blocked(sqlite3* db, const char* id)
|
||||
}
|
||||
return is_blocked;
|
||||
}
|
||||
|
||||
void tf_ssb_db_get_blocks(sqlite3* db, void (*callback)(const char* id, double timestamp, void* user_data), void* user_data)
|
||||
{
|
||||
sqlite3_stmt* statement = NULL;
|
||||
if (sqlite3_prepare_v2(db, "SELECT id, timestamp FROM blocks ORDER BY timestamp", -1, &statement, NULL) == SQLITE_OK)
|
||||
{
|
||||
while (sqlite3_step(statement) == SQLITE_ROW)
|
||||
{
|
||||
callback((const char*)sqlite3_column_text(statement, 0), sqlite3_column_double(statement, 1), user_data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -639,4 +639,12 @@ void tf_ssb_db_remove_block(sqlite3* db, const char* id);
|
||||
*/
|
||||
bool tf_ssb_db_is_blocked(sqlite3* db, const char* id);
|
||||
|
||||
/**
|
||||
** Get block list.
|
||||
** @param db The database.
|
||||
** @param callback Called for each entry.
|
||||
** @param user_data User data to be passed to the callback.
|
||||
*/
|
||||
void tf_ssb_db_get_blocks(sqlite3* db, void (*callback)(const char* id, double timestamp, void* user_data), void* user_data);
|
||||
|
||||
/** @} */
|
||||
|
||||
63
src/ssb.js.c
63
src/ssb.js.c
@@ -2232,6 +2232,68 @@ static JSValue _tf_ssb_remove_block(JSContext* context, JSValueConst this_val, i
|
||||
return result;
|
||||
}
|
||||
|
||||
typedef struct _block_t
|
||||
{
|
||||
char id[k_id_base64_len];
|
||||
double timestamp;
|
||||
} block_t;
|
||||
|
||||
typedef struct _get_blocks_t
|
||||
{
|
||||
block_t* blocks;
|
||||
int count;
|
||||
JSValue promise[2];
|
||||
} get_blocks_t;
|
||||
|
||||
static void _get_blocks_callback(const char* id, double timestamp, void* user_data)
|
||||
{
|
||||
get_blocks_t* work = user_data;
|
||||
work->blocks = tf_resize_vec(work->blocks, sizeof(block_t) * (work->count + 1));
|
||||
work->blocks[work->count] = (block_t) { .timestamp = timestamp };
|
||||
tf_string_set(work->blocks[work->count].id, sizeof(work->blocks[work->count].id), id);
|
||||
work->count++;
|
||||
}
|
||||
|
||||
static void _tf_ssb_get_blocks_work(tf_ssb_t* ssb, void* user_data)
|
||||
{
|
||||
sqlite3* db = tf_ssb_acquire_db_reader(ssb);
|
||||
tf_ssb_db_get_blocks(db, _get_blocks_callback, user_data);
|
||||
tf_ssb_release_db_reader(ssb, db);
|
||||
}
|
||||
|
||||
static void _tf_ssb_get_blocks_after_work(tf_ssb_t* ssb, int status, void* user_data)
|
||||
{
|
||||
get_blocks_t* work = user_data;
|
||||
JSContext* context = tf_ssb_get_context(ssb);
|
||||
|
||||
JSValue result = JS_NewArray(context);
|
||||
for (int i = 0; i < work->count; i++)
|
||||
{
|
||||
JSValue entry = JS_NewObject(context);
|
||||
JS_SetPropertyStr(context, entry, "id", JS_NewString(context, work->blocks[i].id));
|
||||
JS_SetPropertyStr(context, entry, "timestamp", JS_NewFloat64(context, work->blocks[i].timestamp));
|
||||
JS_SetPropertyUint32(context, result, i, entry);
|
||||
}
|
||||
|
||||
JSValue error = JS_Call(context, work->promise[0], JS_UNDEFINED, 1, &result);
|
||||
JS_FreeValue(context, result);
|
||||
tf_util_report_error(context, error);
|
||||
JS_FreeValue(context, error);
|
||||
JS_FreeValue(context, work->promise[0]);
|
||||
JS_FreeValue(context, work->promise[1]);
|
||||
tf_free(work);
|
||||
}
|
||||
|
||||
static JSValue _tf_ssb_get_blocks(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
||||
{
|
||||
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
||||
get_blocks_t* work = tf_malloc(sizeof(get_blocks_t));
|
||||
*work = (get_blocks_t) { 0 };
|
||||
JSValue result = JS_NewPromiseCapability(context, work->promise);
|
||||
tf_ssb_run_work(ssb, _tf_ssb_get_blocks_work, _tf_ssb_get_blocks_after_work, work);
|
||||
return result;
|
||||
}
|
||||
|
||||
void tf_ssb_register(JSContext* context, tf_ssb_t* ssb)
|
||||
{
|
||||
JS_NewClassID(&_tf_ssb_classId);
|
||||
@@ -2289,6 +2351,7 @@ void tf_ssb_register(JSContext* context, tf_ssb_t* ssb)
|
||||
JS_SetPropertyStr(context, object_internal, "removeEventListener", JS_NewCFunction(context, _tf_ssb_remove_event_listener, "removeEventListener", 2));
|
||||
JS_SetPropertyStr(context, object_internal, "addBlock", JS_NewCFunction(context, _tf_ssb_add_block, "addBlock", 1));
|
||||
JS_SetPropertyStr(context, object_internal, "removeBlock", JS_NewCFunction(context, _tf_ssb_remove_block, "removeBlock", 1));
|
||||
JS_SetPropertyStr(context, object_internal, "getBlocks", JS_NewCFunction(context, _tf_ssb_get_blocks, "getBlocks", 0));
|
||||
|
||||
JS_FreeValue(context, global);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user