forked from cory/tildefriends
Add a button in the profile editor to ask the server to follow you. I'm hoping this helps replicating accounts that are otherwise difficult to discover.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4558 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
67
src/ssb.js.c
67
src/ssb.js.c
@ -29,6 +29,7 @@ static const int k_sql_async_timeout_ms = 60 * 1000;
|
||||
static JSClassID _tf_ssb_classId;
|
||||
|
||||
void _tf_ssb_on_rpc(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, JSValue args, const uint8_t* message, size_t size, void* user_data);
|
||||
static JSValue _tf_ssb_appendMessageWithIdentity(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
||||
|
||||
static JSValue _tf_ssb_createIdentity(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
||||
{
|
||||
@ -63,6 +64,71 @@ static JSValue _tf_ssb_createIdentity(JSContext* context, JSValueConst this_val,
|
||||
return result;
|
||||
}
|
||||
|
||||
static JSValue _set_server_following_internal(tf_ssb_t* ssb, JSValueConst this_val, JSValue id, JSValue following)
|
||||
{
|
||||
JSContext* context = tf_ssb_get_context(ssb);
|
||||
JSValue message = JS_NewObject(context);
|
||||
JSValue server_user = JS_NewString(context, ":admin");
|
||||
char server_id_buffer[k_id_base64_len] = { 0 };
|
||||
tf_ssb_whoami(ssb, server_id_buffer, sizeof(server_id_buffer));
|
||||
JSValue server_id = JS_NewString(context, server_id_buffer);
|
||||
JS_SetPropertyStr(context, message, "type", JS_NewString(context, "contact"));
|
||||
JS_SetPropertyStr(context, message, "contact", JS_DupValue(context, id));
|
||||
JS_SetPropertyStr(context, message, "following", JS_DupValue(context, following));
|
||||
JSValue args[] =
|
||||
{
|
||||
server_user,
|
||||
server_id,
|
||||
message,
|
||||
};
|
||||
JSValue result = _tf_ssb_appendMessageWithIdentity(context, this_val, _countof(args), args);
|
||||
JS_FreeValue(context, server_id);
|
||||
JS_FreeValue(context, server_user);
|
||||
JS_FreeValue(context, message);
|
||||
return result;
|
||||
}
|
||||
|
||||
static JSValue _tf_ssb_set_server_following_me(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
||||
{
|
||||
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
||||
JSValue result = JS_UNDEFINED;
|
||||
if (ssb)
|
||||
{
|
||||
char server_id[k_id_base64_len];
|
||||
tf_ssb_whoami(ssb, server_id, sizeof(server_id));
|
||||
const char* user = JS_ToCString(context, argv[0]);
|
||||
const char* key = JS_ToCString(context, argv[1]);
|
||||
if (!tf_ssb_db_identity_get_private_key(ssb, user, key, NULL, 0))
|
||||
{
|
||||
result = JS_ThrowInternalError(context, "User %s does not own key %s.", user, key);
|
||||
}
|
||||
else
|
||||
{
|
||||
const char* server_id_ptr = server_id;
|
||||
const char** current_following = tf_ssb_db_following_deep(ssb, &server_id_ptr, 1, 1);
|
||||
bool is_following = false;
|
||||
for (const char** it = current_following; *it; it++)
|
||||
{
|
||||
if (strcmp(key, *it) == 0)
|
||||
{
|
||||
is_following = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
tf_free(current_following);
|
||||
|
||||
bool want_following = JS_ToBool(context, argv[2]);
|
||||
if ((want_following && !is_following) || (!want_following && is_following))
|
||||
{
|
||||
result = _set_server_following_internal(ssb, this_val, argv[1], argv[2]);
|
||||
}
|
||||
}
|
||||
JS_FreeCString(context, key);
|
||||
JS_FreeCString(context, user);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
typedef struct _identities_visit_t
|
||||
{
|
||||
JSContext* context;
|
||||
@ -1465,6 +1531,7 @@ void tf_ssb_register(JSContext* context, tf_ssb_t* ssb)
|
||||
|
||||
/* Requires an identity. */
|
||||
JS_SetPropertyStr(context, object, "createIdentity", JS_NewCFunction(context, _tf_ssb_createIdentity, "createIdentity", 1));
|
||||
JS_SetPropertyStr(context, object, "setServerFollowingMe", JS_NewCFunction(context, _tf_ssb_set_server_following_me, "setServerFollowingMe", 3));
|
||||
JS_SetPropertyStr(context, object, "getIdentities", JS_NewCFunction(context, _tf_ssb_getIdentities, "getIdentities", 1));
|
||||
JS_SetPropertyStr(context, object, "hmacsha256sign", JS_NewCFunction(context, _tf_ssb_hmacsha256_sign, "hmacsha256sign", 3));
|
||||
JS_SetPropertyStr(context, object, "hmacsha256verify", JS_NewCFunction(context, _tf_ssb_hmacsha256_verify, "hmacsha256verify", 3));
|
||||
|
Reference in New Issue
Block a user