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.

118 lines
2.8 KiB
C

#include "activity_pub.h"
// Submodules
#include "http/server/request.h"
#include "collections/array.h"
#include "util/format.h"
#include "ap/object.h"
// Model
#include "model/server.h"
#include "model/status.h"
#include "model/account.h"
#include "model/activity.h"
// Standard Library
#include <string.h>
#include <stdlib.h>
char* status_render_source( struct status* s );
// Route: /note/
bool route_ap_note( struct http_request* req )
{
char* id_str = http_request_route_get_dir_or_file(req);
if( !id_str || !*id_str ) { return false; }
// Route: /note/%d{id}
int id = -1;
if( 1 != sscanf( id_str, "%d", &id ) ) { return false; }
free(id_str);
struct status* s = status_from_id( id );
if( !s ) { return false; }
if( s->remote ) { return false; }
struct ap_object* act = activity_create_Note( s );
http_request_add_header( req, "Access-Control-Allow-Origin", "*" );
http_request_send_headers( req, 200, "application/ld+json", true );
FILE* f = http_request_get_response_body( req );
ap_object_write_to_FILE( act, f );
fflush(f);
ap_object_free(act);
status_free(s);
return true;
}
// Route: /activity/
bool route_ap_activity( struct http_request* req )
{
char* id_str = http_request_route_get_dir_or_file(req);
if( !id_str || !*id_str ) { return false; }
// Route: /activity/%d{id}
int id = -1;
if( 1 != sscanf( id_str, "%d", &id ) ) { return false; }
free(id_str);
struct ap_object* act = activity_from_local_id( id );
http_request_add_header( req, "Access-Control-Allow-Origin", "*" );
http_request_send_headers( req, 200, "application/ld+json", true );
FILE* f = http_request_get_response_body( req );
ap_object_write_to_FILE( act, f );
fflush(f);
ap_object_free(act);
return true;
}
// Route: /outbox
bool route_ap_outbox( struct http_request* req )
{
bool result = false;
struct account* owner_account = account_from_id( owner_account_id );
if( http_request_route_term(req,"") ) {
struct ap_object* outbox = account_ap_outbox( owner_account );
http_request_send_headers( req, 200, "application/ld+json", true );
FILE* f = http_request_get_response_body( req );
ap_object_write_to_FILE( outbox, f );
fflush(f);
ap_object_free(outbox);
} else if( http_request_route( req, "/page-" ) ) {
char* page_str = http_request_route_get_dir_or_file(req);
int page = -1;
if( !page_str || (1 != sscanf(page_str,"%d",&page) ) || page < 0 ) { goto failed; }
struct ap_object* outbox_page = account_ap_outbox_page( owner_account, page );
if( !outbox_page ) { goto failed; }
http_request_send_headers( req, 200, "application/ld+json", true );
FILE* f = http_request_get_response_body( req );
ap_object_write_to_FILE( outbox_page, f );
fflush(f);
ap_object_free(outbox_page);
}
goto success;
failed:
result = false;
goto cleanup;
success:
result = true;
goto cleanup;
cleanup:
account_free(owner_account);
return result;
}