You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
3.0 KiB
C

#include "mastodon_api.h"
#include "http_server/http_request.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "form.h"
#include "json/json.h"
#include "model/status.h"
#include "model/account.h"
#include "model/notification.h"
#include "api/client_apps.h"
#include "api/status.h"
#include "api/notice.h"
bool handle_timeline( struct http_request* req, const char* which )
{
// "GET /api/v1/timelines/home?with_muted=true&limit=31"
bool with_muted = false;
unsigned int limit = 100;
struct status s = {
.id = 1,
.account_id = 1,
};
show_statuses( req, &s, 1 );
return true;
}
bool handle_mastodon_api_show_account( struct http_request* req, struct account* a )
{
http_request_send_headers( req, 200, "application/json", true );
FILE* f = http_request_get_response_body( req );
account_write_as_json(a,f);
return true;
}
static bool http_request_route_id( struct http_request* req, int* id )
{
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; }
return true;
}
bool route_mastodon_api( struct http_request* req )
{
if( http_request_route( req, "apps" ) ) {
if( http_request_route_method( req, "POST" ) ) {
return handle_mastodon_api_apps(req);
}
}
if( http_request_route( req, "timelines/public" ) ) {
show_statuses( req, NULL, 0 );
return true;
}
/*
if( !check_bearer_token(req) ) { return false; }
printf( "authorization still valid\n" );
//*/
struct account* owner = account_from_id(0);
if( http_request_route( req, "notifications" ) ) {
struct notification note = {
.account_id = 1,
.status_id = 1,
};
show_notifications( req, &note, 1 );
return true;
}
if( http_request_route( req, "statuses" ) ) {
if( http_request_route( req, "/" ) ) {
int id = -1;
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 );
}
return true;
}
} else if( http_request_route_method( req, "POST" ) ) {
return handle_post(req, owner);
}
}
if( http_request_route( req, "timelines/home" ) ) {
return handle_timeline( req, "home" );
} else if( http_request_route( req, "accounts/" ) ) {
if( http_request_route( req, "verify_credentials" ) ) {
return true;
} else if( http_request_route( req, "statuses" ) ) {
show_statuses( req, NULL, 0 );
return true;
}
char* id_str = http_request_route_get_dir_or_file(req);
int id = 0;
if( http_request_route_id( req, &id ) ) {
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 );
}
}
return false;
}