Implement following ActivityPub, remove text templates

master
teknomunk 1 year ago
parent ea63577295
commit 808dbecb88

@ -53,7 +53,7 @@ static bool handle_followers( struct http_request* req )
int page = -1;
if( !page_str ) { goto failed; }
if( 1 != sscanf(page_str,"%d",&page) ) { goto failed; }
if( page < 1 ) { goto failed; }
if( page < 0 ) { goto failed; }
struct ap_object* obj = account_ap_followers_page(owner_account,page);
if( !obj ) { goto failed; }
@ -78,14 +78,44 @@ cleanup:
}
static bool handle_following( struct http_request* req )
{
struct account* owner_account = account_from_id(owner_account_id);
bool result = false;
struct account* owner_account = account_from_id(0);
http_request_send_headers( req, 200, "application/activity+json", true );
FILE* f = http_request_get_response_body(req);
#include "src/view/owner/following.json.inc"
if( http_request_route_term(req,"") ) {
struct ap_object* obj = account_ap_following(owner_account);
http_request_send_headers( req, 200, "application/activity+json", true );
FILE* f = http_request_get_response_body(req);
ap_activity_write_to_FILE( obj, f );
ap_object_free(obj);
} else if( http_request_route( req, "/page-" ) ) {
printf( "/page-\n" );
char* page_str = http_request_route_get_dir_or_file(req);
int page = -1;
if( !page_str ) { goto failed; }
if( 1 != sscanf(page_str,"%d",&page) ) { goto failed; }
if( page < 0 ) { goto failed; }
struct ap_object* obj = account_ap_following_page(owner_account,page);
if( !obj ) { goto failed; }
http_request_send_headers( req, 200, "application/activity+json", true );
FILE* f = http_request_get_response_body(req);
ap_activity_write_to_FILE( obj, f );
ap_object_free(obj);
}
succeded:
result = true;
goto cleanup;
failed:
printf( "! failed\n" );
result = false;
goto cleanup;
cleanup:
account_free( owner_account );
return true;
return result;
}
static bool handle_owner_actor( struct http_request* req )

@ -339,31 +339,11 @@ struct ap_object* account_ap_outbox_page( struct account* a, int page )
return outbox;
}
struct ap_object* account_ap_followers( struct account* a )
static struct ap_object* account_list_page( int page, char* part_of, const char* page_format, const char* trie_filename )
{
if( a->id != 0 ) { return NULL; }
struct ap_object* o;
o = malloc(sizeof(*o));
memset(o,0,sizeof(*o));
o->type = apot_ordered_collection;
o->published = time(NULL);
o->id = aformat( "https://%s/owner/followers", g_server_name );
o->first.tag = apaot_object;
o->first.ptr = account_ap_followers_page( a, 0 );
char buffer[512];
o->total_items = ffdb_trie_count( format( buffer, 512, "data/accounts/%d/followers", a->id ) );
return o;
}
struct ap_object* account_ap_followers_page( struct account* a, int page )
{
if( a->id != 0 ) { return NULL; }
enum { items_per_page = 10 };
char buffer[512];
int total_items = ffdb_trie_count( format( buffer, 512, "data/accounts/%d/followers", a->id ) );
int total_items = ffdb_trie_count( trie_filename );
int page_count = ( total_items + items_per_page - 1 ) / items_per_page;
if( page >= page_count ) { return NULL; }
@ -396,18 +376,84 @@ struct ap_object* account_ap_followers_page( struct account* a, int page )
o->type = apot_ordered_collection_page;
o->published = time(NULL);
o->part_of = aformat( "https://%s/owner/followers", g_server_name );
o->id = aformat( "https://%s/owner/followers/page-%d", g_server_name, page );
o->part_of = part_of;
o->id = aformat( page_format, page );
if( page > 0 ) {
o->prev = aformat( "https://%s/owner/followers/page-%d", g_server_name, page - 1 );
o->prev = aformat( page_format, page - 1 );
}
if( page < page_count - 1 ) {
o->next = aformat( "https://%s/owner/followers/page-%d", g_server_name, page + 1 );
o->next = aformat( page_format, page + 1 );
}
return o;
}
struct ap_object* account_ap_followers( struct account* a )
{
if( a->id != 0 ) { return NULL; }
struct ap_object* o;
o = malloc(sizeof(*o));
memset(o,0,sizeof(*o));
o->type = apot_ordered_collection;
o->published = time(NULL);
o->id = aformat( "https://%s/owner/followers", g_server_name );
o->first.tag = apaot_object;
o->first.ptr = account_ap_followers_page( a, 0 );
char buffer[512];
o->total_items = ffdb_trie_count( format( buffer, 512, "data/accounts/%d/followers", a->id ) );
return o;
}
struct ap_object* account_ap_followers_page( struct account* a, int page )
{
if( a->id != 0 ) { return NULL; }
char* part_of = aformat( "https://%s/owner/followers", g_server_name );
char page_format[512];
snprintf( page_format, 512, "https://%s/owner/followers/page-%%d", g_server_name );
char trie_filename[512];
snprintf( trie_filename, 512, "data/accounts/%d/followers", a->id );
return account_list_page( page, part_of, page_format, trie_filename );
}
struct ap_object* account_ap_following( struct account* a )
{
if( a->id != 0 ) { return NULL; }
struct ap_object* o;
o = malloc(sizeof(*o));
memset(o,0,sizeof(*o));
o->type = apot_ordered_collection;
o->published = time(NULL);
o->id = aformat( "https://%s/owner/following", g_server_name );
o->first.tag = apaot_object;
o->first.ptr = account_ap_following_page( a, 0 );
char buffer[512];
o->total_items = ffdb_trie_count( format( buffer, 512, "data/accounts/%d/following", a->id ) );
return o;
}
struct ap_object* account_ap_following_page( struct account* a, int page )
{
if( a->id != 0 ) { return NULL; }
char* part_of = aformat( "https://%s/owner/following", g_server_name );
char page_format[512];
snprintf( page_format, 512, "https://%s/owner/following/page-%%d", g_server_name );
char trie_filename[512];
snprintf( trie_filename, 512, "data/accounts/%d/following", a->id );
return account_list_page( page, part_of, page_format, trie_filename );
}
struct ap_object* account_activity_pub( struct account* a )
{
return account_ap_actor(a);

@ -92,6 +92,8 @@ struct ap_object* account_ap_outbox( struct account* a );
struct ap_object* account_ap_outbox_page( struct account* a, int page );
struct ap_object* account_ap_followers( struct account* a );
struct ap_object* account_ap_followers_page( struct account* a, int page );
struct ap_object* account_ap_following( struct account* a );
struct ap_object* account_ap_following_page( struct account* a, int page );
// Local actions
void account_add_follower( struct account* a, struct account* follower );

@ -1,10 +0,0 @@
{
"@context":[
"https://www.w3.org/ns/activitystreams",
{"@language":"und"}
],
"orderedItems":[]
"id":"https://%s{g_server_name}/owner/actor/following",
"totalItems":0,
"type":"OrderedCollection"
}

@ -1,10 +0,0 @@
{
"@context":[
"https://www.w3.org/ns/activitystreams",
{"@language":"und"}
],
"orderedItems":[]
"id":"https://%s{g_server_name}/owner/actor/following",
"totalItems":0,
"type":"OrderedCollection"
}
Loading…
Cancel
Save