Rework initialization to allow running only the web server for debugging without needing to recompile, implement status react serdes, update api to return statuses (Soapbox is not showing them...?)

master
teknomunk 1 year ago
parent 3f0b5dac86
commit 9292f5d15e

@ -4,6 +4,7 @@
#include "http_server/http_request.h"
#include "model/status.h"
#include "model/status/react.h"
#include "model/account.h"
#include "model/timeline.h"

@ -13,10 +13,15 @@ bool http_request_route_id( struct http_request* req, int* id );
static bool route_status( struct http_request* req, struct status* s )
{
if( http_request_route( req, "reactions/" ) ) {
char* react = http_request_route_get_dir_or_file( req );
status_add_react( s, react );
free(react);
return true;
if( http_request_route_method( req, "PUT" ) ) {
char* react = http_request_route_get_dir_or_file( req );
status_add_react( s, react );
free(react);
return true;
} else if( http_request_route_method( req, "GET" ) ) {
printf( "TODO: get who react information\n" );
return false;
}
}
return false;

@ -57,34 +57,8 @@ bool run_webserver( struct app_args* args )
return true;
}
void develop();
int main( int argc, char* argv[] )
bool run_everything( struct app_args* args )
{
curl_global_init(CURL_GLOBAL_DEFAULT);
signal(SIGINT, handle_ctrl_c);
signal(SIGPIPE, SIG_IGN);
struct app_args* args = app_args_new( argc, argv );
if( !args ) {
printf( "Error processing argument\n" );
return 1;
}
if( argc > 1 ) {
int section = 0;
sscanf(argv[1],"--section=%d",&section);
switch( section ) {
case 1: process_inbox(); goto exit;
case 2: process_outbox(); goto exit;
case 3: built_in_test(); goto exit;
case 100: develop(); return 0;
}
}
// Process inbox
//*
int inbox_handler_pid = -1;
@ -104,7 +78,46 @@ int main( int argc, char* argv[] )
//*/
int code = 0;
if( !run_webserver(args) ) { code = 1; }
if( !run_webserver(args) ) { return false; }
return true;
}
void develop();
int main( int argc, char* argv[] )
{
curl_global_init(CURL_GLOBAL_DEFAULT);
signal(SIGINT, handle_ctrl_c);
signal(SIGPIPE, SIG_IGN);
struct app_args* args = app_args_new( argc, argv );
if( !args ) {
printf( "Error processing argument\n" );
return 1;
}
int section = -1;
for( int i = 1; i < argc; ++i ) {
if( sscanf(argv[i],"--section=%d",&section) ) {
} else {
printf( "Unknown argument: %s\n", argv[i] );
return 1;
}
}
int code = 0;
switch(section) {
case 0: code = !run_webserver(args); break;
case 1: process_inbox(); break;
case 2: process_outbox(); break;
case 3: built_in_test(); break;
case 100: develop(); return 0;
default: code = !run_everything(args); break;
}
exit:
app_args_release(args);

@ -28,7 +28,7 @@ static struct json_object_field status_layout[] = {
{ "media", offsetof( struct status, media ), false, &json_field_array_of, &json_field_string },
//{ "reacts", offsetof( struct status, reacts ), false, &json_field_array_of, &status_react_type },
{ "reacts", offsetof( struct status, reacts ), false, &json_field_array_of, &status_react_type },
{ "likes", offsetof( struct status, likes ), false, &json_field_array_of, &json_field_integer },
{ "reposts", offsetof( struct status, reposts ), false, &json_field_array_of, &json_field_integer },
@ -53,6 +53,7 @@ struct status* status_from_id( unsigned int id )
s->id = id;
if( !json_read_object_layout_from_file( filename, status_layout, s ) ) {
printf( "Failed to load status %d\n", id );
status_free(s);
return NULL;
}

@ -1,7 +1,83 @@
#include "react.h"
#include "json/json.h"
#include "json/layout.h"
#include "collections/array.h"
#include <stdlib.h>
#include <string.h>
static bool reader( struct json_pull_parser* jpp, void* field_data, struct json_object_field* layout_field_data )
{
int save;
if( !json_pull_parser_begin_object( jpp, &save ) ) {
return false;
}
struct status_react* sr = *(void**)field_data = malloc(sizeof(struct status_react));
memset(sr,0,sizeof(*sr));
char* key = json_pull_parser_read_object_key(jpp);
if( !key ) { return false; }
sr->code = key;
int save2;
if( !json_pull_parser_begin_array(jpp,&save2)) { return false; }
while( !json_pull_parser_end_array(jpp,&save2)) {
int account_id;
if( !json_pull_parser_read_int( jpp, &account_id ) ) {
printf( "! Failed to read int\n" );
return false;
}
array_append( &sr->accounts, sizeof(int), &account_id );
}
if( !json_pull_parser_end_object( jpp, &save ) ) {
printf( "! Failed to end object\n" );
return false;
}
return true;
}
static bool writer( struct json_writer* jw, const char* field_name, void* field_data, struct json_object_field* layout_field_data )
{
struct status_react* sr = *(void**)field_data;
FILE* f = jw->f;
fprintf( f, "{" );
json_write_string( f, sr->code );
fprintf( f, ",[" );
for( int i = 0; i < sr->accounts.count; ++i ) {
fprintf(f, (i!=0)?",%d":"%d", sr->accounts.items[i] );
}
fprintf( f, "]}" );
return true;
}
static void* alloc()
{
struct status_react* react;
react = malloc(sizeof(*react));
memset(react,0,sizeof(*react));
printf( "status::react::alloc = %p\n", react );
return react;
}
static void free_react( void* ptr )
{
struct status_react* react = ptr;
free(react->accounts.items);
free(react);
}
struct json_field_type status_react_type = {
.reader = reader,
.writer = writer,
.size = sizeof(struct status_react*),
.alloc = alloc,
.free = free_react,
};

@ -4,9 +4,9 @@ struct status_react
{
char* code;
struct {
int* accounts;
int* items;
int count;
};
} accounts;
};
extern struct json_field_type status_react_type;

@ -43,12 +43,12 @@
},
"conversation_id": 2935699,
"direct_conversation_id": null,
"emoji_reactions": [%( for( int i = 0; i < s->reacts.count; ++i ) { )
"emoji_reactions": [%( for( int i = 0; i < s->reacts.count; ++i ) { struct status_react* sr = s->reacts.items[i];)
{
"count": 1,
"count": %d{ sr->accounts.count },
"me": false,
"name": "😆"
}
"name": %( json_write_string( f, sr->code ); )
}%s{ i != s->reacts.count-1 ? "," : "" }
%( } )],
"expires_at": null,
"in_reply_to_account_acct": null,

Loading…
Cancel
Save