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.
164 lines
4.3 KiB
C
164 lines
4.3 KiB
C
#include "owner.h"
|
|
|
|
// Submodules
|
|
#include "http/server/request.h"
|
|
#include "ap/object.h"
|
|
|
|
// Model
|
|
#include "model/server.h"
|
|
|
|
// View
|
|
|
|
// Standard Library
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
struct pager
|
|
{
|
|
void* data;
|
|
struct ap_object* (*get_root)( void* );
|
|
struct ap_object* (*get_page)( void*, int );
|
|
};
|
|
|
|
static bool handle_paged_collection( struct http_request* req, struct pager* pg )
|
|
{
|
|
if( http_request_route_term(req,"") ) {
|
|
struct ap_object* obj = pg->get_root( pg->data );
|
|
|
|
http_request_add_header( req, "Access-Control-Allow-Origin", "*" );
|
|
http_request_send_headers( req, 200, "application/activity+json", true );
|
|
FILE* f = http_request_get_response_body(req);
|
|
ap_object_write_to_FILE( obj, f );
|
|
ap_object_free(obj);
|
|
} else if( http_request_route( req, "/page-" ) ) {
|
|
char* page_str = http_request_route_get_dir_or_file(req);
|
|
int page = -1;
|
|
if( !page_str ) { return false; }
|
|
if( 1 != sscanf(page_str,"%d",&page) ) { return false; }
|
|
if( page < 0 ) { return false; }
|
|
|
|
struct ap_object* obj = pg->get_page( pg->data, page );
|
|
if( !obj ) { return false; }
|
|
|
|
http_request_add_header( req, "Access-Control-Allow-Origin", "*" );
|
|
http_request_send_headers( req, 200, "application/activity+json", true );
|
|
FILE* f = http_request_get_response_body(req);
|
|
ap_object_write_to_FILE( obj, f );
|
|
ap_object_free(obj);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
static bool handle_featured( struct http_request* req )
|
|
{
|
|
printf( "User-Agent: %s\n", http_request_get_header(req,"user-agent") );
|
|
struct account* owner_account = account_from_id(0);
|
|
|
|
//*
|
|
struct pager pg = {
|
|
.data = owner_account,
|
|
.get_root = (void*)account_ap_featured,
|
|
.get_page = (void*)account_ap_featured_page,
|
|
};
|
|
|
|
bool result = handle_paged_collection(req,&pg);
|
|
/*/
|
|
http_request_add_header( req, "Access-Control-Allow-Origin", "*" );
|
|
http_request_send_headers( req, 200, "application/activity+json", true );
|
|
FILE* f = http_request_get_response_body(req);
|
|
#include "src/view/owner/featured.json.inc"
|
|
bool result = true;
|
|
//*/
|
|
account_free( owner_account );
|
|
return result;
|
|
}
|
|
static bool handle_followers( struct http_request* req )
|
|
{
|
|
struct account* owner_account = account_from_id(0);
|
|
|
|
struct pager pg = {
|
|
.data = owner_account,
|
|
.get_root = (void*)account_ap_followers,
|
|
.get_page = (void*)account_ap_followers_page,
|
|
};
|
|
|
|
bool result = handle_paged_collection(req,&pg);
|
|
account_free( owner_account );
|
|
return result;
|
|
}
|
|
static bool handle_following( struct http_request* req )
|
|
{
|
|
struct account* owner_account = account_from_id(0);
|
|
|
|
struct pager pg = {
|
|
.data = owner_account,
|
|
.get_root = (void*)account_ap_following,
|
|
.get_page = (void*)account_ap_following_page,
|
|
};
|
|
|
|
bool result = handle_paged_collection(req,&pg);
|
|
account_free( owner_account );
|
|
return result;
|
|
}
|
|
|
|
static bool handle_owner_actor( struct http_request* req )
|
|
{
|
|
printf( "User-Agent: %s\n", http_request_get_header(req,"user-agent") );
|
|
|
|
struct account* owner_account = account_from_id(0);
|
|
|
|
http_request_add_header( req, "Access-Control-Allow-Origin", "*" );
|
|
http_request_send_headers( req, 200, "application/activity+json", true );
|
|
FILE* f = http_request_get_response_body(req);
|
|
|
|
struct ap_object* obj = account_ap_actor( owner_account );
|
|
ap_object_write_to_FILE( obj, f );
|
|
ap_object_free(obj);
|
|
|
|
account_free( owner_account );
|
|
return true;
|
|
}
|
|
|
|
static bool handle_avatar( struct http_request* req )
|
|
{
|
|
FILE* f = fopen("data/owner/avatar.png","r");
|
|
if( f ) {
|
|
fclose(f);
|
|
return http_request_send_file( req, "data/owner/avatar.png", "image/png" );
|
|
}
|
|
|
|
return false;
|
|
}
|
|
static bool handle_banner( struct http_request* req )
|
|
{
|
|
FILE* f = fopen("data/owner/banner.png","r");
|
|
if( f ) {
|
|
fclose(f);
|
|
return http_request_send_file( req, "data/owner/banner.png", "image/png" );
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
bool route_owner( struct http_request* req )
|
|
{
|
|
if( http_request_route( req, "/actor" ) ) {
|
|
if( http_request_route_term( req, "" ) ) {
|
|
return handle_owner_actor(req);
|
|
}
|
|
} else if( http_request_route( req, "/following" ) ) {
|
|
return handle_following(req);
|
|
} else if( http_request_route( req, "/followers" ) ) {
|
|
return handle_followers(req);
|
|
} else if( http_request_route( req, "/collections/featured" ) ) {
|
|
return handle_featured(req);
|
|
} else if( http_request_route_term( req, "/avatar.blob" ) ) {
|
|
return handle_avatar(req);
|
|
} else if( http_request_route_term( req, "/banner.blob" ) ) {
|
|
return handle_banner(req);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|