Meta: Add files to repository

This commit is contained in:
Alec Murphy 2025-02-16 15:21:19 -05:00
parent 6d27d43268
commit 52cb92f587
120 changed files with 71820 additions and 0 deletions

274
Slon/Api/V1/Accounts.HC Normal file
View file

@ -0,0 +1,274 @@
U0 @slon_api_v1_accounts_get(SlonHttpSession* session)
{
SLON_SCRATCH_BUFFER_AND_REQUEST_JSON
no_warn scratch_buffer;
U8* path = @slon_strnew(session, @slon_http_request_path(session));
I64 path_segments_count = 0;
U8** path_segments = String.Split(path, '/', &path_segments_count);
JsonObject* acct = NULL;
if (!StrICmp("verify_credentials", path_segments[3])) {
if (@slon_api_authorized(session)) {
SLON_AUTH_ACCOUNT_ID
acct = @slon_api_account_by_id(account_id);
if (acct) {
@slon_http_send_json(session, acct);
} else {
@slon_http_set_status_code(session, 404);
}
} else {
@slon_http_set_status_code(session, 401);
}
} else {
// Work with account :id
U8* some_account_id = path_segments[3];
acct = @slon_api_account_by_id(some_account_id);
if (!acct) {
@slon_http_set_status_code(session, 404);
goto slon_api_v1_accounts_get_return;
}
if (path_segments_count > 5) {
U8* method = path_segments[4];
if (!StrICmp("following", method)) {
// FIXME: Implement this
@slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY);
goto slon_api_v1_accounts_get_return;
}
if (!StrICmp("statuses", method)) {
// Return the Account's Statuses
JsonArray* status_array = db->o("statuses")->a(some_account_id);
I64 count = 0;
// FILTERS
I64 limit = 20; // default
U64 max_id = 0;
U64 min_id = 0;
Bool only_media = request_json->@("only_media");
Bool exclude_replies = request_json->@("exclude_replies");
Bool exclude_reblogs = request_json->@("exclude_reblogs");
no_warn exclude_reblogs;
Bool pinned = request_json->@("pinned");
// FIXME: Implement "only_media", "exclude_reblogs", "tagged"
Bool exclude_status = FALSE;
U64 status_id = 0;
if (StrLen(request_json->@("limit")) > 0) {
// 40 = maximum per https://docs.joinmastodon.org/methods/accounts/#statuses
limit = MinI64(40, Str2I64(request_json->@("limit")));
}
if (StrLen(request_json->@("max_id")) > 0) {
max_id = Str2I64(request_json->@("max_id"));
}
if (StrLen(request_json->@("min_id")) > 0) {
min_id = Str2I64(request_json->@("min_id"));
}
JsonArray* statuses = Json.CreateArray();
JsonObject* status = NULL;
if (status_array && status_array->length) {
I64 i;
for (i = 0; i < status_array->length; i++) {
status = status_array->o(i);
status_id = Str2I64(status->@("id"));
exclude_status = FALSE;
if (status->@("deleted")) {
exclude_status = TRUE;
}
if (max_id > 0 && status_id >= max_id) {
exclude_status = TRUE;
}
if (min_id > 0 && status_id <= min_id) {
exclude_status = TRUE;
}
if (only_media && !Json.Get(status, "media_attachments")(JsonArray*)->length) {
exclude_status = TRUE;
}
if (exclude_replies && StrLen(status->@("in_reply_to_account_id")) > 0 && StrICmp(account_id, status->@("in_reply_to_account_id"))) {
exclude_status = TRUE;
}
if (pinned && !status->@("pinned")) {
exclude_status = TRUE;
}
if (!exclude_status) {
statuses->append(Json.CreateItem(status, JSON_OBJECT));
count++;
}
if (limit > 0 && count >= limit) {
break;
}
}
}
@slon_http_send_json(session, statuses);
Json.Delete(statuses);
goto slon_api_v1_accounts_get_return;
}
@slon_http_set_status_code(session, 404);
} else {
// Return the Account profile
JsonObject* profile_object = Json.Clone(acct);
profile_object->unset("source");
@slon_http_send_json(session, profile_object);
Json.Delete(profile_object);
}
}
slon_api_v1_accounts_get_return:
@slon_free(session, path);
}
Bool @slon_api_v1_accounts_key_is_boolean(U8* name)
{
return (!StrICmp(name, "locked") || !StrICmp(name, "bot") || !StrICmp(name, "discoverable") || !StrICmp(name, "hide_collections") || !StrICmp(name, "indexable"));
}
U0 @slon_api_v1_accounts_patch(SlonHttpSession* session)
{
SLON_SCRATCH_BUFFER_AND_REQUEST_JSON
U8* path = @slon_strnew(session, @slon_http_request_path(session));
I64 path_segments_count = 0;
U8** path_segments = String.Split(path, '/', &path_segments_count);
JsonObject* acct = NULL;
if (!StrICmp("update_credentials", path_segments[3])) {
if (@slon_api_authorized(session)) {
SLON_AUTH_ACCOUNT_ID
if (!request_json || !request_json->keys) {
@slon_http_set_status_code(session, 400);
goto slon_api_v1_accounts_patch_return;
}
// FIXME: Support avatars/banners
acct = @slon_api_account_by_id(account_id);
if (!acct) {
@slon_http_set_status_code(session, 404);
goto slon_api_v1_accounts_patch_return;
}
JsonObject* source = acct->@("source");
I64 fields_attributes_indexes[16];
I64 fields_attributes_count = 0;
U8* field_name;
U8* field_value;
JsonKey* update_field_index;
JsonObject* field_object;
Bool update_fields_from_form_data = FALSE;
Bool integer_is_in_index = FALSE;
I64 i;
I64 index;
MemSet(fields_attributes_indexes, NULL, sizeof(I64) * 16);
JsonArray* fields_array = Json.CreateArray();
JsonKey* key = request_json->keys;
while (key) {
if (!String.BeginsWith("fields_attributes", key->name) && !String.BeginsWith("source", key->name)) {
if (@slon_api_v1_accounts_key_is_boolean(key->name)) {
switch (key->type) {
case JSON_STRING:
acct->set(key->name, @slon_api_boolean_from_string(key->value), JSON_BOOLEAN);
break;
default:
acct->set(key->name, key->value > 0, JSON_BOOLEAN);
break;
}
} else {
acct->set(key->name, key->value, key->type);
}
} else if (String.BeginsWith("source", key->name)) {
if (!StrICmp("source[language]", key->name)) {
source->set("language", key->value);
}
if (!StrICmp("source[privacy]", key->name)) {
source->set("privacy", key->value);
}
} else if (String.BeginsWith("fields_attributes[", key->name)) {
// Get fields indexes from form data
update_fields_from_form_data = TRUE;
index = Str2I64(key->name + StrLen("fields_attributes["));
if (!fields_attributes_count) {
fields_attributes_indexes[fields_attributes_count] = index;
++fields_attributes_count;
} else {
integer_is_in_index = FALSE;
i = 0;
while (i < fields_attributes_count) {
if (index == fields_attributes_indexes[i])
integer_is_in_index = TRUE;
++i;
}
if (!integer_is_in_index) {
fields_attributes_indexes[fields_attributes_count] = index;
++fields_attributes_count;
}
}
} else if (!StrICmp("fields_attributes", key->name)) {
// Get fields data from JSON object
AdamLog("let's get fields data from JSON object!!\n");
update_field_index = key->value(JsonObject*)->keys;
while (update_field_index) {
field_object = update_field_index->value;
field_object->set("verified_at", NULL, JSON_NULL);
AdamLog("before stringify\n");
AdamLog("%s\n", Json.Stringify(field_object));
AdamLog("after stringify\n");
fields_array->append(Json.CreateItem(field_object, JSON_OBJECT));
update_field_index = update_field_index->next;
}
}
key = key->next;
}
if (update_fields_from_form_data) {
for (i = 0; i < fields_attributes_count; i++) {
index = fields_attributes_indexes[i];
field_name = NULL;
field_value = NULL;
key = request_json->keys;
while (key) {
StrPrint(scratch_buffer, "fields_attributes[%d][name]", index);
if (String.BeginsWith(scratch_buffer, key->name)) {
field_name = key->value;
}
StrPrint(scratch_buffer, "fields_attributes[%d][value]", index);
if (String.BeginsWith(scratch_buffer, key->name)) {
field_value = key->value;
}
if (field_name && field_value) {
// create new field_object, and append to acct->fields
field_object = Json.CreateObject();
field_object->set("name", field_name, JSON_STRING);
field_object->set("value", field_value, JSON_STRING);
field_object->set("verified_at", NULL, JSON_NULL);
fields_array->append(Json.CreateItem(field_object, JSON_OBJECT));
field_name = NULL;
field_value = NULL;
}
key = key->next;
}
}
}
acct->set("fields", fields_array, JSON_ARRAY);
source->set("fields", acct->@("fields"), JSON_ARRAY);
@slon_db_save_accounts_to_disk;
@slon_db_actors_update_user(acct);
@slon_http_send_json(session, acct);
} else {
@slon_http_set_status_code(session, 401);
}
} else {
@slon_http_set_status_code(session, 404);
}
slon_api_v1_accounts_patch_return:
@slon_free(session, path);
}

73
Slon/Api/V1/Apps.HC Normal file
View file

@ -0,0 +1,73 @@
U8* @slon_api_v1_apps_generate_app_id(SlonHttpSession* session)
{
U8* app_id = @slon_calloc(session, 16);
I64 i;
for (i = 0; i < 6; i++) {
String.Append(app_id, "%d", RandU64 % 10);
}
return app_id;
}
U8* @slon_api_v1_apps_generate_client_id(SlonHttpSession* session)
{
U8* client_id = NULL;
Bool client_id_exists = TRUE;
while (client_id_exists) {
if (client_id)
@slon_free(session, client_id);
client_id = @slon_api_generate_random_hex_string(session, 16);
client_id_exists = db->o("apps")->@(client_id) > 0;
}
return client_id;
}
U8* @slon_api_v1_apps_generate_client_secret(SlonHttpSession* session)
{
return @slon_api_generate_random_hex_string(session, 32);
}
U0 @slon_api_v1_apps_post(SlonHttpSession* session)
{
SLON_SCRATCH_BUFFER_AND_REQUEST_JSON
no_warn scratch_buffer;
U8* id = @slon_api_v1_apps_generate_app_id(session);
U8* client_id = @slon_api_v1_apps_generate_client_id(session);
U8* client_secret = @slon_api_v1_apps_generate_client_secret(session);
I64 request_scopes_count = 0;
U8** request_scopes = NULL;
if (StrFind("+", request_json->@("scopes")) > 0) {
request_scopes = String.Split(request_json->@("scopes"), '+', &request_scopes_count);
} else {
request_scopes = String.Split(request_json->@("scopes"), ' ', &request_scopes_count);
}
JsonArray* scopes = Json.CreateArray();
I64 i;
for (i = 0; i < request_scopes_count; i++) {
scopes->append(Json.CreateItem(request_scopes[i], JSON_STRING));
}
JsonArray* redirect_uris = Json.CreateArray();
redirect_uris->append(Json.CreateItem(request_json->@("redirect_uris"), JSON_STRING));
JsonObject* credential_app = Json.CreateObject();
credential_app->set("id", id, JSON_STRING);
credential_app->set("name", request_json->@("client_name"), JSON_STRING);
credential_app->set("website", request_json->@("website"), JSON_STRING);
credential_app->set("scopes", scopes, JSON_ARRAY);
credential_app->set("redirect_uris", redirect_uris, JSON_ARRAY);
credential_app->set("redirect_uri", request_json->@("redirect_uris"), JSON_STRING);
credential_app->set("client_id", client_id, JSON_STRING);
credential_app->set("client_secret", client_secret, JSON_STRING);
credential_app->set("client_secret_expires_at", "0", JSON_STRING);
db->o("apps")->set(client_id, credential_app, JSON_OBJECT);
@slon_db_save_apps_to_disk;
@slon_http_send_json(session, credential_app);
@slon_free(session, id);
@slon_free(session, client_id);
@slon_free(session, client_secret);
}

12
Slon/Api/V1/Blocks.HC Normal file
View file

@ -0,0 +1,12 @@
U0 @slon_api_v1_blocks_get(SlonHttpSession* session)
{
// SLON_SCRATCH_BUFFER_AND_REQUEST_JSON
if (@slon_api_authorized(session)) {
// SLON_AUTH_ACCOUNT_ID
// FIXME: Implement this
@slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY);
} else {
@slon_http_set_status_code(session, 401);
}
}

12
Slon/Api/V1/Bookmarks.HC Normal file
View file

@ -0,0 +1,12 @@
U0 @slon_api_v1_bookmarks_get(SlonHttpSession* session)
{
// SLON_SCRATCH_BUFFER_AND_REQUEST_JSON
if (@slon_api_authorized(session)) {
// SLON_AUTH_ACCOUNT_ID
// FIXME: Implement this
@slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY);
} else {
@slon_http_set_status_code(session, 401);
}
}

View file

@ -0,0 +1,12 @@
U0 @slon_api_v1_conversations_get(SlonHttpSession* session)
{
// SLON_SCRATCH_BUFFER_AND_REQUEST_JSON
if (@slon_api_authorized(session)) {
// SLON_AUTH_ACCOUNT_ID
// FIXME: Implement this
@slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY);
} else {
@slon_http_set_status_code(session, 401);
}
}

View file

@ -0,0 +1,12 @@
U0 @slon_api_v1_custom_emojis_get(SlonHttpSession* session)
{
// SLON_SCRATCH_BUFFER_AND_REQUEST_JSON
if (@slon_api_authorized(session)) {
// SLON_AUTH_ACCOUNT_ID
// FIXME: Implement this
@slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY);
} else {
@slon_http_set_status_code(session, 401);
}
}

12
Slon/Api/V1/Favourites.HC Normal file
View file

@ -0,0 +1,12 @@
U0 @slon_api_v1_favourites_get(SlonHttpSession* session)
{
// SLON_SCRATCH_BUFFER_AND_REQUEST_JSON
if (@slon_api_authorized(session)) {
// SLON_AUTH_ACCOUNT_ID
// FIXME: Implement this
@slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY);
} else {
@slon_http_set_status_code(session, 401);
}
}

12
Slon/Api/V1/Filters.HC Normal file
View file

@ -0,0 +1,12 @@
U0 @slon_api_v1_filters_get(SlonHttpSession* session)
{
// SLON_SCRATCH_BUFFER_AND_REQUEST_JSON
if (@slon_api_authorized(session)) {
// SLON_AUTH_ACCOUNT_ID
// FIXME: Implement this
@slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY);
} else {
@slon_http_set_status_code(session, 401);
}
}

View file

@ -0,0 +1,12 @@
U0 @slon_api_v1_follow_requests_get(SlonHttpSession* session)
{
// SLON_SCRATCH_BUFFER_AND_REQUEST_JSON
if (@slon_api_authorized(session)) {
// SLON_AUTH_ACCOUNT_ID
// FIXME: Implement this
@slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY);
} else {
@slon_http_set_status_code(session, 401);
}
}

View file

@ -0,0 +1,12 @@
U0 @slon_api_v1_followed_tags_get(SlonHttpSession* session)
{
// SLON_SCRATCH_BUFFER_AND_REQUEST_JSON
if (@slon_api_authorized(session)) {
// SLON_AUTH_ACCOUNT_ID
// FIXME: Implement this
@slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY);
} else {
@slon_http_set_status_code(session, 401);
}
}

View file

@ -0,0 +1,16 @@
U0 @slon_api_v1_notifications_get(SlonHttpSession* session)
{
// SLON_SCRATCH_BUFFER_AND_REQUEST_JSON
if (@slon_api_authorized(session)) {
// SLON_AUTH_ACCOUNT_ID
// FIXME: Implement this
if (String.EndsWith("policy", @slon_http_request_path(session))) {
@slon_http_send_json(session, SLON_EMPTY_JSON_OBJECT);
} else {
@slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY);
}
} else {
@slon_http_set_status_code(session, 401);
}
}

144
Slon/Api/V1/Statuses.HC Normal file
View file

@ -0,0 +1,144 @@
U0 (*@slon_api_status_create_fedi)(JsonObject* status) = NULL;
U0 (*@slon_api_status_delete_fedi)(JsonObject* status) = NULL;
U0 @slon_api_v1_statuses_delete(SlonHttpSession* session)
{
if (@slon_api_authorized(session)) {
SLON_AUTH_ACCOUNT_ID
JsonArray* statuses = db->o("statuses")->a(account_id);
if (!statuses || !statuses->length) {
@slon_http_send_json(session, SLON_EMPTY_JSON_OBJECT);
return;
}
U8* path = @slon_strnew(session, @slon_http_request_path(session));
I64 path_segments_count = 0;
U8** path_segments = String.Split(path, '/', &path_segments_count);
if (path_segments_count < 4) {
goto slon_api_v1_statuses_delete_return;
}
U8* id = path_segments[3];
JsonObject* status;
I64 i;
for (i = 0; i < statuses->length; i++) {
status = statuses->@(i);
if (!StrICmp(status->@("id"), id)) {
status->set("deleted", TRUE, JSON_BOOLEAN);
@slon_db_save_statuses_to_disk;
@slon_db_instance_decrement_status_count;
@slon_db_save_instance_to_disk;
if (@slon_api_status_delete_fedi) {
@slon_api_status_delete_fedi(Json.Clone(status));
}
goto slon_api_v1_statuses_delete_return;
}
}
slon_api_v1_statuses_delete_return:
Free(path_segments);
@slon_free(session, path);
@slon_http_send_json(session, SLON_EMPTY_JSON_OBJECT);
} else {
@slon_http_set_status_code(session, 401);
}
}
U0 @slon_api_v1_statuses_post(SlonHttpSession* session)
{
SLON_SCRATCH_BUFFER_AND_REQUEST_JSON
if (@slon_api_authorized(session)) {
SLON_AUTH_ACCOUNT_ID
Bool idempotency_key_already_seen = FALSE;
U8* idempotency_key = @slon_http_request_header(session, "idempotency-key");
if (StrLen(idempotency_key) > 0 && db->o("idempotency_keys")->@(idempotency_key)) {
idempotency_key_already_seen = TRUE;
}
if (!idempotency_key_already_seen) {
Json.Set(db->o("idempotency_keys"), idempotency_key, Now, JSON_NUMBER);
}
U8* id = @slon_api_generate_unique_id(session);
U8* created_at = @slon_api_timestamp_from_cdate(session, Now);
JsonObject* app_object = db->o("apps")->@(Json.Get(session->auth, "client_id"));
JsonObject* status_app = Json.CreateObject();
status_app->set("name", app_object->@("name"), JSON_STRING);
status_app->set("website", app_object->@("website"), JSON_STRING);
JsonObject* account_object = Json.Clone(@slon_api_account_by_id(account_id));
account_object->unset("source");
// U8* language = request_json->@("language");
U8* username = account_object->@("username");
Bool sensitive = request_json->@("sensitive") > 0;
U8* in_reply_to_id = request_json->@("in_reply_to_id");
U8* visibility = request_json->@("visibility");
if (!StrLen(visibility)) {
visibility = "public";
}
StrPrint(scratch_buffer, "https://%s/users/%s/statuses/%s", db->o("instance")->@("uri"), username, id);
U8* uri = @slon_strnew(session, scratch_buffer);
StrPrint(scratch_buffer, "https://%s/@%s/%s", db->o("instance")->@("uri"), username, id);
U8* url = @slon_strnew(session, scratch_buffer);
// Mona lets us post with: id, created_at, content, visibility, uri, url, account, application
// Mastodon iOS app lets us post with +: reblogs_count, favourites_count, emojis, tags, mentions
// IceCubesApp lets us post with +: media_attachments, replies_count, spoiler_text, sensitive
JsonObject* status = Json.CreateObject();
status->set("id", id, JSON_STRING);
status->set("created_at", created_at, JSON_STRING);
status->set("content", request_json->@("status"), JSON_STRING);
status->set("visibility", visibility, JSON_STRING);
status->set("uri", uri, JSON_STRING);
status->set("url", url, JSON_STRING);
status->set("account", account_object, JSON_OBJECT);
status->set("application", status_app, JSON_OBJECT);
status->set("reblogs_count", 0, JSON_NUMBER);
status->set("favourites_count", 0, JSON_NUMBER);
status->set("emojis", SLON_EMPTY_JSON_ARRAY, JSON_ARRAY);
status->set("tags", SLON_EMPTY_JSON_ARRAY, JSON_ARRAY);
status->set("mentions", SLON_EMPTY_JSON_ARRAY, JSON_ARRAY);
status->set("media_attachments", SLON_EMPTY_JSON_ARRAY, JSON_ARRAY);
status->set("replies_count", 0, JSON_NUMBER);
status->set("spoiler_text", "", JSON_STRING);
status->set("sensitive", sensitive, JSON_BOOLEAN);
if (StrLen(in_reply_to_id) > 0) {
status->set("in_reply_to_id", in_reply_to_id, JSON_STRING);
}
if (!idempotency_key_already_seen) {
db->o("statuses")->a(account_id)->append(Json.CreateItem(status, JSON_OBJECT));
@slon_db_save_statuses_to_disk;
@slon_db_instance_increment_status_count;
@slon_db_save_instance_to_disk;
if (@slon_api_status_create_fedi) {
@slon_api_status_create_fedi(Json.Clone(status));
}
}
@slon_http_send_json(session, status);
Json.Delete(status_app);
Json.Delete(account_object);
Json.Delete(app_object);
@slon_free(session, uri);
@slon_free(session, url);
@slon_free(session, id);
@slon_free(session, created_at);
} else {
@slon_http_set_status_code(session, 401);
}
}

12
Slon/Api/V1/Timelines.HC Normal file
View file

@ -0,0 +1,12 @@
U0 @slon_api_v1_timelines_get(SlonHttpSession* session)
{
// SLON_SCRATCH_BUFFER_AND_REQUEST_JSON
if (@slon_api_authorized(session)) {
// SLON_AUTH_ACCOUNT_ID
// FIXME: Implement this
@slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY);
} else {
@slon_http_set_status_code(session, 401);
}
}

12
Slon/Api/V2/Filters.HC Normal file
View file

@ -0,0 +1,12 @@
U0 @slon_api_v2_filters_get(SlonHttpSession* session)
{
// SLON_SCRATCH_BUFFER_AND_REQUEST_JSON
if (@slon_api_authorized(session)) {
// SLON_AUTH_ACCOUNT_ID
// FIXME: Implement this
@slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY);
} else {
@slon_http_set_status_code(session, 401);
}
}

9
Slon/Api/V2/Instance.HC Normal file
View file

@ -0,0 +1,9 @@
U0 @slon_api_v2_instance_get(SlonHttpSession* session)
{
SLON_SCRATCH_BUFFER_AND_REQUEST_JSON
no_warn request_json;
StrPrint(scratch_buffer, "{\"domain\":\"%s\"}", db->o("instance")->@("uri"));
@slon_http_set_content_type(session, "application/json; charset=utf-8");
@slon_http_send_string(session, scratch_buffer);
}

View file

@ -0,0 +1,12 @@
U0 @slon_api_v2_suggestions_get(SlonHttpSession* session)
{
// SLON_SCRATCH_BUFFER_AND_REQUEST_JSON
if (@slon_api_authorized(session)) {
// SLON_AUTH_ACCOUNT_ID
// FIXME: Implement this
@slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY);
} else {
@slon_http_set_status_code(session, 401);
}
}