Start breaking Mastodon API out into multiple (shorted) files

master
teknomunk 1 year ago
parent d52b2643b9
commit 494c0eec6d

@ -0,0 +1,65 @@
#include "client_apps.h"
#include "form.h"
#include "http_server/http_request.h"
#include "model/client_app.h"
#include <stdlib.h>
#include <string.h>
bool handle_mastodon_api_apps( struct http_request* req )
{
FILE* data = http_request_get_request_data( req );
struct form_parser* fp = form_pull_parser_new( data );
if( !fp ) { return false; }
char* client_name = NULL;
char* redirect_uris = NULL;
char* key;
while( key = form_pull_parser_read_key( fp ) ) {
if( 0 == strcmp( "client_name", key ) ) {
client_name = strdup( form_pull_parser_read_value(fp) );
} else if( 0 == strcmp( "redirect_uris", key ) ) {
redirect_uris = strdup( form_pull_parser_read_value(fp) );
} else {
printf( "key: %s\n", key );
printf( "value: %s\n", form_pull_parser_read_value(fp) );
}
}
form_pull_parser_release(fp);
struct client_app* app = client_app_new( client_name );
http_request_send_headers( req, 200, "application/json", true );
FILE* f = http_request_get_response_body( req );
#define RENDER
#include "controller/mastodon_api/apps.json.inc"
#undef RENDER
free(client_name);
free(redirect_uris);
return true;
}
bool check_bearer_token( struct http_request* req )
{
// Check bearer token
const char* auth_token = http_request_get_header( req, "Authorization" );
if( !auth_token ) { return false; }
if( 0 != strncmp( auth_token, "Bearer ", 7 ) ) { return false; }
char* client_id = strndup( &auth_token[7], 32 );
struct client_app* app = client_app_from_id( client_id );
free(client_id);
if( !app ) { return false; }
if( 0 != strcmp( &auth_token[7], app->access_token ) ) { return false; }
client_app_free(app);
return true;
}

@ -0,0 +1,9 @@
#pragma once
#include <stdbool.h>
struct http_request;
bool handle_mastodon_api_apps( struct http_request* req );
bool check_bearer_token( struct http_request* req );

@ -0,0 +1,22 @@
#include "notice.h"
#include "http_server/http_request.h"
#include "model/notification.h"
#include <stdio.h>
void show_notifications( struct http_request* req, struct notification* ns, int count )
{
http_request_send_headers( req, 200, "application/json", true );
FILE* f = http_request_get_response_body( req );
fprintf( f, "[" );
for( int i = 0; i < count; ++i ) {
if( i > 0 ) {
fprintf( f, "," );
}
notification_write_as_json(&ns[i],f);
}
fprintf( f, "]" );
}

@ -0,0 +1,7 @@
#pragma once
struct http_request;
struct notification;
void show_notifications( struct http_request* req, struct notification* ns, int count );

@ -0,0 +1,89 @@
#include "status.h"
#include "json/json.h"
#include "http_server/http_request.h"
#include "model/status.h"
#include "model/account.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void show_status( struct http_request* req, struct status* s )
{
http_request_send_headers( req, 200, "application/json", true );
FILE* f = http_request_get_response_body( req );
status_write_as_json(s,f);
}
void show_status_context( struct http_request* req, struct status* s )
{
http_request_send_headers( req, 200, "application/json", true );
FILE* f = http_request_get_response_body( req );
fprintf( f, "{\"ancestors\":[],\"descendants\":[]}" );
}
void show_statuses( struct http_request* req, struct status* ss, int count )
{
http_request_send_headers( req, 200, "application/json", true );
FILE* f = http_request_get_response_body( req );
fprintf( f, "[" );
for( int i = 0; i < count; ++i ) {
if( i > 0 ) {
fprintf( f, "," );
}
status_write_as_json(&ss[i],f);
}
fprintf( f, "]" );
}
bool handle_post( struct http_request* req, struct account* a )
{
printf( "TODO: new post" );
FILE* data = http_request_get_request_data( req );
struct json_pull_parser* jpp = json_pull_parser_new( data );
if( !jpp ) { return false; }
int save;
if( !json_pull_parser_begin_object( jpp, &save ) ) { return false; }
struct status s;
bool sensitive = false;
char* key;
while( key = json_pull_parser_read_object_key(jpp) ) {
if( 0 == strcmp(key,"media_ids") ) {
json_pull_parser_read_value(jpp);
} else if( 0 == strcmp(key,"sensitive") ) {
json_pull_parser_read_bool(jpp,&sensitive);
} else if( 0 == strcmp(key,"status") ) {
json_pull_parser_read_string(jpp);
} else if( 0 == strcmp(key,"visibility") ) {
json_pull_parser_read_string(jpp);
} else if( 0 == strcmp(key,"spoiler_text") ) {
json_pull_parser_read_string(jpp);
}
}
free(key);
if( !json_pull_parser_end_object(jpp, &save ) ) {
return false;
}
// {"media_ids":[],"sensitive":false,"status":"Test","visibility":"public","spoiler_text":""}
char ch;
while( (ch = fgetc(data)) != EOF ) {
printf( "%c", ch );
}
return false;
}

@ -0,0 +1,14 @@
#pragma once
#include <stdbool.h>
struct http_request;
struct status;
struct account;
void show_status( struct http_request* req, struct status* s );
void show_status_context( struct http_request* req, struct status* s );
void show_statuses( struct http_request* req, struct status* ss, int count );
bool handle_post( struct http_request* req, struct account* a );

@ -7,109 +7,13 @@
#include "form.h"
#include "json/json.h"
#include "model/client_app.h"
#include "model/status.h"
#include "model/account.h"
#include "model/notification.h"
bool handle_mastodon_api_apps( struct http_request* req )
{
FILE* data = http_request_get_request_data( req );
struct form_parser* fp = form_pull_parser_new( data );
if( !fp ) { return false; }
char* client_name = NULL;
char* redirect_uris = NULL;
char* key;
while( key = form_pull_parser_read_key( fp ) ) {
if( 0 == strcmp( "client_name", key ) ) {
client_name = strdup( form_pull_parser_read_value(fp) );
} else if( 0 == strcmp( "redirect_uris", key ) ) {
redirect_uris = strdup( form_pull_parser_read_value(fp) );
} else {
printf( "key: %s\n", key );
printf( "value: %s\n", form_pull_parser_read_value(fp) );
}
}
form_pull_parser_release(fp);
struct client_app* app = client_app_new( client_name );
http_request_send_headers( req, 200, "application/json", true );
FILE* f = http_request_get_response_body( req );
#define RENDER
#include "controller/mastodon_api/apps.json.inc"
#undef RENDER
free(client_name);
free(redirect_uris);
return true;
}
bool check_bearer_token( struct http_request* req )
{
// Check bearer token
const char* auth_token = http_request_get_header( req, "Authorization" );
if( !auth_token ) { return false; }
if( 0 != strncmp( auth_token, "Bearer ", 7 ) ) { return false; }
char* client_id = strndup( &auth_token[7], 32 );
struct client_app* app = client_app_from_id( client_id );
free(client_id);
if( !app ) { return false; }
if( 0 != strcmp( &auth_token[7], app->access_token ) ) { return false; }
client_app_free(app);
return true;
}
void show_status( struct http_request* req, struct status* s )
{
http_request_send_headers( req, 200, "application/json", true );
FILE* f = http_request_get_response_body( req );
status_write_as_json(s,f);
}
void show_status_context( struct http_request* req, struct status* s )
{
http_request_send_headers( req, 200, "application/json", true );
FILE* f = http_request_get_response_body( req );
fprintf( f, "{\"ancestors\":[],\"descendants\":[]}" );
}
void show_statuses( struct http_request* req, struct status* ss, int count )
{
http_request_send_headers( req, 200, "application/json", true );
FILE* f = http_request_get_response_body( req );
fprintf( f, "[" );
for( int i = 0; i < count; ++i ) {
if( i > 0 ) {
fprintf( f, "," );
}
status_write_as_json(&ss[i],f);
}
fprintf( f, "]" );
}
void show_notifications( struct http_request* req, struct notification* ns, int count )
{
http_request_send_headers( req, 200, "application/json", true );
FILE* f = http_request_get_response_body( req );
fprintf( f, "[" );
for( int i = 0; i < count; ++i ) {
if( i > 0 ) {
fprintf( f, "," );
}
notification_write_as_json(&ns[i],f);
}
fprintf( f, "]" );
}
#include "api/client_apps.h"
#include "api/status.h"
#include "api/notice.h"
bool handle_timeline( struct http_request* req, const char* which )
{
@ -137,52 +41,16 @@ bool handle_mastodon_api_show_account( struct http_request* req, struct account*
return true;
}
bool handle_post( struct http_request* req, struct account* a )
static bool http_request_route_id( struct http_request* req, int* id )
{
printf( "TODO: new post" );
FILE* data = http_request_get_request_data( req );
char* id_str = http_request_route_get_dir_or_file(req);
if( !id_str || !*id_str ) { return false; }
*id = -1;
sscanf( id_str, "%d", id );
free(id_str);
if( *id == -1 ) { return false; }
struct json_pull_parser* jpp = json_pull_parser_new( data );
if( !jpp ) { return false; }
int save;
if( !json_pull_parser_begin_object( jpp, &save ) ) { return false; }
struct status s;
bool sensitive = false;
char* key;
while( key = json_pull_parser_read_object_key(jpp) ) {
if( 0 == strcmp(key,"media_ids") ) {
json_pull_parser_read_value(jpp);
} else if( 0 == strcmp(key,"sensitive") ) {
json_pull_parser_read_bool(jpp,&sensitive);
} else if( 0 == strcmp(key,"status") ) {
json_pull_parser_read_string(jpp);
} else if( 0 == strcmp(key,"visibility") ) {
json_pull_parser_read_string(jpp);
} else if( 0 == strcmp(key,"spoiler_text") ) {
json_pull_parser_read_string(jpp);
}
}
free(key);
if( !json_pull_parser_end_object(jpp, &save ) ) {
return false;
}
// {"media_ids":[],"sensitive":false,"status":"Test","visibility":"public","spoiler_text":""}
char ch;
while( (ch = fgetc(data)) != EOF ) {
printf( "%c", ch );
}
return false;
return true;
}
bool route_mastodon_api( struct http_request* req )
@ -215,21 +83,18 @@ bool route_mastodon_api( struct http_request* req )
}
if( http_request_route( req, "statuses" ) ) {
if( http_request_route( req, "/" ) ) {
char* id_str = http_request_route_get_dir_or_file(req);
if( !id_str || !*id_str ) { return false; }
int id = -1;
sscanf( id_str, "%d", &id );
free(id_str);
if( id == -1 ) { return false; }
struct status* s = status_from_id(id);
if( http_request_route_id( req, &id ) ) {
struct status* s = status_from_id(id);
if( http_request_route( req, "context" ) ) {
show_status_context( req, s );
} else {
show_status( req, s );
if( http_request_route( req, "context" ) ) {
show_status_context( req, s );
} else {
show_status( req, s );
}
return true;
}
return true;
} else if( http_request_route_method( req, "POST" ) ) {
return handle_post(req, owner);
}
@ -247,32 +112,21 @@ bool route_mastodon_api( struct http_request* req )
}
char* id_str = http_request_route_get_dir_or_file(req);
int id = -1;
int id = 0;
printf( "id_str = %s\n", id_str );
if( !id_str ) { return false; }
if( http_request_route_id( req, &id ) ) {
struct account* a = account_from_id( id );
if( !a ) { return false; }
if( !*id_str ) {
id = 0;
return handle_mastodon_api_show_account( req, owner );
} else {
sscanf( id_str, "%d", &id );
free( id_str );
if( id == -1 ) {
printf( "Invalid id\n" );
return false;
if( http_request_route( req, "statuses" ) ) {
printf( "TODO: statuses\n" );
show_statuses( req, NULL, 0 );
return true;
} else {
return handle_mastodon_api_show_account( req, a );
}
}
struct account* a = account_from_id( id );
if( !a ) { return false; }
if( http_request_route( req, "statuses" ) ) {
printf( "TODO: statuses\n" );
show_statuses( req, NULL, 0 );
return true;
} else {
return handle_mastodon_api_show_account( req, a );
} else if( id == 0 ) {
return handle_mastodon_api_show_account( req, owner );
}
}

Loading…
Cancel
Save